Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion datafusion/core/tests/dataframe/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async fn describe() -> Result<()> {
| std | 2107.472815166704 | null | 2.8724780750809518 | 2.8724780750809518 | 2.8724780750809518 | 28.724780750809533 | 3.1597258182544645 | 29.012028558317645 | null | null | null | 0.5000342500942125 | 3.44808750051728 |
| min | 0.0 | null | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 01/01/09 | 0 | 2008-12-31T23:00:00 | 2009.0 | 1.0 |
| max | 7299.0 | null | 9.0 | 9.0 | 9.0 | 90.0 | 9.899999618530273 | 90.89999999999999 | 12/31/10 | 9 | 2010-12-31T04:09:13.860 | 2010.0 | 12.0 |
| median | 3649.0 | null | 4.0 | 4.0 | 4.0 | 45.0 | 4.949999809265137 | 45.45 | null | null | null | 2009.0 | 7.0 |
| median | 3649.5 | null | 4.5 | 4.5 | 4.5 | 45.0 | 4.949999809265137 | 45.45 | null | null | null | 2009.5 | 7.0 |
+------------+-------------------+----------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+-----------------+------------+-------------------------+--------------------+-------------------+
");
Ok(())
Expand Down
40 changes: 20 additions & 20 deletions datafusion/core/tests/dataframe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1204,26 +1204,26 @@ async fn window_using_aggregates() -> Result<()> {
| first_value | last_val | approx_distinct | approx_median | median | max | min | c2 | c3 |
+-------------+----------+-----------------+---------------+--------+-----+------+----+------+
| | | | | | | | 1 | -85 |
| -85 | -101 | 14 | -12.0 | -12 | 83 | -101 | 4 | -54 |
| -85 | -101 | 17 | -25.0 | -25 | 83 | -101 | 5 | -31 |
| -85 | -12 | 10 | -32.75 | -34 | 83 | -85 | 3 | 13 |
| -85 | -25 | 3 | -56.0 | -56 | -25 | -85 | 1 | -5 |
| -85 | -31 | 18 | -29.75 | -28 | 83 | -101 | 5 | 36 |
| -85 | -38 | 16 | -25.0 | -25 | 83 | -101 | 4 | 65 |
| -85 | -43 | 7 | -43.0 | -43 | 83 | -85 | 2 | 45 |
| -85 | -48 | 6 | -35.75 | -36 | 83 | -85 | 2 | -43 |
| -85 | -5 | 4 | -37.75 | -40 | -5 | -85 | 1 | 83 |
| -85 | -54 | 15 | -17.0 | -18 | 83 | -101 | 4 | -38 |
| -85 | -56 | 2 | -70.5 | -70 | -56 | -85 | 1 | -25 |
| -85 | -72 | 9 | -43.0 | -43 | 83 | -85 | 3 | -12 |
| -85 | -85 | 1 | -85.0 | -85 | -85 | -85 | 1 | -56 |
| -85 | 13 | 11 | -17.0 | -18 | 83 | -85 | 3 | 14 |
| -85 | 13 | 11 | -25.0 | -25 | 83 | -85 | 3 | 13 |
| -85 | 14 | 12 | -12.0 | -12 | 83 | -85 | 3 | 17 |
| -85 | 17 | 13 | -11.25 | -8 | 83 | -85 | 4 | -101 |
| -85 | 45 | 8 | -34.5 | -34 | 83 | -85 | 3 | -72 |
| -85 | 65 | 17 | -17.0 | -18 | 83 | -101 | 5 | -101 |
| -85 | 83 | 5 | -25.0 | -25 | 83 | -85 | 2 | -48 |
| -85 | -101 | 14 | -12.0 | -12.0 | 83 | -101 | 4 | -54 |
| -85 | -101 | 17 | -25.0 | -25.0 | 83 | -101 | 5 | -31 |
| -85 | -12 | 10 | -32.75 | -34.0 | 83 | -85 | 3 | 13 |
| -85 | -25 | 3 | -56.0 | -56.0 | -25 | -85 | 1 | -5 |
| -85 | -31 | 18 | -29.75 | -28.0 | 83 | -101 | 5 | 36 |
| -85 | -38 | 16 | -25.0 | -25.0 | 83 | -101 | 4 | 65 |
| -85 | -43 | 7 | -43.0 | -43.0 | 83 | -85 | 2 | 45 |
| -85 | -48 | 6 | -35.75 | -36.5 | 83 | -85 | 2 | -43 |
| -85 | -5 | 4 | -37.75 | -40.5 | -5 | -85 | 1 | 83 |
| -85 | -54 | 15 | -17.0 | -18.5 | 83 | -101 | 4 | -38 |
| -85 | -56 | 2 | -70.5 | -70.5 | -56 | -85 | 1 | -25 |
| -85 | -72 | 9 | -43.0 | -43.0 | 83 | -85 | 3 | -12 |
| -85 | -85 | 1 | -85.0 | -85.0 | -85 | -85 | 1 | -56 |
| -85 | 13 | 11 | -17.0 | -18.5 | 83 | -85 | 3 | 14 |
| -85 | 13 | 11 | -25.0 | -25.0 | 83 | -85 | 3 | 13 |
| -85 | 14 | 12 | -12.0 | -12.0 | 83 | -85 | 3 | 17 |
| -85 | 17 | 13 | -11.25 | -8.5 | 83 | -85 | 4 | -101 |
| -85 | 45 | 8 | -34.5 | -34.0 | 83 | -85 | 3 | -72 |
| -85 | 65 | 17 | -17.0 | -18.5 | 83 | -101 | 5 | -101 |
| -85 | 83 | 5 | -25.0 | -25.0 | 83 | -85 | 2 | -48 |
+-------------+----------+-----------------+---------------+--------+-----+------+----+------+
"
);
Expand Down
28 changes: 14 additions & 14 deletions datafusion/core/tests/sql/aggregates/dict_nulls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ async fn test_aggregates_null_handling_comprehensive() -> Result<()> {
+----------------+--------------+
| dict_null_vals | median_value |
+----------------+--------------+
| | 3 |
| group_x | 1 |
| group_y | 5 |
| group_z | 7 |
| | 3.0 |
| group_x | 1.0 |
| group_y | 5.0 |
| group_z | 7.0 |
+----------------+--------------+
");

Expand Down Expand Up @@ -437,16 +437,16 @@ async fn test_median_distinct_with_fuzz_table_dict_nulls() -> Result<()> {
assert_snapshot!(
batches_to_string(&results),
@r"
+--------+---------------------+------+------+------+--------+--------+
| u8_low | dictionary_utf8_low | col1 | col2 | col3 | col4 | col5 |
+--------+---------------------+------+------+------+--------+--------+
| 50 | | | 30 | | 987.65 | 400000 |
| 50 | group_three | 5000 | 50 | 5000 | 555.55 | 500000 |
| 75 | | 4000 | | 4000 | | 450000 |
| 100 | group_one | 1100 | 11 | 1000 | 123.45 | 110000 |
| 100 | group_two | 1500 | 15 | 1500 | 111.11 | 150000 |
| 200 | | 2500 | 22 | 2500 | 506.11 | 250000 |
+--------+---------------------+------+------+------+--------+--------+
+--------+---------------------+--------+------+--------+--------+----------+
| u8_low | dictionary_utf8_low | col1 | col2 | col3 | col4 | col5 |
+--------+---------------------+--------+------+--------+--------+----------+
| 50 | | | 30.0 | | 987.65 | 400000.0 |
| 50 | group_three | 5000.0 | 50.0 | 5000.0 | 555.55 | 500000.0 |
| 75 | | 4000.0 | | 4000.0 | | 450000.0 |
| 100 | group_one | 1100.0 | 11.0 | 1000.0 | 123.45 | 110000.0 |
| 100 | group_two | 1500.0 | 15.0 | 1500.0 | 111.11 | 150000.0 |
| 200 | | 2500.0 | 22.5 | 2500.0 | 506.11 | 250000.0 |
+--------+---------------------+--------+------+--------+--------+----------+
"
);

Expand Down
25 changes: 22 additions & 3 deletions datafusion/functions-aggregate/src/median.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ use arrow::datatypes::{
ArrowNativeType, ArrowPrimitiveType, Decimal32Type, Decimal64Type, FieldRef,
};

use datafusion_common::types::{NativeType, logical_float64};
use datafusion_common::{
DataFusionError, Result, ScalarValue, assert_eq_or_internal_err,
internal_datafusion_err,
};
use datafusion_expr::function::StateFieldsArgs;
use datafusion_expr::{
Accumulator, AggregateUDFImpl, Documentation, Signature, Volatility,
function::AccumulatorArgs, utils::format_state_name,
Accumulator, AggregateUDFImpl, Coercion, Documentation, Signature, TypeSignature,
TypeSignatureClass, Volatility, function::AccumulatorArgs, utils::format_state_name,
};
use datafusion_expr::{EmitTo, GroupsAccumulator};
use datafusion_functions_aggregate_common::aggregate::groups_accumulator::accumulate::accumulate;
Expand Down Expand Up @@ -99,7 +100,25 @@ impl Default for Median {
impl Median {
pub fn new() -> Self {
Self {
signature: Signature::numeric(1, Volatility::Immutable),
// Integer inputs are coerced to Float64 so the average of the two
// middle values is not truncated. This matches DuckDB / PostgreSQL / Spark.
// Float and Decimal inputs preserve their type.
signature: Signature::one_of(
vec![
TypeSignature::Coercible(vec![Coercion::new_exact(
TypeSignatureClass::Decimal,
)]),
TypeSignature::Coercible(vec![Coercion::new_exact(
TypeSignatureClass::Float,
)]),
TypeSignature::Coercible(vec![Coercion::new_implicit(
TypeSignatureClass::Native(logical_float64()),
vec![TypeSignatureClass::Integer],
NativeType::Float64,
)]),
],
Volatility::Immutable,
),
}
}
}
Expand Down
Loading
Loading