Skip to content

fix: fail tolerance comparison when only one side is NaN - #7756

Queued
ErikBPF wants to merge 1 commit into
apache:mainfrom
ErikBPF:fix/7672-tolerance-nan-mismatch
Queued

ErikBPF wants to merge 1 commit into
apache:mainfrom
ErikBPF:fix/7672-tolerance-nan-mismatch

Conversation

@ErikBPF

@ErikBPF ErikBPF commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Issue: #7672 (reference only; this draft does not request auto-closure).

Rationale for this change

checkAnswerWithTolerance in CometTestBase skips a float/double comparison whenever
either side is NaN, so a Comet NaN against a Spark number passes. Every query tolerance=...
SQL fixture uses this comparison, which means such a fixture cannot catch a kernel that
returns NaN where Spark returns a number.

What changes are included in this PR?

  • CometTestBase.checkAnswerWithTolerance compares NaN-ness explicitly: two NaNs match, and
    one-sided NaN fails with actual answer <v> and correct answer <v> differ: only one side is NaN. The infinity branch compares the values rather than only their infinite-ness, so
    +Infinity no longer matches -Infinity, and finite values keep the existing
    abs(actual - expected) < absTol check.
  • The row-level overload becomes protected so the suite that consumes the tolerance path can
    assert the contract directly.
  • Four regression tests in CometSqlFileTestSuite cover both-NaN, one-sided NaN (Float and
    Double, both directions), finite values inside/outside absTol, and infinities including the
    two sign-flipped pairs.
  • One comment in pow.sql justified exact comparison by claiming a tolerance comparison makes
    no assertion for NaN. That claim is now false, so it is corrected; the surviving reasons are
    the signed Infinity and signed zero cases.

No production code and no assertion in an existing suite is modified; the only fixture change is
that comment. The pre-existing hole where +Inf compares equal to -Inf is out of scope and is
named in the infinity test's comment.

How are these changes tested?

  • RED (test-only commit): targeted tolerance tests -> Tests: succeeded 3, failed 1; the
    one-sided NaN test fails with "Expected exception ... TestFailedException to be thrown, but
    no exception was thrown".
  • GREEN: same target -> Tests: succeeded 4, failed 0.
  • org.apache.comet.CometSqlFileTestSuite (all fixtures, 108 query tolerance= queries in
    37 files) plus CometMathExpressionSuite: Tests: succeeded 812, failed 0, run on the final
    commit 59d71fce0.
  • org.apache.comet.CometMathExpressionSuite: Tests: succeeded 7, failed 0.

Run in nix-shell -p jdk17 maven cargo rustc protobuf cmake clang with
./mvnw test -Dtest=none -Dsuites="<suite>".

checkAnswerWithTolerance skipped the comparison for a float/double pair whenever
either side was NaN, so a Comet NaN against a Spark number passed. Every
`query tolerance=...` SQL fixture uses that helper, so none of them could catch
a kernel returning NaN where Spark returns a value.

NaN-ness is now asserted explicitly for both Double and Float. The infinity
branch compares the values instead of only their infinite-ness, so +Infinity no
longer matches -Infinity, which Spark treats as distinct. Four regression tests
cover both types and both directions, finite values inside and outside the
tolerance, and the infinity cases.

The row overload becomes protected so the suite that consumes the tolerance
path can assert the changed method directly, rather than through a copy.

Verified on the Spark 4.1 profile: CometSqlFileTestSuite and
CometMathExpressionSuite ran 812 tests with none failing, including the 108
`query tolerance=` queries. The one-sided NaN test failed before the fix with
"Expected exception ... to be thrown, but no exception was thrown".
@ErikBPF
ErikBPF force-pushed the fix/7672-tolerance-nan-mismatch branch from cf8bde9 to 59d71fc Compare November 24, 2026 10:53
@ErikBPF
ErikBPF marked this pull request as ready for review November 24, 2026 11:02

@dwsmith1983 dwsmith1983 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This fixes #7672. I checked it end to end with a query tolerance=1e-6 fixture over ln(monotonically_increasing_id() % 3 - 1) with spark.comet.batchSize=2, which still hits #7671 on main: Comet returns NaN where Spark returns 0.0. The fixture passes on main and fails on this branch with "only one side is NaN".

The stricter check also backs checkSparkAnswerWithTolerance outside the SQL file suite, and CI hasn't run here. So I ran every test on this path on Spark 3.5, 4.0, 4.1 and 4.2: the full CometSqlFileTestSuite plus the 11 tolerance tests in CometExpressionSuite, CometAggregateSuite and CometWindowExecSuite. All 816 pass on each profile.

A few small things, none blocking:

  • Could the description say Closes #7672? It isn't a draft anymore, and as written the issue stays open after the merge.

  • The infinity branch now compares by value, so +Infinity against -Infinity fails. The description also calls that hole out of scope, which contradicts the code and the test comment.

  • With that change, these still describe the old behavior:

    • the pow.sql comment rewritten here ("ignores the sign of Infinity")
    • csc.sql:28 ("tolerance only checks isInfinity")
    • sql-file-tests.md lines 259-261, 268 and 486-488

    Signed zero is the only case left that needs a plain query.

  • On the old code the infinities test fails too, since isInfinity == isInfinity holds for +Inf and -Inf. So the RED run should show two failures, not one.

@comphead comphead left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The new branch order in checkAnswerWithTolerance is correct for both arms. Infinity is compared by value first, so +Inf against -Inf or against NaN fails. Then any NaN requires both sides to be NaN, and only finite pairs use absTol. Both directions are covered because the check is symmetric, and null against NaN still reaches the fallback actual == expected arm and fails. I found no fixture added to main since the merge base that uses tolerance=. I did not run any tests. The author reports 812 passing on one profile and dwsmith1983 reports 816 on Spark 3.5, 4.0, 4.1 and 4.2, which is the evidence that no existing test relied on a NaN against a number matching. The stale comment and documentation points from dwsmith1983's review (pow.sql, csc.sql:28, sql-file-tests.md line 260) are still present at this head.

}
}

test("tolerance comparison: one-sided NaN") {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The one-sided cases only use finite numbers. Two other routes into the NaN logic are untested. Row(null) against Row(Double.NaN) goes through the final actual == expected arm, and Row(Double.PositiveInfinity) against Row(Double.NaN) goes through the infinity branch, whose message does not say only one side is NaN. Adding both pairs to this test (in both directions) pins the behavior for a null Spark result against a NaN Comet result, which is the likely shape of a real kernel bug.

@comphead comphead left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

thanks @ErikBPF, the PR goes into right direction, please check some nits!

@github-actions github-actions Bot added the bug Something isn't working label Dec 4, 2026
@sunchao
sunchao added this pull request to the merge queue Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants