-
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 1 commit
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 |
---|---|---|
|
@@ -62,22 +62,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 +78,120 @@ 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| { | ||
left_isnull != right_isnull | ||
|| left_value.is_nan() != right_value.is_nan() | ||
|| (!left_value.is_nan() | ||
&& !right_value.is_nan() | ||
&& left_value != right_value) | ||
}, | ||
) | ||
} | ||
|
||
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| { | ||
!(left_isnull != right_isnull | ||
|| left_value.is_nan() != right_value.is_nan() | ||
|| (!left_value.is_nan() | ||
&& !right_value.is_nan() | ||
&& left_value != right_value)) | ||
}, | ||
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. It took me quite some time to see that the only difference here is that there is a 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. Done |
||
) | ||
} | ||
|
||
pub(crate) fn is_distinct_from_f64( | ||
left: &Float64Array, | ||
right: &Float64Array, | ||
) -> Result<BooleanArray> { | ||
distinct( | ||
left, | ||
right, | ||
|left_value, right_value, left_isnull, right_isnull| { | ||
left_isnull != right_isnull | ||
|| left_value.is_nan() != right_value.is_nan() | ||
|| (!left_value.is_nan() | ||
&& !right_value.is_nan() | ||
&& left_value != right_value) | ||
}, | ||
) | ||
} | ||
|
||
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| { | ||
!(left_isnull != right_isnull | ||
|| left_value.is_nan() != right_value.is_nan() | ||
|| (!left_value.is_nan() | ||
&& !right_value.is_nan() | ||
&& left_value != right_value)) | ||
}, | ||
) | ||
} | ||
|
||
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.
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)
https://github.com/apache/arrow-datafusion/blob/main/datafusion/core/tests/sqllogictests/test_files/pg_compat/pg_compat_simple.slt perhaps?
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.
Ive duplicated in both
select.slt
andpg_compat_simple.slt
Reason for that is PG doesn't support double datatype