Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 73 additions & 12 deletions datafusion/datasource-parquet/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use object_store::path::Path;
use object_store::{ObjectMeta, ObjectStore};
use parquet::DecodeResult;
use parquet::arrow::arrow_reader::statistics::StatisticsConverter;
use parquet::arrow::parquet_to_arrow_schema;
use parquet::arrow::{ArrowSchemaConverter, parquet_column, parquet_to_arrow_schema};
use parquet::basic::{ColumnOrder, SortOrder, Type as PhysicalType};
use parquet::file::metadata::{
PageIndexPolicy, ParquetMetaData, ParquetMetaDataPushDecoder, ParquetMetaDataReader,
Expand Down Expand Up @@ -1121,12 +1121,13 @@ impl FileMetadata for CachedParquetMetaData {
///
/// Returns `Ok(None)` if the referenced column is not in `writer_schema`, such as a
/// hive partition column that is removed before writing the Parquet file. Returns
/// `Err` if the expression is not a simple column reference or references a column
/// outside `input_schema`.
/// `Err` if the expression is not a simple column reference, references a column
/// outside `input_schema`, or references a nested column.
pub(crate) fn sort_expr_to_sorting_column(
sort_expr: &PhysicalSortExpr,
input_schema: &Schema,
writer_schema: &Schema,
parquet_schema: &SchemaDescriptor,
) -> Result<Option<SortingColumn>> {
let column = sort_expr.expr.downcast_ref::<Column>().ok_or_else(|| {
DataFusionError::Plan(format!(
Expand All @@ -1144,14 +1145,25 @@ pub(crate) fn sort_expr_to_sorting_column(
input_schema.fields().len()
)
})?;
let Some((writer_index, _)) = writer_schema.column_with_name(input_field.name())
else {
if writer_schema.column_with_name(input_field.name()).is_none() {
return Ok(None);
}

// `column_idx` is a Parquet leaf column index, which differs from the Arrow
// field index when an earlier field is nested, such as a struct.
let Some((leaf_index, _)) =
parquet_column(parquet_schema, writer_schema, input_field.name())
else {
return Err(DataFusionError::Plan(format!(
"Parquet sorting_columns only supports primitive columns, \
but got nested column: {}",
input_field.name()
)));
};

let column_idx: i32 = writer_index.try_into().map_err(|_| {
let column_idx: i32 = leaf_index.try_into().map_err(|_| {
DataFusionError::Plan(format!(
"Column index {writer_index} is too large to be represented as i32"
"Column index {leaf_index} is too large to be represented as i32"
))
})?;

Expand All @@ -1165,18 +1177,24 @@ pub(crate) fn sort_expr_to_sorting_column(
/// Convert a [`LexOrdering`] to `Vec<SortingColumn>` for Parquet.
///
/// Columns that are not present in `writer_schema` are omitted from the resulting
/// metadata. Returns `Err` if any expression is not a simple column reference or
/// references a column outside `input_schema`.
/// metadata. Returns `Err` if any expression is not a simple column reference,
/// references a column outside `input_schema`, or references a nested column.
pub(crate) fn lex_ordering_to_sorting_columns(
ordering: &LexOrdering,
input_schema: &Schema,
writer_schema: &Schema,
) -> Result<Vec<SortingColumn>> {
let parquet_schema = ArrowSchemaConverter::new().convert(writer_schema)?;
ordering
.iter()
.filter_map(|sort_expr| {
sort_expr_to_sorting_column(sort_expr, input_schema, writer_schema)
.transpose()
sort_expr_to_sorting_column(
sort_expr,
input_schema,
writer_schema,
&parquet_schema,
)
.transpose()
})
.collect()
}
Expand Down Expand Up @@ -1253,7 +1271,7 @@ mod tests {
use super::*;
use arrow::array::Int32Array;
use arrow::compute::SortOptions;
use arrow::datatypes::Field;
use arrow::datatypes::{Field, Fields};

#[test]
fn test_lex_ordering_to_sorting_columns_uses_writer_schema() -> Result<()> {
Expand Down Expand Up @@ -1304,6 +1322,49 @@ mod tests {
Ok(())
}

#[test]
fn test_lex_ordering_to_sorting_columns_uses_leaf_index() -> Result<()> {
let schema = Schema::new(vec![
Field::new(
"st",
DataType::Struct(Fields::from(vec![
Field::new("x", DataType::Int32, true),
Field::new("y", DataType::Int32, true),
])),
true,
),
Field::new("a", DataType::Int32, true),
Field::new("b", DataType::Int32, true),
]);

// The struct takes leaves 0 and 1, so `b` is leaf 3, not field 2
let ordering = LexOrdering::new(vec![PhysicalSortExpr::new(
Arc::new(Column::new("b", 2)),
SortOptions::default(),
)])
.unwrap();
let sorting_columns =
lex_ordering_to_sorting_columns(&ordering, &schema, &schema)?;
assert_eq!(
sorting_columns,
vec![SortingColumn {
column_idx: 3,
descending: false,
nulls_first: true,
}]
);

// A nested column can't be a Parquet sorting column
let ordering = LexOrdering::new(vec![PhysicalSortExpr::new(
Arc::new(Column::new("st", 0)),
SortOptions::default(),
)])
.unwrap();
assert!(lex_ordering_to_sorting_columns(&ordering, &schema, &schema).is_err());

Ok(())
}

#[test]
fn test_has_any_exact_match() {
// Case 1: Mixed exact and inexact matches
Expand Down
106 changes: 106 additions & 0 deletions datafusion/sqllogictest/test_files/copy.slt
Original file line number Diff line number Diff line change
Expand Up @@ -932,3 +932,109 @@ DROP TABLE copy_with_order;

statement ok
DROP TABLE union_const_src;

# A struct column before the sort key must not shift the recorded sort column
query I
COPY (
SELECT named_struct('x', i, 'y', i) AS st, i % 3 AS a, i AS b
FROM generate_series(1, 6) AS t(i)
ORDER BY b
) TO 'test_files/scratch/copy/struct_before_sort_key/sorted.parquet';
----
6

statement ok
CREATE EXTERNAL TABLE struct_before_sort_key STORED AS PARQUET
LOCATION 'test_files/scratch/copy/struct_before_sort_key/sorted.parquet';

query I
SELECT a FROM struct_before_sort_key ORDER BY a;
----
0
0
1
1
2
2

# The file is not sorted by a, so the physical plan keeps the SortExec
query TT
EXPLAIN SELECT a FROM struct_before_sort_key ORDER BY a;
----
logical_plan
01)Sort: struct_before_sort_key.a ASC NULLS LAST
02)--TableScan: struct_before_sort_key projection=[a]
physical_plan
01)SortExec: expr=[a@0 ASC NULLS LAST], preserve_partitioning=[false]
02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/copy/struct_before_sort_key/sorted.parquet]]}, projection=[a], file_type=parquet, sort_order_for_reorder=[a@0 ASC NULLS LAST]

# The logical plan keeps the Sort, but the file is sorted by b,
# so the physical plan has no SortExec
query TT
EXPLAIN SELECT b FROM struct_before_sort_key ORDER BY b;
----
logical_plan
01)Sort: struct_before_sort_key.b ASC NULLS LAST
02)--TableScan: struct_before_sort_key projection=[b]
physical_plan DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/copy/struct_before_sort_key/sorted.parquet]]}, projection=[b], output_ordering=[b@0 ASC NULLS LAST], file_type=parquet

# A struct sort key can't be recorded, so no sort order is written
query I
COPY (
SELECT named_struct('x', -i, 'y', i) AS st, i % 3 AS a, i AS b
FROM generate_series(1, 6) AS t(i)
ORDER BY st, b
) TO 'test_files/scratch/copy/struct_sort_key/sorted.parquet';
----
6

statement ok
CREATE EXTERNAL TABLE struct_sort_key STORED AS PARQUET
LOCATION 'test_files/scratch/copy/struct_sort_key/sorted.parquet';

query I
SELECT a FROM struct_sort_key ORDER BY a;
----
0
0
1
1
2
2

query I
SELECT b FROM struct_sort_key ORDER BY b;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please show these plans too via EXPLAIN so we cn verify if there is a sort or not?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the plans, thanks!

----
1
2
3
4
5
6

# No sort order is recorded, so both physical plans keep the SortExec
query TT
EXPLAIN SELECT a FROM struct_sort_key ORDER BY a;
----
logical_plan
01)Sort: struct_sort_key.a ASC NULLS LAST
02)--TableScan: struct_sort_key projection=[a]
physical_plan
01)SortExec: expr=[a@0 ASC NULLS LAST], preserve_partitioning=[false]
02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/copy/struct_sort_key/sorted.parquet]]}, projection=[a], file_type=parquet, sort_order_for_reorder=[a@0 ASC NULLS LAST]

query TT
EXPLAIN SELECT b FROM struct_sort_key ORDER BY b;
----
logical_plan
01)Sort: struct_sort_key.b ASC NULLS LAST
02)--TableScan: struct_sort_key projection=[b]
physical_plan
01)SortExec: expr=[b@0 ASC NULLS LAST], preserve_partitioning=[false]
02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/copy/struct_sort_key/sorted.parquet]]}, projection=[b], file_type=parquet, sort_order_for_reorder=[b@0 ASC NULLS LAST]

statement ok
DROP TABLE struct_before_sort_key;

statement ok
DROP TABLE struct_sort_key;