-
Notifications
You must be signed in to change notification settings - Fork 2.4k
feat: support floating-point ASOF equality keys #24375
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
Open
Xuanwo
wants to merge
2
commits into
apache:main
Choose a base branch
from
Xuanwo:xuanwo/asof-float-equality
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
255 changes: 255 additions & 0 deletions
255
datafusion/physical-expr/src/expressions/normalize_float_zero.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,255 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| //! Floating-point signed-zero normalization expression. | ||
|
|
||
| use std::hash::Hash; | ||
| use std::sync::Arc; | ||
|
|
||
| use arrow::datatypes::{DataType, FieldRef, Schema}; | ||
| use arrow::record_batch::RecordBatch; | ||
| use datafusion_common::Result; | ||
| use datafusion_common::utils::{normalize_float_zero, normalize_float_zero_scalar}; | ||
| use datafusion_expr::ColumnarValue; | ||
| use datafusion_expr::interval_arithmetic::Interval; | ||
| use datafusion_expr::sort_properties::ExprProperties; | ||
|
|
||
| use crate::PhysicalExpr; | ||
|
|
||
| /// Replaces floating-point `-0.0` values with `+0.0`. | ||
| /// | ||
| /// Other values and data types are returned unchanged. This expression is | ||
| /// order-preserving but not strictly order-preserving because it collapses the | ||
| /// two signed-zero representations. | ||
| #[derive(Debug, Eq)] | ||
| pub struct NormalizeFloatZeroExpr { | ||
| arg: Arc<dyn PhysicalExpr>, | ||
| } | ||
|
|
||
| impl PartialEq for NormalizeFloatZeroExpr { | ||
| fn eq(&self, other: &Self) -> bool { | ||
| self.arg.eq(&other.arg) | ||
| } | ||
| } | ||
|
|
||
| impl Hash for NormalizeFloatZeroExpr { | ||
| fn hash<H: std::hash::Hasher>(&self, state: &mut H) { | ||
| self.arg.hash(state); | ||
| } | ||
| } | ||
|
|
||
| impl NormalizeFloatZeroExpr { | ||
| /// Creates a signed-zero normalization expression. | ||
| pub fn new(arg: Arc<dyn PhysicalExpr>) -> Self { | ||
| Self { arg } | ||
| } | ||
|
|
||
| /// Returns the input expression. | ||
| pub fn arg(&self) -> &Arc<dyn PhysicalExpr> { | ||
| &self.arg | ||
| } | ||
| } | ||
|
|
||
| impl std::fmt::Display for NormalizeFloatZeroExpr { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| write!(f, "normalize_float_zero({})", self.arg) | ||
| } | ||
| } | ||
|
|
||
| impl PhysicalExpr for NormalizeFloatZeroExpr { | ||
| fn data_type(&self, input_schema: &Schema) -> Result<DataType> { | ||
| self.arg.data_type(input_schema) | ||
| } | ||
|
|
||
| fn nullable(&self, input_schema: &Schema) -> Result<bool> { | ||
| self.arg.nullable(input_schema) | ||
| } | ||
|
|
||
| fn evaluate(&self, batch: &RecordBatch) -> Result<ColumnarValue> { | ||
| Ok(match self.arg.evaluate(batch)? { | ||
| ColumnarValue::Array(array) => { | ||
| ColumnarValue::Array(normalize_float_zero(&array)) | ||
| } | ||
| ColumnarValue::Scalar(scalar) => { | ||
| ColumnarValue::Scalar(normalize_float_zero_scalar(scalar)) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| fn return_field(&self, input_schema: &Schema) -> Result<FieldRef> { | ||
| self.arg.return_field(input_schema) | ||
| } | ||
|
|
||
| fn children(&self) -> Vec<&Arc<dyn PhysicalExpr>> { | ||
| vec![&self.arg] | ||
| } | ||
|
|
||
| fn with_new_children( | ||
| self: Arc<Self>, | ||
| children: Vec<Arc<dyn PhysicalExpr>>, | ||
| ) -> Result<Arc<dyn PhysicalExpr>> { | ||
| Ok(Arc::new(Self::new(Arc::clone(&children[0])))) | ||
| } | ||
|
|
||
| fn evaluate_bounds(&self, children: &[&Interval]) -> Result<Interval> { | ||
| Interval::try_new( | ||
| normalize_float_zero_scalar(children[0].lower().clone()), | ||
| normalize_float_zero_scalar(children[0].upper().clone()), | ||
| ) | ||
| } | ||
|
|
||
| fn get_properties(&self, children: &[ExprProperties]) -> Result<ExprProperties> { | ||
| let range = self.evaluate_bounds(&[&children[0].range])?; | ||
| Ok(children[0] | ||
| .clone() | ||
| .with_range(range) | ||
| .with_strictly_order_preserving(false)) | ||
| } | ||
|
|
||
| fn fmt_sql(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| write!(f, "normalize_float_zero(")?; | ||
| self.arg.fmt_sql(f)?; | ||
| write!(f, ")") | ||
| } | ||
|
|
||
| #[cfg(feature = "proto")] | ||
| fn try_to_proto( | ||
| &self, | ||
| ctx: &datafusion_physical_expr_common::physical_expr::proto_encode::PhysicalExprEncodeCtx<'_>, | ||
| ) -> Result<Option<datafusion_proto_models::protobuf::PhysicalExprNode>> { | ||
| use datafusion_proto_models::protobuf; | ||
|
|
||
| Ok(Some(protobuf::PhysicalExprNode { | ||
| expr_id: None, | ||
| expr_type: Some(protobuf::physical_expr_node::ExprType::NormalizeFloatZero( | ||
| Box::new(protobuf::PhysicalNormalizeFloatZeroNode { | ||
| expr: Some(Box::new(ctx.encode_child(&self.arg)?)), | ||
| }), | ||
| )), | ||
| })) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "proto")] | ||
| impl NormalizeFloatZeroExpr { | ||
| /// Reconstructs a [`NormalizeFloatZeroExpr`] from protobuf. | ||
| pub fn try_from_proto( | ||
| node: &datafusion_proto_models::protobuf::PhysicalExprNode, | ||
| ctx: &datafusion_physical_expr_common::physical_expr::proto_decode::PhysicalExprDecodeCtx<'_>, | ||
| ) -> Result<Arc<dyn PhysicalExpr>> { | ||
| use datafusion_physical_expr_common::expect_expr_variant; | ||
| use datafusion_proto_models::protobuf; | ||
|
|
||
| let node = expect_expr_variant!( | ||
| node, | ||
| protobuf::physical_expr_node::ExprType::NormalizeFloatZero, | ||
| "NormalizeFloatZero", | ||
| ); | ||
| let arg = ctx.decode_required_expression( | ||
| node.expr.as_deref(), | ||
| "NormalizeFloatZeroExpr", | ||
| "expr", | ||
| )?; | ||
| Ok(Arc::new(Self::new(arg))) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| use arrow::array::{ArrayRef, AsArray, Float64Array}; | ||
| use datafusion_common::ScalarValue; | ||
| use datafusion_expr::sort_properties::SortProperties; | ||
| use half::f16; | ||
|
|
||
| use crate::expressions::{Column, Literal}; | ||
|
|
||
| #[test] | ||
| fn normalizes_array_and_scalar_signed_zero() -> Result<()> { | ||
| let batch = RecordBatch::try_from_iter(vec![( | ||
| "a", | ||
| Arc::new(Float64Array::from(vec![-0.0, 0.0, 1.0])) as ArrayRef, | ||
| )])?; | ||
| let expr = NormalizeFloatZeroExpr::new(Arc::new(Column::new("a", 0))); | ||
| let ColumnarValue::Array(array) = expr.evaluate(&batch)? else { | ||
| panic!("column evaluation must return an array"); | ||
| }; | ||
| let array = array.as_primitive::<arrow::datatypes::Float64Type>(); | ||
| assert_eq!(array.value(0).to_bits(), 0.0_f64.to_bits()); | ||
| assert_eq!(array.value(1).to_bits(), 0.0_f64.to_bits()); | ||
| assert_eq!(array.value(2), 1.0); | ||
|
|
||
| let expr = NormalizeFloatZeroExpr::new(Arc::new(Literal::new( | ||
| ScalarValue::Float64(Some(-0.0)), | ||
| ))); | ||
| let ColumnarValue::Scalar(ScalarValue::Float64(Some(value))) = | ||
| expr.evaluate(&RecordBatch::new_empty(Arc::new(Schema::empty())))? | ||
| else { | ||
| panic!("literal evaluation must return a Float64 scalar"); | ||
| }; | ||
| assert_eq!(value.to_bits(), 0.0_f64.to_bits()); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn normalizes_signed_zero_bounds_and_properties() -> Result<()> { | ||
| let batch = RecordBatch::new_empty(Arc::new(Schema::empty())); | ||
| let cases = [ | ||
| ( | ||
| ScalarValue::Float16(Some(f16::NEG_ZERO)), | ||
| ScalarValue::Float16(Some(f16::ZERO)), | ||
| ), | ||
| ( | ||
| ScalarValue::Float32(Some(-0.0)), | ||
| ScalarValue::Float32(Some(0.0)), | ||
| ), | ||
| ( | ||
| ScalarValue::Float64(Some(-0.0)), | ||
| ScalarValue::Float64(Some(0.0)), | ||
| ), | ||
| ]; | ||
|
|
||
| for (negative_zero, positive_zero) in cases { | ||
| let child_range = | ||
| Interval::try_new(negative_zero.clone(), negative_zero.clone())?; | ||
| let expected_range = | ||
| Interval::try_new(positive_zero.clone(), positive_zero.clone())?; | ||
| let child_properties = ExprProperties::new_unknown() | ||
| .with_order(SortProperties::Singleton) | ||
| .with_range(child_range.clone()) | ||
| .with_preserves_lex_ordering(true) | ||
| .with_strictly_order_preserving(true); | ||
| let expr = NormalizeFloatZeroExpr::new(Arc::new(Literal::new(negative_zero))); | ||
|
|
||
| let ColumnarValue::Scalar(value) = expr.evaluate(&batch)? else { | ||
| panic!("literal evaluation must return a scalar"); | ||
| }; | ||
| let bounds = expr.evaluate_bounds(&[&child_range])?; | ||
| assert_eq!(value, positive_zero); | ||
| assert_eq!(bounds, expected_range); | ||
| assert!(bounds.contains_value(&value)?); | ||
|
|
||
| let properties = expr.get_properties(&[child_properties])?; | ||
| assert_eq!(properties.sort_properties, SortProperties::Singleton); | ||
| assert_eq!(properties.range, expected_range); | ||
| assert!(properties.preserves_lex_ordering); | ||
| assert!(!properties.strictly_order_preserving); | ||
| } | ||
| Ok(()) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I still have a question about why we introduce another PhysicalExpr to handle this type issue instead of just calling normalize_float_zero.
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.
Good question.
normalize_float_zeroworks on an already evaluated array, but here normalization must be part of the required ordering.For example, raw
[key, ts]may be(-0, 10), (+0, 1). Normalizing only during comparison makes them one equality group but leavestsas[10, 1], breaking the forward scan. We need the input sorted by[normalize_float_zero(key), ts].The new
PhysicalExpris the plan-level wrapper around the existing helper, allowingSortExec, ordering enforcement, and protobuf to represent this without changing float sorting globally.Let me know if this addressed your concerns. I'm open to better ideas!
Uh oh!
There was an error while loading. Please reload this page.
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.
I'll take a look at this. My read is that it's handling the edge cases for asof join.
One thing I'm worried about: we'd end up with
NormalizeFloatZeroExprandnormalize_float_zerospread across a lot of call sites, which seems likely to cause confusion down the line.Would it make sense to land a minimal PR first — just enough to get a simple asof join working — and then iterate on the other cases after that? What do you think?
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.
Yep, only ASOF join use this part of logic. I'm open to leave it as a follow-up. I'll adjust the stack and get it a out so that we can work on ASOF join first.