Skip to main content

datafusion_functions_aggregate_common/
order.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5/// Represents the sensitivity of an aggregate expression to ordering.
6#[derive(Debug, PartialEq, Eq, Clone, Copy)]
7pub enum AggregateOrderSensitivity {
8    /// Indicates that the aggregate expression is insensitive to ordering.
9    /// Ordering at the input is not important for the result of the aggregator.
10    Insensitive,
11    /// Indicates that the aggregate expression has a hard requirement on ordering.
12    /// The aggregator cannot produce a correct result unless its ordering
13    /// requirement is satisfied.
14    HardRequirement,
15    /// Indicates that the aggregator is more efficient when the input is ordered
16    /// but can still produce its result correctly regardless of the input ordering.
17    /// This is similar to, but stronger than, [`Self::Beneficial`].
18    ///
19    /// Similarly to [`Self::HardRequirement`], when possible DataFusion will insert
20    /// a `SortExec`, to reorder the input to match the SoftRequirement. However,
21    /// when such a `SortExec` cannot be inserted, (for example, due to conflicting
22    /// [`Self::HardRequirement`] with other ordered aggregates in the query),
23    /// the aggregate function will still execute, without the preferred order, unlike
24    /// with [`Self::HardRequirement`]
25    SoftRequirement,
26    /// Indicates that ordering is beneficial for the aggregate expression in terms
27    /// of evaluation efficiency. The aggregator can produce its result efficiently
28    /// when its required ordering is satisfied; however, it can still produce the
29    /// correct result (albeit less efficiently) when its required ordering is not met.
30    Beneficial,
31}
32
33impl AggregateOrderSensitivity {
34    pub fn is_insensitive(&self) -> bool {
35        self.eq(&AggregateOrderSensitivity::Insensitive)
36    }
37
38    pub fn is_beneficial(&self) -> bool {
39        matches!(self, Self::SoftRequirement | Self::Beneficial)
40    }
41
42    pub fn hard_requires(&self) -> bool {
43        self.eq(&AggregateOrderSensitivity::HardRequirement)
44    }
45}