andygrove commented on code in PR #6447:
URL: https://github.com/apache/datafusion-comet/pull/6447#discussion_r4147895534
##########
native/spark-expr/src/float_semantics/normalize.rs:
##########
@@ -87,15 +88,17 @@ impl PhysicalExpr for NormalizeNaNAndZero {
}
fn evaluate(&self, batch: &RecordBatch) -> Result<ColumnarValue> {
- let cv = self.child.evaluate(batch)?;
- let array = cv.into_array(batch.num_rows())?;
-
match &self.data_type {
- DataType::Float32 | DataType::Float64 => {
- Ok(ColumnarValue::Array(normalize_floats(&array)))
- }
+ DataType::Float32 | DataType::Float64 => {}
dt => panic!("Unexpected data type {dt:?}"),
}
+ // A scalar stays a scalar, so a constant operand is not expanded into
a column.
+ match self.child.evaluate(batch)? {
+ ColumnarValue::Array(array) =>
Ok(ColumnarValue::Array(normalize_floats(&array))),
+ ColumnarValue::Scalar(value) => {
Review Comment:
I measured it, and the check costs about as much as the copy it saves. Over
8192 doubles with no NaN or `-0.0`, the copy takes 1.71 µs, a branch-free check
in blocks of 64 takes 1.66 µs, and the fastest form I found, on the raw bits,
1.28 µs. An `any()` that stops at the first match does not vectorize and took
4.4 µs. So the best case recovers about 0.4 µs per operand, roughly 8% of a
column-column comparison, and an array that does need normalizing pays for the
check as well as the copy. Most of the gap is the per-value NaN and `-0.0` test
itself, so I'd rather close it with a kernel that compares in Spark's order
directly, without normalized copies, in a follow-up. Does that work for you?
##########
spark/src/test/resources/sql-tests/expressions/conditional/float_comparisons.sql:
##########
@@ -0,0 +1,140 @@
+-- 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.
+
+-- Comparisons follow Spark's SQL ordering for floats
(SQLOrderingUtil.compareDoubles): -0.0
+-- equals 0.0, every NaN equals every other NaN, and NaN sorts above every
other value. This
+-- must hold in every operator that evaluates a comparison, not only in
Project and Filter.
+--
+-- `-d` flips the sign bit, so `-d` of the NaN row is a NaN with the sign bit
set on every
+-- platform. That is the NaN that arithmetic produces on x86-64, and Arrow's
total order sorts
+-- it below -Infinity.
+
+statement
+CREATE TABLE float_cmp(id INT, d DOUBLE, f FLOAT) USING parquet
+
+statement
+INSERT INTO float_cmp VALUES
+ (1, 0.0D, float('0.0')),
+ (2, double('-0.0'), float('-0.0')),
+ (3, double('NaN'), float('NaN')),
+ (4, 1.0D, float('1.0')),
+ (5, -1.0D, float('-1.0')),
+ (6, double('Infinity'), float('Infinity')),
+ (7, NULL, NULL)
+
+-- Project
+query
+SELECT id, -d = d, -d <=> d, -d < d, -d <= d, -d > d, -d >= d, -d != d FROM
float_cmp
+
+query
+SELECT id, -f = f, -f <=> f, -f < f, -f <= f, -f > f, -f >= f, -f != f FROM
float_cmp
+
+-- Comparisons against a constant on either side. `-0.0D` and `-0.0F` are
literals with the sign
+-- bit set, while `double('NaN')` is a cast, because the suite turns off
constant folding.
+query
+SELECT id, -d > 0.0D, -d >= -0.0D, -0.0D = d, -d = double('NaN'),
double('NaN') <= -d,
+ -d < double('-0.0'), -f > 0.0F, -0.0F <=> f, -f = float('NaN'),
float('-0.0') >= -f
+FROM float_cmp
+
+-- Filter
+query
+SELECT id FROM float_cmp WHERE -d > 0.0D
Review Comment:
Added `WHERE d >= 0.0D`, `WHERE f <= -0.0F`, `WHERE d = double('NaN')` and
`WHERE d > double('Infinity')`, and the whole file now runs with row-level
pushdown both off and on, which reproduces the `-d > 0.0D` case from the
`planner.rs` comment. The suite turns off constant folding, so `double('NaN')`
stays a cast and only the first two have the `column op literal` shape.
##########
native/core/src/execution/planner.rs:
##########
@@ -1007,6 +1017,23 @@ impl PhysicalPlanner {
}
}
+ /// Create a data filter that a scan pushes into the Parquet reader. The
filter prunes row
+ /// groups and pages, and rows too when row-level pushdown is enabled.
Pruning only recognizes
+ /// a column compared with a literal, so the filter's comparisons leave
float operands as they
+ /// are rather than normalizing them. Spark's Filter above the scan
evaluates the filter again
+ /// with Spark's semantics.
+ fn create_data_filter(
+ &self,
+ spark_expr: &Expr,
+ input_schema: SchemaRef,
+ ) -> Result<Arc<dyn PhysicalExpr>, ExecutionError> {
+ let planner = Self {
+ float_operands: FloatOperands::Raw,
Review Comment:
Confirmed: with `spark.comet.parquet.rowFilterPushdown.enabled=true`, `WHERE
-d > 0.0D` returned only row 5, where Spark returns rows 3 and 5. In 12e95e697,
`FloatOperands::Raw` leaves only a float column compared with a literal as it
is, and still normalizes the literal. Every other comparison in a data filter
is normalized, so the NaN row survives the reader, and the pruning test still
prunes. The remaining raw shape compares stored values, and Spark writes floats
through `doubleToLongBits` and `floatToIntBits`, so a Spark-written file only
holds canonical NaNs. The compatibility guide now says that a noncanonical NaN
written by another engine can still be filtered differently with row-level
pushdown.
##########
native/spark-expr/src/float_semantics/normalize.rs:
##########
@@ -240,6 +273,16 @@ pub fn has_float_leaf(dt: &DataType) -> bool {
}
}
+fn is_nested_with_float_leaf(dt: &DataType) -> bool {
Review Comment:
Done. `needs_spark_equality` is gone, and `nested_comparison.rs` uses
`is_nested_with_float_leaf` from `float_semantics`, now `pub(crate)` and
written as `dt.is_nested() && has_float_leaf(dt)`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]