datafusion-datasource 56.0.0

datafusion-datasource
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
// 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

//! Use statistics to optimize physical planning.
//!
//! Currently, this module houses code to sort file groups if they are non-overlapping with
//! respect to the required sort order. See [`MinMaxStatistics`]

use std::sync::Arc;

use crate::PartitionedFile;
use crate::file_groups::FileGroup;

use arrow::array::RecordBatch;
use arrow::compute::{SortColumn, SortOptions};
use arrow::datatypes::{Field, SchemaRef};
use arrow::row::{Row, Rows};
use datafusion_common::stats::{NdvFallback, Precision};
use datafusion_common::{
    DataFusionError, Result, ScalarValue, plan_datafusion_err, plan_err,
};
use datafusion_physical_expr::expressions::Column;
use datafusion_physical_expr_common::sort_expr::{LexOrdering, PhysicalSortExpr};
use datafusion_physical_plan::Statistics;

/// A normalized representation of file min/max statistics that allows for efficient sorting & comparison.
/// The min/max values are ordered by [`Self::sort_order`].
/// Furthermore, any columns that are reversed in the sort order have their min/max values swapped.
pub(crate) struct MinMaxStatistics {
    min_by_sort_order: Rows,
    max_by_sort_order: Rows,
    sort_order: LexOrdering,
}

impl MinMaxStatistics {
    /// Sort order used to sort the statistics
    #[expect(unused)]
    pub fn sort_order(&self) -> &LexOrdering {
        &self.sort_order
    }

