From b5362e92ff034f5b9296914a4bc04380edd9f3e6 Mon Sep 17 00:00:00 2001 From: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Date: Sun, 29 Nov 2026 23:41:09 -0800 Subject: [PATCH 1/2] test: add struct field pruning tests Predicates on struct fields such as `s['a'] > 5` do not prune Parquet row groups or pages today. These tests record that behavior. A later change adds the pruning and updates the expected output, so its diff shows the effect. The tests add a sqllogictest file with EXPLAIN ANALYZE pruning metrics. They also add a core integration test for a struct field without a null count statistic. Co-Authored-By: Claude Fable 5.2 --- datafusion/core/tests/parquet/mod.rs | 1 + .../tests/parquet/struct_field_pruning.rs | 98 ++++ .../parquet_struct_field_pruning.slt | 498 ++++++++++++++++++ 3 files changed, 597 insertions(+) create mode 100644 datafusion/core/tests/parquet/struct_field_pruning.rs create mode 100644 datafusion/sqllogictest/test_files/parquet_struct_field_pruning.slt diff --git a/datafusion/core/tests/parquet/mod.rs b/datafusion/core/tests/parquet/mod.rs index 54482eaa5d02d..a76e742e8ecd7 100644 --- a/datafusion/core/tests/parquet/mod.rs +++ b/datafusion/core/tests/parquet/mod.rs @@ -47,6 +47,7 @@ mod row_group_pruning; mod schema; mod schema_coercion; mod string_in_list_pruning; +mod struct_field_pruning; mod utils; #[cfg(test)] diff --git a/datafusion/core/tests/parquet/struct_field_pruning.rs b/datafusion/core/tests/parquet/struct_field_pruning.rs new file mode 100644 index 0000000000000..d24809ef7ed2d --- /dev/null +++ b/datafusion/core/tests/parquet/struct_field_pruning.rs @@ -0,0 +1,98 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership +// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements + +//! Pruning on struct fields, for files that SQL cannot write. + +use std::io::Write; +use std::sync::Arc; + +use arrow::array::{ArrayRef, Int64Array, RecordBatch, StructArray}; +use arrow::datatypes::{DataType, Field, Fields, Schema}; +use datafusion::prelude::{ParquetReadOptions, SessionContext}; +use datafusion_common::ScalarValue; +use parquet::arrow::ArrowWriter; +use parquet::file::metadata::{ParquetMetaData, ParquetMetaDataWriter}; +use parquet::file::statistics::Statistics; +use parquet::file::writer::TrackedWrite; +use tempfile::NamedTempFile; + +/// Writes `s: {a: Int64}` with rows `{a: 1}` and `{a: null}` in one row +/// group. The footer keeps the min and max of `s.a`, but not its null count. +fn struct_file_without_null_count() -> NamedTempFile { + let a = Field::new("a", DataType::Int64, true); + let schema = Arc::new(Schema::new(vec![Field::new( + "s", + DataType::Struct(Fields::from(vec![a.clone()])), + true, + )])); + let values: ArrayRef = Arc::new(Int64Array::from(vec![Some(1), None])); + let s = StructArray::new(Fields::from(vec![a]), vec![values], None); + let batch = RecordBatch::try_new(Arc::clone(&schema), vec![Arc::new(s)]).unwrap(); + let mut bytes = Vec::new(); + let mut writer = ArrowWriter::try_new(&mut bytes, schema, None).unwrap(); + writer.write(&batch).unwrap(); + let metadata = writer.close().unwrap(); + + let group = &metadata.row_groups()[0]; + let column = group.column(0).clone(); + let Statistics::Int64(stats) = column.statistics().unwrap() else { + panic!("expected Int64 statistics"); + }; + let stats = Statistics::int64( + stats.min_opt().copied(), + stats.max_opt().copied(), + None, + None, + false, + ); + let column = column.into_builder().set_statistics(stats).build().unwrap(); + let group = group + .clone() + .into_builder() + .set_column_metadata(vec![column]) + .build() + .unwrap(); + let metadata = ParquetMetaData::new(metadata.file_metadata().clone(), vec![group]); + + // Replace the footer. The encoded rows and their offsets do not change. + let footer_len = + u32::from_le_bytes(bytes[bytes.len() - 8..bytes.len() - 4].try_into().unwrap()); + bytes.truncate(bytes.len() - 8 - footer_len as usize); + let mut file = tempfile::Builder::new() + .suffix(".parquet") + .tempfile() + .unwrap(); + let mut output = TrackedWrite::new(file.as_file_mut()); + output.write_all(&bytes).unwrap(); + ParquetMetaDataWriter::new_with_tracked(output, &metadata) + .finish() + .unwrap(); + file +} + +#[tokio::test] +async fn missing_struct_field_null_count_keeps_row_group() { + // A missing null count is unknown, not zero. `s['a'] IS NULL` must not + // prune the row group. + let file = struct_file_without_null_count(); + let ctx = SessionContext::new(); + ctx.register_parquet( + "t", + file.path().to_str().unwrap(), + ParquetReadOptions::default(), + ) + .await + .unwrap(); + let batches = ctx + .sql("SELECT count(*) FROM t WHERE s['a'] IS NULL") + .await + .unwrap() + .collect() + .await + .unwrap(); + assert_eq!( + ScalarValue::try_from_array(batches[0].column(0), 0).unwrap(), + ScalarValue::Int64(Some(1)), + ); +} diff --git a/datafusion/sqllogictest/test_files/parquet_struct_field_pruning.slt b/datafusion/sqllogictest/test_files/parquet_struct_field_pruning.slt new file mode 100644 index 0000000000000..26fe96be89e48 --- /dev/null +++ b/datafusion/sqllogictest/test_files/parquet_struct_field_pruning.slt @@ -0,0 +1,498 @@ +# SPDX-License-Identifier: Apache-2.0 +# SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership +# SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements + +# Row group pruning with statistics for predicates on struct fields +# (`get_field(s, 'a')`, written `s['a']`). + +statement ok +CREATE TABLE src AS VALUES +-- ***** Row Group 0: a in [1, 3] ***** + (1, 100), + (2, 200), + (3, 300), +-- ***** Row Group 1: a in [10, 12] ***** + (10, 100), + (11, 200), + (12, 300), +-- ***** Row Group 2: null struct, a = 20, null a ***** + (NULL, NULL), + (20, 400), + (-1, 500), +-- ***** Row Group 3: a in [30, 32] ***** + (30, 100), + (31, 200), + (32, 300); + +# Row 7 (column1 = NULL) is a null struct. Row 9 (column1 = -1) is a non-null +# struct with a null `a`. +statement ok +COPY ( + SELECT + CASE + WHEN column1 IS NULL THEN NULL + WHEN column1 = -1 THEN named_struct('a', CAST(NULL AS INT), 'b', column2) + ELSE named_struct('a', column1, 'b', column2) + END AS s, + column1 AS id + FROM src +) +TO 'test_files/scratch/parquet_struct_field_pruning/data.parquet' +STORED AS PARQUET +OPTIONS ('format.max_row_group_size' '3'); + +statement ok +DROP TABLE src; + +statement ok +CREATE EXTERNAL TABLE t +STORED AS PARQUET +LOCATION 'test_files/scratch/parquet_struct_field_pruning/data.parquet'; + +statement ok +set datafusion.explain.analyze_level = summary; + +statement ok +set datafusion.explain.analyze_categories = 'rows'; + +# Results are correct. +query ?I rowsort +SELECT s, id FROM t WHERE s['a'] > 15; +---- +{a: 20, b: 400} 20 +{a: 30, b: 100} 30 +{a: 31, b: 200} 31 +{a: 32, b: 300} 32 + +query I rowsort +SELECT id FROM t WHERE s['a'] IS NULL; +---- +-1 +NULL + +# Row groups 0 and 1 are pruned. +query TT +EXPLAIN ANALYZE SELECT * FROM t WHERE s['a'] > 15; +---- +Plan with Metrics +01)FilterExec: __datafusion_extracted_1@0 > 15, projection=[s@1, id@2], metrics=[output_rows=4, selectivity=33.33% (4/12)] +02)--RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1, metrics=[output_rows=12] +03)----DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/parquet_struct_field_pruning/data.parquet]]}, projection=[get_field(s@0, a) as __datafusion_extracted_1, s, id], file_type=parquet, predicate=get_field(s@0, a) > 15, metrics=[output_rows=12, files_ranges_pruned_statistics=1 total → 1 matched, row_groups_pruned_statistics=4 total → 4 matched, page_index_pages_pruned=0 total → 0 matched, limit_pruned_row_groups=0 total → 0 matched, scan_efficiency_ratio=27.52% (926/3.37 K)] + +# Only row group 0 matches. +query TT +EXPLAIN ANALYZE SELECT * FROM t WHERE s['a'] <= 2; +---- +Plan with Metrics +01)FilterExec: __datafusion_extracted_1@0 <= 2, projection=[s@1, id@2], metrics=[output_rows=2, selectivity=16.67% (2/12)] +02)--RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1, metrics=[output_rows=12] +03)----DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/parquet_struct_field_pruning/data.parquet]]}, projection=[get_field(s@0, a) as __datafusion_extracted_1, s, id], file_type=parquet, predicate=get_field(s@0, a) <= 2, metrics=[output_rows=12, files_ranges_pruned_statistics=1 total → 1 matched, row_groups_pruned_statistics=4 total → 4 matched, page_index_pages_pruned=0 total → 0 matched, limit_pruned_row_groups=0 total → 0 matched, scan_efficiency_ratio=27.52% (926/3.37 K)] + +# Equality, with the literal on the left. +query TT +EXPLAIN ANALYZE SELECT * FROM t WHERE 11 = s['a']; +---- +Plan with Metrics +01)FilterExec: __datafusion_extracted_1@0 = 11, projection=[s@1, id@2], metrics=[output_rows=1, selectivity=8.33% (1/12)] +02)--RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1, metrics=[output_rows=12] +03)----DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/parquet_struct_field_pruning/data.parquet]]}, projection=[get_field(s@0, a) as __datafusion_extracted_1, s, id], file_type=parquet, predicate=get_field(s@0, a) = 11, metrics=[output_rows=12, files_ranges_pruned_statistics=1 total → 1 matched, row_groups_pruned_statistics=4 total → 4 matched, page_index_pages_pruned=0 total → 0 matched, limit_pruned_row_groups=0 total → 0 matched, scan_efficiency_ratio=27.52% (926/3.37 K)] + +# Row group 2 holds only one non-null `a`, so `s['a'] = 20` keeps it. +query TT +EXPLAIN ANALYZE SELECT * FROM t WHERE s['a'] = 20; +---- +Plan with Metrics +01)FilterExec: __datafusion_extracted_1@0 = 20, projection=[s@1, id@2], metrics=[output_rows=1, selectivity=8.33% (1/12)] +02)--RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1, metrics=[output_rows=12] +03)----DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/parquet_struct_field_pruning/data.parquet]]}, projection=[get_field(s@0, a) as __datafusion_extracted_1, s, id], file_type=parquet, predicate=get_field(s@0, a) = 20, metrics=[output_rows=12, files_ranges_pruned_statistics=1 total → 1 matched, row_groups_pruned_statistics=4 total → 4 matched, page_index_pages_pruned=0 total → 0 matched, limit_pruned_row_groups=0 total → 0 matched, scan_efficiency_ratio=27.52% (926/3.37 K)] + +# Two different struct fields in one comparison are not prunable. +query TT +EXPLAIN ANALYZE SELECT * FROM t WHERE s['a'] > s['b']; +---- +Plan with Metrics +01)FilterExec: __datafusion_extracted_2@1 < __datafusion_extracted_1@0, projection=[s@2, id@3], metrics=[output_rows=0, selectivity=0% (0/12)] +02)--RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1, metrics=[output_rows=12] +03)----DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/parquet_struct_field_pruning/data.parquet]]}, projection=[get_field(s@0, a) as __datafusion_extracted_1, get_field(s@0, b) as __datafusion_extracted_2, s, id], file_type=parquet, predicate=get_field(s@0, b) < get_field(s@0, a), metrics=[output_rows=12, files_ranges_pruned_statistics=1 total → 1 matched, row_groups_pruned_statistics=4 total → 4 matched, page_index_pages_pruned=0 total → 0 matched, limit_pruned_row_groups=0 total → 0 matched, scan_efficiency_ratio=27.52% (926/3.37 K)] + +# A struct field combined with a plain column in OR. +query TT +EXPLAIN ANALYZE SELECT * FROM t WHERE s['a'] > 25 OR id < 2; +---- +Plan with Metrics +01)FilterExec: __datafusion_extracted_1@0 > 25 OR id@2 < 2, projection=[s@1, id@2], metrics=[output_rows=5, selectivity=41.67% (5/12)] +02)--RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1, metrics=[output_rows=12] +03)----DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/parquet_struct_field_pruning/data.parquet]]}, projection=[get_field(s@0, a) as __datafusion_extracted_1, s, id], file_type=parquet, predicate=get_field(s@0, a) > 25 OR id@1 < 2, metrics=[output_rows=12, files_ranges_pruned_statistics=1 total → 1 matched, row_groups_pruned_statistics=4 total → 4 matched, page_index_pages_pruned=0 total → 0 matched, limit_pruned_row_groups=0 total → 0 matched, scan_efficiency_ratio=27.52% (926/3.37 K)] + +# An IN list longer than 20 values uses the compact form, when the limit +# allows it. Only row group 3 (`a` in [30, 32]) can match. A table reads +# the limit when it is created, so `t_in` is created after the `set`. +statement ok +set datafusion.execution.parquet.max_in_list_size = 100; + +statement ok +CREATE EXTERNAL TABLE t_in +STORED AS PARQUET +LOCATION 'test_files/scratch/parquet_struct_field_pruning/data.parquet'; + +query TT +EXPLAIN ANALYZE SELECT * FROM t_in WHERE s['a'] IN (30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50); +---- +Plan with Metrics +01)FilterExec: __datafusion_extracted_1@0 IN (SET) ([30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]), projection=[s@1, id@2], metrics=[output_rows=3, selectivity=25% (3/12)] +02)--RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1, metrics=[output_rows=12] +03)----DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/parquet_struct_field_pruning/data.parquet]]}, projection=[get_field(s@0, a) as __datafusion_extracted_1, s, id], file_type=parquet, predicate=get_field(s@0, a) IN (SET) ([30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]), metrics=[output_rows=12, files_ranges_pruned_statistics=1 total → 1 matched, row_groups_pruned_statistics=4 total → 4 matched, page_index_pages_pruned=0 total → 0 matched, limit_pruned_row_groups=0 total → 0 matched, scan_efficiency_ratio=27.52% (926/3.37 K)] + +statement ok +DROP TABLE t_in; + +statement ok +reset datafusion.execution.parquet.max_in_list_size; + +statement ok +DROP TABLE t; + +# Two files with one row group each. The row group of the file with +# `a` in [30, 32] is pruned by statistics, so the scan reads no data pages +# from it. File-level pruning (`files_ranges_pruned_statistics`) is not +# covered: it reads `Statistics`, which has no struct field statistics. +statement ok +COPY (SELECT named_struct('a', column1) AS s FROM (VALUES (1), (2), (3))) +TO 'test_files/scratch/parquet_struct_field_pruning/two_files/low.parquet' +STORED AS PARQUET; + +statement ok +COPY (SELECT named_struct('a', column1) AS s FROM (VALUES (30), (31), (32))) +TO 'test_files/scratch/parquet_struct_field_pruning/two_files/high.parquet' +STORED AS PARQUET; + +statement ok +CREATE EXTERNAL TABLE two_files +STORED AS PARQUET +LOCATION 'test_files/scratch/parquet_struct_field_pruning/two_files/'; + +query ? +SELECT s FROM two_files WHERE s['a'] < 10 ORDER BY s['a']; +---- +{a: 1} +{a: 2} +{a: 3} + +query TT +EXPLAIN ANALYZE SELECT * FROM two_files WHERE s['a'] < 10; +---- +Plan with Metrics +01)FilterExec: __datafusion_extracted_1@0 < 10, projection=[s@1], metrics=[output_rows=3, selectivity=50% (3/6)] +02)--RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=2, metrics=[output_rows=6] +03)----DataSourceExec: file_groups={2 groups: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/parquet_struct_field_pruning/two_files/high.parquet], [WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/parquet_struct_field_pruning/two_files/low.parquet]]}, projection=[get_field(s@0, a) as __datafusion_extracted_1, s], file_type=parquet, predicate=get_field(s@0, a) < 10, metrics=[output_rows=6, files_ranges_pruned_statistics=2 total → 2 matched, row_groups_pruned_statistics=2 total → 2 matched, page_index_pages_pruned=0 total → 0 matched, limit_pruned_row_groups=0 total → 0 matched, scan_efficiency_ratio=26.69% (158/592)] + +statement ok +DROP TABLE two_files; + +# One row group with 10 pages of 10 rows each. Page i holds `a` in +# [10 * i, 10 * i + 9], so `s['a'] >= 85` can skip 8 of the 10 pages. +statement ok +COPY (SELECT named_struct('a', value) AS s FROM generate_series(0, 99)) +TO 'test_files/scratch/parquet_struct_field_pruning/pages.parquet' +STORED AS PARQUET +OPTIONS ( + 'format.max_row_group_size' '100', + 'format.data_page_row_count_limit' '10', + 'format.write_batch_size' '10' +); + +statement ok +CREATE EXTERNAL TABLE pages +STORED AS PARQUET +LOCATION 'test_files/scratch/parquet_struct_field_pruning/pages.parquet'; + +query I +SELECT count(*) FROM pages WHERE s['a'] >= 85; +---- +15 + +query TT +EXPLAIN ANALYZE SELECT * FROM pages WHERE s['a'] >= 85; +---- +Plan with Metrics +01)FilterExec: __datafusion_extracted_1@0 >= 85, projection=[s@1], metrics=[output_rows=15, selectivity=15% (15/100)] +02)--RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1, metrics=[output_rows=100] +03)----DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/parquet_struct_field_pruning/pages.parquet]]}, projection=[get_field(s@0, a) as __datafusion_extracted_1, s], file_type=parquet, predicate=get_field(s@0, a) >= 85, metrics=[output_rows=100, files_ranges_pruned_statistics=1 total → 1 matched, row_groups_pruned_statistics=1 total → 1 matched, page_index_pages_pruned=0 total → 0 matched, limit_pruned_row_groups=0 total → 0 matched, scan_efficiency_ratio=43.97% (624/1.42 K)] + +statement ok +DROP TABLE pages; + +# Dynamic TopK: 5 row groups of 3 rows, `s['a']` = 14 down to 0, so the +# first row group holds the top 3. After it, the TopK filter +# `s['a'] IS NULL OR s['a'] > 12` can skip the other 4 row groups. +statement ok +COPY (SELECT named_struct('a', 14 - value) AS s, 14 - value AS v FROM generate_series(0, 14)) +TO 'test_files/scratch/parquet_struct_field_pruning/topk.parquet' +STORED AS PARQUET +OPTIONS ('format.max_row_group_size' '3'); + +statement ok +CREATE EXTERNAL TABLE topk +STORED AS PARQUET +LOCATION 'test_files/scratch/parquet_struct_field_pruning/topk.parquet'; + +statement ok +set datafusion.execution.parquet.pushdown_filters = true; + +statement ok +set datafusion.execution.target_partitions = 1; + +# `row_groups_pruned_dynamic_filter` is an uncategorized metric. +statement ok +set datafusion.explain.analyze_categories = 'rows,uncategorized'; + +query I +SELECT s['a'] FROM topk ORDER BY s['a'] DESC LIMIT 3; +---- +14 +13 +12 + +# The same query on a plain column, for comparison. +query TT +EXPLAIN ANALYZE SELECT v FROM topk ORDER BY v DESC LIMIT 3; +---- +Plan with Metrics +01)SortExec: TopK(fetch=3), expr=[v@0 DESC], preserve_partitioning=[false], filter=[v@0 IS NULL OR v@0 > 12], metrics=[output_rows=3] +02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/parquet_struct_field_pruning/topk.parquet]]}, projection=[v], file_type=parquet, predicate=DynamicFilter [ v@1 IS NULL OR v@1 > 12 ], sort_order_for_reorder=[v@0 DESC], reverse_row_groups=true, dynamic_rg_pruning=eligible, pruning_predicate=v_null_count@0 > 0 OR v_null_count@0 != row_count@2 AND v_max@1 > 12, required_guarantees=[], metrics=[output_rows=3, files_ranges_pruned_statistics=1 total → 1 matched, row_groups_pruned_statistics=5 total → 5 matched, page_index_pages_pruned=0 total → 0 matched, limit_pruned_row_groups=0 total → 0 matched, row_groups_pruned_dynamic_filter=4, scan_efficiency_ratio=2.66% (73/2.75 K)] + +query TT +EXPLAIN ANALYZE SELECT s['a'] FROM topk ORDER BY s['a'] DESC LIMIT 3; +---- +Plan with Metrics +01)SortExec: TopK(fetch=3), expr=[topk.s[a]@0 DESC], preserve_partitioning=[false], filter=[topk.s[a]@0 IS NULL OR topk.s[a]@0 > 12], metrics=[output_rows=3] +02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/parquet_struct_field_pruning/topk.parquet]]}, projection=[get_field(s@0, a) as topk.s[a]], file_type=parquet, predicate=DynamicFilter [ get_field(s@0, a) IS NULL OR get_field(s@0, a) > 12 ], dynamic_rg_pruning=eligible, metrics=[output_rows=3, files_ranges_pruned_statistics=1 total → 1 matched, row_groups_pruned_statistics=5 total → 5 matched, page_index_pages_pruned=0 total → 0 matched, limit_pruned_row_groups=0 total → 0 matched, row_groups_pruned_dynamic_filter=0, scan_efficiency_ratio=14.08% (387/2.75 K)] + +statement ok +reset datafusion.execution.parquet.pushdown_filters; + +statement ok +set datafusion.execution.target_partitions = 4; + +statement ok +set datafusion.explain.analyze_categories = 'rows'; + +statement ok +DROP TABLE topk; + +# Struct type drift: the file stores `s.a` as INT, the table declares +# DOUBLE. The schema adapter rewrites `s['a']` to +# `CAST(get_field(s, 'a') AS DOUBLE)` for this file. Comparing with 6.5 +# keeps the cast, because 6.5 has no INT equivalent. +statement ok +COPY (SELECT named_struct('a', CAST(value AS INT)) AS s FROM generate_series(0, 8)) +TO 'test_files/scratch/parquet_struct_field_pruning/drift/data.parquet' +STORED AS PARQUET +OPTIONS ('format.max_row_group_size' '3'); + +statement ok +CREATE EXTERNAL TABLE drift (s STRUCT) +STORED AS PARQUET +LOCATION 'test_files/scratch/parquet_struct_field_pruning/drift/'; + +query R +SELECT s['a'] FROM drift WHERE s['a'] > 6.5 ORDER BY s['a']; +---- +7 +8 + +# Row groups 0 and 1 (`a` in [0, 5]) can be pruned. +query TT +EXPLAIN ANALYZE SELECT * FROM drift WHERE s['a'] > 6.5; +---- +Plan with Metrics +01)FilterExec: __datafusion_extracted_1@0 > 6.5, projection=[s@1], metrics=[output_rows=2, selectivity=22.22% (2/9)] +02)--RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1, metrics=[output_rows=9] +03)----DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/parquet_struct_field_pruning/drift/data.parquet]]}, projection=[get_field(s@0, a) as __datafusion_extracted_1, s], file_type=parquet, predicate=get_field(s@0, a) > 6.5, metrics=[output_rows=9, files_ranges_pruned_statistics=1 total → 1 matched, row_groups_pruned_statistics=3 total → 3 matched, page_index_pages_pruned=0 total → 0 matched, limit_pruned_row_groups=0 total → 0 matched, scan_efficiency_ratio=21.39% (213/996)] + +statement ok +DROP TABLE drift; + +# Bloom filters on struct fields. Each row group spans a wide range, so +# min/max cannot rule out `s['a'] = 50`, but the bloom filters can. +# +# | Row group | s.a | s.i8 | s.d | s.dec | +# |-----------|---------|--------|---------------------------|--------------| +# | 0 | 1, 100 | 1, 100 | 2024-01-01, 2024-04-09 | 1.00, 100.00 | +# | 1 | 2, 99 | 2, 99 | 2024-01-02, 2024-04-08 | 2.00, 99.00 | +statement ok +COPY ( + SELECT named_struct( + 'a', column1, + 'i8', CAST(column1 AS TINYINT), + 'd', CAST('2024-01-01' AS DATE) + CAST(column1 - 1 AS INT), + 'dec', CAST(column1 AS DECIMAL(10, 2)) + ) AS s + FROM (VALUES (1), (100), (2), (99)) +) +TO 'test_files/scratch/parquet_struct_field_pruning/bloom.parquet' +STORED AS PARQUET +OPTIONS ( + 'format.max_row_group_size' '2', + 'format.bloom_filter_on_write' 'true' +); + +statement ok +CREATE EXTERNAL TABLE bloom +STORED AS PARQUET +LOCATION 'test_files/scratch/parquet_struct_field_pruning/bloom.parquet'; + +# Both row groups can be skipped by their bloom filters. +query TT +EXPLAIN ANALYZE SELECT * FROM bloom WHERE s['a'] = 50; +---- +Plan with Metrics +01)FilterExec: __datafusion_extracted_1@0 = 50, projection=[s@1], metrics=[output_rows=0, selectivity=0% (0/4)] +02)--RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1, metrics=[output_rows=4] +03)----DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/parquet_struct_field_pruning/bloom.parquet]]}, projection=[get_field(s@0, a) as __datafusion_extracted_1, s], file_type=parquet, predicate=get_field(s@0, a) = 50, metrics=[output_rows=4, files_ranges_pruned_statistics=1 total → 1 matched, row_groups_pruned_statistics=2 total → 2 matched, page_index_pages_pruned=0 total → 0 matched, limit_pruned_row_groups=0 total → 0 matched, scan_efficiency_ratio=20.1% (560/2.79 K)] + +# `!=` can never be proven false by a bloom filter. +query I +SELECT count(*) FROM bloom WHERE s['a'] != 50; +---- +4 + +# Values that are present are found for every field type. +query I +SELECT count(*) FROM bloom WHERE s['a'] = 99; +---- +1 + +query I +SELECT count(*) FROM bloom WHERE s['i8'] = 99; +---- +1 + +query I +SELECT count(*) FROM bloom WHERE s['d'] = DATE '2024-04-08'; +---- +1 + +query I +SELECT count(*) FROM bloom WHERE s['dec'] = 99.00; +---- +1 + +statement ok +DROP TABLE bloom; + +# Hash join dynamic filter. The build side `u` has ids 0, 5000 and 9999, so +# its bounds [0, 9999] cover all 10 row groups of `hj`, but its key bitmap +# only reaches row groups 0, 5 and 9. A hash-lookup filter (forced by an +# IN-list limit of 0) carries that bitmap to the probe-side scan. +statement ok +COPY (SELECT named_struct('a', value) AS s, value AS v FROM generate_series(0, 9999) ORDER BY value) +TO 'test_files/scratch/parquet_struct_field_pruning/hj.parquet' +STORED AS PARQUET +OPTIONS ('format.max_row_group_size' '1000'); + +statement ok +CREATE EXTERNAL TABLE hj +STORED AS PARQUET +LOCATION 'test_files/scratch/parquet_struct_field_pruning/hj.parquet'; + +statement ok +CREATE TABLE u(id BIGINT) AS VALUES (0), (5000), (9999); + +statement ok +set datafusion.execution.target_partitions = 1; + +statement ok +set datafusion.optimizer.hash_join_inlist_pushdown_max_size = 0; + +statement ok +set datafusion.optimizer.hash_join_inlist_pushdown_max_distinct_values = 0; + +query I +SELECT hj.v FROM u JOIN hj ON hj.s['a'] = u.id ORDER BY hj.v; +---- +0 +5000 +9999 + +# The same join on a plain column, for comparison. +query TT +EXPLAIN ANALYZE SELECT hj.v FROM u JOIN hj ON hj.v = u.id; +---- +Plan with Metrics +01)HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(id@0, v@0)], projection=[v@1], metrics=[output_rows=3, avg_fanout=100% (3/3), probe_hit_rate=0.1% (3/3.00 K)] +02)--DataSourceExec: partitions=1, partition_sizes=[1], metrics=[] +03)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/parquet_struct_field_pruning/hj.parquet]]}, projection=[v], output_ordering=[v@0 ASC NULLS LAST], file_type=parquet, predicate=DynamicFilter [ v@1 >= 0 AND v@1 <= 9999 AND hash_lookup ], dynamic_rg_pruning=eligible, pruning_predicate=v_null_count@1 != row_count@2 AND v_max@0 >= 0 AND v_null_count@1 != row_count@2 AND v_min@3 <= 9999 AND v_null_count@1 != row_count@2 AND KEY_RANGE_BITMAP(v_min@3, v_max@0, 3/16384 buckets), required_guarantees=[], metrics=[output_rows=3.00 K, files_ranges_pruned_statistics=1 total → 1 matched, row_groups_pruned_statistics=10 total → 3 matched, page_index_pages_pruned=3 total → 3 matched, limit_pruned_row_groups=0 total → 0 matched, scan_efficiency_ratio=14.55% (7.59 K/52.19 K)] + +query TT +EXPLAIN ANALYZE SELECT hj.v FROM u JOIN hj ON hj.s['a'] = u.id; +---- +Plan with Metrics +01)HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(id@0, __datafusion_extracted_1@0)], projection=[v@2], metrics=[output_rows=3, avg_fanout=100% (3/3), probe_hit_rate=0.03% (3/10.00 K)] +02)--DataSourceExec: partitions=1, partition_sizes=[1], metrics=[] +03)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/parquet_struct_field_pruning/hj.parquet]]}, projection=[get_field(s@0, a) as __datafusion_extracted_1, v], output_ordering=[v@1 ASC NULLS LAST], file_type=parquet, predicate=DynamicFilter [ get_field(s@0, a) >= 0 AND get_field(s@0, a) <= 9999 AND hash_lookup ], dynamic_rg_pruning=eligible, metrics=[output_rows=10.00 K, files_ranges_pruned_statistics=1 total → 1 matched, row_groups_pruned_statistics=10 total → 10 matched, page_index_pages_pruned=0 total → 0 matched, limit_pruned_row_groups=0 total → 0 matched, scan_efficiency_ratio=92.57% (48.31 K/52.19 K)] + +statement ok +reset datafusion.optimizer.hash_join_inlist_pushdown_max_size; + +statement ok +reset datafusion.optimizer.hash_join_inlist_pushdown_max_distinct_values; + +statement ok +set datafusion.execution.target_partitions = 4; + +statement ok +DROP TABLE u; + +statement ok +DROP TABLE hj; + +statement ok +reset datafusion.explain.analyze_level; + +statement ok +reset datafusion.explain.analyze_categories; + +# A struct field can be NULL when the struct column itself is not +# nullable. A row group is fully matched only if the field has no nulls. +statement ok +COPY (SELECT named_struct('a', column1) AS s, column2 AS id FROM (VALUES (1, 1), (CAST(NULL AS INT), 2), (3, 3))) +TO 'test_files/scratch/parquet_struct_field_pruning/full_match/f.parquet' +STORED AS PARQUET; + +statement ok +CREATE EXTERNAL TABLE full_match STORED AS PARQUET +LOCATION 'test_files/scratch/parquet_struct_field_pruning/full_match/f.parquet'; + +statement ok +set datafusion.execution.parquet.pushdown_filters = true; + +query I +SELECT id FROM full_match WHERE s['a'] <= 5 ORDER BY id; +---- +1 +3 + +statement ok +set datafusion.execution.parquet.pushdown_filters = false; + +# A real column whose name looks like a statistics placeholder must not +# take the bloom filter guarantees of a struct field. +statement ok +COPY (SELECT column1 AS "__datafusion_pruning_stats_expr_0", named_struct('a', column2) AS s FROM (VALUES (99, 7), (6, 8))) +TO 'test_files/scratch/parquet_struct_field_pruning/placeholder/f.parquet' +STORED AS PARQUET OPTIONS ('format.bloom_filter_on_write' 'true'); + +statement ok +CREATE EXTERNAL TABLE placeholder STORED AS PARQUET +LOCATION 'test_files/scratch/parquet_struct_field_pruning/placeholder/f.parquet'; + +query I +SELECT count(*) FROM placeholder WHERE "__datafusion_pruning_stats_expr_0" = 99 AND s['a'] > 0; +---- +1 + +query I +SELECT count(*) FROM placeholder WHERE s['a'] > 0 AND "__datafusion_pruning_stats_expr_0" IN (99, 100); +---- +1 From 869d67e5063310040c0af38245a9add888a38c26 Mon Sep 17 00:00:00 2001 From: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Date: Tue, 1 Dec 2026 17:29:20 -0800 Subject: [PATCH 2/2] docs: explain why the struct field test writes its own footer A reviewer asked why the test builds a file without a null count by hand. The doc comment gives the reason and the method. Co-Authored-By: Claude Fable 5.2 --- datafusion/core/tests/parquet/struct_field_pruning.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/datafusion/core/tests/parquet/struct_field_pruning.rs b/datafusion/core/tests/parquet/struct_field_pruning.rs index d24809ef7ed2d..9a2b62d50f7a2 100644 --- a/datafusion/core/tests/parquet/struct_field_pruning.rs +++ b/datafusion/core/tests/parquet/struct_field_pruning.rs @@ -19,6 +19,14 @@ use tempfile::NamedTempFile; /// Writes `s: {a: Int64}` with rows `{a: 1}` and `{a: null}` in one row /// group. The footer keeps the min and max of `s.a`, but not its null count. +/// +/// The null count is optional in the Parquet format, and some writers omit +/// it. A scan that reads a missing null count as zero prunes this row group +/// for `s['a'] IS NULL` and loses a row. +/// +/// The Arrow writer always writes the null count. So this function writes the +/// file with the Arrow writer, then writes the footer again without the null +/// count. fn struct_file_without_null_count() -> NamedTempFile { let a = Field::new("a", DataType::Int64, true); let schema = Arc::new(Schema::new(vec![Field::new(