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


##########
datafusion/datasource-parquet/src/metadata.rs:
##########
@@ -1248,6 +1266,134 @@ fn sorting_columns_to_physical_exprs(
         .collect()
 }
 
+/// Returns how many leading `sorting_columns` are ordered across the whole
+/// file, given that every row group is sorted by all of them.
+///
+/// The row group statistics give the values that sort first and last in each
+/// row group. The file is ordered if the value that sorts last in each row
+/// group is not after the value that sorts first in the next row group. For
+/// `a ASC`:
+///
+/// ```text
+/// row groups [1, 2] [3, 4]: ordered, because 2 <= 3
+/// row groups [1, 4] [2, 3]: not ordered, because 4 > 2
+/// ```
+///
+/// A later column must be ordered only where all earlier columns are equal.
+/// For `a ASC, b ASC`:
+///
+/// ```text
+/// row groups [(1, 8), (1, 9)] [(2, 0), (2, 1)]: ordered by (a, b), because a 
increases
+/// row groups [(1, 8), (1, 9)] [(1, 0), (1, 1)]: ordered by (a) only, because 
a is equal and 9 > 0
+/// ```
+///
+/// A column whose order cannot be proved, for example because it has no
+/// usable statistics, counts as not ordered. Ordering is optional information,
+/// so this never fails the query.
+fn num_columns_ordered_across_row_groups(
+    metadata: &ParquetMetaData,
+    sorting_columns: &[SortingColumn],
+) -> usize {
+    let row_groups = metadata.row_groups();
+    if row_groups.len() < 2 {
+        return sorting_columns.len();
+    }
+    let file_metadata = metadata.file_metadata();
+    let parquet_schema = file_metadata.schema_descr();
+    let Ok(arrow_schema) =
+        parquet_to_arrow_schema(parquet_schema, 
file_metadata.key_value_metadata())
+    else {
+        return 0;
+    };
+    let resolver = LeafResolver::new(parquet_schema, &arrow_schema);
+
+    // Compares the value that sorts last in one row group with the value that
+    // sorts first in another
+    let comparator = |sorting_column: &SortingColumn| -> Option<DynComparator> 
{
+        let (first, last) =
+            row_group_sort_bounds(row_groups, parquet_schema, &resolver, 
sorting_column)?;
+        let options =
+            SortOptions::new(sorting_column.descending, 
sorting_column.nulls_first);
+        make_comparator(&last, &first, options).ok()
+    };
+    // One comparator per sorting column, created when the column is first
+    // needed. A later column is often not needed at all.
+    let mut comparators: Vec<Option<DynComparator>> = vec![];
+
+    let mut num_ordered = sorting_columns.len();
+    // Compare each row group with the next one, column by column
+    for i in 0..row_groups.len() - 1 {

Review Comment:
   Thanks for the Feedback. I want to flag that MinMaxStatistics is currently 
private. I updated the PR with this but it required an API change to make 
MinMaxStatistics public. I marked it as internal use.
   
   I also ran some tests on files with 3 Int32 sort columns and 10,000 row 
groups and this takes about 550 µs. The previous commit without the code reuse 
was a slight bit faster at around 480 µs, but it's not significant and worth it 
to reuse the code.



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