datafusion-physical-plan 56.0.0

Physical (ExecutionPlan) implementations for DataFusion query engine
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
// 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

//! Optimizer hooks for rewriting and specializing [`AggregateExec`].

use std::sync::Arc;

use arrow::compute::SortOptions;
use arrow::datatypes::DataType;
use arrow_schema::FieldRef;
use datafusion_common::Result;
use datafusion_common::tree_node::Transformed;
use datafusion_physical_expr::aggregate::AggregateFunctionExpr;
use datafusion_physical_expr::physical_exprs_equal;
use datafusion_physical_expr_common::sort_expr::OrderingRequirements;
use itertools::Itertools;

use super::topk::hash_table::is_supported_hash_key_type;
use super::topk::heap::is_supported_heap_type;
use super::{
    AggregateExec, AggregateKind, AggregateMode, GroupClusteringMode,
    aggregate_expressions,
};
use crate::execution_plan::EmissionType;
use crate::metrics::ExecutionPlanMetricsSet;
use crate::{ExecutionPlan, ExecutionPlanProperties};

// Hooks are grouped by the optimizer rules that use them.
impl AggregateExec {
    // ========================================================================
    // LimitedDistinctAggregation
    // ========================================================================
    /// Try to stop after enough distinct grouping keys have been accumulated.
    ///
    /// The caller must establish that discarding other groups is legal, for
    /// example beneath `SELECT DISTINCT k FROM t LIMIT 10`. The parent limit
    /// remains responsible for enforcing the exact number of rows.
    ///
    /// # Trigger conditions
    ///
    /// - Grouping with no aggregate expressions or aggregate filters.
    /// - Ordering eligible under [`Self::is_unordered_unfiltered_group_by_distinct`],
    ///   including no existing Top-K direction.
    /// - A positive limit that is tighter than any existing limit.
    ///
    /// # Consistency
    ///
    /// This is a safe, atomic optimization: it preserves the grouping keys and
    /// leaves the aggregate in a consistent state. Ineligible aggregates return
    /// `None`. Eligible aggregates are returned unchanged when the limit is zero
    /// or no tighter than the existing limit.
    ///
    /// # Public Only for Internal Use:
    ///
    /// This is not a public API and is for internal use only; see [API policy] for details.
    ///
    /// [API policy]: https://datafusion.apache.org/contributor-guide/api-health.html#internal-public-apis
    #[doc(hidden)]
    pub fn try_optimize_distinct_soft_limit(
        mut self,
        limit: usize,
    ) -> Option<Transformed<Self>> {
        if !self.is_unordered_unfiltered_group_by_distinct() {
            return None;
        }
        if limit == 0
            || self
                .limit_options()
                .is_some_and(|existing| existing.limit <= limit)
        {
            return Some(Transformed::no(self));
        }
        self.kind = AggregateKind::DistinctLimit {
            group_by: Arc::clone(self.group_by()),
            limit,
        };
        Some(Transformed::yes(self))
    }

    /// Returns whether this is a compatible partial/final aggregation pair.
    ///
    /// # Public Only for Internal Use:
    ///
    /// This is not a public API and is for internal use only; see [API policy] for details.
    ///
    /// [API policy]: https://datafusion.apache.org/contributor-guide/api-health.html#internal-public-apis
    #[doc(hidden)]
    pub fn matches_partial(&self, partial: &Self) -> bool {
        matches!(
            (self.mode, partial.mode),
            (
                AggregateMode::Final | AggregateMode::FinalPartitioned,
                AggregateMode::Partial,
            )
        ) && self.group_expr() == &partial.group_expr().as_final()
    }

