Skip to content

Treat -0.0 as equal to 0.0 in floating-point comparisons - #19442

Open
Karman580 wants to merge 1 commit into
apache:masterfrom
Karman580:fix-signed-zero-comparison
Open

Karman580 wants to merge 1 commit into
apache:masterfrom
Karman580:fix-signed-zero-comparison

Conversation

@Karman580

Copy link
Copy Markdown
Contributor

Description

Fixes #19441

Problem

For FLOAT/DOUBLE values equal to -0.0, the result of a comparison depended on whether the predicate was pushed down:

  • The pushed-down TsFile value filters use primitive comparison, so -0.0 == 0.0.
  • The comparison and BETWEEN evaluators used Double.compare, which orders -0.0 before 0.0.

So a row holding -0.0 matched WHERE v = 0.0, but not WHERE v = 0.0 OR dev = 'nope' (that predicate stays in a Filter node). Projections such as SELECT v = 0.0 and constant folding (SELECT -0.0 = 0.0) reported false. This affected both the table and tree models.

Change

I added CommonTransformUtils.compare(double, double) next to the existing compare(Binary, Binary):

return first == second ? 0 : Double.compare(first, second);

It differs from Double.compare only for the pair -0.0 / 0.0. == is never true for NaN, so NaN still goes through Double.compare and keeps its current results (NaN semantics are discussed separately in #19439).

The evaluators that used Double.compare now call this helper:

  • CompareBinaryColumnTransformer and BetweenColumnTransformer (calc-commons, table model)
  • IrExpressionInterpreter.evaluateNumericComparison (constant folding)
  • Compare{EqualTo,NonEqual,LessThan,LessEqual,GreaterThan,GreaterEqual}Transformer and BetweenTransformer (tree model)

Ordering code (sort, ORDER BY, extreme aggregation) is not changed.

Before / after

I ran the same statements on a standalone server built from 96a0c6d (before) and from this branch (after).

CREATE TABLE zdb.t (dev STRING TAG, v DOUBLE FIELD, f FLOAT FIELD);
INSERT INTO zdb.t(time, dev, v, f) VALUES (1,'d1',-0.0,-0.0),(2,'d1',0.0,0.0),(3,'d1',1.0,1.0);
Query Before After
WHERE v = 0.0 (pushed down) t1 (-0.0), t2 (0.0) t1, t2
WHERE v = 0.0 OR dev = 'nope' t2 t1, t2
WHERE v >= 0.0 OR dev = 'nope' t2, t3 t1, t2, t3
WHERE v < 0.0 OR dev = 'nope' t1 empty
WHERE f = 0.0 OR dev = 'nope' (FLOAT) t2 t1, t2
v = 0.0, v <> 0.0, v < 0.0, v >= 0.0, v BETWEEN 0.0 AND 1.0 for t1 false, true, true, false, false true, false, false, true, true
SELECT -0.0 = 0.0, -0.0 < 0.0 false, true true, false
tree: SELECT v FROM root.zsg.d1 WHERE v * 1.0 = 0.0 1 row 2 rows (same as WHERE v = 0.0)
tree: v = 0.0, v < 0.0, v >= 0.0 for -0.0 false, true, false true, false, true

After the fix (table model):

IoTDB> SELECT time, v FROM zdb.t WHERE v = 0.0 OR dev = 'nope' ORDER BY time
+-----------------------------+----+
|                         time|   v|
+-----------------------------+----+
|1970-01-01T05:30:00.001+05:30|-0.0|
|1970-01-01T05:30:00.002+05:30| 0.0|
+-----------------------------+----+
Total line number = 2

IoTDB> SELECT time, v, v = 0.0 AS eq, v <> 0.0 AS ne, v < 0.0 AS lt, v >= 0.0 AS ge, v BETWEEN 0.0 AND 1.0 AS btw FROM zdb.t ORDER BY time
+-----------------------------+----+-----+-----+-----+----+----+
|                         time|   v|   eq|   ne|   lt|  ge| btw|
+-----------------------------+----+-----+-----+-----+----+----+
|1970-01-01T05:30:00.001+05:30|-0.0| true|false|false|true|true|
|1970-01-01T05:30:00.002+05:30| 0.0| true|false|false|true|true|
|1970-01-01T05:30:00.003+05:30| 1.0|false| true|false|true|true|
+-----------------------------+----+-----+-----+-----+----+----+
Total line number = 3

IoTDB> SELECT -0.0 = 0.0, -0.0 < 0.0
+-----+-----+
|_col0|_col1|
+-----+-----+
| true|false|
+-----+-----+
Total line number = 1

Tests

  • New FloatingPointCompareColumnTransformerTest covers DOUBLE and FLOAT over the column -0.0, 0.0, NaN, 1.0, testing =, <>, <, <=, >, >=, reversed operands, BETWEEN and NOT BETWEEN. The NaN rows assert the current results, so they stay unchanged. On 96a0c6d the test fails for both types: arrays first differed at element [0]; expected:<true> but was:<false>. With this change it passes.
  • IoTDBFilterTableIT#testSignedZeroComparison checks that pushed-down and non-pushed-down predicates return the same rows, and covers projections and SELECT -0.0 = 0.0.
  • IoTDBFilterIT#testSignedZeroComparison (tree model) checks that WHERE v = 0.0 and WHERE v * 1.0 = 0.0 agree, and covers projected comparisons.

Commands I ran locally (JDK 25, macOS arm64):

  • ./mvnw -o package -pl distribution -am -Drat.skip=true '-Dtest=FloatingPointCompareColumnTransformerTest,ArithmeticAndCompareBinaryColumnTransformerTest,*Transformer*Test,*ExpressionInterpreter*Test,*Between*Test,*ConstantFold*Test' -Dsurefire.failIfNoSpecifiedTests=false
    • datanode: Tests run: 250, Failures: 0, Errors: 0. BUILD SUCCESS.
  • ./mvnw -o test -pl iotdb-core/calc-commons -am '-Dtest=org/apache/iotdb/calc/**/*' -Dsurefire.failIfNoSpecifiedTests=false
    • Tests run: 91, Failures: 0. BUILD SUCCESS.
  • ./mvnw -o verify -DskipUTs -Dit.test=IoTDBFilterTableIT -pl integration-test -am -PTableSimpleIT -P with-integration-tests ...
    • Tests run: 8, Failures: 0, Errors: 0, Skipped: 1. The skip is the existing @Ignore on testFilterWithUDTF.
  • ./mvnw -o verify -DskipUTs -Dit.test=IoTDBFilterIT -pl integration-test -am -PClusterIT -P with-integration-tests ...
    • Tests run: 7, Failures: 0, Errors: 0, Skipped: 0.
  • ./mvnw -o test-compile -pl iotdb-core/datanode -am -P with-zh-locale -DskipTests
    • Exit code 0.

This PR has:

  • been self-reviewed.
  • added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
  • added unit tests or modified existing tests to cover new code paths, ensuring the threshold for code coverage.
  • added integration tests.

Key changed/added classes (or packages if there are too many classes) in this PR
  • CommonTransformUtils
  • CompareBinaryColumnTransformer, BetweenColumnTransformer
  • IrExpressionInterpreter
  • tree-model Compare*Transformer, BetweenTransformer

The comparison and BETWEEN evaluators (table-model column transformers,
constant folding in IrExpressionInterpreter and the tree-model
transformers) used Double.compare, which orders -0.0 before 0.0. The
pushed-down TsFile value filters use primitive comparison, so a row
holding -0.0 matched WHERE v = 0.0 but not WHERE v = 0.0 OR dev = 'x'.

Compare through a shared CommonTransformUtils.compare(double, double)
that returns 0 when the values are ==, and otherwise falls back to
Double.compare, so NaN handling is unchanged.

Fixes apache#19441
@Karman580

Karman580 commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor Author

The CI workflows for this PR are waiting for approval since it comes from a fork. Could a maintainer approve them when convenient? The new unit test and both affected IT classes pass locally.
@JackieTien97

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] -0.0 and 0.0 compare unequal in projections and non-pushed-down filters but equal in pushed-down filters (FLOAT/DOUBLE)

1 participant