datafusion_physical_expr/
aggregate.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18pub(crate) mod groups_accumulator {
19    #[allow(unused_imports)]
20    pub(crate) mod accumulate {
21        pub use datafusion_functions_aggregate_common::aggregate::groups_accumulator::accumulate::NullState;
22    }
23    pub use datafusion_functions_aggregate_common::aggregate::groups_accumulator::{
24        accumulate::NullState, GroupsAccumulatorAdapter,
25    };
26}
27pub(crate) mod stats {
28    pub use datafusion_functions_aggregate_common::stats::StatsType;
29}
30pub mod utils {
31    #[allow(deprecated)] // allow adjust_output_array
32    pub use datafusion_functions_aggregate_common::utils::{
33        adjust_output_array, get_accum_scalar_values_as_arrays, get_sort_options,
34        ordering_fields, DecimalAverager, Hashable,
35    };
36}
37
38use std::fmt::Debug;
39use std::sync::Arc;
40
41use crate::expressions::Column;
42
43use arrow::compute::SortOptions;
44use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
45use datafusion_common::{internal_err, not_impl_err, Result, ScalarValue};
46use datafusion_expr::{AggregateUDF, ReversedUDAF, SetMonotonicity};
47use datafusion_expr_common::accumulator::Accumulator;
48use datafusion_expr_common::groups_accumulator::GroupsAccumulator;
49use datafusion_expr_common::type_coercion::aggregates::check_arg_count;
50use datafusion_functions_aggregate_common::accumulator::{
51    AccumulatorArgs, StateFieldsArgs,
52};
53use datafusion_functions_aggregate_common::order::AggregateOrderSensitivity;
54use datafusion_physical_expr_common::physical_expr::PhysicalExpr;
55use datafusion_physical_expr_common::sort_expr::{LexOrdering, PhysicalSortExpr};
56use datafusion_physical_expr_common::utils::reverse_order_bys;
57
58/// Builder for physical [`AggregateFunctionExpr`]
59///
60/// `AggregateFunctionExpr` contains the information necessary to call
61/// an aggregate expression.
62#[derive(Debug, Clone)]
63pub struct AggregateExprBuilder {
64    fun: Arc<AggregateUDF>,
65    /// Physical expressions of the aggregate function
66    args: Vec<Arc<dyn PhysicalExpr>>,
67    alias: Option<String>,
68    /// A human readable name
69    human_display: String,
70    /// Arrow Schema for the aggregate function
71    schema: SchemaRef,
72    /// The physical order by expressions
73    ordering_req: LexOrdering,
74    /// Whether to ignore null values
75    ignore_nulls: bool,
76    /// Whether is distinct aggregate function
77    is_distinct: bool,
78    /// Whether the expression is reversed
79    is_reversed: bool,
80}
81
82impl AggregateExprBuilder {
83    pub fn new(fun: Arc<AggregateUDF>, args: Vec<Arc<dyn PhysicalExpr>>) -> Self {
84        Self {
85            fun,
86            args,
87            alias: None,
88            human_display: String::default(),
89            schema: Arc::new(Schema::empty()),
90            ordering_req: LexOrdering::default(),
91            ignore_nulls: false,
92            is_distinct: false,
93            is_reversed: false,
94        }
95    }
96
97    /// Constructs an `AggregateFunctionExpr` from the builder
98    ///
99    /// Note that an [`Self::alias`] must be provided before calling this method.
100    ///
101    /// # Example: Create an [`AggregateUDF`]
102    ///
103    /// In the following example, [`AggregateFunctionExpr`] will be built using [`AggregateExprBuilder`]
104    /// which provides a build function. Full example could be accessed from the source file.
105    ///
106    /// ```
107    /// # use std::any::Any;
108    /// # use std::sync::Arc;
109    /// # use arrow::datatypes::DataType;
110    /// # use datafusion_common::{Result, ScalarValue};
111    /// # use datafusion_expr::{col, ColumnarValue, Documentation, Signature, Volatility, Expr};
112    /// # use datafusion_expr::{AggregateUDFImpl, AggregateUDF, Accumulator, function::{AccumulatorArgs, StateFieldsArgs}};
113    /// # use arrow::datatypes::Field;
114    /// #
115    /// # #[derive(Debug, Clone)]
116    /// # struct FirstValueUdf {
117    /// #     signature: Signature,
118    /// # }
119    /// #
120    /// # impl FirstValueUdf {
121    /// #     fn new() -> Self {
122    /// #         Self {
123    /// #             signature: Signature::any(1, Volatility::Immutable),
124    /// #         }
125    /// #     }
126    /// # }
127    /// #
128    /// # impl AggregateUDFImpl for FirstValueUdf {
129    /// #     fn as_any(&self) -> &dyn Any {
130    /// #         unimplemented!()
131    /// # }
132    /// #     fn name(&self) -> &str {
133    /// #         unimplemented!()
134    /// }
135    /// #     fn signature(&self) -> &Signature {
136    /// #         unimplemented!()
137    /// # }
138    /// #     fn return_type(&self, args: &[DataType]) -> Result<DataType> {
139    /// #         unimplemented!()
140    /// #     }
141    /// #     
142    /// #     fn accumulator(&self, acc_args: AccumulatorArgs) -> Result<Box<dyn Accumulator>> {
143    /// #         unimplemented!()
144    /// #         }
145    /// #     
146    /// #     fn state_fields(&self, args: StateFieldsArgs) -> Result<Vec<Field>> {
147    /// #         unimplemented!()
148    /// #     }
149    /// #     
150    /// #     fn documentation(&self) -> Option<&Documentation> {
151    /// #         unimplemented!()
152    /// #     }
153    /// # }
154    /// #
155    /// # let first_value = AggregateUDF::from(FirstValueUdf::new());
156    /// # let expr = first_value.call(vec![col("a")]);
157    /// #
158    /// # use datafusion_physical_expr::expressions::Column;
159    /// # use datafusion_physical_expr_common::physical_expr::PhysicalExpr;
160    /// # use datafusion_physical_expr::aggregate::AggregateExprBuilder;
161    /// # use datafusion_physical_expr::expressions::PhysicalSortExpr;
162    /// # use datafusion_physical_expr::PhysicalSortRequirement;
163    /// #
164    /// fn build_aggregate_expr() -> Result<()> {
165    ///     let args = vec![Arc::new(Column::new("a", 0)) as Arc<dyn PhysicalExpr>];
166    ///     let order_by = vec![PhysicalSortExpr {
167    ///         expr: Arc::new(Column::new("x", 1)) as Arc<dyn PhysicalExpr>,
168    ///         options: Default::default(),
169    ///     }];
170    ///
171    ///     let first_value = AggregateUDF::from(FirstValueUdf::new());
172    ///     
173    ///     let aggregate_expr = AggregateExprBuilder::new(
174    ///         Arc::new(first_value),
175    ///         args
176    ///     )
177    ///     .order_by(order_by.into())
178    ///     .alias("first_a_by_x")
179    ///     .ignore_nulls()
180    ///     .build()?;
181    ///     
182    ///     Ok(())
183    /// }
184    /// ```
185    ///
186    /// This creates a physical expression equivalent to SQL:
187    /// `first_value(a ORDER BY x) IGNORE NULLS AS first_a_by_x`
188    pub fn build(self) -> Result<AggregateFunctionExpr> {
189        let Self {
190            fun,
191            args,
192            alias,
193            human_display,
194            schema,
195            ordering_req,
196            ignore_nulls,
197            is_distinct,
198            is_reversed,
199        } = self;
200        if args.is_empty() {
201            return internal_err!("args should not be empty");
202        }
203
204        let mut ordering_fields = vec![];
205
206        if !ordering_req.is_empty() {
207            let ordering_types = ordering_req
208                .iter()
209                .map(|e| e.expr.data_type(&schema))
210                .collect::<Result<Vec<_>>>()?;
211
212            ordering_fields =
213                utils::ordering_fields(ordering_req.as_ref(), &ordering_types);
214        }
215
216        let input_exprs_types = args
217            .iter()
218            .map(|arg| arg.data_type(&schema))
219            .collect::<Result<Vec<_>>>()?;
220
221        check_arg_count(
222            fun.name(),
223            &input_exprs_types,
224            &fun.signature().type_signature,
225        )?;
226
227        let data_type = fun.return_type(&input_exprs_types)?;
228        let is_nullable = fun.is_nullable();
229        let name = match alias {
230            None => {
231                return internal_err!(
232                    "AggregateExprBuilder::alias must be provided prior to calling build"
233                )
234            }
235            Some(alias) => alias,
236        };
237
238        Ok(AggregateFunctionExpr {
239            fun: Arc::unwrap_or_clone(fun),
240            args,
241            data_type,
242            name,
243            human_display,
244            schema: Arc::unwrap_or_clone(schema),
245            ordering_req,
246            ignore_nulls,
247            ordering_fields,
248            is_distinct,
249            input_types: input_exprs_types,
250            is_reversed,
251            is_nullable,
252        })
253    }
254
255    pub fn alias(mut self, alias: impl Into<String>) -> Self {
256        self.alias = Some(alias.into());
257        self
258    }
259
260    pub fn human_display(mut self, name: String) -> Self {
261        self.human_display = name;
262        self
263    }
264
265    pub fn schema(mut self, schema: SchemaRef) -> Self {
266        self.schema = schema;
267        self
268    }
269
270    pub fn order_by(mut self, order_by: LexOrdering) -> Self {
271        self.ordering_req = order_by;
272        self
273    }
274
275    pub fn reversed(mut self) -> Self {
276        self.is_reversed = true;
277        self
278    }
279
280    pub fn with_reversed(mut self, is_reversed: bool) -> Self {
281        self.is_reversed = is_reversed;
282        self
283    }
284
285    pub fn distinct(mut self) -> Self {
286        self.is_distinct = true;
287        self
288    }
289
290    pub fn with_distinct(mut self, is_distinct: bool) -> Self {
291        self.is_distinct = is_distinct;
292        self
293    }
294
295    pub fn ignore_nulls(mut self) -> Self {
296        self.ignore_nulls = true;
297        self
298    }
299
300    pub fn with_ignore_nulls(mut self, ignore_nulls: bool) -> Self {
301        self.ignore_nulls = ignore_nulls;
302        self
303    }
304}
305
306/// Physical aggregate expression of a UDAF.
307///
308/// Instances are constructed via [`AggregateExprBuilder`].
309#[derive(Debug, Clone)]
310pub struct AggregateFunctionExpr {
311    fun: AggregateUDF,
312    args: Vec<Arc<dyn PhysicalExpr>>,
313    /// Output / return type of this aggregate
314    data_type: DataType,
315    /// Output column name that this expression creates
316    name: String,
317    /// Simplified name for `tree` explain.
318    human_display: String,
319    schema: Schema,
320    // The physical order by expressions
321    ordering_req: LexOrdering,
322    // Whether to ignore null values
323    ignore_nulls: bool,
324    // fields used for order sensitive aggregation functions
325    ordering_fields: Vec<Field>,
326    is_distinct: bool,
327    is_reversed: bool,
328    input_types: Vec<DataType>,
329    is_nullable: bool,
330}
331
332impl AggregateFunctionExpr {
333    /// Return the `AggregateUDF` used by this `AggregateFunctionExpr`
334    pub fn fun(&self) -> &AggregateUDF {
335        &self.fun
336    }
337
338    /// expressions that are passed to the Accumulator.
339    /// Single-column aggregations such as `sum` return a single value, others (e.g. `cov`) return many.
340    pub fn expressions(&self) -> Vec<Arc<dyn PhysicalExpr>> {
341        self.args.clone()
342    }
343
344    /// Human readable name such as `"MIN(c2)"`.
345    pub fn name(&self) -> &str {
346        &self.name
347    }
348
349    /// Simplified name for `tree` explain.
350    pub fn human_display(&self) -> &str {
351        &self.human_display
352    }
353
354    /// Return if the aggregation is distinct
355    pub fn is_distinct(&self) -> bool {
356        self.is_distinct
357    }
358
359    /// Return if the aggregation ignores nulls
360    pub fn ignore_nulls(&self) -> bool {
361        self.ignore_nulls
362    }
363
364    /// Return if the aggregation is reversed
365    pub fn is_reversed(&self) -> bool {
366        self.is_reversed
367    }
368
369    /// Return if the aggregation is nullable
370    pub fn is_nullable(&self) -> bool {
371        self.is_nullable
372    }
373
374    /// the field of the final result of this aggregation.
375    pub fn field(&self) -> Field {
376        Field::new(&self.name, self.data_type.clone(), self.is_nullable)
377    }
378
379    /// the accumulator used to accumulate values from the expressions.
380    /// the accumulator expects the same number of arguments as `expressions` and must
381    /// return states with the same description as `state_fields`
382    pub fn create_accumulator(&self) -> Result<Box<dyn Accumulator>> {
383        let acc_args = AccumulatorArgs {
384            return_type: &self.data_type,
385            schema: &self.schema,
386            ignore_nulls: self.ignore_nulls,
387            ordering_req: self.ordering_req.as_ref(),
388            is_distinct: self.is_distinct,
389            name: &self.name,
390            is_reversed: self.is_reversed,
391            exprs: &self.args,
392        };
393
394        self.fun.accumulator(acc_args)
395    }
396
397    /// the field of the final result of this aggregation.
398    pub fn state_fields(&self) -> Result<Vec<Field>> {
399        let args = StateFieldsArgs {
400            name: &self.name,
401            input_types: &self.input_types,
402            return_type: &self.data_type,
403            ordering_fields: &self.ordering_fields,
404            is_distinct: self.is_distinct,
405        };
406
407        self.fun.state_fields(args)
408    }
409
410    /// Order by requirements for the aggregate function
411    /// By default it is `None` (there is no requirement)
412    /// Order-sensitive aggregators, such as `FIRST_VALUE(x ORDER BY y)` should implement this
413    pub fn order_bys(&self) -> Option<&LexOrdering> {
414        if self.ordering_req.is_empty() {
415            return None;
416        }
417
418        if !self.order_sensitivity().is_insensitive() {
419            return Some(self.ordering_req.as_ref());
420        }
421
422        None
423    }
424
425    /// Indicates whether aggregator can produce the correct result with any
426    /// arbitrary input ordering. By default, we assume that aggregate expressions
427    /// are order insensitive.
428    pub fn order_sensitivity(&self) -> AggregateOrderSensitivity {
429        if !self.ordering_req.is_empty() {
430            // If there is requirement, use the sensitivity of the implementation
431            self.fun.order_sensitivity()
432        } else {
433            // If no requirement, aggregator is order insensitive
434            AggregateOrderSensitivity::Insensitive
435        }
436    }
437
438    /// Sets the indicator whether ordering requirements of the aggregator is
439    /// satisfied by its input. If this is not the case, aggregators with order
440    /// sensitivity `AggregateOrderSensitivity::Beneficial` can still produce
441    /// the correct result with possibly more work internally.
442    ///
443    /// # Returns
444    ///
445    /// Returns `Ok(Some(updated_expr))` if the process completes successfully.
446    /// If the expression can benefit from existing input ordering, but does
447    /// not implement the method, returns an error. Order insensitive and hard
448    /// requirement aggregators return `Ok(None)`.
449    pub fn with_beneficial_ordering(
450        self: Arc<Self>,
451        beneficial_ordering: bool,
452    ) -> Result<Option<AggregateFunctionExpr>> {
453        let Some(updated_fn) = self
454            .fun
455            .clone()
456            .with_beneficial_ordering(beneficial_ordering)?
457        else {
458            return Ok(None);
459        };
460
461        AggregateExprBuilder::new(Arc::new(updated_fn), self.args.to_vec())
462            .order_by(self.ordering_req.clone())
463            .schema(Arc::new(self.schema.clone()))
464            .alias(self.name().to_string())
465            .with_ignore_nulls(self.ignore_nulls)
466            .with_distinct(self.is_distinct)
467            .with_reversed(self.is_reversed)
468            .build()
469            .map(Some)
470    }
471
472    /// Creates accumulator implementation that supports retract
473    pub fn create_sliding_accumulator(&self) -> Result<Box<dyn Accumulator>> {
474        let args = AccumulatorArgs {
475            return_type: &self.data_type,
476            schema: &self.schema,
477            ignore_nulls: self.ignore_nulls,
478            ordering_req: self.ordering_req.as_ref(),
479            is_distinct: self.is_distinct,
480            name: &self.name,
481            is_reversed: self.is_reversed,
482            exprs: &self.args,
483        };
484
485        let accumulator = self.fun.create_sliding_accumulator(args)?;
486
487        // Accumulators that have window frame startings different
488        // than `UNBOUNDED PRECEDING`, such as `1 PRECEDING`, need to
489        // implement retract_batch method in order to run correctly
490        // currently in DataFusion.
491        //
492        // If this `retract_batches` is not present, there is no way
493        // to calculate result correctly. For example, the query
494        //
495        // ```sql
496        // SELECT
497        //  SUM(a) OVER(ORDER BY a ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS sum_a
498        // FROM
499        //  t
500        // ```
501        //
502        // 1. First sum value will be the sum of rows between `[0, 1)`,
503        //
504        // 2. Second sum value will be the sum of rows between `[0, 2)`
505        //
506        // 3. Third sum value will be the sum of rows between `[1, 3)`, etc.
507        //
508        // Since the accumulator keeps the running sum:
509        //
510        // 1. First sum we add to the state sum value between `[0, 1)`
511        //
512        // 2. Second sum we add to the state sum value between `[1, 2)`
513        // (`[0, 1)` is already in the state sum, hence running sum will
514        // cover `[0, 2)` range)
515        //
516        // 3. Third sum we add to the state sum value between `[2, 3)`
517        // (`[0, 2)` is already in the state sum).  Also we need to
518        // retract values between `[0, 1)` by this way we can obtain sum
519        // between [1, 3) which is indeed the appropriate range.
520        //
521        // When we use `UNBOUNDED PRECEDING` in the query starting
522        // index will always be 0 for the desired range, and hence the
523        // `retract_batch` method will not be called. In this case
524        // having retract_batch is not a requirement.
525        //
526        // This approach is a a bit different than window function
527        // approach. In window function (when they use a window frame)
528        // they get all the desired range during evaluation.
529        if !accumulator.supports_retract_batch() {
530            return not_impl_err!(
531                "Aggregate can not be used as a sliding accumulator because \
532                     `retract_batch` is not implemented: {}",
533                self.name
534            );
535        }
536        Ok(accumulator)
537    }
538
539    /// If the aggregate expression has a specialized
540    /// [`GroupsAccumulator`] implementation. If this returns true,
541    /// `[Self::create_groups_accumulator`] will be called.
542    pub fn groups_accumulator_supported(&self) -> bool {
543        let args = AccumulatorArgs {
544            return_type: &self.data_type,
545            schema: &self.schema,
546            ignore_nulls: self.ignore_nulls,
547            ordering_req: self.ordering_req.as_ref(),
548            is_distinct: self.is_distinct,
549            name: &self.name,
550            is_reversed: self.is_reversed,
551            exprs: &self.args,
552        };
553        self.fun.groups_accumulator_supported(args)
554    }
555
556    /// Return a specialized [`GroupsAccumulator`] that manages state
557    /// for all groups.
558    ///
559    /// For maximum performance, a [`GroupsAccumulator`] should be
560    /// implemented in addition to [`Accumulator`].
561    pub fn create_groups_accumulator(&self) -> Result<Box<dyn GroupsAccumulator>> {
562        let args = AccumulatorArgs {
563            return_type: &self.data_type,
564            schema: &self.schema,
565            ignore_nulls: self.ignore_nulls,
566            ordering_req: self.ordering_req.as_ref(),
567            is_distinct: self.is_distinct,
568            name: &self.name,
569            is_reversed: self.is_reversed,
570            exprs: &self.args,
571        };
572        self.fun.create_groups_accumulator(args)
573    }
574
575    /// Construct an expression that calculates the aggregate in reverse.
576    /// Typically the "reverse" expression is itself (e.g. SUM, COUNT).
577    /// For aggregates that do not support calculation in reverse,
578    /// returns None (which is the default value).
579    pub fn reverse_expr(&self) -> Option<AggregateFunctionExpr> {
580        match self.fun.reverse_udf() {
581            ReversedUDAF::NotSupported => None,
582            ReversedUDAF::Identical => Some(self.clone()),
583            ReversedUDAF::Reversed(reverse_udf) => {
584                let reverse_ordering_req = reverse_order_bys(self.ordering_req.as_ref());
585                let mut name = self.name().to_string();
586                // If the function is changed, we need to reverse order_by clause as well
587                // i.e. First(a order by b asc null first) -> Last(a order by b desc null last)
588                if self.fun().name() == reverse_udf.name() {
589                } else {
590                    replace_order_by_clause(&mut name);
591                }
592                replace_fn_name_clause(&mut name, self.fun.name(), reverse_udf.name());
593
594                AggregateExprBuilder::new(reverse_udf, self.args.to_vec())
595                    .order_by(reverse_ordering_req)
596                    .schema(Arc::new(self.schema.clone()))
597                    .alias(name)
598                    .with_ignore_nulls(self.ignore_nulls)
599                    .with_distinct(self.is_distinct)
600                    .with_reversed(!self.is_reversed)
601                    .build()
602                    .ok()
603            }
604        }
605    }
606
607    /// Returns all expressions used in the [`AggregateFunctionExpr`].
608    /// These expressions are  (1)function arguments, (2) order by expressions.
609    pub fn all_expressions(&self) -> AggregatePhysicalExpressions {
610        let args = self.expressions();
611        let order_bys = self
612            .order_bys()
613            .cloned()
614            .unwrap_or_else(LexOrdering::default);
615        let order_by_exprs = order_bys
616            .iter()
617            .map(|sort_expr| Arc::clone(&sort_expr.expr))
618            .collect::<Vec<_>>();
619        AggregatePhysicalExpressions {
620            args,
621            order_by_exprs,
622        }
623    }
624
625    /// Rewrites [`AggregateFunctionExpr`], with new expressions given. The argument should be consistent
626    /// with the return value of the [`AggregateFunctionExpr::all_expressions`] method.
627    /// Returns `Some(Arc<dyn AggregateExpr>)` if re-write is supported, otherwise returns `None`.
628    pub fn with_new_expressions(
629        &self,
630        _args: Vec<Arc<dyn PhysicalExpr>>,
631        _order_by_exprs: Vec<Arc<dyn PhysicalExpr>>,
632    ) -> Option<AggregateFunctionExpr> {
633        None
634    }
635
636    /// If this function is max, return (output_field, true)
637    /// if the function is min, return (output_field, false)
638    /// otherwise return None (the default)
639    ///
640    /// output_field is the name of the column produced by this aggregate
641    ///
642    /// Note: this is used to use special aggregate implementations in certain conditions
643    pub fn get_minmax_desc(&self) -> Option<(Field, bool)> {
644        self.fun.is_descending().map(|flag| (self.field(), flag))
645    }
646
647    /// Returns default value of the function given the input is Null
648    /// Most of the aggregate function return Null if input is Null,
649    /// while `count` returns 0 if input is Null
650    pub fn default_value(&self, data_type: &DataType) -> Result<ScalarValue> {
651        self.fun.default_value(data_type)
652    }
653
654    /// Indicates whether the aggregation function is monotonic as a set
655    /// function. See [`SetMonotonicity`] for details.
656    pub fn set_monotonicity(&self) -> SetMonotonicity {
657        let field = self.field();
658        let data_type = field.data_type();
659        self.fun.inner().set_monotonicity(data_type)
660    }
661
662    /// Returns `PhysicalSortExpr` based on the set monotonicity of the function.
663    pub fn get_result_ordering(&self, aggr_func_idx: usize) -> Option<PhysicalSortExpr> {
664        // If the aggregate expressions are set-monotonic, the output data is
665        // naturally ordered with it per group or partition.
666        let monotonicity = self.set_monotonicity();
667        if monotonicity == SetMonotonicity::NotMonotonic {
668            return None;
669        }
670        let expr = Arc::new(Column::new(self.name(), aggr_func_idx));
671        let options =
672            SortOptions::new(monotonicity == SetMonotonicity::Decreasing, false);
673        Some(PhysicalSortExpr { expr, options })
674    }
675}
676
677/// Stores the physical expressions used inside the `AggregateExpr`.
678pub struct AggregatePhysicalExpressions {
679    /// Aggregate function arguments
680    pub args: Vec<Arc<dyn PhysicalExpr>>,
681    /// Order by expressions
682    pub order_by_exprs: Vec<Arc<dyn PhysicalExpr>>,
683}
684
685impl PartialEq for AggregateFunctionExpr {
686    fn eq(&self, other: &Self) -> bool {
687        self.name == other.name
688            && self.data_type == other.data_type
689            && self.fun == other.fun
690            && self.args.len() == other.args.len()
691            && self
692                .args
693                .iter()
694                .zip(other.args.iter())
695                .all(|(this_arg, other_arg)| this_arg.eq(other_arg))
696    }
697}
698
699fn replace_order_by_clause(order_by: &mut String) {
700    let suffixes = [
701        (" DESC NULLS FIRST]", " ASC NULLS LAST]"),
702        (" ASC NULLS FIRST]", " DESC NULLS LAST]"),
703        (" DESC NULLS LAST]", " ASC NULLS FIRST]"),
704        (" ASC NULLS LAST]", " DESC NULLS FIRST]"),
705    ];
706
707    if let Some(start) = order_by.find("ORDER BY [") {
708        if let Some(end) = order_by[start..].find(']') {
709            let order_by_start = start + 9;
710            let order_by_end = start + end;
711
712            let column_order = &order_by[order_by_start..=order_by_end];
713            for (suffix, replacement) in suffixes {
714                if column_order.ends_with(suffix) {
715                    let new_order = column_order.replace(suffix, replacement);
716                    order_by.replace_range(order_by_start..=order_by_end, &new_order);
717                    break;
718                }
719            }
720        }
721    }
722}
723
724fn replace_fn_name_clause(aggr_name: &mut String, fn_name_old: &str, fn_name_new: &str) {
725    *aggr_name = aggr_name.replace(fn_name_old, fn_name_new);
726}