    /// true, if this Aggregate has a group-by with no required or explicit ordering,
    /// no filtering and no aggregate expressions
    /// This method qualifies the use of the LimitedDistinctAggregation rewrite rule
    /// on an AggregateExec.
    ///
    /// # Public Only for Internal Use:
    ///
    /// This is not a public API and is for internal use only; see [API policy] for details.
    ///
    /// [API policy]: https://datafusion.apache.org/contributor-guide/api-health.html#internal-public-apis
    #[doc(hidden)]
    pub fn is_unordered_unfiltered_group_by_distinct(&self) -> bool {
        if self
            .limit_options()
            .and_then(|config| config.descending)
            .is_some()
        {
            return false;
        }
        // ensure there is a group by
        if self.group_expr().is_empty() && !self.group_expr().has_grouping_set() {
            return false;
        }
        // ensure there are no aggregate expressions
        if !self.aggr_expr().is_empty() {
            return false;
        }
        // ensure there are no filters on aggregate expressions; the above check
        // may preclude this case
        if self.filter_expr().iter().any(|e| e.is_some()) {
            return false;
        }
        // ensure there are no order by expressions
        if !self.aggr_expr().iter().all(|e| e.order_bys().is_empty()) {
            return false;
        }
        // ensure there is no output ordering; can this rule be relaxed?
        if self.properties().output_ordering().is_some() {
            return false;
        }
        // ensure no ordering is required on the input
        if let Some(requirement) = self.required_input_ordering().swap_remove(0) {
            return matches!(requirement, OrderingRequirements::Hard(_));
        }
        true
    }

    // ========================================================================
    // EnsureRequirements / SanityCheckPlan
    // ========================================================================
    /// Whether a candidate input keeps each group in one partition.
    ///
    /// Unlike co-partitioning, aggregation only needs equality groups to be
    /// local. A final stage can inherit that guarantee from a partial stage
    /// even when projecting the grouping expressions loses range metadata.
    /// This check does not require the two stages to be combined.
    ///
    /// # Public Only for Internal Use:
    ///
    /// This is not a public API and is for internal use only; see [API policy] for details.
    ///
    /// [API policy]: https://datafusion.apache.org/contributor-guide/api-health.html#internal-public-apis
    #[doc(hidden)]
    pub fn input_keeps_groups_local(
        &self,
        input: &dyn ExecutionPlan,
        allow_subset: bool,
    ) -> bool {
        if !matches!(
            self.mode,
            AggregateMode::Partial
                | AggregateMode::SinglePartitioned
                | AggregateMode::FinalPartitioned
        ) || self.group_by().is_empty()
            || self.group_by().has_grouping_set()
        {
            return false;
        }

        if input.output_partitioning().keeps_keys_local(
            &self.group_by().input_exprs(),
            input.equivalence_properties(),
            allow_subset,
        ) {
            return true;
        }

        self.mode == AggregateMode::FinalPartitioned
            && input
                .downcast_ref::<AggregateExec>()
                .is_some_and(|partial| {
                    partial.mode == AggregateMode::Partial
                        && physical_exprs_equal(
                            &partial.output_group_expr(),
                            &self.group_by().input_exprs(),
                        )
                        && partial.input_keeps_groups_local(
                            partial.input.as_ref(),
                            allow_subset,
                        )
                })
    }

