Skip to content
Closed
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
67 changes: 67 additions & 0 deletions datafusion/common/src/float_semantics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,20 @@ pub fn normalize_floats(array: &ArrayRef) -> ArrayRef {
}
}

/// Like [`normalize_floats`], but takes ownership of the array.
///
/// This avoids any copying (including arc ref counts) if there is nothing, and
/// will reuse the underlying allocation if possible
pub fn normalize_floats_owned(array: ArrayRef) -> ArrayRef {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given how much of @geoffreyclaude 's obsession has gone into performance, I wanted to minimize any impact of this fix, so this is a version that avoids a clone and reuses the allocation when needed

I could have just changed normalize_floats to take an owned array but that makes for a larger diff. I am happy to do so if reviewers prefer

match array.data_type() {
DataType::Float16 => normalize_primitive_owned::<Float16Type>(array),
DataType::Float32 => normalize_primitive_owned::<Float32Type>(array),
DataType::Float64 => normalize_primitive_owned::<Float64Type>(array),
dt if has_float_leaf(dt) => normalize_floats(&array),
_ => array,
}
}

fn normalize_primitive<T>(array: &ArrayRef) -> ArrayRef
where
T: ArrowPrimitiveType,
Expand All @@ -165,6 +179,28 @@ where
Arc::new(arr.unary::<_, T>(normalize_float))
}

/// Like [`normalize_primitive`] except it takes an owned array
/// and tries to reuse the allocation when possible;
fn normalize_primitive_owned<T>(array: ArrayRef) -> ArrayRef
where
T: ArrowPrimitiveType,
T::Native: FloatSemantics,
{
let arr = array.as_primitive::<T>();
if !arr.values().iter().any(|v| v.needs_normalization()) {
return array;
}
// Drop the `ArrayRef` so the values buffer is uniquely owned (when nothing
// else refers to it) and can be rewritten in place.
let arr = arr.clone();
drop(array);
let normalized = match arr.unary_mut(normalize_float) {
Ok(normalized) => normalized, // updated in place
Err(arr) => arr.unary::<_, T>(normalize_float), // needed to copy
};
Arc::new(normalized)
}

