Skip to content

Commit

Permalink
Support NULL literal in Min/Max (#11812)
Browse files Browse the repository at this point in the history
* Support NULL literal in Min/Max

* Fix ut

* fix fmt

---------

Co-authored-by: Andrew Lamb <[email protected]>
  • Loading branch information
xinlifoobar and alamb authored Aug 8, 2024
1 parent 1c9583a commit 56be714
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
5 changes: 4 additions & 1 deletion datafusion/core/src/dataframe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,10 @@ impl DataFrame {
{
let column =
batchs[0].column_by_name(field.name()).unwrap();
if field.data_type().is_numeric() {

if column.data_type().is_null() {
Arc::new(StringArray::from(vec!["null"]))
} else if field.data_type().is_numeric() {
cast(column, &DataType::Float64)?
} else {
cast(column, &DataType::Utf8)?
Expand Down
4 changes: 2 additions & 2 deletions datafusion/core/tests/dataframe/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ async fn describe_null() -> Result<()> {
"| null_count | 0 | 1 |",
"| mean | null | null |",
"| std | null | null |",
"| min | null | null |",
"| max | null | null |",
"| min | a | null |",
"| max | a | null |",
"| median | null | null |",
"+------------+------+------+"
];
Expand Down
2 changes: 2 additions & 0 deletions datafusion/functions-aggregate/src/min_max.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ macro_rules! typed_min_max_batch {
macro_rules! min_max_batch {
($VALUES:expr, $OP:ident) => {{
match $VALUES.data_type() {
DataType::Null => ScalarValue::Null,
DataType::Decimal128(precision, scale) => {
typed_min_max_batch!(
$VALUES,
Expand Down Expand Up @@ -579,6 +580,7 @@ macro_rules! interval_min_max {
macro_rules! min_max {
($VALUE:expr, $DELTA:expr, $OP:ident) => {{
Ok(match ($VALUE, $DELTA) {
(ScalarValue::Null, ScalarValue::Null) => ScalarValue::Null,
(
lhs @ ScalarValue::Decimal128(lhsv, lhsp, lhss),
rhs @ ScalarValue::Decimal128(rhsv, rhsp, rhss)
Expand Down
6 changes: 6 additions & 0 deletions datafusion/sqllogictest/test_files/aggregate.slt
Original file line number Diff line number Diff line change
Expand Up @@ -5548,3 +5548,9 @@ set datafusion.explain.logical_plan_only = false;

statement ok
drop table employee_csv;

# test null literal handling in supported aggregate functions
query I??III?T
select count(null), min(null), max(null), bit_and(NULL), bit_or(NULL), bit_xor(NULL), nth_value(NULL, 1), string_agg(NULL, ',');
----
0 NULL NULL NULL NULL NULL NULL NULL

0 comments on commit 56be714

Please sign in to comment.