    // ========================================================================
    // CombinePartialFinalAggregate
    // ========================================================================
    /// Combines adjacent partial and final aggregates in the given plan if they are
    /// compatible. See [`CombinePartialFinalAggregate`] for the invariants required
    /// by this transformation.
    ///
    /// If input plan is not aggregate, this function will short-circuit with
    /// `Transformed::No`.
    ///
    /// ```txt
    /// Before Plan:
    /// AggregateExec(mode=Final)          <-- current plan
    ///   AggregateExec(mode=Partial)
    ///     DataSourceExec
    ///
    /// After Plan:
    /// AggregateExec(mode=Single)         <-- current plan
    ///   DataSourceExec
    /// ```
    ///
    /// # Public Only for Internal Use:
    ///
    /// This is not a public API and is for internal use only; see [API policy] for details.
    ///
    /// [`CombinePartialFinalAggregate`]: https://docs.rs/datafusion/latest/datafusion/physical_optimizer/combine_partial_final_agg/struct.CombinePartialFinalAggregate.html
    /// [API policy]: https://datafusion.apache.org/contributor-guide/api-health.html#internal-public-apis
    #[doc(hidden)]
    pub fn try_combine_partial_final(
        plan: Arc<dyn ExecutionPlan>,
    ) -> Result<Transformed<Arc<dyn ExecutionPlan>>> {
        // Step 1: Ensure the below plan shape
        //
        // AggregateExec(mode=final) <-- current plan
        //   AggregateExec(mode=partial)

        let Some(final_agg) = plan.downcast_ref::<Self>() else {
            return Ok(Transformed::no(plan));
        };
        let mode = match final_agg.mode {
            AggregateMode::Final => AggregateMode::Single,
            AggregateMode::FinalPartitioned => AggregateMode::SinglePartitioned,
            _ => return Ok(Transformed::no(plan)),
        };
        let Some(partial) = final_agg.input.downcast_ref::<Self>() else {
            return Ok(Transformed::no(plan));
        };
        if partial.mode != AggregateMode::Partial {
            return Ok(Transformed::no(plan));
        }

        // Step 2: Check compatibility between partial and final `AggregateExec`

        let final_group_by = final_agg.group_expr();
        let input_group_by = partial.group_expr();
        let final_aggr_expr = final_agg.aggr_expr();
        let input_aggr_expr = partial.aggr_expr();
        let final_filter_expr = final_agg.filter_expr();
        let input_filter_expr = partial.filter_expr();

        // Compare the partial's output expressions with the final's input expressions.
        let can_combine = physical_exprs_equal(
            &input_group_by.output_exprs(),
            &final_group_by.input_exprs(),
        ) && input_group_by.groups() == final_group_by.groups()
            && input_group_by.null_expr().len() == final_group_by.null_expr().len()
            && input_group_by
                .null_expr()
                .iter()
                .zip(final_group_by.null_expr().iter())
                .all(|((lhs_expr, lhs_str), (rhs_expr, rhs_str))| {
                    lhs_expr.eq(rhs_expr) && lhs_str == rhs_str
                })
            && final_aggr_expr.len() == input_aggr_expr.len()
            && final_aggr_expr
                .iter()
                .zip(input_aggr_expr.iter())
                .all(|(final_expr, partial_expr)| final_expr.eq(partial_expr))
            && final_filter_expr.len() == input_filter_expr.len()
            && final_filter_expr.iter().zip(input_filter_expr.iter()).all(
                |(final_expr, partial_expr)| match (final_expr, partial_expr) {
                    (Some(l), Some(r)) => l.eq(r),
                    (None, None) => true,
                    _ => false,
                },
            );
        if !can_combine {
            return Ok(Transformed::no(plan));
        }

        let Ok(mut combined) = Self::try_new(
            mode,
            input_group_by.clone(),
            input_aggr_expr.to_vec(),
            input_filter_expr.to_vec(),
            Arc::clone(partial.input()),
            partial.input_schema(),
        ) else {
            return Ok(Transformed::no(plan));
        };

        // LimitedDistinctAggregation runs before this optimization. Preserve its
        // limit while using the partial's grouping expressions over raw input.
        //
        // The following logic recognizes all possible patterns produced by `LimitedDistinctAggregation`
        // See its comment for details.
        combined.kind = match (&final_agg.kind, &partial.kind) {
            (AggregateKind::General { .. }, AggregateKind::General { .. }) => {
                combined.kind
            }
            (
                AggregateKind::DistinctLimit { limit, .. },
                AggregateKind::DistinctLimit {
                    group_by,
                    limit: partial_limit,
                },
            ) if limit == partial_limit => AggregateKind::DistinctLimit {
                group_by: Arc::clone(group_by),
                limit: *limit,
            },
            // Not possible in the default optimizer pipeline
            _ => return Ok(Transformed::no(plan)),
        };
        Ok(Transformed::yes(Arc::new(combined)))
    }

    // ========================================================================
    // OptimizeAggregateOrder
    // ========================================================================
    /// Clone with replacement aggregate expressions.
    ///
    /// The caller must preserve the schema, filters, and ordering requirements.
    /// Changing expressions in a TopK aggregate drops its specialization.
    ///
    /// # Public Only for Internal Use:
    ///
    /// This is not a public API and is for internal use only; see [API policy] for details.
    ///
    /// [API policy]: https://datafusion.apache.org/contributor-guide/api-health.html#internal-public-apis
    #[doc(hidden)]
    pub fn with_new_aggr_exprs(
        &self,
        aggr_expr: impl Into<Arc<[Arc<AggregateFunctionExpr>]>>,
    ) -> Self {
        let aggr_expr = aggr_expr.into();
        let mut new = self.clone();
        match &mut new.kind {
            AggregateKind::General { aggr_expr: old, .. } => *old = aggr_expr,
            AggregateKind::DistinctLimit { .. } | AggregateKind::TopKDistinct { .. }
                if aggr_expr.is_empty() => {}
            // Ordering optimization can revisit an existing TopK with the
            // same expressions. Preserve its specialization in that case.
            AggregateKind::TopKMinMax {
                aggr_expr: existing,
                ..
            } if matches!(aggr_expr.as_ref(), [new] if Arc::ptr_eq(existing, new)) => {}
            AggregateKind::DistinctLimit { group_by, .. }
            | AggregateKind::TopKMinMax { group_by, .. }
            | AggregateKind::TopKDistinct { group_by, .. } => {
                // A replacement expression cannot inherit a specialization
                // validated for the previous aggregate.
                new.kind = AggregateKind::General {
                    group_by: Arc::clone(group_by),
                    filter_expr: vec![None; aggr_expr.len()].into(),
                    aggr_expr,
                };
            }
        }
        new.metrics = ExecutionPlanMetricsSet::new();
        new
    }

