-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix is_distinct from for float NaN values #5446
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,3 +80,29 @@ select '1' from foo order by column1; | |
# foo distinct order by | ||
statement error DataFusion error: Error during planning: For SELECT DISTINCT, ORDER BY expressions column1 must appear in select list | ||
select distinct '1' from foo order by column1; | ||
|
||
# distincts for float nan | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might be a good test to add to https://github.com/apache/arrow-datafusion/tree/main/datafusion/core/tests/sqllogictests/test_files/pg_compat so they are automatically compared with postgres as part of CI (kudos to @melgenek for help there) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ive duplicated in both |
||
query BBBBBBBBBBBBBBBBB | ||
select | ||
'nan'::double is distinct from 'nan'::double v1, | ||
'nan'::double is not distinct from 'nan'::double v2, | ||
'nan'::double is not distinct from null v3, | ||
'nan'::double is distinct from null v4, | ||
null is distinct from 'nan'::double v5, | ||
null is not distinct from 'nan'::double v6, | ||
'nan'::float is distinct from 'nan'::float v7, | ||
'nan'::float is not distinct from 'nan'::float v8, | ||
'nan'::float is not distinct from null v9, | ||
'nan'::float is distinct from null v10, | ||
null is distinct from 'nan'::float v11, | ||
null is not distinct from 'nan'::float v12, | ||
1::float is distinct from 2::float v13, | ||
'nan'::float is distinct from 1::float v14, | ||
'nan'::float is not distinct from 1::float v15, | ||
1::float is not distinct from null v16, | ||
1::float is distinct from null v17 | ||
; | ||
---- | ||
false true false true true false false true false true true false true true false false true | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,14 @@ use std::sync::Arc; | |
// Simple (low performance) kernels until optimized kernels are added to arrow | ||
// See https://github.com/apache/arrow-rs/issues/960 | ||
|
||
macro_rules! distinct_float { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thank you -- this is great |
||
($LEFT:expr, $RIGHT:expr, $LEFT_ISNULL:expr, $RIGHT_ISNULL:expr) => {{ | ||
$LEFT_ISNULL != $RIGHT_ISNULL | ||
|| $LEFT.is_nan() != $RIGHT.is_nan() | ||
|| (!$LEFT.is_nan() && !$RIGHT.is_nan() && $LEFT != $RIGHT) | ||
}}; | ||
} | ||
|
||
pub(crate) fn is_distinct_from_bool( | ||
left: &BooleanArray, | ||
right: &BooleanArray, | ||
|
@@ -62,22 +70,13 @@ pub(crate) fn is_distinct_from<T>( | |
where | ||
T: ArrowNumericType, | ||
{ | ||
let left_data = left.data(); | ||
let right_data = right.data(); | ||
let array_len = left_data.len().min(right_data.len()); | ||
|
||
let left_values = left.values(); | ||
let right_values = right.values(); | ||
|
||
let distinct = arrow_buffer::MutableBuffer::collect_bool(array_len, |i| { | ||
left_data.is_null(i) != right_data.is_null(i) || left_values[i] != right_values[i] | ||
}); | ||
|
||
let array_data = ArrayData::builder(arrow_schema::DataType::Boolean) | ||
.len(array_len) | ||
.add_buffer(distinct.into()); | ||
|
||
Ok(BooleanArray::from(unsafe { array_data.build_unchecked() })) | ||
distinct( | ||
left, | ||
right, | ||
|left_value, right_value, left_isnull, right_isnull| { | ||
left_isnull != right_isnull || left_value != right_value | ||
}, | ||
) | ||
} | ||
|
||
pub(crate) fn is_not_distinct_from<T>( | ||
|
@@ -87,25 +86,104 @@ pub(crate) fn is_not_distinct_from<T>( | |
where | ||
T: ArrowNumericType, | ||
{ | ||
let left_data = left.data(); | ||
let right_data = right.data(); | ||
let array_len = left_data.len().min(right_data.len()); | ||
distinct( | ||
left, | ||
right, | ||
|left_value, right_value, left_isnull, right_isnull| { | ||
!(left_isnull != right_isnull || left_value != right_value) | ||
}, | ||
) | ||
} | ||
|
||
fn distinct< | ||
T, | ||
F: FnMut( | ||
<T as ArrowPrimitiveType>::Native, | ||
<T as ArrowPrimitiveType>::Native, | ||
bool, | ||
bool, | ||
) -> bool, | ||
>( | ||
left: &PrimitiveArray<T>, | ||
right: &PrimitiveArray<T>, | ||
mut op: F, | ||
) -> Result<BooleanArray> | ||
where | ||
T: ArrowNumericType, | ||
{ | ||
let left_values = left.values(); | ||
let right_values = right.values(); | ||
let left_data = left.data(); | ||
let right_data = right.data(); | ||
|
||
let array_len = left_data.len().min(right_data.len()); | ||
let distinct = arrow_buffer::MutableBuffer::collect_bool(array_len, |i| { | ||
!(left_data.is_null(i) != right_data.is_null(i) | ||
|| left_values[i] != right_values[i]) | ||
op( | ||
left_values[i], | ||
right_values[i], | ||
left_data.is_null(i), | ||
right_data.is_null(i), | ||
) | ||
}); | ||
|
||
let array_data = ArrayData::builder(arrow_schema::DataType::Boolean) | ||
.len(array_len) | ||
.add_buffer(distinct.into()); | ||
|
||
Ok(BooleanArray::from(unsafe { array_data.build_unchecked() })) | ||
} | ||
|
||
pub(crate) fn is_distinct_from_f32( | ||
left: &Float32Array, | ||
right: &Float32Array, | ||
) -> Result<BooleanArray> { | ||
distinct( | ||
left, | ||
right, | ||
|left_value, right_value, left_isnull, right_isnull| { | ||
distinct_float!(left_value, right_value, left_isnull, right_isnull) | ||
}, | ||
) | ||
} | ||
|
||
pub(crate) fn is_not_distinct_from_f32( | ||
left: &Float32Array, | ||
right: &Float32Array, | ||
) -> Result<BooleanArray> { | ||
distinct( | ||
left, | ||
right, | ||
|left_value, right_value, left_isnull, right_isnull| { | ||
!(distinct_float!(left_value, right_value, left_isnull, right_isnull)) | ||
}, | ||
) | ||
} | ||
|
||
pub(crate) fn is_distinct_from_f64( | ||
left: &Float64Array, | ||
right: &Float64Array, | ||
) -> Result<BooleanArray> { | ||
distinct( | ||
left, | ||
right, | ||
|left_value, right_value, left_isnull, right_isnull| { | ||
distinct_float!(left_value, right_value, left_isnull, right_isnull) | ||
}, | ||
) | ||
} | ||
|
||
pub(crate) fn is_not_distinct_from_f64( | ||
left: &Float64Array, | ||
right: &Float64Array, | ||
) -> Result<BooleanArray> { | ||
distinct( | ||
left, | ||
right, | ||
|left_value, right_value, left_isnull, right_isnull| { | ||
!(distinct_float!(left_value, right_value, left_isnull, right_isnull)) | ||
}, | ||
) | ||
} | ||
|
||
pub(crate) fn is_distinct_from_utf8<OffsetSize: OffsetSizeTrait>( | ||
left: &GenericStringArray<OffsetSize>, | ||
right: &GenericStringArray<OffsetSize>, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️