kevalds51 commented on code in PR #27794:
URL: https://github.com/apache/datafusion/pull/27794#discussion_r4744742874
##########
datafusion/core/tests/parquet/ordering.rs:
##########
@@ -148,6 +148,78 @@ async fn test_partitioned_table_remaps_sorting_columns()
-> Result<()> {
Ok(())
}
+/// Sort key after a struct column is written and read by leaf index.
+#[tokio::test]
+async fn test_sorting_columns_after_struct_round_trip() -> Result<()> {
+ use parquet::file::reader::FileReader;
+ use parquet::file::serialized_reader::SerializedFileReader;
+ use std::fs::File;
+
+ let ctx = SessionContext::new();
+ let tmp_dir = tempdir()?;
+ let table_path = tmp_dir.path().join("sorted_after_struct");
+ std::fs::create_dir_all(&table_path)?;
+
+ // Leaves: payload.a, payload.b, b, id
+ ctx.sql(&format!(
+ "CREATE EXTERNAL TABLE sorted_by_id (payload STRUCT<a INT, b INT>, b
INT, id INT) \
+ STORED AS PARQUET \
+ LOCATION '{}' \
+ WITH ORDER (id ASC)",
+ table_path.display()
+ ))
+ .await?;
+ // `b` decreases as `id` increases
+ ctx.sql(
+ "INSERT INTO sorted_by_id VALUES \
+ (named_struct('a', 1, 'b', 30), 30, 1), \
+ (named_struct('a', 2, 'b', 20), 20, 2), \
+ (named_struct('a', 3, 'b', 10), 10, 3)",
+ )
+ .await?
+ .collect()
+ .await?;
+
+ // No WITH ORDER, so the ordering comes from the file metadata
+ ctx.sql(&format!(
+ "CREATE EXTERNAL TABLE inferred (payload STRUCT<a INT, b INT>, b INT,
id INT) \
+ STORED AS PARQUET LOCATION '{}'",
+ table_path.display()
+ ))
+ .await?;
+ let batches = ctx
+ .sql("SELECT b, id FROM inferred ORDER BY b")
+ .await?
+ .collect()
+ .await?;
+ datafusion::assert_batches_eq!(
+ [
+ "+----+----+",
+ "| b | id |",
+ "+----+----+",
+ "| 10 | 3 |",
+ "| 20 | 2 |",
+ "| 30 | 1 |",
+ "+----+----+",
+ ],
+ &batches
+ );
Review Comment:
This assertion proves the stale ordering isn't trusted, but it would also
pass if the reader stopped inferring any ordering at all. Could you also check
that the correct ordering is still picked up? Something like:
```rust
let plan = ctx
.sql("EXPLAIN SELECT id FROM inferred ORDER BY id")
.await?
.collect()
.await?;
let plan =
datafusion::arrow::util::pretty::pretty_format_batches(&plan)?.to_string();
assert!(plan.contains("output_ordering=[id@"), "{plan}");
```
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]