    // ========================================================================
    // TopKAggregation
    // ========================================================================
    /// Try to use TopK (min/max heap) optimization in AggregateExec.
    ///
    /// The optimizer must keep the limit operator above because the aggregate may
    /// still produce more than K rows.
    /// Returns `None` if not applicable.
    ///
    /// # Public Only for Internal Use:
    ///
    /// This is not a public API and is for internal use only; see [API policy] for details.
    ///
    /// [API policy]: https://datafusion.apache.org/contributor-guide/api-health.html#internal-public-apis
    ///
    /// # TopK Optimization Overview
    ///
    /// Retain candidate groups for a parent ORDER BY / LIMIT. There are two applicable
    /// cases:
    ///
    /// ## TopK MIN/MAX
    ///
    /// ```txt
    /// SELECT k, MAX(v) AS score FROM t GROUP BY k
    /// ORDER BY score DESC NULLS LAST LIMIT 10;
    ///
    /// Before:
    /// Sort: score DESC, fetch=10
    ///   Aggregate: k, MAX(v)
    ///     input
    ///
    /// After (Added info in `AggregateExec` to trigger heap optimization):
    /// Sort: score DESC, fetch=10
    ///   Aggregate: k, MAX(v), TopKMinMax(10)
    ///     input
    /// ```
    ///
    /// For an aggregate output named `score`, the example uses:
    /// ```txt
    /// try_optimize_topk(10, "score", SortOptions {
    ///     descending: true,   // DESC
    ///     nulls_first: false, // NULLS LAST
    /// })
    /// ```
    ///
    /// A min-heap keeps the 10 largest non-NULL scores.
    /// MIN with ASC uses a max-heap.
    ///
    /// ## TopK DISTINCT
    ///
    /// ```txt
    /// SELECT DISTINCT k FROM t ORDER BY k ASC NULLS LAST LIMIT 10;
    ///
    /// Before:
    /// Sort: k ASC, fetch=10
    ///   Aggregate: k, no aggregates
    ///     input
    ///
    /// After (Added info in `AggregateExec` to trigger heap optimization):
    /// Sort: k ASC, fetch=10
    ///   Aggregate: k, TopKDistinct(10)
    ///     input
    /// ```
    ///
    /// Here the grouping key is the output used for ordering:
    /// ```txt
    /// try_optimize_topk(10, "k", SortOptions {
    ///     descending: false,  // ASC
    ///     nulls_first: false, // NULLS LAST
    /// })
    /// ```
    ///
    /// A max-heap keeps the 10 smallest non-NULL keys.
    /// DESC uses a min-heap.
    #[doc(hidden)]
    pub fn try_optimize_topk(
        mut self,
        limit: usize,
        order_by: &str,
        options: SortOptions,
    ) -> Option<Transformed<Self>> {
        if limit == 0
            || matches!(self.kind, AggregateKind::DistinctLimit { .. })
            || self.group_by().has_grouping_set()
            || self.filter_expr().iter().any(Option::is_some)
        {
            return None;
        }
        // The priority map needs one supported key and value type.
        let (group_key, group_key_alias) =
            self.group_expr().expr().iter().exactly_one().ok()?;
        let input_schema = self.input.schema();
        let key_type = group_key.data_type(&input_schema).ok()?;
        let minmax = self.get_minmax_desc();
        let value_type = minmax
            .as_ref()
            .map_or(&key_type, |(field, _)| field.data_type());
        if !topk_types_supported(&key_type, value_type)
            || self.schema.fields().len() != 1 + self.aggr_expr().len()
        {
            return None;
        }

        let kind = if let Some((field, descending)) = minmax {
            let aggregate = self.aggr_expr().iter().exactly_one().ok()?;
            if field.name() != order_by
                || descending != options.descending
                || !aggregate.order_bys().is_empty()
            {
                return None;
            }
            let argument = aggregate.expressions().into_iter().exactly_one().ok()?;
            // NULLS FIRST can worsen a group's rank when its first value arrives.
            if options.nulls_first && argument.nullable(&self.input_schema).ok()? {
                return None;
            }

            // Raw input, partial state, and output must fit the same heap value.
            let state_field = aggregate
                .state_fields()
                .ok()?
                .into_iter()
                .exactly_one()
                .ok()?;
            let input_argument = aggregate_expressions(self.aggr_expr(), &self.mode, 1)
                .ok()?
                .into_iter()
                .flatten()
                .exactly_one()
                .ok()?;
            let value_type = field.data_type();
            if state_field.data_type() != value_type
                || argument.data_type(&self.input_schema).ok()? != *value_type
                || input_argument.data_type(&input_schema).ok()? != *value_type
            {
                return None;
            }
            AggregateKind::TopKMinMax {
                group_by: Arc::clone(self.group_by()),
                aggr_expr: Arc::clone(aggregate),
                limit,
                descending,
                nulls_first: options.nulls_first,
            }
        } else if self.aggr_expr().is_empty() && group_key_alias == order_by {
            AggregateKind::TopKDistinct {
                group_by: Arc::clone(self.group_by()),
                limit,
                descending: options.descending,
            }
        } else {
            return None;
        };

        // Preserve compatible tighter bounds; never switch an existing order.
        match &self.kind {
            AggregateKind::TopKMinMax { nulls_first, .. }
                if *nulls_first != options.nulls_first =>
            {
                return None;
            }
            AggregateKind::TopKMinMax {
                limit: existing,
                descending,
                ..
            }
            | AggregateKind::TopKDistinct {
                limit: existing,
                descending,
                ..
            } => {
                if *descending != options.descending {
                    return None;
                }
                if *existing <= limit {
                    return Some(Transformed::no(self));
                }
            }
            _ => {}
        }

        // Commit the kind and properties together: heap output is unordered and final.
        self.kind = kind;
        self.group_clustering_mode = GroupClusteringMode::None;
        self.required_input_ordering = None;
        // Keep unchanged properties so parent aggregates do not need rebuilding.
        if !self.cache.eq_properties.oeq_class().is_empty()
            || self.cache.emission_type != EmissionType::Final
        {
            let mut equivalence = self.cache.eq_properties.clone();
            equivalence.clear_orderings();
            let cache = Arc::make_mut(&mut self.cache);
            cache.set_eq_properties(equivalence);
            cache.emission_type = EmissionType::Final;
        }
        self.metrics = ExecutionPlanMetricsSet::new();
        Some(Transformed::yes(self))
    }

