Skip to content

Apache DataFusion SQL Query Engine

Feature: AggregateMonotonicity (#14271)

mertak-synnada48a28af801cc
diff --git a/datafusion/core/tests/physical_optimizer/enforce_sorting.rs b/datafusion/core/tests/physical_optimizer/enforce_sorting.rs
index 66bd1c37d3..ab90eab74d 100644
--- a/datafusion/core/tests/physical_optimizer/enforce_sorting.rs
+++ b/datafusion/core/tests/physical_optimizer/enforce_sorting.rs
@@ -18,12 +18,14 @@
use std::sync::Arc;
use crate::physical_optimizer::test_utils::{
- aggregate_exec, bounded_window_exec, check_integrity, coalesce_batches_exec,
+ aggregate_exec, bounded_window_exec, bounded_window_exec_non_set_monotonic,
+ bounded_window_exec_with_partition, check_integrity, coalesce_batches_exec,
coalesce_partitions_exec, create_test_schema, create_test_schema2,
- create_test_schema3, filter_exec, global_limit_exec, hash_join_exec, limit_exec,
- local_limit_exec, memory_exec, parquet_exec, repartition_exec, sort_exec, sort_expr,
- sort_expr_options, sort_merge_join_exec, sort_preserving_merge_exec,
- spr_repartition_exec, stream_exec_ordered, union_exec, RequirementsTestExec,
+ create_test_schema3, create_test_schema4, filter_exec, global_limit_exec,
+ hash_join_exec, limit_exec, local_limit_exec, memory_exec, parquet_exec,
+ repartition_exec, sort_exec, sort_expr, sort_expr_options, sort_merge_join_exec,
+ sort_preserving_merge_exec, spr_repartition_exec, stream_exec_ordered, union_exec,
+ RequirementsTestExec,
};
use datafusion_physical_plan::displayable;
@@ -238,6 +240,208 @@ async fn test_remove_unnecessary_sort5() -> Result<()> {
Ok(())
}
+#[tokio::test]
+async fn test_bounded_window_set_monotonic_no_partition() -> Result<()> {
+ let schema = create_test_schema()?;
+
+ let source = parquet_exec_sorted(&schema, vec![]);
+
+ let sort_exprs = vec![sort_expr_options(
+ "nullable_col",
+ &schema,
+ SortOptions {
+ descending: true,
+ nulls_first: false,
+ },
+ )];
+ let sort = sort_exec(sort_exprs.clone(), source);
+
+ let bounded_window = bounded_window_exec("nullable_col", vec![], sort);
+
+ let output_schema = bounded_window.schema();
+ let sort_exprs2 = vec![sort_expr_options(
+ "count",
+ &output_schema,
+ SortOptions {
+ descending: false,
+ nulls_first: false,
+ },
+ )];
+ let physical_plan = sort_exec(sort_exprs2.clone(), bounded_window);
+
+ let expected_input = [
+ "SortExec: expr=[count@2 ASC NULLS LAST], preserve_partitioning=[false]",
+ " BoundedWindowAggExec: wdw=[count: Ok(Field { name: \"count\", data_type: Int64, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(NULL), end_bound: CurrentRow, is_causal: false }], mode=[Sorted]",
+ " SortExec: expr=[nullable_col@0 DESC NULLS LAST], preserve_partitioning=[false]",
+ " ParquetExec: file_groups={1 group: [[x]]}, projection=[nullable_col, non_nullable_col]",
+ ];
+ let expected_optimized = [
+ "BoundedWindowAggExec: wdw=[count: Ok(Field { name: \"count\", data_type: Int64, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(NULL), end_bound: CurrentRow, is_causal: false }], mode=[Sorted]",
+ " ParquetExec: file_groups={1 group: [[x]]}, projection=[nullable_col, non_nullable_col]",
+ ];
+ assert_optimized!(expected_input, expected_optimized, physical_plan, true);
+
+ Ok(())
+}
+
+#[tokio::test]
+async fn test_bounded_plain_window_set_monotonic_with_partitions() -> Result<()> {
+ let schema = create_test_schema()?;
+
+ let source = parquet_exec_sorted(&schema, vec![]);
+
+ let sort_exprs = vec![sort_expr_options(
+ "nullable_col",
+ &schema,
+ SortOptions {
+ descending: true,
+ nulls_first: false,
+ },
+ )];
+ let sort = sort_exec(sort_exprs.clone(), source);
+
+ let partition_bys = &[col("nullable_col", &schema)?];
+ let bounded_window = bounded_window_exec_with_partition(
+ "non_nullable_col",
+ vec![],
+ partition_bys,
+ sort,
+ false,
+ );
+
+ let output_schema = bounded_window.schema();
+ let sort_exprs2 = vec![sort_expr_options(
+ "count",
+ &output_schema,
+ SortOptions {
+ descending: false,
+ nulls_first: false,
+ },
+ )];
+ let physical_plan = sort_exec(sort_exprs2.clone(), bounded_window);
+
+ let expected_input = [
+ "SortExec: expr=[count@2 ASC NULLS LAST], preserve_partitioning=[false]",
+ " BoundedWindowAggExec: wdw=[count: Ok(Field { name: \"count\", data_type: Int64, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(NULL), end_bound: CurrentRow, is_causal: false }], mode=[Sorted]",
+ " SortExec: expr=[nullable_col@0 DESC NULLS LAST], preserve_partitioning=[false]",
+ " ParquetExec: file_groups={1 group: [[x]]}, projection=[nullable_col, non_nullable_col]",
+ ];
+ let expected_optimized = [
+ "SortExec: expr=[count@2 ASC NULLS LAST], preserve_partitioning=[false]",
+ " BoundedWindowAggExec: wdw=[count: Ok(Field { name: \"count\", data_type: Int64, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(NULL), end_bound: CurrentRow, is_causal: false }], mode=[Sorted]",
+ " SortExec: expr=[nullable_col@0 ASC NULLS LAST], preserve_partitioning=[false]",
+ " ParquetExec: file_groups={1 group: [[x]]}, projection=[nullable_col, non_nullable_col]",
+ ];
+ assert_optimized!(expected_input, expected_optimized, physical_plan, true);
+
+ Ok(())
+}
+
+#[tokio::test]
+async fn test_bounded_plain_window_set_monotonic_with_partitions_partial() -> Result<()> {
+ let schema = create_test_schema()?;
+
+ let source = parquet_exec_sorted(&schema, vec![]);
+
+ let sort_exprs = vec![sort_expr_options(
+ "nullable_col",
+ &schema,
+ SortOptions {
+ descending: true,
+ nulls_first: false,
+ },
+ )];
+ let sort = sort_exec(sort_exprs.clone(), source);
+
+ let partition_bys = &[col("nullable_col", &schema)?];
+ let bounded_window = bounded_window_exec_with_partition(
+ "non_nullable_col",
+ vec![],
+ partition_bys,
+ sort,
+ false,
+ );
+
+ let output_schema = bounded_window.schema();
+ let sort_exprs2 = vec![
+ sort_expr_options(
+ "nullable_col",
+ &output_schema,
+ SortOptions {
+ descending: true,
+ nulls_first: false,
+ },
+ ),
+ sort_expr_options(
+ "count",
+ &output_schema,
+ SortOptions {
+ descending: false,
+ nulls_first: false,
+ },
+ ),
+ ];
+ let physical_plan = sort_exec(sort_exprs2.clone(), bounded_window);
+
+ let expected_input = [
+ "SortExec: expr=[nullable_col@0 DESC NULLS LAST, count@2 ASC NULLS LAST], preserve_partitioning=[false]",
+ " BoundedWindowAggExec: wdw=[count: Ok(Field { name: \"count\", data_type: Int64, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(NULL), end_bound: CurrentRow, is_causal: false }], mode=[Sorted]",
+ " SortExec: expr=[nullable_col@0 DESC NULLS LAST], preserve_partitioning=[false]",
+ " ParquetExec: file_groups={1 group: [[x]]}, projection=[nullable_col, non_nullable_col]",
+ ];
+ let expected_optimized = [
+ "BoundedWindowAggExec: wdw=[count: Ok(Field { name: \"count\", data_type: Int64, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(NULL), end_bound: CurrentRow, is_causal: false }], mode=[Sorted]",
+ " SortExec: expr=[nullable_col@0 DESC NULLS LAST, count@2 ASC NULLS LAST], preserve_partitioning=[false]",
+ " ParquetExec: file_groups={1 group: [[x]]}, projection=[nullable_col, non_nullable_col]",
+ ];
+ assert_optimized!(expected_input, expected_optimized, physical_plan, true);
+
+ Ok(())
+}
+
+#[tokio::test]
+async fn test_bounded_window_non_set_monotonic_sort() -> Result<()> {
+ let schema = create_test_schema4()?;
+ let sort_exprs = vec![sort_expr_options(
+ "a",
+ &schema,
+ SortOptions {
+ descending: true,
+ nulls_first: false,
+ },
+ )];
+ let source = parquet_exec_sorted(&schema, sort_exprs.clone());
+ let sort = sort_exec(sort_exprs.clone(), source);
+
+ let bounded_window =
+ bounded_window_exec_non_set_monotonic("a", sort_exprs.clone(), sort);
+ let output_schema = bounded_window.schema();
+ let sort_exprs2 = vec![sort_expr_options(
+ "avg",
+ &output_schema,
+ SortOptions {
+ descending: false,
+ nulls_first: false,
+ },
+ )];
+ let physical_plan = sort_exec(sort_exprs2.clone(), bounded_window);
+
+ let expected_input = [
+ "SortExec: expr=[avg@5 ASC NULLS LAST], preserve_partitioning=[false]",
+ " BoundedWindowAggExec: wdw=[avg: Ok(Field { name: \"avg\", data_type: Float64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(NULL), end_bound: CurrentRow, is_causal: false }], mode=[Sorted]",
+ " SortExec: expr=[a@0 DESC NULLS LAST], preserve_partitioning=[false]",
+ " ParquetExec: file_groups={1 group: [[x]]}, projection=[a, b, c, d, e], output_ordering=[a@0 DESC NULLS LAST]",
+ ];
+ let expected_optimized = [
+ "SortExec: expr=[avg@5 ASC NULLS LAST], preserve_partitioning=[false]",
+ " BoundedWindowAggExec: wdw=[avg: Ok(Field { name: \"avg\", data_type: Float64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(NULL), end_bound: CurrentRow, is_causal: false }], mode=[Sorted]",
+ " ParquetExec: file_groups={1 group: [[x]]}, projection=[a, b, c, d, e], output_ordering=[a@0 DESC NULLS LAST]",
+ ];
+ assert_optimized!(expected_input, expected_optimized, physical_plan, true);
+
+ Ok(())
+}
+
#[tokio::test]
async fn test_do_not_remove_sort_with_limit() -> Result<()> {
let schema = create_test_schema()?;
diff --git a/datafusion/core/tests/physical_optimizer/test_utils.rs b/datafusion/core/tests/physical_optimizer/test_utils.rs
index 6f0a4e46cd..b26b1227ea 100644
--- a/datafusion/core/tests/physical_optimizer/test_utils.rs
+++ b/datafusion/core/tests/physical_optimizer/test_utils.rs
@@ -33,6 +33,7 @@ use datafusion_common::{JoinType, Result};
use datafusion_execution::object_store::ObjectStoreUrl;
use datafusion_execution::{SendableRecordBatchStream, TaskContext};
use datafusion_expr::{WindowFrame, WindowFunctionDefinition};
+use datafusion_functions_aggregate::average::avg_udaf;
use datafusion_functions_aggregate::count::count_udaf;
use datafusion_physical_expr::aggregate::{AggregateExprBuilder, AggregateFunctionExpr};
use datafusion_physical_expr::expressions::col;
@@ -120,6 +121,17 @@ pub fn create_test_schema3() -> Result<SchemaRef> {
Ok(schema)
}
+// Generate a schema which consists of 5 columns (a, b, c, d, e) of Uint64
+pub fn create_test_schema4() -> Result<SchemaRef> {
+ let a = Field::new("a", DataType::UInt64, true);
+ let b = Field::new("b", DataType::UInt64, false);
+ let c = Field::new("c", DataType::UInt64, true);
+ let d = Field::new("d", DataType::UInt64, false);
+ let e = Field::new("e", DataType::Int64, false);
+ let schema = Arc::new(Schema::new(vec![a, b, c, d, e]));
+ Ok(schema)
+}
+
pub fn sort_merge_join_exec(
left: Arc<dyn ExecutionPlan>,
right: Arc<dyn ExecutionPlan>,
@@ -188,6 +200,49 @@ pub fn bounded_window_exec(
col_name: &str,
sort_exprs: impl IntoIterator<Item = PhysicalSortExpr>,
input: Arc<dyn ExecutionPlan>,
+) -> Arc<dyn ExecutionPlan> {
+ bounded_window_exec_with_partition(col_name, sort_exprs, &[], input, false)
+}
+
+pub fn bounded_window_exec_with_partition(
+ col_name: &str,
+ sort_exprs: impl IntoIterator<Item = PhysicalSortExpr>,
+ partition_by: &[Arc<dyn PhysicalExpr>],
+ input: Arc<dyn ExecutionPlan>,
+ should_reverse: bool,
+) -> Arc<dyn ExecutionPlan> {
+ let sort_exprs: LexOrdering = sort_exprs.into_iter().collect();
+ let schema = input.schema();
+ let mut window_expr = create_window_expr(
+ &WindowFunctionDefinition::AggregateUDF(count_udaf()),
+ "count".to_owned(),
+ &[col(col_name, &schema).unwrap()],
+ partition_by,
+ sort_exprs.as_ref(),
+ Arc::new(WindowFrame::new(Some(false))),
+ schema.as_ref(),
+ false,
+ )
+ .unwrap();
+ if should_reverse {
+ window_expr = window_expr.get_reverse_expr().unwrap();
+ }
+
+ Arc::new(
+ BoundedWindowAggExec::try_new(
+ vec![window_expr],
+ Arc::clone(&input),
+ vec![],
+ InputOrderMode::Sorted,
+ )
+ .unwrap(),
+ )
+}
+
+pub fn bounded_window_exec_non_set_monotonic(
+ col_name: &str,
+ sort_exprs: impl IntoIterator<Item = PhysicalSortExpr>,
+ input: Arc<dyn ExecutionPlan>,
) -> Arc<dyn ExecutionPlan> {
let sort_exprs: LexOrdering = sort_exprs.into_iter().collect();
let schema = input.schema();
@@ -195,8 +250,8 @@ pub fn bounded_window_exec(
Arc::new(
BoundedWindowAggExec::try_new(
vec![create_window_expr(
- &WindowFunctionDefinition::AggregateUDF(count_udaf()),
- "count".to_owned(),
+ &WindowFunctionDefinition::AggregateUDF(avg_udaf()),
+ "avg".to_owned(),
&[col(col_name, &schema).unwrap()],
&[],
sort_exprs.as_ref(),
diff --git a/datafusion/expr/src/lib.rs b/datafusion/expr/src/lib.rs
index 017415da8f..aaa65c676a 100644
--- a/datafusion/expr/src/lib.rs
+++ b/datafusion/expr/src/lib.rs
@@ -91,7 +91,8 @@ pub use partition_evaluator::PartitionEvaluator;
pub use sqlparser;
pub use table_source::{TableProviderFilterPushDown, TableSource, TableType};
pub use udaf::{
- aggregate_doc_sections, AggregateUDF, AggregateUDFImpl, ReversedUDAF, StatisticsArgs,
+ aggregate_doc_sections, AggregateUDF, AggregateUDFImpl, ReversedUDAF,
+ SetMonotonicity, StatisticsArgs,
};
pub use udf::{
scalar_doc_sections, ReturnInfo, ReturnTypeArgs, ScalarFunctionArgs, ScalarUDF,
diff --git a/datafusion/expr/src/udaf.rs b/datafusion/expr/src/udaf.rs
index 56c9822495..84a513b21b 100644
--- a/datafusion/expr/src/udaf.rs
+++ b/datafusion/expr/src/udaf.rs
@@ -389,7 +389,7 @@ pub trait AggregateUDFImpl: Debug + Send + Sync {
/// Whether the aggregate function is nullable.
///
- /// Nullable means that that the function could return `null` for any inputs.
+ /// Nullable means that the function could return `null` for any inputs.
/// For example, aggregate functions like `COUNT` always return a non null value
/// but others like `MIN` will return `NULL` if there is nullable input.
/// Note that if the function is declared as *not* nullable, make sure the [`AggregateUDFImpl::default_value`] is `non-null`
@@ -635,6 +635,12 @@ pub trait AggregateUDFImpl: Debug + Send + Sync {
fn documentation(&self) -> Option<&Documentation> {
None
}
+
+ /// Indicates whether the aggregation function is monotonic as a set
+ /// function. See [`SetMonotonicity`] for details.
+ fn set_monotonicity(&self, _data_type: &DataType) -> SetMonotonicity {
+ SetMonotonicity::NotMonotonic
+ }
}
impl PartialEq for dyn AggregateUDFImpl {
@@ -818,6 +824,27 @@ pub mod aggregate_doc_sections {
};
}
+/// Indicates whether an aggregation function is monotonic as a set
+/// function. A set function is monotonically increasing if its value
+/// increases as its argument grows (as a set). Formally, `f` is a
+/// monotonically increasing set function if `f(S) >= f(T)` whenever `S`
+/// is a superset of `T`.
+///
+/// For example `COUNT` and `MAX` are monotonically increasing as their
+/// values always increase (or stay the same) as new values are seen. On
+/// the other hand, `MIN` is monotonically decreasing as its value always
+/// decreases or stays the same as new values are seen.
+#[derive(Debug, Clone, PartialEq)]
+pub enum SetMonotonicity {
+ /// Aggregate value increases or stays the same as the input set grows.
+ Increasing,
+ /// Aggregate value decreases or stays the same as the input set grows.
+ Decreasing,
+ /// Aggregate value may increase, decrease, or stay the same as the input
+ /// set grows.
+ NotMonotonic,
+}
+
#[cfg(test)]
mod test {
use crate::{AggregateUDF, AggregateUDFImpl};
diff --git a/datafusion/expr/src/window_frame.rs b/datafusion/expr/src/window_frame.rs
index 815d5742af..82b3365052 100644
--- a/datafusion/expr/src/window_frame.rs
+++ b/datafusion/expr/src/window_frame.rs
@@ -291,6 +291,13 @@ impl WindowFrame {
&& (self.end_bound.is_unbounded()
|| self.end_bound == WindowFrameBound::CurrentRow)
}
+
+ /// Is the window frame ever-expanding (it always grows in the superset sense).
+ /// Useful when understanding if set-monotonicity properties of functions can
+ /// be exploited.
+ pub fn is_ever_expanding(&self) -> bool {
+ self.start_bound.is_unbounded()
+ }
}
/// There are five ways to describe starting and ending frame boundaries:
diff --git a/datafusion/functions-aggregate/src/count.rs b/datafusion/functions-aggregate/src/count.rs
index f2621b48be..fa04e1aca2 100644
--- a/datafusion/functions-aggregate/src/count.rs
+++ b/datafusion/functions-aggregate/src/count.rs
@@ -49,7 +49,7 @@ use datafusion_common::{
use datafusion_expr::function::StateFieldsArgs;
use datafusion_expr::{
function::AccumulatorArgs, utils::format_state_name, Accumulator, AggregateUDFImpl,
- Documentation, EmitTo, GroupsAccumulator, Signature, Volatility,
+ Documentation, EmitTo, GroupsAccumulator, SetMonotonicity, Signature, Volatility,
};
use datafusion_expr::{Expr, ReversedUDAF, StatisticsArgs, TypeSignature};
use datafusion_functions_aggregate_common::aggregate::count_distinct::{
@@ -351,6 +351,12 @@ impl AggregateUDFImpl for Count {
fn documentation(&self) -> Option<&Documentation> {
self.doc()
}
+
+ fn set_monotonicity(&self, _data_type: &DataType) -> SetMonotonicity {
+ // `COUNT` is monotonically increasing as it always increases or stays
+ // the same as new values are seen.
+ SetMonotonicity::Increasing
+ }
}
#[derive(Debug)]
diff --git a/datafusion/functions-aggregate/src/min_max.rs b/datafusion/functions-aggregate/src/min_max.rs
index c4e05bd57d..da5ec739ad 100644
--- a/datafusion/functions-aggregate/src/min_max.rs
+++ b/datafusion/functions-aggregate/src/min_max.rs
@@ -56,8 +56,8 @@ use arrow::datatypes::{
use crate::min_max::min_max_bytes::MinMaxBytesAccumulator;
use datafusion_common::ScalarValue;
use datafusion_expr::{
- function::AccumulatorArgs, Accumulator, AggregateUDFImpl, Documentation, Signature,
- Volatility,
+ function::AccumulatorArgs, Accumulator, AggregateUDFImpl, Documentation,
+ SetMonotonicity, Signature, Volatility,
};
use datafusion_expr::{GroupsAccumulator, StatisticsArgs};
use datafusion_macros::user_doc;
@@ -361,6 +361,12 @@ impl AggregateUDFImpl for Max {
fn documentation(&self) -> Option<&Documentation> {
self.doc()
}
+
+ fn set_monotonicity(&self, _data_type: &DataType) -> SetMonotonicity {
+ // `MAX` is monotonically increasing as it always increases or stays
+ // the same as new values are seen.
+ SetMonotonicity::Increasing
+ }
}
// Statically-typed version of min/max(array) -> ScalarValue for string types
@@ -1183,6 +1189,12 @@ impl AggregateUDFImpl for Min {
fn documentation(&self) -> Option<&Documentation> {
self.doc()
}
+
+ fn set_monotonicity(&self, _data_type: &DataType) -> SetMonotonicity {
+ // `MIN` is monotonically decreasing as it always decreases or stays
+ // the same as new values are seen.
+ SetMonotonicity::Decreasing
+ }
}
/// An accumulator to compute the minimum value
diff --git a/datafusion/functions-aggregate/src/sum.rs b/datafusion/functions-aggregate/src/sum.rs
index 6c2854f6bc..9615ca33a5 100644
--- a/datafusion/functions-aggregate/src/sum.rs
+++ b/datafusion/functions-aggregate/src/sum.rs
@@ -39,7 +39,7 @@ use datafusion_expr::function::StateFieldsArgs;
use datafusion_expr::utils::format_state_name;
use datafusion_expr::{
Accumulator, AggregateUDFImpl, Documentation, GroupsAccumulator, ReversedUDAF,
- Signature, Volatility,
+ SetMonotonicity, Signature, Volatility,
};
use datafusion_functions_aggregate_common::aggregate::groups_accumulator::prim_op::PrimitiveGroupsAccumulator;
use datafusion_functions_aggregate_common::utils::Hashable;
@@ -254,6 +254,18 @@ impl AggregateUDFImpl for Sum {
fn documentation(&self) -> Option<&Documentation> {
self.doc()
}
+
+ fn set_monotonicity(&self, data_type: &DataType) -> SetMonotonicity {
+ // `SUM` is only monotonically increasing when its input is unsigned.
+ // TODO: Expand these utilizing statistics.
+ match data_type {
+ DataType::UInt8 => SetMonotonicity::Increasing,
+ DataType::UInt16 => SetMonotonicity::Increasing,
+ DataType::UInt32 => SetMonotonicity::Increasing,
+ DataType::UInt64 => SetMonotonicity::Increasing,
+ _ => SetMonotonicity::NotMonotonic,
+ }
+ }
}
/// This accumulator computes SUM incrementally
diff --git a/datafusion/physical-expr/src/aggregate.rs b/datafusion/physical-expr/src/aggregate.rs
index 4eaabace72..84406f5005 100644
--- a/datafusion/physical-expr/src/aggregate.rs
+++ b/datafusion/physical-expr/src/aggregate.rs
@@ -35,24 +35,26 @@ pub mod utils {
};
}
+use std::fmt::Debug;
+use std::sync::Arc;
+
+use crate::expressions::Column;
+
use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
-use datafusion_common::ScalarValue;
-use datafusion_common::{internal_err, not_impl_err, Result};
-use datafusion_expr::AggregateUDF;
-use datafusion_expr::ReversedUDAF;
+use arrow_schema::SortOptions;
+use datafusion_common::{internal_err, not_impl_err, Result, ScalarValue};
+use datafusion_expr::{AggregateUDF, ReversedUDAF, SetMonotonicity};
use datafusion_expr_common::accumulator::Accumulator;
+use datafusion_expr_common::groups_accumulator::GroupsAccumulator;
use datafusion_expr_common::type_coercion::aggregates::check_arg_count;
-use datafusion_functions_aggregate_common::accumulator::AccumulatorArgs;
-use datafusion_functions_aggregate_common::accumulator::StateFieldsArgs;
+use datafusion_functions_aggregate_common::accumulator::{
+ AccumulatorArgs, StateFieldsArgs,
+};
use datafusion_functions_aggregate_common::order::AggregateOrderSensitivity;
use datafusion_physical_expr_common::physical_expr::PhysicalExpr;
-use datafusion_physical_expr_common::sort_expr::LexOrdering;
+use datafusion_physical_expr_common::sort_expr::{LexOrdering, PhysicalSortExpr};
use datafusion_physical_expr_common::utils::reverse_order_bys;
-use datafusion_expr_common::groups_accumulator::GroupsAccumulator;
-use std::fmt::Debug;
-use std::sync::Arc;
-
/// Builder for physical [`AggregateFunctionExpr`]
///
/// `AggregateFunctionExpr` contains the information necessary to call
@@ -533,6 +535,28 @@ impl AggregateFunctionExpr {
pub fn default_value(&self, data_type: &DataType) -> Result<ScalarValue> {
self.fun.default_value(data_type)
}
+
+ /// Indicates whether the aggregation function is monotonic as a set
+ /// function. See [`SetMonotonicity`] for details.
+ pub fn set_monotonicity(&self) -> SetMonotonicity {
+ let field = self.field();
+ let data_type = field.data_type();
+ self.fun.inner().set_monotonicity(data_type)
+ }
+
+ /// Returns `PhysicalSortExpr` based on the set monotonicity of the function.
+ pub fn get_result_ordering(&self, aggr_func_idx: usize) -> Option<PhysicalSortExpr> {
+ // If the aggregate expressions are set-monotonic, the output data is
+ // naturally ordered with it per group or partition.
+ let monotonicity = self.set_monotonicity();
+ if monotonicity == SetMonotonicity::NotMonotonic {
+ return None;
+ }
+ let expr = Arc::new(Column::new(self.name(), aggr_func_idx));
+ let options =
+ SortOptions::new(monotonicity == SetMonotonicity::Decreasing, false);
+ Some(PhysicalSortExpr { expr, options })
+ }
}
/// Stores the physical expressions used inside the `AggregateExpr`.
diff --git a/datafusion/physical-expr/src/window/aggregate.rs b/datafusion/physical-expr/src/window/aggregate.rs
index 0c56bdc929..a94d5b1212 100644
--- a/datafusion/physical-expr/src/window/aggregate.rs
+++ b/datafusion/physical-expr/src/window/aggregate.rs
@@ -21,18 +21,18 @@ use std::any::Any;
use std::ops::Range;
use std::sync::Arc;
-use arrow::array::Array;
-use arrow::record_batch::RecordBatch;
-use arrow::{array::ArrayRef, datatypes::Field};
-
use crate::aggregate::AggregateFunctionExpr;
+use crate::window::standard::add_new_ordering_expr_with_partition_by;
use crate::window::window_expr::AggregateWindowExpr;
use crate::window::{
PartitionBatches, PartitionWindowAggStates, SlidingAggregateWindowExpr, WindowExpr,
};
-use crate::{reverse_order_bys, PhysicalExpr};
-use datafusion_common::ScalarValue;
-use datafusion_common::{DataFusionError, Result};
+use crate::{reverse_order_bys, EquivalenceProperties, PhysicalExpr};
+
+use arrow::array::Array;
+use arrow::record_batch::RecordBatch;
+use arrow::{array::ArrayRef, datatypes::Field};
+use datafusion_common::{DataFusionError, Result, ScalarValue};
use datafusion_expr::{Accumulator, WindowFrame};
use datafusion_physical_expr_common::sort_expr::LexOrdering;
@@ -67,6 +67,23 @@ impl PlainAggregateWindowExpr {
pub fn get_aggregate_expr(&self) -> &AggregateFunctionExpr {
&self.aggregate
}
+
+ pub fn add_equal_orderings(
+ &self,
+ eq_properties: &mut EquivalenceProperties,
+ window_expr_index: usize,
+ ) {
+ if let Some(expr) = self
+ .get_aggregate_expr()
+ .get_result_ordering(window_expr_index)
+ {
+ add_new_ordering_expr_with_partition_by(
+ eq_properties,
+ expr,
+ &self.partition_by,
+ );
+ }
+ }
}
/// peer based evaluation based on the fact that batch is pre-sorted given the sort columns
@@ -135,7 +152,7 @@ impl WindowExpr for PlainAggregateWindowExpr {
fn get_reverse_expr(&self) -> Option<Arc<dyn WindowExpr>> {
self.aggregate.reverse_expr().map(|reverse_expr| {
let reverse_window_frame = self.window_frame.reverse();
- if reverse_window_frame.start_bound.is_unbounded() {
+ if reverse_window_frame.is_ever_expanding() {
Arc::new(PlainAggregateWindowExpr::new(
Arc::new(reverse_expr),
&self.partition_by.clone(),
diff --git a/datafusion/physical-expr/src/window/sliding_aggregate.rs b/datafusion/physical-expr/src/window/sliding_aggregate.rs
index 572eb8866a..23967e78f0 100644
--- a/datafusion/physical-expr/src/window/sliding_aggregate.rs
+++ b/datafusion/physical-expr/src/window/sliding_aggregate.rs
@@ -21,16 +21,16 @@ use std::any::Any;
use std::ops::Range;
use std::sync::Arc;
-use arrow::array::{Array, ArrayRef};
-use arrow::datatypes::Field;
-use arrow::record_batch::RecordBatch;
-
use crate::aggregate::AggregateFunctionExpr;
use crate::window::window_expr::AggregateWindowExpr;
use crate::window::{
PartitionBatches, PartitionWindowAggStates, PlainAggregateWindowExpr, WindowExpr,
};
use crate::{expressions::PhysicalSortExpr, reverse_order_bys, PhysicalExpr};
+
+use arrow::array::{Array, ArrayRef};
+use arrow::datatypes::Field;
+use arrow::record_batch::RecordBatch;
use datafusion_common::{Result, ScalarValue};
use datafusion_expr::{Accumulator, WindowFrame};
use datafusion_physical_expr_common::sort_expr::LexOrdering;
@@ -119,7 +119,7 @@ impl WindowExpr for SlidingAggregateWindowExpr {
fn get_reverse_expr(&self) -> Option<Arc<dyn WindowExpr>> {
self.aggregate.reverse_expr().map(|reverse_expr| {
let reverse_window_frame = self.window_frame.reverse();
- if reverse_window_frame.start_bound.is_unbounded() {
+ if reverse_window_frame.is_ever_expanding() {
Arc::new(PlainAggregateWindowExpr::new(
Arc::new(reverse_expr),
&self.partition_by.clone(),
diff --git a/datafusion/physical-expr/src/window/standard.rs b/datafusion/physical-expr/src/window/standard.rs
index 82e48a5f93..2682a7467a 100644
--- a/datafusion/physical-expr/src/window/standard.rs
+++ b/datafusion/physical-expr/src/window/standard.rs
@@ -33,7 +33,7 @@ use datafusion_common::utils::evaluate_partition_ranges;
use datafusion_common::{Result, ScalarValue};
use datafusion_expr::window_state::{WindowAggState, WindowFrameContext};
use datafusion_expr::WindowFrame;
-use datafusion_physical_expr_common::sort_expr::LexOrdering;
+use datafusion_physical_expr_common::sort_expr::{LexOrdering, PhysicalSortExpr};
/// A window expr that takes the form of a [`StandardWindowFunctionExpr`].
#[derive(Debug)]
@@ -65,8 +65,7 @@ impl StandardWindowExpr {
&self.expr
}
- /// Adds any equivalent orderings generated by the `self.expr`
- /// to `builder`.
+ /// Adds any equivalent orderings generated by `self.expr` to `builder`.
///
/// If `self.expr` doesn't have an ordering, ordering equivalence properties
/// are not updated. Otherwise, ordering equivalence properties are updated
@@ -74,24 +73,11 @@ impl StandardWindowExpr {
pub fn add_equal_orderings(&self, eq_properties: &mut EquivalenceProperties) {
let schema = eq_properties.schema();
if let Some(fn_res_ordering) = self.expr.get_result_ordering(schema) {
- if self.partition_by.is_empty() {
- // In the absence of a PARTITION BY, ordering of `self.expr` is global:
- eq_properties
- .add_new_orderings([LexOrdering::new(vec![fn_res_ordering])]);
- } else {
- // If we have a PARTITION BY, standard functions can not introduce
- // a global ordering unless the existing ordering is compatible
- // with PARTITION BY expressions. To elaborate, when PARTITION BY
- // expressions and existing ordering expressions are equal (w.r.t.
- // set equality), we can prefix the ordering of `self.expr` with
- // the existing ordering.
- let (mut ordering, _) =
- eq_properties.find_longest_permutation(&self.partition_by);
- if ordering.len() == self.partition_by.len() {
- ordering.push(fn_res_ordering);
- eq_properties.add_new_orderings([ordering]);
- }
- }
+ add_new_ordering_expr_with_partition_by(
+ eq_properties,
+ fn_res_ordering,
+ &self.partition_by,
+ );
}
}
}
@@ -283,3 +269,28 @@ impl WindowExpr for StandardWindowExpr {
}
}
}
+
+/// Adds a new ordering expression into existing ordering equivalence class(es) based on
+/// PARTITION BY information (if it exists).
+pub(crate) fn add_new_ordering_expr_with_partition_by(
+ eqp: &mut EquivalenceProperties,
+ expr: PhysicalSortExpr,
+ partition_by: &Vec<Arc<dyn PhysicalExpr>>,
+) {
+ if partition_by.is_empty() {
+ // In the absence of a PARTITION BY, ordering of `self.expr` is global:
+ eqp.add_new_orderings([LexOrdering::new(vec![expr])]);
+ } else {
+ // If we have a PARTITION BY, standard functions can not introduce
+ // a global ordering unless the existing ordering is compatible
+ // with PARTITION BY expressions. To elaborate, when PARTITION BY
+ // expressions and existing ordering expressions are equal (w.r.t.
+ // set equality), we can prefix the ordering of `self.expr` with
+ // the existing ordering.
+ let (mut ordering, _) = eqp.find_longest_permutation(partition_by);
+ if ordering.len() == partition_by.len() {
+ ordering.push(expr);
+ eqp.add_new_orderings([ordering]);
+ }
+ }
+}
diff --git a/datafusion/physical-plan/src/aggregates/mod.rs b/datafusion/physical-plan/src/aggregates/mod.rs
index 4502466f0b..3aaa766f92 100644
--- a/datafusion/physical-plan/src/aggregates/mod.rs
+++ b/datafusion/physical-plan/src/aggregates/mod.rs
@@ -45,7 +45,7 @@ use datafusion_expr::{Accumulator, Aggregate};
use datafusion_physical_expr::aggregate::AggregateFunctionExpr;
use datafusion_physical_expr::{
equivalence::ProjectionMapping, expressions::Column, physical_exprs_contains,
- EquivalenceProperties, LexOrdering, LexRequirement, PhysicalExpr,
+ ConstExpr, EquivalenceProperties, LexOrdering, LexRequirement, PhysicalExpr,
PhysicalSortRequirement,
};
@@ -512,6 +512,7 @@ impl AggregateExec {
&group_expr_mapping,
&mode,
&input_order_mode,
+ aggr_expr.as_slice(),
);
Ok(AggregateExec {
@@ -648,12 +649,24 @@ impl AggregateExec {
group_expr_mapping: &ProjectionMapping,
mode: &AggregateMode,
input_order_mode: &InputOrderMode,
+ aggr_exprs: &[Arc<AggregateFunctionExpr>],
) -> PlanProperties {
// Construct equivalence properties:
let mut eq_properties = input
.equivalence_properties()
.project(group_expr_mapping, schema);
+ // If the group by is empty, then we ensure that the operator will produce
+ // only one row, and mark the generated result as a constant value.
+ if group_expr_mapping.map.is_empty() {
+ let mut constants = eq_properties.constants().to_vec();
+ let new_constants = aggr_exprs.iter().enumerate().map(|(idx, func)| {
+ ConstExpr::new(Arc::new(Column::new(func.name(), idx)))
+ });
+ constants.extend(new_constants);
+ eq_properties = eq_properties.with_constants(constants);
+ }
+
// Group by expression will be a distinct value after the aggregation.
// Add it into the constraint set.
let mut constraints = eq_properties.constraints().to_vec();
@@ -927,7 +940,7 @@ fn create_schema(
AggregateMode::Partial => {
// in partial mode, the fields of the accumulator's state
for expr in aggr_expr {
- fields.extend(expr.state_fields()?.iter().cloned())
+ fields.extend(expr.state_fields()?.iter().cloned());
}
}
AggregateMode::Final
diff --git a/datafusion/physical-plan/src/windows/bounded_window_agg_exec.rs b/datafusion/physical-plan/src/windows/bounded_window_agg_exec.rs
index 2ac86da92e..cf19d0daf3 100644
--- a/datafusion/physical-plan/src/windows/bounded_window_agg_exec.rs
+++ b/datafusion/physical-plan/src/windows/bounded_window_agg_exec.rs
@@ -190,10 +190,10 @@ impl BoundedWindowAggExec {
fn compute_properties(
input: &Arc<dyn ExecutionPlan>,
schema: &SchemaRef,
- window_expr: &[Arc<dyn WindowExpr>],
+ window_exprs: &[Arc<dyn WindowExpr>],
) -> PlanProperties {
// Calculate equivalence properties:
- let eq_properties = window_equivalence_properties(schema, input, window_expr);
+ let eq_properties = window_equivalence_properties(schema, input, window_exprs);
// As we can have repartitioning using the partition keys, this can
// be either one or more than one, depending on the presence of
diff --git a/datafusion/physical-plan/src/windows/mod.rs b/datafusion/physical-plan/src/windows/mod.rs
index 510cbc248b..3785230c0e 100644
--- a/datafusion/physical-plan/src/windows/mod.rs
+++ b/datafusion/physical-plan/src/windows/mod.rs
@@ -133,7 +133,7 @@ fn window_expr_from_aggregate_expr(
aggregate: Arc<AggregateFunctionExpr>,
) -> Arc<dyn WindowExpr> {
// Is there a potentially unlimited sized window frame?
- let unbounded_window = window_frame.start_bound.is_unbounded();
+ let unbounded_window = window_frame.is_ever_expanding();
if !unbounded_window {
Arc::new(SlidingAggregateWindowExpr::new(
@@ -336,18 +336,32 @@ pub(crate) fn get_partition_by_sort_exprs(
pub(crate) fn window_equivalence_properties(
schema: &SchemaRef,
input: &Arc<dyn ExecutionPlan>,
- window_expr: &[Arc<dyn WindowExpr>],
+ window_exprs: &[Arc<dyn WindowExpr>],
) -> EquivalenceProperties {
// We need to update the schema, so we can not directly use
// `input.equivalence_properties()`.
let mut window_eq_properties = EquivalenceProperties::new(Arc::clone(schema))
.extend(input.equivalence_properties().clone());
- for expr in window_expr {
+ let schema_len = schema.fields.len();
+ let window_expr_indices =
+ ((schema_len - window_exprs.len())..schema_len).collect::<Vec<_>>();
+ for (i, expr) in window_exprs.iter().enumerate() {
if let Some(udf_window_expr) = expr.as_any().downcast_ref::<StandardWindowExpr>()
{
udf_window_expr.add_equal_orderings(&mut window_eq_properties);
+ } else if let Some(aggregate_udf_window_expr) =
+ expr.as_any().downcast_ref::<PlainAggregateWindowExpr>()
+ {
+ let window_expr_index = window_expr_indices[i];
+ aggregate_udf_window_expr
+ .add_equal_orderings(&mut window_eq_properties, window_expr_index);
}
+ // TODO: SlidingAggregateWindowExpr cannot introduce a new ordering yet
+ // because we cannot determine whether the window's incoming elements
+ // are greater than its outgoing elements. However, we do have
+ // the necessary tools to support this, and we can extend support
+ // for these cases in the future.
}
window_eq_properties
}
diff --git a/datafusion/physical-plan/src/windows/window_agg_exec.rs b/datafusion/physical-plan/src/windows/window_agg_exec.rs
index b132c32470..f0c258a025 100644
--- a/datafusion/physical-plan/src/windows/window_agg_exec.rs
+++ b/datafusion/physical-plan/src/windows/window_agg_exec.rs
@@ -34,6 +34,7 @@ use crate::{
ExecutionPlanProperties, PhysicalExpr, PlanProperties, RecordBatchStream,
SendableRecordBatchStream, Statistics, WindowExpr,
};
+
use arrow::array::ArrayRef;
use arrow::compute::{concat, concat_batches};
use arrow::datatypes::SchemaRef;
@@ -44,6 +45,7 @@ use datafusion_common::utils::{evaluate_partition_ranges, transpose};
use datafusion_common::{internal_err, Result};
use datafusion_execution::TaskContext;
use datafusion_physical_expr_common::sort_expr::{LexOrdering, LexRequirement};
+
use futures::{ready, Stream, StreamExt};
/// Window execution plan
@@ -118,10 +120,10 @@ impl WindowAggExec {
fn compute_properties(
schema: SchemaRef,
input: &Arc<dyn ExecutionPlan>,
- window_expr: &[Arc<dyn WindowExpr>],
+ window_exprs: &[Arc<dyn WindowExpr>],
) -> PlanProperties {
// Calculate equivalence properties:
- let eq_properties = window_equivalence_properties(&schema, input, window_expr);
+ let eq_properties = window_equivalence_properties(&schema, input, window_exprs);
// Get output partitioning:
// Because we can have repartitioning using the partition keys this
diff --git a/datafusion/sqllogictest/test_files/aggregate.slt b/datafusion/sqllogictest/test_files/aggregate.slt
index 4838911649..c8c544785d 100644
--- a/datafusion/sqllogictest/test_files/aggregate.slt
+++ b/datafusion/sqllogictest/test_files/aggregate.slt
@@ -6260,6 +6260,23 @@ physical_plan
15)------------ProjectionExec: expr=[1 as id, 2 as foo]
16)--------------PlaceholderRowExec
+# SortExec is removed if it is coming after one-row producing AggregateExec's having an empty group by expression
+query TT
+EXPLAIN SELECT COUNT(c5) as count_c5 FROM aggregate_test_100 ORDER BY count_c5;
+----
+logical_plan
+01)Sort: count_c5 ASC NULLS LAST
+02)--Projection: count(aggregate_test_100.c5) AS count_c5
+03)----Aggregate: groupBy=[[]], aggr=[[count(aggregate_test_100.c5)]]
+04)------TableScan: aggregate_test_100 projection=[c5]
+physical_plan
+01)ProjectionExec: expr=[count(aggregate_test_100.c5)@0 as count_c5]
+02)--AggregateExec: mode=Final, gby=[], aggr=[count(aggregate_test_100.c5)]
+03)----CoalescePartitionsExec
+04)------AggregateExec: mode=Partial, gby=[], aggr=[count(aggregate_test_100.c5)]
+05)--------RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1
+06)----------CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100.csv]]}, projection=[c5], has_header=true
+
#######
# Group median test
#######
diff --git a/datafusion/sqllogictest/test_files/tpch/plans/q13.slt.part b/datafusion/sqllogictest/test_files/tpch/plans/q13.slt.part
index 2667bb23a4..f584892e8a 100644
--- a/datafusion/sqllogictest/test_files/tpch/plans/q13.slt.part
+++ b/datafusion/sqllogictest/test_files/tpch/plans/q13.slt.part
@@ -72,4 +72,4 @@ physical_plan
17)------------------------RepartitionExec: partitioning=Hash([o_custkey@1], 4), input_partitions=4
18)--------------------------CoalesceBatchesExec: target_batch_size=8192
19)----------------------------FilterExec: o_comment@2 NOT LIKE %special%requests%, projection=[o_orderkey@0, o_custkey@1]
-20)------------------------------CsvExec: file_groups={4 groups: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/tpch/data/orders.tbl:0..4223281], [WORKSPACE_ROOT/datafusion/sqllogictest/test_files/tpch/data/orders.tbl:4223281..8446562], [WORKSPACE_ROOT/datafusion/sqllogictest/test_files/tpch/data/orders.tbl:8446562..12669843], [WORKSPACE_ROOT/datafusion/sqllogictest/test_files/tpch/data/orders.tbl:12669843..16893122]]}, projection=[o_orderkey, o_custkey, o_comment], has_header=false
+20)------------------------------CsvExec: file_groups={4 groups: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/tpch/data/orders.tbl:0..4223281], [WORKSPACE_ROOT/datafusion/sqllogictest/test_files/tpch/data/orders.tbl:4223281..8446562], [WORKSPACE_ROOT/datafusion/sqllogictest/test_files/tpch/data/orders.tbl:8446562..12669843], [WORKSPACE_ROOT/datafusion/sqllogictest/test_files/tpch/data/orders.tbl:12669843..16893122]]}, projection=[o_orderkey, o_custkey, o_comment], has_header=false
\ No newline at end of file
diff --git a/datafusion/sqllogictest/test_files/window.slt b/datafusion/sqllogictest/test_files/window.slt
index 3e387008eb..679e0f637f 100644
--- a/datafusion/sqllogictest/test_files/window.slt
+++ b/datafusion/sqllogictest/test_files/window.slt
@@ -3127,9 +3127,9 @@ physical_plan
04)------BoundedWindowAggExec: wdw=[row_number() ORDER BY [annotated_data_infinite2.a ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW: Ok(Field { name: "row_number() ORDER BY [annotated_data_infinite2.a ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW", data_type: UInt64, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(Int32(NULL)), end_bound: CurrentRow, is_causal: false }], mode=[Sorted]
05)--------StreamingTableExec: partition_sizes=1, projection=[a0, a, b, c, d], infinite_source=true, output_ordering=[a@1 ASC NULLS LAST, b@2 ASC NULLS LAST, c@3 ASC NULLS LAST]
-# this is a negative test for asserting that window functions (other than ROW_NUMBER)
-# are not added to ordering equivalence
-# physical plan should contain SortExec.
+# Top level sort is pushed down through BoundedWindowAggExec as its SUM result does already satisfy the required
+# global order. The existing sort is for the second-term lexicographical ordering requirement, which is being
+# preserved also at lexicographical level during the BoundedWindowAggExec.
query TT
EXPLAIN SELECT c9, sum1 FROM (SELECT c9,
SUM(c9) OVER(ORDER BY c9 DESC) as sum1
@@ -3144,8 +3144,8 @@ logical_plan
03)----WindowAggr: windowExpr=[[sum(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c9 DESC NULLS FIRST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]]
04)------TableScan: aggregate_test_100 projection=[c9]
physical_plan
-01)SortExec: TopK(fetch=5), expr=[sum1@1 ASC NULLS LAST, c9@0 DESC], preserve_partitioning=[false]
-02)--ProjectionExec: expr=[c9@0 as c9, sum(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c9 DESC NULLS FIRST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@1 as sum1]
+01)ProjectionExec: expr=[c9@0 as c9, sum(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c9 DESC NULLS FIRST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@1 as sum1]
+02)--GlobalLimitExec: skip=0, fetch=5
03)----BoundedWindowAggExec: wdw=[sum(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c9 DESC NULLS FIRST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW: Ok(Field { name: "sum(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c9 DESC NULLS FIRST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW", data_type: UInt64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(UInt64(NULL)), end_bound: CurrentRow, is_causal: false }], mode=[Sorted]
04)------SortExec: expr=[c9@0 DESC], preserve_partitioning=[false]
05)--------CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100.csv]]}, projection=[c9], has_header=true
@@ -5452,3 +5452,89 @@ order by c1, c2, rank1, rank2;
statement ok
drop table t1;
+
+
+# Set-Monotonic Window Aggregate functions can output results in order
+statement ok
+CREATE EXTERNAL TABLE aggregate_test_100_ordered (
+ c1 VARCHAR NOT NULL,
+ c2 TINYINT NOT NULL,
+ c3 SMALLINT NOT NULL,
+ c4 SMALLINT,
+ c5 INT,
+ c6 BIGINT NOT NULL,
+ c7 SMALLINT NOT NULL,
+ c8 INT NOT NULL,
+ c9 INT UNSIGNED NOT NULL,
+ c10 BIGINT UNSIGNED NOT NULL,
+ c11 FLOAT NOT NULL,
+ c12 DOUBLE NOT NULL,
+ c13 VARCHAR NOT NULL
+)
+STORED AS CSV
+LOCATION '../../testing/data/csv/aggregate_test_100.csv'
+WITH ORDER (c1)
+OPTIONS ('format.has_header' 'true');
+
+statement ok
+set datafusion.optimizer.prefer_existing_sort = true;
+
+query TT
+EXPLAIN SELECT c1, SUM(c9) OVER(PARTITION BY c1) as sum_c9 FROM aggregate_test_100_ordered ORDER BY c1, sum_c9;
+----
+logical_plan
+01)Sort: aggregate_test_100_ordered.c1 ASC NULLS LAST, sum_c9 ASC NULLS LAST
+02)--Projection: aggregate_test_100_ordered.c1, sum(aggregate_test_100_ordered.c9) PARTITION BY [aggregate_test_100_ordered.c1] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING AS sum_c9
+03)----WindowAggr: windowExpr=[[sum(CAST(aggregate_test_100_ordered.c9 AS UInt64)) PARTITION BY [aggregate_test_100_ordered.c1] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING]]
+04)------TableScan: aggregate_test_100_ordered projection=[c1, c9]
+physical_plan
+01)SortPreservingMergeExec: [c1@0 ASC NULLS LAST, sum_c9@1 ASC NULLS LAST]
+02)--ProjectionExec: expr=[c1@0 as c1, sum(aggregate_test_100_ordered.c9) PARTITION BY [aggregate_test_100_ordered.c1] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING@2 as sum_c9]
+03)----WindowAggExec: wdw=[sum(aggregate_test_100_ordered.c9) PARTITION BY [aggregate_test_100_ordered.c1] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING: Ok(Field { name: "sum(aggregate_test_100_ordered.c9) PARTITION BY [aggregate_test_100_ordered.c1] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING", data_type: UInt64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Rows, start_bound: Preceding(UInt64(NULL)), end_bound: Following(UInt64(NULL)), is_causal: false }]
+04)------CoalesceBatchesExec: target_batch_size=1
+05)--------RepartitionExec: partitioning=Hash([c1@0], 2), input_partitions=2, preserve_order=true, sort_exprs=c1@0 ASC NULLS LAST
+06)----------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
+07)------------CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100.csv]]}, projection=[c1, c9], output_ordering=[c1@0 ASC NULLS LAST], has_header=true
+
+query TT
+EXPLAIN SELECT SUM(c9) OVER() as sum_c9 FROM aggregate_test_100_ordered ORDER BY sum_c9;
+----
+logical_plan
+01)Sort: sum_c9 ASC NULLS LAST
+02)--Projection: sum(aggregate_test_100_ordered.c9) ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING AS sum_c9
+03)----WindowAggr: windowExpr=[[sum(CAST(aggregate_test_100_ordered.c9 AS UInt64)) ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING]]
+04)------TableScan: aggregate_test_100_ordered projection=[c9]
+physical_plan
+01)ProjectionExec: expr=[sum(aggregate_test_100_ordered.c9) ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING@1 as sum_c9]
+02)--WindowAggExec: wdw=[sum(aggregate_test_100_ordered.c9) ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING: Ok(Field { name: "sum(aggregate_test_100_ordered.c9) ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING", data_type: UInt64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Rows, start_bound: Preceding(UInt64(NULL)), end_bound: Following(UInt64(NULL)), is_causal: false }]
+03)----CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100.csv]]}, projection=[c9], has_header=true
+
+query TT
+EXPLAIN SELECT c1, MIN(c5) OVER(PARTITION BY c1) as min_c5 FROM aggregate_test_100_ordered ORDER BY c1, min_c5 DESC NULLS LAST;
+----
+logical_plan
+01)Sort: aggregate_test_100_ordered.c1 ASC NULLS LAST, min_c5 DESC NULLS LAST
+02)--Projection: aggregate_test_100_ordered.c1, min(aggregate_test_100_ordered.c5) PARTITION BY [aggregate_test_100_ordered.c1] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING AS min_c5
+03)----WindowAggr: windowExpr=[[min(aggregate_test_100_ordered.c5) PARTITION BY [aggregate_test_100_ordered.c1] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING]]
+04)------TableScan: aggregate_test_100_ordered projection=[c1, c5]
+physical_plan
+01)SortPreservingMergeExec: [c1@0 ASC NULLS LAST, min_c5@1 DESC NULLS LAST]
+02)--ProjectionExec: expr=[c1@0 as c1, min(aggregate_test_100_ordered.c5) PARTITION BY [aggregate_test_100_ordered.c1] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING@2 as min_c5]
+03)----WindowAggExec: wdw=[min(aggregate_test_100_ordered.c5) PARTITION BY [aggregate_test_100_ordered.c1] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING: Ok(Field { name: "min(aggregate_test_100_ordered.c5) PARTITION BY [aggregate_test_100_ordered.c1] ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING", data_type: Int32, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Rows, start_bound: Preceding(UInt64(NULL)), end_bound: Following(UInt64(NULL)), is_causal: false }]
+04)------CoalesceBatchesExec: target_batch_size=1
+05)--------RepartitionExec: partitioning=Hash([c1@0], 2), input_partitions=2, preserve_order=true, sort_exprs=c1@0 ASC NULLS LAST
+06)----------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
+07)------------CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100.csv]]}, projection=[c1, c5], output_ordering=[c1@0 ASC NULLS LAST], has_header=true
+
+query TT
+EXPLAIN SELECT MAX(c5) OVER() as max_c5 FROM aggregate_test_100_ordered ORDER BY max_c5;
+----
+logical_plan
+01)Sort: max_c5 ASC NULLS LAST
+02)--Projection: max(aggregate_test_100_ordered.c5) ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING AS max_c5
+03)----WindowAggr: windowExpr=[[max(aggregate_test_100_ordered.c5) ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING]]
+04)------TableScan: aggregate_test_100_ordered projection=[c5]
+physical_plan
+01)ProjectionExec: expr=[max(aggregate_test_100_ordered.c5) ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING@1 as max_c5]
+02)--WindowAggExec: wdw=[max(aggregate_test_100_ordered.c5) ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING: Ok(Field { name: "max(aggregate_test_100_ordered.c5) ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING", data_type: Int32, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Rows, start_bound: Preceding(UInt64(NULL)), end_bound: Following(UInt64(NULL)), is_causal: false }]
+03)----CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100.csv]]}, projection=[c5], has_header=true