/// Whether `data_type` is a float or contains a float at any depth of its
/// nested children.
pub fn has_float_leaf(data_type: &DataType) -> bool {
Expand Down Expand Up @@ -274,6 +310,37 @@ mod tests {
assert!(Arc::ptr_eq(&normalize_floats(&array), &array));
}

#[test]
fn normalize_floats_owned_rewrites_in_place_when_unique() {
let array: ArrayRef = Arc::new(Float64Array::from(vec![-0.0, 1.0]));
let values_ptr = array.as_primitive::<Float64Type>().values().as_ptr();
let result = normalize_floats_owned(array);
let result = result.as_primitive::<Float64Type>();
assert_eq!(result.values().as_ptr(), values_ptr);
assert_eq!(result.value(0).to_bits(), 0.0_f64.to_bits());

// A shared buffer is copied and the original is left as it is
let array: ArrayRef = Arc::new(Float64Array::from(vec![-0.0, 1.0]));
let result = normalize_floats_owned(Arc::clone(&array));
let result = result.as_primitive::<Float64Type>();
assert_ne!(
result.values().as_ptr(),
array.as_primitive::<Float64Type>().values().as_ptr()
);
assert_eq!(result.value(0).to_bits(), 0.0_f64.to_bits());
assert_eq!(
array.as_primitive::<Float64Type>().value(0).to_bits(),
(-0.0_f64).to_bits()
);

// Nothing to normalize: the input is returned as is
let array: ArrayRef = Arc::new(Float64Array::from(vec![0.0, 1.0]));
assert!(Arc::ptr_eq(
&normalize_floats_owned(Arc::clone(&array)),
&array
));
}

#[test]
fn normalize_floats_rewrites_nested_floats() {
let list = ListArray::from_iter_primitive::<Float64Type, _, _>(vec![
Expand Down
14 changes: 14 additions & 0 deletions datafusion/expr-common/src/columnar_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use arrow::{
datatypes::{DataType, TimeUnit},
util::pretty::pretty_format_columns,
};
use datafusion_common::float_semantics::normalize_floats_owned;
use datafusion_common::internal_datafusion_err;
use datafusion_common::{
Result, ScalarValue,
Expand Down Expand Up @@ -113,6 +114,19 @@ impl ColumnarValue {
}
}

/// Applies DataFusion's [float equality semantics] to this value (e.g.
/// `-0.0` becomes `+0.0`)
///
/// [float equality semantics]: datafusion_common::float_semantics
pub fn normalize(self) -> Self {
match self {
ColumnarValue::Array(array) => {
ColumnarValue::Array(normalize_floats_owned(array))
}
ColumnarValue::Scalar(scalar) => ColumnarValue::Scalar(scalar.normalize()),
}
}

/// Convert any [`Self::Scalar`] into an Arrow [`ArrayRef`] with the specified
/// number of rows by repeating the same scalar multiple times,
/// which is not as efficient as handling the scalar directly.
Expand Down
22 changes: 12 additions & 10 deletions datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4990,17 +4990,19 @@ mod tests {
if list_len > THRESHOLD_INLINE_INLIST || retains_short_lists {
// Floating-point literals must bypass structural set rewrites.
assert_eq!(simplified, expr);
} else {
let actual = create_physical_expr(
&simplified,
&schema,
&ExecutionProps::new(),
&PhysicalPlanningContext::default(),
)?
.evaluate(&batch)?
.into_array(batch.num_rows())?;
assert_eq!(actual.as_boolean(), &BooleanArray::from(expected));
}

// Regardless of the form the list takes (rewritten or not)
// it must correctly report that `-0.0` matches `0.0`.
let actual = create_physical_expr(
&simplified,
&schema,
&ExecutionProps::new(),
&PhysicalPlanningContext::default(),
)?
.evaluate(&batch)?
.into_array(batch.num_rows())?;
assert_eq!(actual.as_boolean(), &BooleanArray::from(expected));
}
}
}
Expand Down
76 changes: 75 additions & 1 deletion datafusion/physical-expr/src/expressions/in_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ impl PhysicalExpr for InListExpr {
fn evaluate(&self, batch: &RecordBatch) -> Result<ColumnarValue> {
let num_rows = batch.num_rows();
let value = self.expr.evaluate(batch)?;
let value = value.normalize(); // so 0.0 == -0.0

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the fix is basically to sprinkle calls to normalize around

let r = match &self.static_filter {
Some(filter) => {
match value {
Expand Down Expand Up @@ -373,7 +374,8 @@ impl PhysicalExpr for InListExpr {

// Helper: compare value against a single list expression
let compare_one = |expr: &Arc<dyn PhysicalExpr>| -> Result<BooleanArray> {
match expr.evaluate(batch)? {
let list_value = expr.evaluate(batch)?.normalize(); // 0.0 == -0.0

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here is the other call

match list_value {
ColumnarValue::Array(array) => {
if lhs_supports_arrow_eq
&& supports_arrow_eq(array.data_type())
Expand Down Expand Up @@ -1137,6 +1139,78 @@ mod tests {
Ok(())
}

#[test]
fn in_list_float_signed_zero() -> Result<()> {
// `IN` must agree with `=`: `-0.0` and `+0.0` are equal, whichever side
// of the comparison they appear on and whichever filter is used.
let schema = Schema::new(vec![
Field::new("a", DataType::Float64, true),
Field::new("b", DataType::Float64, true),
]);
let a = Float64Array::from(vec![Some(0.0), Some(-0.0), Some(1.0), None]);
let b = Float64Array::from(vec![Some(-0.0), Some(0.0), Some(-0.0), Some(0.0)]);
let col_a = col("a", &schema)?;
let batch = RecordBatch::try_new(
Arc::new(schema.clone()),
vec![Arc::new(a), Arc::new(b)],
)?;
let expected = vec![Some(true), Some(true), Some(false), None];
let expected_negated = vec![Some(false), Some(false), Some(true), None];

// Short literal lists use the branchless filter
let list = || vec![lit(-0.0f64), lit(2.0f64)];
in_list!(
batch,
list(),
&false,
expected.clone(),
Arc::clone(&col_a),
&schema
);
in_list!(
batch,
list(),
&true,
expected_negated,
Arc::clone(&col_a),
&schema
);

// Longer literal lists use the hash set filter
let list: Vec<Arc<dyn PhysicalExpr>> = std::iter::once(lit(0.0f64))
.chain((2..22).map(|i| lit(i as f64)))
.collect();
in_list!(
batch,
list,
&false,
expected.clone(),
Arc::clone(&col_a),
&schema
);

// A column in the list is compared at runtime
let expr = make_in_list_with_columns(
Arc::clone(&col_a),
vec![col("b", &schema)?],
false,
);
let result = expr.evaluate(&batch)?.into_array(batch.num_rows())?;
assert_eq!(
as_boolean_array(&result),
&BooleanArray::from(expected.clone())
);

// Lists built directly from an array are normalized as well
let haystack: ArrayRef = Arc::new(Float64Array::from(vec![-0.0]));
let expr =
InListExpr::try_new_from_array(Arc::clone(&col_a), haystack, false, &schema)?;
let result = expr.evaluate(&batch)?.into_array(batch.num_rows())?;
assert_eq!(as_boolean_array(&result), &BooleanArray::from(expected));

Ok(())
}

#[test]
fn in_list_bool() -> Result<()> {
let schema = Schema::new(vec![Field::new("a", DataType::Boolean, true)]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
//! different NaN values. [`BranchlessFilterType`] defines these safe,
//! same-sized mappings and checks their sizes at compile time.
//!
//! It is important therefore for [`InListExpr`] to normalize `-0.0` to `+0.0` in
//! both the list and the input before they reach this filter, so signed zeros
//! compare equal, as they do for `=` (see [`datafusion_common::float_semantics`]).
//!
//! [`InListExpr`]: crate::expressions::InListExpr
//!
//! The fast path is limited to short lists:
//!
//! - 16 values for 1-byte types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,11 @@ where

/// Wrapper for f32 that implements Hash and Eq using bit comparison.
/// This treats NaN values as equal to each other when they have the same bit pattern.
///
/// Note: [`InListExpr`] must normalize the values first, see
/// [`datafusion_common::float_semantics`].
///
/// [`InListExpr`]: crate::expressions::InListExpr
#[derive(Clone, Copy)]
struct OrderedFloat32(f32);

Expand All @@ -346,6 +351,11 @@ impl From<f32> for OrderedFloat32 {

/// Wrapper for f64 that implements Hash and Eq using bit comparison.
/// This treats NaN values as equal to each other when they have the same bit pattern.
///
/// Note: [`InListExpr`] must normalize the values first, see
/// [`datafusion_common::float_semantics`].
///
/// [`InListExpr`]: crate::expressions::InListExpr
#[derive(Clone, Copy)]
struct OrderedFloat64(f64);

Expand Down
2 changes: 2 additions & 0 deletions datafusion/physical-expr/src/expressions/in_list/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use arrow::array::{ArrayRef, AsArray};
use arrow::compute::take;
use arrow::datatypes::DataType;
use datafusion_common::Result;
use datafusion_common::float_semantics::normalize_floats_owned;

use super::array_static_filter::ArrayStaticFilter;
use super::byte_view_filter::instantiate_byte_view_filter;
Expand All @@ -21,6 +22,7 @@ pub(super) fn instantiate_static_filter(
needle_data_type: &DataType,
) -> Result<StaticFilterRef> {
let in_array = flatten_dictionary_haystack(in_array)?;
let in_array = normalize_floats_owned(in_array);

let filter = if view_types_match(needle_data_type, in_array.data_type())
&& let Some(filter) = instantiate_byte_view_filter(&in_array)?
Expand Down
46 changes: 46 additions & 0 deletions datafusion/sqllogictest/test_files/in_list.slt
Original file line number Diff line number Diff line change
Expand Up @@ -1147,3 +1147,49 @@ true

statement ok
DROP TABLE in_list_nulls;

# IN must agree with = on signed zeros: -0.0 and +0.0 are equal
# https://github.com/apache/datafusion/issues/27121
statement ok
CREATE TABLE signed_zeros AS
SELECT id, x, y, arrow_cast(x, 'Float16') AS h
FROM (VALUES
(1, -0.0, CAST(0.0 AS FLOAT)),
(2, 0.0, CAST(-0.0 AS FLOAT)),
(3, 5.0, CAST(5.0 AS FLOAT))
) AS t(id, x, y);

query IBBBBBBBBBBBBBBB
SELECT
id,
x = 0.0,
-- constant folded with a scalar input
-0.0 IN (0.0, 1.0),
-- dictionary encoded input
arrow_cast(x, 'Dictionary(Int32, Float64)') IN (0.0, 5.0),
-- short literal list: branchless filter
x IN (0.0, 5.0),
x NOT IN (0.0, 5.0),
x IN (-0.0, 7.0),
-- non-column expression and four literals keep the membership filter
-x IN (0.0, -5.0),
x IN (0.0, 1.0, 2.0, 3.0),
x NOT IN (0.0, 1.0, 2.0, 3.0),
-- seventeen literals: hash set filter
x IN (0.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0),
-- column in the list: compared at runtime
x IN (y, 7.0, 8.0, 9.0),
x NOT IN (y, 7.0, 8.0, 9.0),
y IN (CAST(0 AS FLOAT), CAST(9 AS FLOAT)),
y NOT IN (CAST(0 AS FLOAT), CAST(9 AS FLOAT)),
-- nine Float16 literals: bitmap filter
h IN (arrow_cast(0, 'Float16'), arrow_cast(1, 'Float16'), arrow_cast(2, 'Float16'), arrow_cast(3, 'Float16'), arrow_cast(4, 'Float16'), arrow_cast(6, 'Float16'), arrow_cast(7, 'Float16'), arrow_cast(8, 'Float16'), arrow_cast(9, 'Float16'))
FROM signed_zeros
ORDER BY id;
----
1 true true true true false true true true false true true false true false true
2 true true true true false true true true false true true false true false true
3 false true true true false false true false true false true false false true false

statement ok
DROP TABLE signed_zeros;
Loading