    /// Finds the DataType and SortDirection for this Aggregate, if there is one
    ///
    /// # Public Only for Internal Use:
    ///
    /// This is not a public API and is for internal use only; see [API policy] for details.
    ///
    /// [API policy]: https://datafusion.apache.org/contributor-guide/api-health.html#internal-public-apis
    #[doc(hidden)]
    pub fn get_minmax_desc(&self) -> Option<(FieldRef, bool)> {
        let agg_expr = self.aggr_expr().iter().exactly_one().ok()?;
        agg_expr.get_minmax_desc()
    }
}

/// Returns true if TopK aggregation data structures support the provided key and value types.
///
/// This function checks whether both the key type (used for grouping) and value type
/// (used in min/max aggregation) can be handled by the TopK aggregation heap and hash table.
/// Supported types include Arrow primitives (integers, floats, decimals, intervals) and
/// UTF-8 strings (`Utf8`, `LargeUtf8`, `Utf8View`).
///
/// # Public Only for Internal Use:
///
/// This is not a public API and is for internal use only; see [API policy] for details.
///
/// [API policy]: https://datafusion.apache.org/contributor-guide/api-health.html#internal-public-apis
#[doc(hidden)]
pub fn topk_types_supported(key_type: &DataType, value_type: &DataType) -> bool {
    is_supported_hash_key_type(key_type) && is_supported_heap_type(value_type)
}