JingsongLi commented on code in PR #145:
URL: https://github.com/apache/paimon-rust/pull/145#discussion_r2982263771
##########
crates/integration_tests/tests/read_tables.rs:
##########
@@ -55,57 +53,231 @@ async fn read_rows(table_name: &str) -> Vec<(i32, String)>
{
!batches.is_empty(),
"Expected at least one batch from table {table_name}"
);
+ (plan, batches)
+}
- let mut actual_rows: Vec<(i32, String)> = Vec::new();
-
- for batch in &batches {
- let id_array = batch
+fn extract_id_name(batches: &[RecordBatch]) -> Vec<(i32, String)> {
+ let mut rows = Vec::new();
+ for batch in batches {
+ let id = batch
.column_by_name("id")
.and_then(|c| c.as_any().downcast_ref::<Int32Array>())
- .expect("Expected Int32Array for id column");
- let name_array = batch
+ .expect("Expected Int32Array for id");
+ let name = batch
.column_by_name("name")
.and_then(|c| c.as_any().downcast_ref::<StringArray>())
- .expect("Expected StringArray for name column");
-
+ .expect("Expected StringArray for name");
for i in 0..batch.num_rows() {
- actual_rows.push((id_array.value(i),
name_array.value(i).to_string()));
+ rows.push((id.value(i), name.value(i).to_string()));
}
}
-
- actual_rows.sort_by_key(|(id, _)| *id);
- actual_rows
+ rows.sort_by_key(|(id, _)| *id);
+ rows
}
#[tokio::test]
async fn test_read_log_table() {
- let actual_rows = read_rows("simple_log_table").await;
- let expected_rows = vec![
+ let (_, batches) = scan_and_read("simple_log_table").await;
+ let actual = extract_id_name(&batches);
+ let expected = vec![
(1, "alice".to_string()),
(2, "bob".to_string()),
(3, "carol".to_string()),
];
-
- assert_eq!(
- actual_rows, expected_rows,
- "Rows should match expected values"
- );
+ assert_eq!(actual, expected, "Rows should match expected values");
}
#[tokio::test]
async fn test_read_dv_primary_key_table() {
- let actual_rows = read_rows("simple_dv_pk_table").await;
- let expected_rows = vec![
+ let (_, batches) = scan_and_read("simple_dv_pk_table").await;
+ let actual = extract_id_name(&batches);
+ let expected = vec![
(1, "alice-v2".to_string()),
(2, "bob-v2".to_string()),
(3, "carol-v2".to_string()),
(4, "dave-v2".to_string()),
(5, "eve-v2".to_string()),
(6, "frank-v1".to_string()),
];
-
assert_eq!(
- actual_rows, expected_rows,
+ actual, expected,
"DV-enabled PK table should only expose the latest row per key"
);
}
+
+#[tokio::test]
+async fn test_read_partitioned_log_table() {
+ let (plan, batches) = scan_and_read("partitioned_log_table").await;
+
+ let mut seen_partitions: HashSet<String> = HashSet::new();
+ for split in plan.splits() {
+ let partition = split.partition();
+ assert_eq!(partition.arity(), 1);
+ assert!(!partition.is_empty());
+ let dt = partition.get_string(0).expect("Failed to decode dt");
+ let expected_suffix = format!("dt={dt}/bucket-{}", split.bucket());
+ assert!(
+ split.bucket_path().ends_with(&expected_suffix),
+ "bucket_path should end with '{expected_suffix}', got: {}",
+ split.bucket_path()
+ );
+ seen_partitions.insert(dt.to_string());
+ }
+ assert_eq!(
+ seen_partitions,
+ HashSet::from(["2024-01-01".into(), "2024-01-02".into()])
+ );
+
+ let mut rows: Vec<(i32, String, String)> = Vec::new();
+ for batch in &batches {
+ let id = batch
+ .column_by_name("id")
+ .and_then(|c| c.as_any().downcast_ref::<Int32Array>())
+ .expect("id");
+ let name = batch
+ .column_by_name("name")
+ .and_then(|c| c.as_any().downcast_ref::<StringArray>())
+ .expect("name");
+ let dt = batch
+ .column_by_name("dt")
+ .and_then(|c| c.as_any().downcast_ref::<StringArray>())
+ .expect("dt");
+ for i in 0..batch.num_rows() {
+ rows.push((id.value(i), name.value(i).into(), dt.value(i).into()));
+ }
+ }
+ rows.sort_by_key(|(id, _, _)| *id);
+
+ assert_eq!(
+ rows,
+ vec![
+ (1, "alice".into(), "2024-01-01".into()),
+ (2, "bob".into(), "2024-01-01".into()),
+ (3, "carol".into(), "2024-01-02".into()),
+ ]
+ );
+}
+
+#[tokio::test]
+async fn test_read_multi_partitioned_log_table() {
+ let (plan, batches) = scan_and_read("multi_partitioned_log_table").await;
+
+ let mut seen_partitions: HashSet<(String, i32)> = HashSet::new();
+ for split in plan.splits() {
+ let partition = split.partition();
+ assert_eq!(partition.arity(), 2);
+ assert!(!partition.is_empty());
+ let dt = partition.get_string(0).expect("Failed to decode dt");
+ let hr = partition.get_int(1).expect("Failed to decode hr");
+ let expected_suffix = format!("dt={dt}/hr={hr}/bucket-{}",
split.bucket());
+ assert!(
+ split.bucket_path().ends_with(&expected_suffix),
+ "bucket_path should end with '{expected_suffix}', got: {}",
+ split.bucket_path()
+ );
+ seen_partitions.insert((dt.to_string(), hr));
+ }
+ assert_eq!(
+ seen_partitions,
+ HashSet::from([
+ ("2024-01-01".into(), 10),
+ ("2024-01-01".into(), 20),
+ ("2024-01-02".into(), 10),
+ ])
+ );
+
+ let mut rows: Vec<(i32, String, String, i32)> = Vec::new();
+ for batch in &batches {
+ let id = batch
+ .column_by_name("id")
+ .and_then(|c| c.as_any().downcast_ref::<Int32Array>())
+ .expect("id");
+ let name = batch
+ .column_by_name("name")
+ .and_then(|c| c.as_any().downcast_ref::<StringArray>())
+ .expect("name");
+ let dt = batch
+ .column_by_name("dt")
+ .and_then(|c| c.as_any().downcast_ref::<StringArray>())
+ .expect("dt");
+ let hr = batch
+ .column_by_name("hr")
+ .and_then(|c| c.as_any().downcast_ref::<Int32Array>())
+ .expect("hr");
+ for i in 0..batch.num_rows() {
+ rows.push((
+ id.value(i),
+ name.value(i).into(),
+ dt.value(i).into(),
+ hr.value(i),
+ ));
+ }
+ }
+ rows.sort_by_key(|(id, _, _, _)| *id);
+
+ assert_eq!(
+ rows,
+ vec![
+ (1, "alice".into(), "2024-01-01".into(), 10),
+ (2, "bob".into(), "2024-01-01".into(), 10),
+ (3, "carol".into(), "2024-01-01".into(), 20),
+ (4, "dave".into(), "2024-01-02".into(), 10),
+ ]
+ );
+}
+
+#[tokio::test]
+async fn test_read_partitioned_dv_pk_table() {
+ let (plan, batches) = scan_and_read("partitioned_dv_pk_table").await;
+
+ // Verify partition metadata on each split.
+ let mut seen_partitions: HashSet<String> = HashSet::new();
+ for split in plan.splits() {
+ let partition = split.partition();
+ assert_eq!(partition.arity(), 1);
+ assert!(!partition.is_empty());
+ let dt = partition.get_string(0).expect("Failed to decode dt");
+ let expected_suffix = format!("dt={dt}/bucket-{}", split.bucket());
+ assert!(
+ split.bucket_path().ends_with(&expected_suffix),
+ "bucket_path should end with '{expected_suffix}', got: {}",
+ split.bucket_path()
+ );
+ seen_partitions.insert(dt.to_string());
+ }
+ assert_eq!(
+ seen_partitions,
+ HashSet::from(["2024-01-01".into(), "2024-01-02".into()])
+ );
+
+ let mut rows: Vec<(i32, String, String)> = Vec::new();
+ for batch in &batches {
+ let id = batch
+ .column_by_name("id")
+ .and_then(|c| c.as_any().downcast_ref::<Int32Array>())
+ .expect("id");
+ let name = batch
+ .column_by_name("name")
+ .and_then(|c| c.as_any().downcast_ref::<StringArray>())
+ .expect("name");
+ let dt = batch
+ .column_by_name("dt")
+ .and_then(|c| c.as_any().downcast_ref::<StringArray>())
+ .expect("dt");
+ for i in 0..batch.num_rows() {
+ rows.push((id.value(i), name.value(i).into(), dt.value(i).into()));
+ }
+ }
+ rows.sort_by_key(|(id, _, _)| *id);
Review Comment:
`rows.sort_by(|a, b| a.0.cmp(&b.0).then(a.2.cmp(&b.2)))` sort by partition
too to ensure the determination of result sequence.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]