datafusion_functions_aggregate_common/
order.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
18/// Represents the sensitivity of an aggregate expression to ordering.
19#[derive(Debug, PartialEq, Eq, Clone, Copy)]
20pub enum AggregateOrderSensitivity {
21    /// Indicates that the aggregate expression is insensitive to ordering.
22    /// Ordering at the input is not important for the result of the aggregator.
23    Insensitive,
24    /// Indicates that the aggregate expression has a hard requirement on ordering.
25    /// The aggregator cannot produce a correct result unless its ordering
26    /// requirement is satisfied.
27    HardRequirement,
28    /// Indicates that the aggregator is more efficient when the input is ordered
29    /// but can still produce its result correctly regardless of the input ordering.
30    /// This is similar to, but stronger than, [`Self::Beneficial`].
31    ///
32    /// Similarly to [`Self::HardRequirement`], when possible DataFusion will insert
33    /// a `SortExec`, to reorder the input to match the SoftRequirement. However,
34    /// when such a `SortExec` cannot be inserted, (for example, due to conflicting
35    /// [`Self::HardRequirement`] with other ordered aggregates in the query),
36    /// the aggregate function will still execute, without the preferred order, unlike
37    /// with [`Self::HardRequirement`]
38    SoftRequirement,
39    /// Indicates that ordering is beneficial for the aggregate expression in terms
40    /// of evaluation efficiency. The aggregator can produce its result efficiently
41    /// when its required ordering is satisfied; however, it can still produce the
42    /// correct result (albeit less efficiently) when its required ordering is not met.
43    Beneficial,
44}
45
46impl AggregateOrderSensitivity {
47    pub fn is_insensitive(&self) -> bool {
48        self.eq(&AggregateOrderSensitivity::Insensitive)
49    }
50
51    pub fn is_beneficial(&self) -> bool {
52        matches!(self, Self::SoftRequirement | Self::Beneficial)
53    }
54
55    pub fn hard_requires(&self) -> bool {
56        self.eq(&AggregateOrderSensitivity::HardRequirement)
57    }
58}