From 327f65ea04673078425160d2d11d19a0911b9bca Mon Sep 17 00:00:00 2001 From: Noah Date: Thu, 3 Dec 2026 21:31:19 -0500 Subject: [PATCH] fix: keep NaN and -0.0 rows when pushing down float filters DataFusion treats -0.0 as equal to 0.0, and compares NaN by its bits: NaN is greater than every number and -NaN less. Iceberg's file skipping orders -0.0 before 0.0 and matches NaN only with IS NAN, which can't tell NaNs apart. So float comparisons and IN lists pushed down unchanged skipped files holding rows the filter keeps. Convert them to compare the values other than NaN, with a zero literal matching both zeros, then add or exclude the NaN rows. The conversion is exact when every NaN row matches or none does, and otherwise keeps them all and is only implied, so NOT isn't pushed through it. float_predicate_pushdown.slt checks DataFusion's current NaN rules, which apache/datafusion#26091 may change. Closes #101. --- AGENTS.md | 4 +- crates/datafusion/src/read/predicate.rs | 258 +++++++++++++++++- crates/datafusion/tests/filter_pushdown.rs | 2 +- crates/datafusion/tests/scan_metrics.rs | 16 +- .../df_float_predicate_pushdown.toml | 23 ++ .../slts/df_test/float_predicate_pushdown.slt | 251 +++++++++++++++++ 6 files changed, 534 insertions(+), 20 deletions(-) create mode 100644 crates/sqllogictest/testdata/schedules/df_float_predicate_pushdown.toml create mode 100644 crates/sqllogictest/testdata/slts/df_test/float_predicate_pushdown.slt diff --git a/AGENTS.md b/AGENTS.md index ea33c416..856af185 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -91,7 +91,7 @@ The playground config holds `[[catalogs]]` entries with `name`, `type` (`rest` o `FileScanTaskProviderBuilder` converts the projection to column names and the filters to a single Iceberg `Predicate` (`read/predicate.rs`). Lazy discovery advertises the session's target partition count and shares one discovery stream per execution. In a parallel lazy scan, a producer sends planned file tasks to one unbounded Flume queue. Each partition takes a receiver on first poll; a late reader can claim remaining tasks without restarting discovery. A single-partition scan reads directly without a queue. Use DataFusion's `reset_plan_states` to run the same parallel scan again; partition-open order is not an execution boundary. `IcebergDataSource` reads tasks with a shared `ArrowReader` and applies fetch per partition. Eager planning still collects tasks into round-robin groups. -The base table provider reports every filter as `Inexact`, so DataFusion re-applies the original filters after the scan. The pushed-down predicate therefore only has to be *implied by* the original filter. It may match extra rows but must never drop a row that matches. Unconvertible parts are dropped: an `AND` keeps whichever side converted, while an `OR` needs both sides. Preserve this soundness rule when extending `predicate.rs`; for example, it is why casts and some NaN arithmetic are deliberately not pushed down. Removing a cast is left to DataFusion's simplifier (`unwrap_cast_in_comparison`), which rewrites a comparison only when the result doesn't change. +The base table provider reports every filter as `Inexact`, so DataFusion re-applies the original filters after the scan. The pushed-down predicate therefore only has to be *implied by* the original filter. It may match extra rows but must never drop a row that matches. Unconvertible parts are dropped: an `AND` keeps whichever side converted, while an `OR` needs both sides. Preserve this soundness rule when extending `predicate.rs`; for example, it is why casts and some NaN arithmetic are deliberately not pushed down. Comparisons and `IN` lists with float literals go through `float_comparison` and `float_in_list`, because DataFusion and Iceberg compare NaN and -0.0 differently. Removing a cast is left to DataFusion's simplifier (`unwrap_cast_in_comparison`), which rewrites a comparison only when the result doesn't change. ### Write path (`IcebergTableProvider::insert_into`) @@ -129,7 +129,7 @@ Library code returns `datafusion::error::Result`. Convert iceberg errors with `t ## Dependencies - **iceberg-rust:** `iceberg` and `iceberg-catalog-rest` are git dependencies on `apache/iceberg-rust`, pinned to the same `rev` in the root `Cargo.toml`. The writers, partition splitting, table properties, encryption, and metadata tables all come from there. A missing API usually has to land upstream first; then bump both `rev`s together. -- **Arrow/Parquet version:** DataFusion, iceberg-rust, and `crates/datafusion`'s direct `parquet` dependency must all resolve to one Arrow/Parquet version (currently 60.x). For that reason Dependabot skips semver-major `datafusion*`/`parquet` bumps. Do those upgrades manually, together with a compatible iceberg-rust `rev`. +- **Arrow/Parquet version:** DataFusion, iceberg-rust, and `crates/datafusion`'s direct `parquet` dependency must all resolve to one Arrow/Parquet version (currently 60.x). For that reason Dependabot skips semver-major `datafusion*`/`parquet` bumps. Do those upgrades manually, together with a compatible iceberg-rust `rev`. Float filter pushdown copies DataFusion's NaN rules, which apache/datafusion#26091 may change; `float_predicate_pushdown.slt` checks them, and the TODO on `float_comparison` says what to update. ## Conventions diff --git a/crates/datafusion/src/read/predicate.rs b/crates/datafusion/src/read/predicate.rs index 6c26b433..f60b5767 100644 --- a/crates/datafusion/src/read/predicate.rs +++ b/crates/datafusion/src/read/predicate.rs @@ -15,6 +15,8 @@ // specific language governing permissions and limitations // under the License. +use std::cmp::Ordering; + use datafusion::arrow::datatypes::DataType; use datafusion::logical_expr::expr::{InList, ScalarFunction}; use datafusion::logical_expr::{BinaryExpr, Expr, Like, Operator}; @@ -27,7 +29,8 @@ enum Converted { /// Matches exactly the rows the filter matches. Exact(Predicate), /// Implied by the filter but not equivalent to it, because an unconvertible - /// AND operand was dropped. Sound to push down, unsound to negate. + /// AND operand was dropped, or because the filter matches some NaN rows but + /// not others. Sound to push down, unsound to negate. Implied(Predicate), } @@ -73,7 +76,7 @@ fn predicate(expr: &Expr) -> Option { op: Operator::Or, right, }) => return Some(or(predicate(left)?, predicate(right)?)), - Expr::BinaryExpr(binary) => comparison(binary)?, + Expr::BinaryExpr(binary) => return comparison(binary), Expr::Not(inner) => match column(inner) { // NOT of a bare boolean column: `col = false` Some(column) => column.equal_to(Datum::bool(false)), @@ -89,10 +92,10 @@ fn predicate(expr: &Expr) -> Option { }) => { let column = column(expr)?; let datums = list.iter().map(literal).collect::>>()?; - if *negated { - column.is_not_in(datums) - } else { - column.is_in(datums) + match float_values(&datums) { + Some(values) => return Some(float_in_list(column, &values, *negated)), + None if *negated => column.is_not_in(datums), + None => column.is_in(datums), } } Expr::IsNull(expr) => column(expr)?.is_null(), @@ -164,7 +167,7 @@ fn or(left: Converted, right: Converted) -> Converted { } /// Converts `column op literal`, or `literal op column` with `op` swapped. -fn comparison(binary: &BinaryExpr) -> Option { +fn comparison(binary: &BinaryExpr) -> Option { let (column, op, datum) = match (column(&binary.left), literal(&binary.right)) { (Some(column), Some(datum)) => (column, binary.op, datum), _ => ( @@ -182,7 +185,156 @@ fn comparison(binary: &BinaryExpr) -> Option { Operator::GtEq => PredicateOperator::GreaterThanOrEq, _ => return None, }; - Some(Predicate::Binary(BinaryExpression::new(op, column, datum))) + if let PrimitiveLiteral::Double(value) = datum.literal() { + return Some(float_comparison(column, op, value.0)); + } + let predicate = Predicate::Binary(BinaryExpression::new(op, column, datum)); + Some(Converted::Exact(predicate)) +} + +/// Converts `column op value` for a float `value`, so that it keeps the rows +/// DataFusion's comparison matches. +/// +/// DataFusion treats -0.0 as equal to 0.0 (see +/// `datafusion::common::float_semantics`), but compares NaN by its bits: a NaN +/// with the sign bit set is less than every number and one without is greater, +/// and two NaNs are equal only if their bits are. Iceberg skips files by its own +/// rules: -0.0 is less than 0.0, and no comparison matches NaN, only `IS NAN` and +/// `IS NOT NAN` do, and those can't tell NaNs apart. So this compares the values +/// other than NaN, with a zero standing for both zeros, and then adds or excludes +/// the NaN rows; see [`with_nan`]. +/// +/// TODO: When apache/datafusion#26091 makes every NaN equal and greater than every +/// number, a NaN row compares with a number as `Greater` only, and with a NaN +/// literal as `Equal` only, and every number is less than a NaN literal. +/// `float_predicate_pushdown.slt` checks the current rules and fails when they +/// change. +fn float_comparison(column: Reference, op: PredicateOperator, value: f64) -> Converted { + use Ordering::{Equal, Greater, Less}; + if value.is_nan() { + // Every number is less than a NaN literal, or greater if the literal's sign + // bit is set. + let number = if value.is_sign_negative() { + Greater + } else { + Less + }; + let numbers = if holds(op, number) { + Predicate::AlwaysTrue + } else { + Predicate::AlwaysFalse + }; + // A NaN row may be less than, equal to or greater than it, by its bits. + return with_nan( + column, + numbers, + &[Less, Equal, Greater].map(|o| holds(op, o)), + ); + } + let numbers = number_comparison(column.clone(), op, value); + // A NaN row is less than any number if its sign bit is set, and greater if not. + with_nan(column, numbers, &[Less, Greater].map(|o| holds(op, o))) +} + +/// Compares `column` with a number other than NaN. +fn number_comparison(column: Reference, op: PredicateOperator, value: f64) -> Predicate { + if value != 0.0 { + return Predicate::Binary(BinaryExpression::new( + op, + column, + Datum::double(value), + )); + } + // A zero stands for both -0.0 and 0.0, which Iceberg orders -0.0 < 0.0. + match op { + PredicateOperator::Eq => zeros(column), + PredicateOperator::NotEq => column + .clone() + .less_than(Datum::double(-0.0)) + .or(column.greater_than(Datum::double(0.0))), + PredicateOperator::LessThan => column.less_than(Datum::double(-0.0)), + PredicateOperator::LessThanOrEq => { + column.less_than_or_equal_to(Datum::double(0.0)) + } + PredicateOperator::GreaterThan => column.greater_than(Datum::double(0.0)), + PredicateOperator::GreaterThanOrEq => { + column.greater_than_or_equal_to(Datum::double(-0.0)) + } + _ => unreachable!("not a comparison: {op}"), + } +} + +/// Converts `column IN (values)`, or `NOT IN` if `negated`, for float `values`: +/// a row matches if it equals any of them, as compared by [`float_comparison`]. +fn float_in_list(column: Reference, values: &[f64], negated: bool) -> Converted { + // Zeros are matched by `zeros`, as in comparisons: Iceberg compares set members + // by its own order, so a set holding 0.0 alone would not match -0.0. + let numbers: Vec = values + .iter() + .filter(|value| !value.is_nan() && **value != 0.0) + .map(|value| Datum::double(*value)) + .collect(); + let mut matches = if numbers.is_empty() { + Predicate::AlwaysFalse + } else { + column.clone().is_in(numbers) + }; + if values.contains(&0.0) { + matches = matches.or(zeros(column.clone())); + } + // A NaN row never equals a number, and may equal a NaN in the list. + let nan_matches = [false, values.iter().any(|value| value.is_nan())]; + if negated { + with_nan(column, matches.negate(), &nan_matches.map(|m| !m)) + } else { + with_nan(column, matches, &nan_matches) + } +} + +/// Matches -0.0 and 0.0. +fn zeros(column: Reference) -> Predicate { + column + .clone() + .greater_than_or_equal_to(Datum::double(-0.0)) + .and(column.less_than_or_equal_to(Datum::double(0.0))) +} + +/// Adds the NaN rows to the rows `numbers` matches, or excludes them. +/// `nan_matches` holds whether the filter matches a NaN row, for each way a NaN +/// row can compare with the literal. If it matches in some ways but not others, +/// the NaN rows are kept, and the result is only implied by the filter. +fn with_nan(column: Reference, numbers: Predicate, nan_matches: &[bool]) -> Converted { + if nan_matches.iter().all(|m| *m) { + Converted::Exact(numbers.or(column.is_nan())) + } else if nan_matches.iter().any(|m| *m) { + Converted::Implied(numbers.or(column.is_nan())) + } else { + Converted::Exact(numbers.and(column.is_not_nan())) + } +} + +/// Whether `a op b` holds when `a.cmp(b)` is `ordering`. +fn holds(op: PredicateOperator, ordering: Ordering) -> bool { + match op { + PredicateOperator::Eq => ordering.is_eq(), + PredicateOperator::NotEq => ordering.is_ne(), + PredicateOperator::LessThan => ordering.is_lt(), + PredicateOperator::LessThanOrEq => ordering.is_le(), + PredicateOperator::GreaterThan => ordering.is_gt(), + PredicateOperator::GreaterThanOrEq => ordering.is_ge(), + _ => unreachable!("not a comparison: {op}"), + } +} + +/// The values of `datums` if all of them are floats. +fn float_values(datums: &[Datum]) -> Option> { + datums + .iter() + .map(|datum| match datum.literal() { + PrimitiveLiteral::Double(value) => Some(value.0), + _ => None, + }) + .collect() } fn column(expr: &Expr) -> Option { @@ -375,7 +527,8 @@ mod tests { use datafusion::arrow::datatypes::{DataType, Field, Schema, TimeUnit}; use datafusion::common::DFSchema; use datafusion::logical_expr::utils::split_conjunction; - use datafusion::prelude::{Expr, SessionContext}; + use datafusion::logical_expr::{Operator, binary_expr}; + use datafusion::prelude::{Expr, SessionContext, col, lit}; use iceberg::expr::{Predicate, Reference}; use iceberg::spec::Datum; use parquet::arrow::PARQUET_FIELD_ID_META_KEY; @@ -640,6 +793,93 @@ mod tests { assert_eq!(predicate, expected_predicate); } + #[test] + fn test_predicate_conversion_with_float_literals() { + let qux = || col("qux"); + // Every comparison with a number, a zero, NaN and NaN with the sign bit + // set. They are named here, since a NaN literal displays without its sign. + let mut filters = Vec::new(); + for (name, value) in [ + ("1.5", 1.5), + ("0", 0.0), + ("NaN", f64::NAN), + ("-NaN", -f64::NAN), + ] { + for op in [ + Operator::Eq, + Operator::NotEq, + Operator::Lt, + Operator::LtEq, + Operator::Gt, + Operator::GtEq, + ] { + let filter = binary_expr(qux(), op, lit(value)); + filters.push((format!("qux {op} {name}"), filter)); + } + } + filters.extend( + [ + // -0.0 is the same zero, a REAL literal the same number, and a + // literal on the left the same comparison. + qux().eq(lit(-0.0)), + qux().gt(lit(1.5f32)), + lit(1.5).lt(qux()), + qux().in_list(vec![lit(1.5), lit(0.0), lit(f64::NAN)], false), + qux().in_list(vec![lit(1.5), lit(2.5)], true), + qux().in_list(vec![lit(1.5), lit(f64::NAN)], true), + // `NOT` negates an exact conversion only. + !qux().eq(lit(1.5)), + !qux().lt(lit(1.5)), + ] + .map(|filter| (filter.to_string(), filter)), + ); + + let converted = filters + .into_iter() + .map(|(name, filter)| { + let predicate = convert_filters_to_predicate(&[filter]) + .map_or("not pushed".to_string(), |p| p.to_string()); + format!("{name} => {predicate}") + }) + .collect::>() + .join("\n"); + + insta::assert_snapshot!(converted, @" + qux = 1.5 => (qux = 1.5) AND (qux IS NOT NAN) + qux != 1.5 => (qux != 1.5) OR (qux IS NAN) + qux < 1.5 => (qux < 1.5) OR (qux IS NAN) + qux <= 1.5 => (qux <= 1.5) OR (qux IS NAN) + qux > 1.5 => (qux > 1.5) OR (qux IS NAN) + qux >= 1.5 => (qux >= 1.5) OR (qux IS NAN) + qux = 0 => ((qux >= -0) AND (qux <= 0)) AND (qux IS NOT NAN) + qux != 0 => ((qux < -0) OR (qux > 0)) OR (qux IS NAN) + qux < 0 => (qux < -0) OR (qux IS NAN) + qux <= 0 => (qux <= 0) OR (qux IS NAN) + qux > 0 => (qux > 0) OR (qux IS NAN) + qux >= 0 => (qux >= -0) OR (qux IS NAN) + qux = NaN => qux IS NAN + qux != NaN => TRUE + qux < NaN => TRUE + qux <= NaN => TRUE + qux > NaN => qux IS NAN + qux >= NaN => qux IS NAN + qux = -NaN => qux IS NAN + qux != -NaN => TRUE + qux < -NaN => qux IS NAN + qux <= -NaN => qux IS NAN + qux > -NaN => TRUE + qux >= -NaN => TRUE + qux = Float64(-0) => ((qux >= -0) AND (qux <= 0)) AND (qux IS NOT NAN) + qux > Float32(1.5) => (qux > 1.5) OR (qux IS NAN) + Float64(1.5) < qux => (qux > 1.5) OR (qux IS NAN) + qux IN ([Float64(1.5), Float64(0), Float64(NaN)]) => ((qux IN (1.5)) OR ((qux >= -0) AND (qux <= 0))) OR (qux IS NAN) + qux NOT IN ([Float64(1.5), Float64(2.5)]) => (qux NOT IN (1.5, 2.5)) OR (qux IS NAN) + qux NOT IN ([Float64(1.5), Float64(NaN)]) => (qux NOT IN (1.5)) OR (qux IS NAN) + NOT qux = Float64(1.5) => NOT ((qux = 1.5) AND (qux IS NOT NAN)) + NOT qux < Float64(1.5) => not pushed + "); + } + #[test] fn test_predicate_conversion_with_cast() { for sql in [ diff --git a/crates/datafusion/tests/filter_pushdown.rs b/crates/datafusion/tests/filter_pushdown.rs index 783a1a26..08a04f66 100644 --- a/crates/datafusion/tests/filter_pushdown.rs +++ b/crates/datafusion/tests/filter_pushdown.rs @@ -51,7 +51,7 @@ mod tests { CoalescePartitionsExec AggregateExec: mode=Partial, gby=[], aggr=[count(Int64(1))] FilterExec: pickup_date@2 = 2024-01-10 AND payment_type@1 IN (SET) ([1, 2]) AND trip_distance@0 >= 2, projection=[] - DataSourceExec: format=iceberg, projection=[trip_distance, payment_type, pickup_date], provider=lazy, predicate=((pickup_date = 2024-01-10) AND (payment_type IN (1, 2))) AND (trip_distance >= 2) + DataSourceExec: format=iceberg, projection=[trip_distance, payment_type, pickup_date], provider=lazy, predicate=((pickup_date = 2024-01-10) AND (payment_type IN (1, 2))) AND ((trip_distance >= 2) OR (trip_distance IS NAN)) "); insta::assert_snapshot!(batches, @r" +-------+ diff --git a/crates/datafusion/tests/scan_metrics.rs b/crates/datafusion/tests/scan_metrics.rs index c9b61622..19b23514 100644 --- a/crates/datafusion/tests/scan_metrics.rs +++ b/crates/datafusion/tests/scan_metrics.rs @@ -42,14 +42,14 @@ async fn lazy_scan_reports_bytes_even_when_no_rows_match() -> Result<()> { .await?; insta::assert_snapshot!(explain, @" - +-------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ - | plan_type | plan | - +-------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ - | Plan with Metrics | FilterExec: pickup_date@1 <= 2024-01-10 AND fare_amount@0 = 12.345, projection=[fare_amount@0], metrics=[output_rows=0, output_bytes=0.0 B, output_batches=0, selectivity=N/A (0/0)] | - | | CooperativeExec, metrics=[] | - | | DataSourceExec: format=iceberg, projection=[fare_amount, pickup_date], provider=lazy, predicate=(pickup_date <= 2024-01-10) AND (fare_amount = 12.345), metrics=[output_rows=0, output_bytes=0.0 B, output_batches=0, batches_split=0, bytes_scanned=2.1 MB] | - | | | - +-------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ + +-------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ + | plan_type | plan | + +-------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ + | Plan with Metrics | FilterExec: pickup_date@1 <= 2024-01-10 AND fare_amount@0 = 12.345, projection=[fare_amount@0], metrics=[output_rows=0, output_bytes=0.0 B, output_batches=0, selectivity=N/A (0/0)] | + | | CooperativeExec, metrics=[] | + | | DataSourceExec: format=iceberg, projection=[fare_amount, pickup_date], provider=lazy, predicate=(pickup_date <= 2024-01-10) AND ((fare_amount = 12.345) AND (fare_amount IS NOT NAN)), metrics=[output_rows=0, output_bytes=0.0 B, output_batches=0, batches_split=0, bytes_scanned=2.1 MB] | + | | | + +-------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ "); Ok(()) } diff --git a/crates/sqllogictest/testdata/schedules/df_float_predicate_pushdown.toml b/crates/sqllogictest/testdata/schedules/df_float_predicate_pushdown.toml new file mode 100644 index 00000000..5351c01c --- /dev/null +++ b/crates/sqllogictest/testdata/schedules/df_float_predicate_pushdown.toml @@ -0,0 +1,23 @@ +# 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. + +[engines] +df = { type = "datafusion" } + +[[steps]] +engine = "df" +slt = "df_test/float_predicate_pushdown.slt" diff --git a/crates/sqllogictest/testdata/slts/df_test/float_predicate_pushdown.slt b/crates/sqllogictest/testdata/slts/df_test/float_predicate_pushdown.slt new file mode 100644 index 00000000..3764653a --- /dev/null +++ b/crates/sqllogictest/testdata/slts/df_test/float_predicate_pushdown.slt @@ -0,0 +1,251 @@ +# 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. + +# Filters on float columns return the rows DataFusion returns for the same data +# in memory. DataFusion treats -0.0 as equal to 0.0, and compares NaN by its bits: +# NaN is greater than any number, and NaN with the sign bit set (-NaN) is less. +# Iceberg orders -0.0 before 0.0 and matches NaN only with IS NAN, so pushdown +# must not skip files that hold matching zeros or NaN. + +statement ok +CREATE TABLE default.default.test_float_table (id INT NOT NULL, x DOUBLE, y REAL) + +# One data file per INSERT. Rows 3 and 9 hold NaN in files whose bounds are +# 0.5 and 3.0. +query I +INSERT INTO default.default.test_float_table VALUES (1, CAST('NaN' AS DOUBLE), CAST('NaN' AS REAL)) +---- +1 + +query I +INSERT INTO default.default.test_float_table VALUES (2, 0.5, 0.5), (3, CAST('NaN' AS DOUBLE), CAST('NaN' AS REAL)) +---- +2 + +query I +INSERT INTO default.default.test_float_table VALUES (4, 2.0, 2.0) +---- +1 + +query I +INSERT INTO default.default.test_float_table VALUES (5, CAST('-0.0' AS DOUBLE), CAST('-0.0' AS REAL)) +---- +1 + +query I +INSERT INTO default.default.test_float_table VALUES (6, 0.0, 0.0) +---- +1 + +query I +INSERT INTO default.default.test_float_table VALUES (7, -1.0, -1.0), (8, NULL, NULL) +---- +2 + +query I +INSERT INTO default.default.test_float_table VALUES (9, -CAST('NaN' AS DOUBLE), -CAST('NaN' AS REAL)), (10, 3.0, 3.0) +---- +2 + +# DataFusion's NaN rules, which `float_comparison` in predicate.rs follows: row 9 +# holds -NaN, which is less than any number and not equal to NaN. +# apache/datafusion#26091 proposes making every NaN equal and greater than any +# number. If this fails after a DataFusion upgrade, follow the TODO on +# `float_comparison`, and take the expected rows below from DataFusion again. +query BBB +SELECT x < 1.0, x = CAST('NaN' AS DOUBLE), x > -1.0 FROM default.default.test_float_table WHERE id = 9 +---- +true false false + +# The pushed-down predicate keeps the files that hold NaN +query TT +EXPLAIN SELECT id FROM default.default.test_float_table WHERE x > 2.0 +---- +logical_plan +01)Projection: default.default.test_float_table.id +02)--Filter: default.default.test_float_table.x > Float64(2) +03)----TableScan: default.default.test_float_table projection=[id, x], partial_filters=[default.default.test_float_table.x > Float64(2)] +physical_plan +01)FilterExec: x@1 > 2, projection=[id@0] +02)--CooperativeExec +03)----DataSourceExec: format=iceberg, projection=[id, x], provider=lazy, predicate=(x > 2) OR (x IS NAN) + +# A number +query T +SELECT string_agg(CAST(id AS VARCHAR), ', ' ORDER BY id) FROM default.default.test_float_table WHERE x = 2.0 +---- +4 + +query T +SELECT string_agg(CAST(id AS VARCHAR), ', ' ORDER BY id) FROM default.default.test_float_table WHERE x != 2.0 +---- +1, 2, 3, 5, 6, 7, 9, 10 + +query T +SELECT string_agg(CAST(id AS VARCHAR), ', ' ORDER BY id) FROM default.default.test_float_table WHERE x < 2.0 +---- +2, 5, 6, 7, 9 + +query T +SELECT string_agg(CAST(id AS VARCHAR), ', ' ORDER BY id) FROM default.default.test_float_table WHERE x <= 2.0 +---- +2, 4, 5, 6, 7, 9 + +query T +SELECT string_agg(CAST(id AS VARCHAR), ', ' ORDER BY id) FROM default.default.test_float_table WHERE x > 2.0 +---- +1, 3, 10 + +query T +SELECT string_agg(CAST(id AS VARCHAR), ', ' ORDER BY id) FROM default.default.test_float_table WHERE x >= 2.0 +---- +1, 3, 4, 10 + +# A zero, which stands for both -0.0 and 0.0 +query T +SELECT string_agg(CAST(id AS VARCHAR), ', ' ORDER BY id) FROM default.default.test_float_table WHERE x = 0.0 +---- +5, 6 + +query T +SELECT string_agg(CAST(id AS VARCHAR), ', ' ORDER BY id) FROM default.default.test_float_table WHERE x != 0.0 +---- +1, 2, 3, 4, 7, 9, 10 + +query T +SELECT string_agg(CAST(id AS VARCHAR), ', ' ORDER BY id) FROM default.default.test_float_table WHERE x < 0.0 +---- +7, 9 + +query T +SELECT string_agg(CAST(id AS VARCHAR), ', ' ORDER BY id) FROM default.default.test_float_table WHERE x <= -0.0 +---- +5, 6, 7, 9 + +query T +SELECT string_agg(CAST(id AS VARCHAR), ', ' ORDER BY id) FROM default.default.test_float_table WHERE x > 0.0 +---- +1, 2, 3, 4, 10 + +query T +SELECT string_agg(CAST(id AS VARCHAR), ', ' ORDER BY id) FROM default.default.test_float_table WHERE x >= 0.0 +---- +1, 2, 3, 4, 5, 6, 10 + +query T +SELECT string_agg(CAST(id AS VARCHAR), ', ' ORDER BY id) FROM default.default.test_float_table WHERE x = -0.0 +---- +5, 6 + +# NaN +query T +SELECT string_agg(CAST(id AS VARCHAR), ', ' ORDER BY id) FROM default.default.test_float_table WHERE x = CAST('NaN' AS DOUBLE) +---- +1, 3 + +query T +SELECT string_agg(CAST(id AS VARCHAR), ', ' ORDER BY id) FROM default.default.test_float_table WHERE x != CAST('NaN' AS DOUBLE) +---- +2, 4, 5, 6, 7, 9, 10 + +query T +SELECT string_agg(CAST(id AS VARCHAR), ', ' ORDER BY id) FROM default.default.test_float_table WHERE x < CAST('NaN' AS DOUBLE) +---- +2, 4, 5, 6, 7, 9, 10 + +query T +SELECT string_agg(CAST(id AS VARCHAR), ', ' ORDER BY id) FROM default.default.test_float_table WHERE x <= CAST('NaN' AS DOUBLE) +---- +1, 2, 3, 4, 5, 6, 7, 9, 10 + +query T +SELECT string_agg(CAST(id AS VARCHAR), ', ' ORDER BY id) FROM default.default.test_float_table WHERE x > CAST('NaN' AS DOUBLE) +---- +NULL + +query T +SELECT string_agg(CAST(id AS VARCHAR), ', ' ORDER BY id) FROM default.default.test_float_table WHERE x >= CAST('NaN' AS DOUBLE) +---- +1, 3 + +# -NaN +query T +SELECT string_agg(CAST(id AS VARCHAR), ', ' ORDER BY id) FROM default.default.test_float_table WHERE x = -CAST('NaN' AS DOUBLE) +---- +9 + +query T +SELECT string_agg(CAST(id AS VARCHAR), ', ' ORDER BY id) FROM default.default.test_float_table WHERE x != -CAST('NaN' AS DOUBLE) +---- +1, 2, 3, 4, 5, 6, 7, 10 + +query T +SELECT string_agg(CAST(id AS VARCHAR), ', ' ORDER BY id) FROM default.default.test_float_table WHERE x < -CAST('NaN' AS DOUBLE) +---- +NULL + +query T +SELECT string_agg(CAST(id AS VARCHAR), ', ' ORDER BY id) FROM default.default.test_float_table WHERE x <= -CAST('NaN' AS DOUBLE) +---- +9 + +query T +SELECT string_agg(CAST(id AS VARCHAR), ', ' ORDER BY id) FROM default.default.test_float_table WHERE x > -CAST('NaN' AS DOUBLE) +---- +1, 2, 3, 4, 5, 6, 7, 10 + +query T +SELECT string_agg(CAST(id AS VARCHAR), ', ' ORDER BY id) FROM default.default.test_float_table WHERE x >= -CAST('NaN' AS DOUBLE) +---- +1, 2, 3, 4, 5, 6, 7, 9, 10 + +# IN lists +query T +SELECT string_agg(CAST(id AS VARCHAR), ', ' ORDER BY id) FROM default.default.test_float_table WHERE x IN (0.0, 2.0) +---- +4, 5, 6 + +query T +SELECT string_agg(CAST(id AS VARCHAR), ', ' ORDER BY id) FROM default.default.test_float_table WHERE x IN (CAST('NaN' AS DOUBLE), 0.5) +---- +1, 2, 3 + +query T +SELECT string_agg(CAST(id AS VARCHAR), ', ' ORDER BY id) FROM default.default.test_float_table WHERE x NOT IN (0.0, 2.0) +---- +1, 2, 3, 7, 9, 10 + +query T +SELECT string_agg(CAST(id AS VARCHAR), ', ' ORDER BY id) FROM default.default.test_float_table WHERE x NOT IN (CAST('NaN' AS DOUBLE)) +---- +2, 4, 5, 6, 7, 9, 10 + +# A REAL column +query T +SELECT string_agg(CAST(id AS VARCHAR), ', ' ORDER BY id) FROM default.default.test_float_table WHERE y > CAST(1 AS REAL) +---- +1, 3, 4, 10 + +query T +SELECT string_agg(CAST(id AS VARCHAR), ', ' ORDER BY id) FROM default.default.test_float_table WHERE y < CAST(1 AS REAL) +---- +2, 5, 6, 7, 9 + +query T +SELECT string_agg(CAST(id AS VARCHAR), ', ' ORDER BY id) FROM default.default.test_float_table WHERE y = CAST(0 AS REAL) +---- +5, 6