    /// Min value at index
    #[expect(unused)]
    pub fn min(&'_ self, idx: usize) -> Row<'_> {
        self.min_by_sort_order.row(idx)
    }

    /// Max value at index
    pub fn max(&'_ self, idx: usize) -> Row<'_> {
        self.max_by_sort_order.row(idx)
    }

    pub fn new_from_files<'a>(
        projected_sort_order: &LexOrdering, // Sort order with respect to projected schema
        projected_schema: &SchemaRef,       // Projected schema
        projection: Option<&[usize]>, // Indices of projection in full table schema (None = all columns)
        files: impl IntoIterator<Item = &'a PartitionedFile>,
    ) -> Result<Self> {
        let Some(statistics_and_partition_values) = files
            .into_iter()
            .map(|file| {
                file.statistics
                    .as_ref()
                    .zip(Some(file.partition_values.as_slice()))
            })
            .collect::<Option<Vec<_>>>()
        else {
            return plan_err!("Parquet file missing statistics");
        };

        // Helper function to get min/max statistics for a given column of projected_schema
        let get_min_max = |i: usize,
                           field: &Field,
                           options: SortOptions|
         -> Result<(Vec<ScalarValue>, Vec<ScalarValue>)> {
            let null = ScalarValue::try_new_null(field.data_type())?;
            Ok(statistics_and_partition_values
                .iter()
                .map(|(s, pv)| {
                    if i < s.column_statistics.len() {
                        let column_statistics = &s.column_statistics[i];
                        let all_null = matches!(
                            (s.num_rows, column_statistics.null_count),
                            (Precision::Exact(rows), Precision::Exact(nulls))
                                if rows > 0 && rows == nulls
                        );
                        let may_have_nulls = match column_statistics.null_count {
                            Precision::Exact(nulls) => nulls > 0,
                            _ => field.is_nullable(),
                        };
                        // Self::new swaps the bounds for descending sort keys.
                        let nulls_on_min = options.nulls_first != options.descending;
                        let min = if all_null || (may_have_nulls && nulls_on_min) {
                            Some(null.clone())
                        } else {
                            column_statistics
                                .min_value
                                .get_value()
                                .filter(|value| !value.is_null())
                                .cloned()
                        };
                        let max = if all_null || (may_have_nulls && !nulls_on_min) {
                            Some(null.clone())
                        } else {
                            column_statistics
                                .max_value
                                .get_value()
                                .filter(|value| !value.is_null())
                                .cloned()
                        };
                        min.zip(max)
                            .ok_or_else(|| plan_datafusion_err!("statistics not found"))
                    } else if let Some(partition_value) =
                        pv.get(i - s.column_statistics.len())
                    {
                        Ok((partition_value.clone(), partition_value.clone()))
                    } else {
                        Err(plan_datafusion_err!(
                            "statistics not found for partition, expected at most {}",
                            s.column_statistics.len()
                        ))
                    }
                })
                .collect::<Result<Vec<_>>>()?
                .into_iter()
                .unzip())
        };

        let Some(sort_columns) =
            sort_columns_from_physical_sort_exprs(projected_sort_order)
        else {
            return plan_err!("sort expression must be on column");
        };

        // Project the schema & sort order down to just the relevant columns
        let min_max_schema = Arc::new(
            projected_schema
                .project(&(sort_columns.iter().map(|c| c.index()).collect::<Vec<_>>()))?,
        );

        let min_max_sort_order = projected_sort_order
            .iter()
            .zip(sort_columns.iter())
            .enumerate()
            .map(|(idx, (sort_expr, col))| {
                let expr = Arc::new(Column::new(col.name(), idx));
                PhysicalSortExpr::new(expr, sort_expr.options)
            });
        // Safe to `unwrap` as we know that sort columns are non-empty:
        let min_max_sort_order = LexOrdering::new(min_max_sort_order).unwrap();

        let (min_values, max_values): (Vec<_>, Vec<_>) = sort_columns
            .iter()
            .zip(projected_sort_order.iter())
            .map(|(c, sort_expr)| {
                // Reverse the projection to get the index of the column in the full statistics
                // The file statistics contains _every_ column , but the sort column's index()
                // refers to the index in projected_schema
                let i = projection
                    .map(|p| p[c.index()])
                    .unwrap_or_else(|| c.index());

                let (min, max) =
                    get_min_max(i, projected_schema.field(c.index()), sort_expr.options)
                        .map_err(|e| {
                            e.context(format!("get min/max for column: '{}'", c.name()))
                        })?;
                Ok((
                    ScalarValue::iter_to_array(min)?,
                    ScalarValue::iter_to_array(max)?,
                ))
            })
            .collect::<Result<Vec<_>>>()
            .map_err(|e| e.context("collect min/max values"))?
            .into_iter()
            .unzip();

        let min_batch = RecordBatch::try_new(Arc::clone(&min_max_schema), min_values)
            .map_err(|e| {
                DataFusionError::ArrowError(
                    Box::new(e),
                    Some("\ncreate min batch".to_string()),
                )
            })?;
        let max_batch = RecordBatch::try_new(Arc::clone(&min_max_schema), max_values)
            .map_err(|e| {
                DataFusionError::ArrowError(
                    Box::new(e),
                    Some("\ncreate max batch".to_string()),
                )
            })?;

        Self::new(&min_max_sort_order, &min_max_schema, min_batch, max_batch)
    }

    #[expect(clippy::needless_pass_by_value)]
    pub fn new(
        sort_order: &LexOrdering,
        schema: &SchemaRef,
        min_values: RecordBatch,
        max_values: RecordBatch,
    ) -> Result<Self> {
        use arrow::row::*;

        let sort_fields = sort_order
            .iter()
            .map(|expr| {
                expr.expr
                    .data_type(schema)
                    .map(|data_type| SortField::new_with_options(data_type, expr.options))
            })
            .collect::<Result<Vec<_>>>()
            .map_err(|e| e.context("create sort fields"))?;
        let converter = RowConverter::new(sort_fields)?;

        let Some(sort_columns) = sort_columns_from_physical_sort_exprs(sort_order) else {
            return plan_err!("sort expression must be on column");
        };

        // swap min/max if they're reversed in the ordering
        let (new_min_cols, new_max_cols): (Vec<_>, Vec<_>) = sort_order
            .iter()
            .zip(sort_columns.iter().copied())
            .map(|(sort_expr, column)| {
                let maxes = max_values.column_by_name(column.name());
                let mins = min_values.column_by_name(column.name());
                let opt_value = if sort_expr.options.descending {
                    maxes.zip(mins)
                } else {
                    mins.zip(maxes)
                };
                opt_value.ok_or_else(|| {
                    plan_datafusion_err!(
                        "missing column in MinMaxStatistics::new: '{}'",
                        column.name()
                    )
                })
            })
            .collect::<Result<Vec<_>>>()?
            .into_iter()
            .unzip();

        let [min, max] = [new_min_cols, new_max_cols].map(|cols| {
            let values = RecordBatch::try_new(
                min_values.schema(),
                cols.into_iter().cloned().collect(),
            )?;
            let sorting_columns = sort_order
                .iter()
                .zip(sort_columns.iter().copied())
                .map(|(sort_expr, column)| {
                    let schema = values.schema();
                    let idx = schema.index_of(column.name())?;

                    Ok(SortColumn {
                        values: Arc::clone(values.column(idx)),
                        options: Some(sort_expr.options),
                    })
                })
                .collect::<Result<Vec<_>>>()
                .map_err(|e| e.context("create sorting columns"))?;
            converter
                .convert_columns(
                    &sorting_columns
                        .into_iter()
                        .map(|c| c.values)
                        .collect::<Vec<_>>(),
                )
                .map_err(|e| {
                    DataFusionError::ArrowError(
                        Box::new(e),
                        Some("convert columns".to_string()),
                    )
                })
        });

        Ok(Self {
            min_by_sort_order: min.map_err(|e| e.context("build min rows"))?,
            max_by_sort_order: max.map_err(|e| e.context("build max rows"))?,
            sort_order: sort_order.clone(),
        })
    }

    /// Return a sorted list of the min statistics together with the original indices
    pub fn min_values_sorted(&self) -> Vec<(usize, Row<'_>)> {
        let mut sort: Vec<_> = self.min_by_sort_order.iter().enumerate().collect();
        sort.sort_unstable_by_key(|(_, row)| *row);
        sort
    }

    /// Check if the min/max statistics are in order and non-overlapping
    /// (or touching at boundaries)
    pub fn is_sorted(&self) -> bool {
        self.max_by_sort_order
            .iter()
            .zip(self.min_by_sort_order.iter().skip(1))
            .all(|(max, next_min)| max <= next_min)
    }
}

fn sort_columns_from_physical_sort_exprs(
    sort_order: &LexOrdering,
) -> Option<Vec<&Column>> {
    sort_order
        .iter()
        .map(|expr| expr.expr.downcast_ref::<Column>())
        .collect()
}

/// Computes the summary statistics for a group of files(`FileGroup` level's statistics).
///
/// This function combines statistics from all files in the file group to create
/// summary statistics. It handles the following aspects:
/// - Merges row counts and byte sizes across files
/// - Computes column-level statistics like min/max values
/// - Maintains appropriate precision information (exact, inexact, absent)
///
/// # Parameters
/// * `file_group` - The group of files to process
/// * `file_schema` - Schema of the files
/// * `collect_stats` - Whether to collect statistics (if false, returns original file group)
///
/// # Returns
/// A new file group with summary statistics attached
#[expect(clippy::needless_pass_by_value)]
pub fn compute_file_group_statistics(
    file_group: FileGroup,
    file_schema: SchemaRef,
    collect_stats: bool,
) -> Result<FileGroup> {
    if !collect_stats {
        return Ok(file_group);
    }

    let file_group_stats = file_group.iter().filter_map(|file| {
        let stats = file.statistics.as_ref()?;
        Some(stats.as_ref())
    });
    let statistics = Statistics::try_merge_iter_with_ndv_fallback(
        file_group_stats,
        &file_schema,
        NdvFallback::Max,
    )?;

    Ok(file_group.with_statistics(Arc::new(statistics)))
}

/// Computes statistics for all files across multiple file groups.
///
/// This function:
/// 1. Computes statistics for each individual file group
/// 2. Summary statistics across all file groups
/// 3. Optionally marks statistics as inexact
///
/// # Parameters
/// * `file_groups` - Vector of file groups to process
/// * `table_schema` - Schema of the table
/// * `collect_stats` - Whether to collect statistics
/// * `inexact_stats` - Whether to mark the resulting statistics as inexact
///
/// # Returns
/// A tuple containing:
/// * The processed file groups with their individual statistics attached
/// * The summary statistics across all file groups, aka all files summary statistics
#[expect(clippy::needless_pass_by_value)]
pub fn compute_all_files_statistics(
    file_groups: Vec<FileGroup>,
    table_schema: SchemaRef,
    collect_stats: bool,
    inexact_stats: bool,
) -> Result<(Vec<FileGroup>, Statistics)> {
    let file_groups_with_stats = file_groups
        .into_iter()
        .map(|file_group| {
            compute_file_group_statistics(
                file_group,
                Arc::clone(&table_schema),
                collect_stats,
            )
        })
        .collect::<Result<Vec<_>>>()?;

    // Then summary statistics across all file groups
    let file_groups_statistics = file_groups_with_stats
        .iter()
        .filter_map(|file_group| file_group.file_statistics(None));

    let mut statistics = Statistics::try_merge_iter_with_ndv_fallback(
        file_groups_statistics,
        &table_schema,
        NdvFallback::Max,
    )?;

    if inexact_stats {
        statistics = statistics.to_inexact()
    }

    Ok((file_groups_with_stats, statistics))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::PartitionedFile;
    use crate::file_groups::FileGroup;
    use arrow::datatypes::{DataType, Field, Schema};
    use datafusion_physical_plan::ColumnStatistics;

    fn test_schema() -> SchemaRef {
        Arc::new(Schema::new(vec![Field::new("a", DataType::Int64, true)]))
    }

    fn utf8_file_stats(ndv: usize, min: &str, max: &str) -> Statistics {
        Statistics {
            num_rows: Precision::Exact(1),
            total_byte_size: Precision::Exact(16),
            column_statistics: vec![ColumnStatistics {
                null_count: Precision::Exact(0),
                max_value: Precision::Exact(ScalarValue::Utf8(Some(max.to_string()))),
                min_value: Precision::Exact(ScalarValue::Utf8(Some(min.to_string()))),
                sum_value: Precision::Absent,
                distinct_count: Precision::Exact(ndv),
                byte_size: Precision::Exact(16),
            }],
        }
    }

    fn file_with_stats(path: &str, stats: Statistics) -> PartitionedFile {
        PartitionedFile::new(path, 1).with_statistics(Arc::new(stats))
    }

    #[test]
    fn test_compute_file_group_statistics_uses_max_ndv_fallback() -> Result<()> {
        let schema = Arc::new(Schema::new(vec![Field::new("c1", DataType::Utf8, true)]));
        let file_group = FileGroup::new(vec![
            file_with_stats("f1.parquet", utf8_file_stats(5, "a", "x")),
            file_with_stats("f2.parquet", utf8_file_stats(8, "b", "z")),
        ]);

        let file_group =
            compute_file_group_statistics(file_group, Arc::clone(&schema), true)?;
        let stats = file_group.file_statistics(None).unwrap();

        assert_eq!(
            stats.column_statistics[0].distinct_count,
            Precision::Inexact(8)
        );
        assert_eq!(
            stats.column_statistics[0].min_value,
            Precision::Exact(ScalarValue::Utf8(Some("a".to_string())))
        );
        assert_eq!(
            stats.column_statistics[0].max_value,
            Precision::Exact(ScalarValue::Utf8(Some("z".to_string())))
        );

        Ok(())
    }

    #[test]
    fn test_compute_all_files_statistics_uses_max_ndv_fallback() -> Result<()> {
        let schema = Arc::new(Schema::new(vec![Field::new("c1", DataType::Utf8, true)]));
        let file_groups = vec![
            FileGroup::new(vec![
                file_with_stats("f1.parquet", utf8_file_stats(5, "a", "x")),
                file_with_stats("f2.parquet", utf8_file_stats(8, "b", "z")),
            ]),
            FileGroup::new(vec![
                file_with_stats("f3.parquet", utf8_file_stats(3, "c", "w")),
                file_with_stats("f4.parquet", utf8_file_stats(6, "d", "y")),
            ]),
        ];

        let (file_groups, stats) =
            compute_all_files_statistics(file_groups, schema, true, false)?;

        assert_eq!(
            file_groups[0]
                .file_statistics(None)
                .unwrap()
                .column_statistics[0]
                .distinct_count,
            Precision::Inexact(8)
        );
        assert_eq!(
            file_groups[1]
                .file_statistics(None)
                .unwrap()
                .column_statistics[0]
                .distinct_count,
            Precision::Inexact(6)
        );
        assert_eq!(
            stats.column_statistics[0].distinct_count,
            Precision::Inexact(8)
        );

        Ok(())
    }

    #[test]
    fn min_max_statistics_missing_column_stats_returns_error() {
        let schema = test_schema();
        let sort_order =
            [PhysicalSortExpr::new_default(Arc::new(Column::new("a", 0)))].into();
        let files = [
            file_with_stats("f1.parquet", Statistics::default()),
            file_with_stats("f2.parquet", Statistics::default()),
        ];

        let Err(err) =
            MinMaxStatistics::new_from_files(&sort_order, &schema, None, files.iter())
        else {
            panic!("expected missing statistics error")
        };

        assert!(
            err.to_string()
                .contains("statistics not found for partition"),
            "unexpected error: {err:?}"
        );
    }

    #[test]
    fn min_max_statistics_null_partition_value() -> Result<()> {
        let schema = Arc::new(Schema::new(vec![
            Field::new("a", DataType::Int64, true),
            Field::new("d", DataType::Date32, true),
        ]));
        let sort_order =
            [PhysicalSortExpr::new_default(Arc::new(Column::new("d", 1)))].into();
        let file = |path: &str, d: Option<i32>| {
            let mut pf = PartitionedFile::new(path, 1);
            pf.partition_values = vec![ScalarValue::Date32(d)];
            pf.with_statistics(Arc::new(
                Statistics::new_unknown(&test_schema())
                    .with_num_rows(Precision::Exact(1)),
            ))
        };
        let files = [file("d=1/f.parquet", Some(1)), file("null/f.parquet", None)];

        let stats =
            MinMaxStatistics::new_from_files(&sort_order, &schema, None, files.iter())?;

        let sorted = stats.min_values_sorted();
        assert_eq!(sorted.iter().map(|(i, _)| *i).collect::<Vec<_>>(), [1, 0]);
        assert!(!stats.is_sorted());

        Ok(())
    }

    #[test]
    fn min_max_statistics_null_partition_value_unknown_num_rows() {
        let schema = Arc::new(Schema::new(vec![
            Field::new("a", DataType::Int64, true),
            Field::new("d", DataType::Date32, true),
        ]));
        let sort_order =
            [PhysicalSortExpr::new_default(Arc::new(Column::new("d", 1)))].into();
        let mut pf = PartitionedFile::new("null/f.parquet", 1);
        pf.partition_values = vec![ScalarValue::Date32(None)];
        let files =
            [pf.with_statistics(Arc::new(Statistics::new_unknown(&test_schema())))];

        let Err(err) =
            MinMaxStatistics::new_from_files(&sort_order, &schema, None, files.iter())
        else {
            panic!("expected missing statistics error")
        };
        assert!(
            err.to_string().contains("statistics not found"),
            "unexpected error: {err:?}"
        );
    }
}