rank-p commented on code in PR #27163:
URL: https://github.com/apache/datafusion/pull/27163#discussion_r4596239800


##########
datafusion/core/tests/parquet/ordering.rs:
##########
@@ -168,3 +178,257 @@ fn find_parquet_file(
 
     Ok(None)
 }
+
+const ASC_NULLS_LAST: SortOptions = SortOptions {
+    descending: false,
+    nulls_first: false,
+};
+
+/// Each row group is sorted by `a`, but the file is not.
+#[tokio::test]
+async fn test_unordered_row_groups_are_sorted() -> Result<()> {
+    let row_groups = [
+        record_batch!(("a", Int64, [1, 4]))?,
+        record_batch!(("a", Int64, [2, 3]))?,
+    ];
+
+    let output = query_row_groups(
+        &row_groups,
+        ASC_NULLS_LAST,
+        "SELECT a FROM t ORDER BY a LIMIT 2",
+    )
+    .await?;
+
+    assert_snapshot!(output, @r"
+    SortExec: TopK(fetch=2), expr=[a@0 ASC NULLS LAST], 
preserve_partitioning=[false]
+      DataSourceExec: file_groups={1 group: [[data.parquet]]}, projection=[a], 
file_type=parquet, predicate=DynamicFilter [ empty ], 
sort_order_for_reorder=[a@0 ASC NULLS LAST], dynamic_rg_pruning=eligible
+    +---+
+    | a |
+    +---+
+    | 1 |
+    | 2 |
+    +---+
+    ");
+    Ok(())
+}
+
+#[tokio::test]
+async fn test_ordered_row_groups_are_not_sorted() -> Result<()> {
+    let row_groups = [
+        record_batch!(("a", Int64, [1, 2]))?,
+        record_batch!(("a", Int64, [2, 3]))?,
+    ];
+
+    let output =
+        query_row_groups(&row_groups, ASC_NULLS_LAST, "SELECT a FROM t ORDER 
BY a")
+            .await?;
+
+    assert_snapshot!(output, @r"
+    DataSourceExec: file_groups={1 group: [[data.parquet]]}, projection=[a], 
output_ordering=[a@0 ASC NULLS LAST], file_type=parquet
+    +---+
+    | a |
+    +---+
+    | 1 |
+    | 2 |
+    | 2 |
+    | 3 |
+    +---+
+    ");
+    Ok(())
+}
+
+#[tokio::test]
+async fn test_unordered_descending_row_groups_are_sorted() -> Result<()> {
+    let row_groups = [
+        record_batch!(("a", Int64, [4, 1]))?,
+        record_batch!(("a", Int64, [3, 2]))?,
+    ];
+    let sort_options = SortOptions {
+        descending: true,
+        nulls_first: true,
+    };
+
+    let output =
+        query_row_groups(&row_groups, sort_options, "SELECT a FROM t ORDER BY 
a DESC")
+            .await?;
+
+    assert_snapshot!(output, @r"
+    SortExec: expr=[a@0 DESC], preserve_partitioning=[false]
+      DataSourceExec: file_groups={1 group: [[data.parquet]]}, projection=[a], 
file_type=parquet, sort_order_for_reorder=[a@0 DESC], reverse_row_groups=true
+    +---+
+    | a |
+    +---+
+    | 4 |
+    | 3 |
+    | 2 |
+    | 1 |
+    +---+
+    ");
+    Ok(())
+}
+
+/// The NULL at the end of the first row group comes before the values of the
+/// second row group.
+#[tokio::test]
+async fn test_row_groups_with_unordered_nulls_are_sorted() -> Result<()> {
+    let row_groups = [
+        record_batch!(("a", Int64, [Some(1), None]))?,
+        record_batch!(("a", Int64, [Some(2), Some(3)]))?,
+    ];
+
+    let output =
+        query_row_groups(&row_groups, ASC_NULLS_LAST, "SELECT a FROM t ORDER 
BY a")
+            .await?;
+
+    assert_snapshot!(output, @r"
+    SortExec: expr=[a@0 ASC NULLS LAST], preserve_partitioning=[false]
+      DataSourceExec: file_groups={1 group: [[data.parquet]]}, projection=[a], 
file_type=parquet, sort_order_for_reorder=[a@0 ASC NULLS LAST]
+    +---+
+    | a |
+    +---+
+    | 1 |
+    | 2 |
+    | 3 |
+    |   |
+    +---+
+    ");
+    Ok(())
+}
+
+#[tokio::test]
+async fn test_row_groups_with_ordered_nulls_are_not_sorted() -> Result<()> {
+    let row_groups = [
+        record_batch!(("a", Int64, [None, None]))?,
+        record_batch!(("a", Int64, [None, Some(1)]))?,
+        record_batch!(("a", Int64, [Some(2), Some(3)]))?,
+    ];
+    let sort_options = SortOptions {
+        descending: false,
+        nulls_first: true,
+    };
+
+    let output = query_row_groups(
+        &row_groups,
+        sort_options,
+        "SELECT a FROM t ORDER BY a NULLS FIRST",
+    )
+    .await?;
+
+    assert_snapshot!(output, @r"
+    DataSourceExec: file_groups={1 group: [[data.parquet]]}, projection=[a], 
output_ordering=[a@0 ASC], file_type=parquet
+    +---+
+    | a |
+    +---+
+    |   |
+    |   |
+    |   |
+    | 1 |
+    | 2 |
+    | 3 |
+    +---+
+    ");
+    Ok(())
+}
+
+/// `b` starts again in the second row group. This is ordered because `a`
+/// increases between the row groups.
+#[tokio::test]
+async fn test_row_groups_ordered_by_two_columns_are_not_sorted() -> Result<()> 
{
+    let row_groups = [
+        record_batch!(("a", Int64, [1, 1]), ("b", Int64, [8, 9]))?,
+        record_batch!(("a", Int64, [2, 2]), ("b", Int64, [0, 1]))?,
+    ];
+
+    let output = query_row_groups(
+        &row_groups,
+        ASC_NULLS_LAST,
+        "SELECT a, b FROM t ORDER BY a, b",
+    )
+    .await?;
+
+    assert_snapshot!(output, @r"
+    DataSourceExec: file_groups={1 group: [[data.parquet]]}, projection=[a, 
b], output_ordering=[a@0 ASC NULLS LAST, b@1 ASC NULLS LAST], file_type=parquet
+    +---+---+
+    | a | b |
+    +---+---+
+    | 1 | 8 |
+    | 1 | 9 |
+    | 2 | 0 |
+    | 2 | 1 |
+    +---+---+
+    ");
+    Ok(())
+}
+
+/// `b` starts again in the second row group, but `a` does not increase. The
+/// file is ordered by `a` only.
+#[tokio::test]
+async fn test_row_groups_ordered_by_first_column_only_are_sorted() -> 
Result<()> {
+    let row_groups = [
+        record_batch!(("a", Int64, [1, 1]), ("b", Int64, [8, 9]))?,
+        record_batch!(("a", Int64, [1, 1]), ("b", Int64, [0, 1]))?,
+    ];
+
+    let output = query_row_groups(
+        &row_groups,
+        ASC_NULLS_LAST,
+        "SELECT a, b FROM t ORDER BY a, b",
+    )
+    .await?;
+
+    assert_snapshot!(output, @r"
+    SortExec: expr=[a@0 ASC NULLS LAST, b@1 ASC NULLS LAST], 
preserve_partitioning=[false]
+      DataSourceExec: file_groups={1 group: [[data.parquet]]}, projection=[a, 
b], output_ordering=[a@0 ASC NULLS LAST], file_type=parquet
+    +---+---+
+    | a | b |
+    +---+---+
+    | 1 | 0 |
+    | 1 | 1 |
+    | 1 | 8 |
+    | 1 | 9 |
+    +---+---+
+    ");
+    Ok(())
+}
+
+/// Writes one Parquet file with one row group per batch. Every row group
+/// declares that it is sorted by all columns with `sort_options`. Returns the
+/// plan and the results of `sql`, which reads the file as table `t`.
+async fn query_row_groups(
+    row_groups: &[RecordBatch],
+    sort_options: SortOptions,
+    sql: &str,
+) -> Result<String> {
+    let tmp_dir = tempdir()?;
+    let path = tmp_dir.path().join("data.parquet");
+    let schema = row_groups[0].schema();
+    let sorting_columns = (0..schema.fields().len() as i32)

Review Comment:
   No, these test cases aim at the situation where row groups are individually 
sorted but the file is globally not sorted. 
   The parquet writer we're using here can't produce a file that has different 
`sorting_columns` per row group. This behavior is tested as unit test in 
`metadata.rs`.



-- 
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]

Reply via email to