Skip to content

Commit d021cd9

Browse files
Sigma-Mamazhengxuan
authored andcommitted
fix: support untyped NULL input for median (apache#24104)
## Which issue does this PR close? - Closes apache#24077. ## Rationale for this change `median(NULL)` worked in DataFusion 53 but regressed in DataFusion 54. The untyped NULL argument remained `DataType::Null`, causing physical planning to create a median accumulator for an unsupported input type. ## What changes are included in this PR? - Keep the existing declarative signature for `median`. - Handle `DataType::Null` in physical execution using `NoopAccumulator`. - Use a Null state field for Null input. - Fall back to the generic groups accumulator adapter for Null input. - Add a SQL logic regression test for `median(NULL)`. ## Are these changes tested? Yes. - `cargo fmt --all -- --check` - `cargo test --profile=ci --test sqllogictests -- aggregate.slt` - `cargo test -p datafusion-functions-aggregate` - `cargo clippy -p datafusion-functions-aggregate --all-targets --all-features -- -D warnings` ## Are there any user-facing changes? Yes. `median(NULL)` now returns NULL with `DataType::Null` instead of failing during physical execution. --------- Co-authored-by: mazhengxuan <[email protected]>
1 parent 4e77f21 commit d021cd9

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

‎datafusion/functions-aggregate/src/median.rs‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ use datafusion_expr::{
5353
use datafusion_expr::{EmitTo, GroupsAccumulator};
5454
use datafusion_functions_aggregate_common::aggregate::groups_accumulator::accumulate::accumulate;
5555
use datafusion_functions_aggregate_common::aggregate::groups_accumulator::nulls::filtered_null_mask;
56+
use datafusion_functions_aggregate_common::noop_accumulator::NoopAccumulator;
5657
use datafusion_functions_aggregate_common::utils::{GenericDistinctBuffer, Hashable};
5758
use datafusion_macros::user_doc;
5859
use std::collections::HashMap;
@@ -138,6 +139,17 @@ impl AggregateUDFImpl for Median {
138139
}
139140

140141
fn state_fields(&self, args: StateFieldsArgs) -> Result<Vec<FieldRef>> {
142+
if args.input_fields[0].data_type().is_null() {
143+
return Ok(vec![
144+
Field::new(
145+
format_state_name(args.name, self.name()),
146+
DataType::Null,
147+
true,
148+
)
149+
.into(),
150+
]);
151+
}
152+
141153
//Intermediate state is a list of the elements we have collected so far
142154
let field = Field::new_list_field(args.input_fields[0].data_type().clone(), true);
143155
let state_name = if args.is_distinct {
@@ -174,6 +186,10 @@ impl AggregateUDFImpl for Median {
174186
}
175187

176188
let dt = acc_args.expr_fields[0].data_type().clone();
189+
if dt.is_null() {
190+
return Ok(Box::new(NoopAccumulator::default()));
191+
}
192+
177193
downcast_integer! {
178194
dt => (helper, dt),
179195
DataType::Float16 => helper!(Float16Type, dt),
@@ -192,7 +208,7 @@ impl AggregateUDFImpl for Median {
192208
}
193209

194210
fn groups_accumulator_supported(&self, args: AccumulatorArgs) -> bool {
195-
!args.is_distinct
211+
!args.is_distinct && !args.expr_fields[0].data_type().is_null()
196212
}
197213

198214
fn create_groups_accumulator(

‎datafusion/sqllogictest/test_files/aggregate.slt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,11 @@ select approx_median(NULL), arrow_typeof(approx_median(NULL)) from median_table;
12001200
----
12011201
NULL Null
12021202

1203+
query ?T
1204+
select median(NULL), arrow_typeof(median(NULL));
1205+
----
1206+
NULL Null
1207+
12031208
# median decimal
12041209
statement ok
12051210
create table t(c decimal(10, 4)) as values (0.0001), (0.0002), (0.0003), (0.0004), (0.0005), (0.0006);

0 commit comments

Comments
 (0)