Skip to content

Instantly share code, notes, and snippets.

@neilconway
Created September 17, 2026 14:05
Show Gist options
  • Select an option

  • Save neilconway/551457d97cea924b879be0dbc256deb7 to your computer and use it in GitHub Desktop.

Select an option

Save neilconway/551457d97cea924b879be0dbc256deb7 to your computer and use it in GitHub Desktop.
diff --git a/datafusion/physical-expr/src/aggregate.rs b/datafusion/physical-expr/src/aggregate.rs
index 90ceb996c..7b74a1b53 100644
--- a/datafusion/physical-expr/src/aggregate.rs
+++ b/datafusion/physical-expr/src/aggregate.rs
@@ -1201,7 +1201,9 @@ mod tests {
use arrow::datatypes::Field;
use datafusion_common::metadata::FieldMetadata;
- use datafusion_expr::{col, test::function_stub::sum};
+ use datafusion_expr::{
+ AggregateUDFImpl, Signature, Volatility, col, test::function_stub::sum,
+ };
fn aggregate_test_schema() -> Result<(Schema, DFSchema)> {
let schema = Schema::new(vec![Field::new("column1", DataType::Int64, true)]);
@@ -1272,4 +1274,98 @@ mod tests {
Ok(())
}
+
+ /// An aggregate that uses the default `AggregateUDFImpl::state_fields`,
+ /// which appends the ordering fields to the state.
+ #[derive(Debug, PartialEq, Eq, Hash)]
+ struct DefaultStateUdaf {
+ signature: Signature,
+ order_insensitive: bool,
+ }
+
+ impl DefaultStateUdaf {
+ fn new(order_insensitive: bool) -> Self {
+ Self {
+ signature: Signature::any(1, Volatility::Immutable),
+ order_insensitive,
+ }
+ }
+ }
+
+ impl AggregateUDFImpl for DefaultStateUdaf {
+ fn name(&self) -> &str {
+ "default_state_udaf"
+ }
+
+ fn signature(&self) -> &Signature {
+ &self.signature
+ }
+
+ fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
+ Ok(arg_types[0].clone())
+ }
+
+ /// Always fails, reporting how many ORDER BY expressions it was given.
+ fn accumulator(&self, acc_args: AccumulatorArgs) -> Result<Box<dyn Accumulator>> {
+ not_impl_err!("accumulator with {} order_bys", acc_args.order_bys.len())
+ }
+
+ fn order_sensitivity(&self) -> AggregateOrderSensitivity {
+ if self.order_insensitive {
+ AggregateOrderSensitivity::Insensitive
+ } else {
+ AggregateOrderSensitivity::HardRequirement
+ }
+ }
+ }
+
+ /// Builds `default_state_udaf(v ORDER BY k)`.
+ fn build_with_order_by(order_insensitive: bool) -> Result<AggregateFunctionExpr> {
+ let schema = Arc::new(Schema::new(vec![
+ Field::new("v", DataType::Int64, true),
+ Field::new("k", DataType::Int64, true),
+ ]));
+ let fun = AggregateUDF::from(DefaultStateUdaf::new(order_insensitive));
+ AggregateExprBuilder::new(Arc::new(fun), vec![Arc::new(Column::new("v", 0))])
+ .order_by(vec![PhysicalSortExpr {
+ expr: Arc::new(Column::new("k", 1)),
+ options: SortOptions::default(),
+ }])
+ .schema(schema)
+ .alias("default_state_udaf(v) ORDER BY [k ASC NULLS LAST]")
+ .build()
+ }
+
+ #[test]
+ fn order_insensitive_aggregate_discards_order_by() -> Result<()> {
+ let expr = build_with_order_by(true)?;
+ assert!(expr.order_bys().is_empty());
+ assert!(expr.all_expressions().order_by_exprs.is_empty());
+ // Only the value: the default `state_fields` has no ordering fields to append
+ assert_eq!(expr.state_fields()?.len(), 1);
+ let err = expr.create_accumulator().unwrap_err();
+ assert!(err.message().contains("accumulator with 0 order_bys"));
+
+ // Rewriting the expressions does not bring the ORDER BY back
+ let rewritten = expr
+ .with_new_expressions(expr.expressions(), vec![])
+ .expect("rewrite is supported");
+ assert!(rewritten.order_bys().is_empty());
+ assert_eq!(rewritten.state_fields()?.len(), 1);
+
+ Ok(())
+ }
+
+ #[test]
+ fn order_sensitive_aggregate_keeps_order_by() -> Result<()> {
+ let expr = build_with_order_by(false)?;
+ assert_eq!(expr.order_bys().len(), 1);
+ assert_eq!(expr.all_expressions().order_by_exprs.len(), 1);
+ // The value, followed by the ordering field
+ assert_eq!(expr.state_fields()?.len(), 2);
+ let err = expr.create_accumulator().unwrap_err();
+ assert!(err.message().contains("accumulator with 1 order_bys"));
+
+ Ok(())
+ }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment