Skip to content

expression: keep +0 and -0 apart when propagating equal real values - #73809

Open
A-RYAN-1 wants to merge 1 commit into
pingcap:masterfrom
A-RYAN-1:fix/73686-atan2-signed-zero
Open

A-RYAN-1 wants to merge 1 commit into
pingcap:masterfrom
A-RYAN-1:fix/73686-atan2-signed-zero

Conversation

@A-RYAN-1

@A-RYAN-1 A-RYAN-1 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: close #73686

Problem Summary: constant propagation derives ATAN2(y, -1) > 0 from x = y and drops rows, because DOUBLE equality treats +0 and -0 as equal while ATAN2 and a cast of a real value to a string tell them apart. With x double, y double as (-x) and a stored 0, select x = y, atan2(x, -1) > 0, atan2(y, -1) > 0 returns 1 1 0, so the substitution changes the answer.

What changed and how does it work?

Skip the substitution when the predicate contains a function that can observe the sign of a zero (ATAN2, two-argument ATAN, and a cast from a real type to a string type). It is applied in tryToReplaceCond for column to column, and before the two ColumnSubstitute calls (inner and outer join solvers) when the substituted constant is a real zero of either sign. Other predicates are still propagated, which the test checks with x + 1 > 0. The check is one function-name comparison per node.

Not covered: decimal and JSON casts gave the same result for both zeros in a probe, so they are not treated as sinks, and json_array is not covered.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No need to test
    • I checked and no code files have been changed.

TestSignedZeroPropagation in pkg/expression/test/constantpropagation fails on the base and passes with this change.

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

Fix a wrong result when constant propagation replaced a column with another column or constant that is equal only under +0 and -0, in predicates using `ATAN2` or a cast of a real value to a string.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed query optimization for floating-point values that compare equal but have different signs, such as 0 and -0. Sign-sensitive expressions and casts now retain the correct behavior in filters and join conditions, while sign-insensitive calculations can still be optimized.

Constant propagation replaces a column with a column or constant that
equality says is equal. DOUBLE equality treats +0 and -0 as equal, but
ATAN2 and a cast of a real value to a string tell them apart, so the
substitution changed the result of a predicate such as
ATAN2(y, -1) > 0 derived from x = y.

Skip the substitution when the predicate contains such a function: in
tryToReplaceCond for column to column, and before the two
ColumnSubstitute calls for a zero real constant.

Fixes pingcap#73686

Signed-off-by: Aryan Deshmukh <[email protected]>
@ti-chi-bot ti-chi-bot Bot added needs-cherry-pick-release-nextgen-202603 Should cherry pick this PR to release-nextgen-202603 branch. needs-cherry-pick-release-nextgen-202609 Should cherry pick this PR to release-nextgen-202609 branch. release-note Denotes a PR that will be considered when it comes time to generate release notes. contribution This PR is from a community contributor. needs-ok-to-test Indicates a PR created by contributors and need ORG member send '/ok-to-test' to start testing. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Nov 25, 2026
@ti-chi-bot

ti-chi-bot Bot commented Nov 25, 2026

Copy link
Copy Markdown

Hi @A-RYAN-1. Thanks for your PR.

I'm waiting for a pingcap member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@ti-chi-bot

ti-chi-bot Bot commented Nov 25, 2026

Copy link
Copy Markdown

In response to a cherrypick label: only pingcap org members may request cherry picks. If you are already part of the org, make sure to change your membership to public. Otherwise you can still do the cherry-pick manually.

1 similar comment
@ti-chi-bot

ti-chi-bot Bot commented Nov 25, 2026

Copy link
Copy Markdown

In response to a cherrypick label: only pingcap org members may request cherry picks. If you are already part of the org, make sure to change your membership to public. Otherwise you can still do the cherry-pick manually.

@ti-chi-bot

ti-chi-bot Bot commented Nov 25, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign zanmato1984 for approval. For more information see the Code Review Process.
Please ensure that each of them provides their approval before proceeding.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Constant propagation now detects expressions that distinguish positive and negative floating-point zero. It avoids substituting equal real columns or constants into those expressions when zero is involved. Tests cover filters, join conditions, casts, and sign-insensitive arithmetic.

Changes: Signed-zero-safe constant propagation

Layer / File(s) Summary
Detect and guard sign-sensitive propagation
pkg/expression/constant_propagation.go, pkg/expression/test/constantpropagation/constant_propagation_test.go
The propagation logic identifies sign-sensitive functions and real zero constants. It leaves affected filter and join conditions unchanged. Tests cover ATAN2, two-argument ATAN, real-to-string casts, and sign-insensitive arithmetic.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: winoros, windtalker

Merge Risk: Minimal · up to 22d0b

This fix keeps rows that compare equal as +0 and -0 from being dropped when a predicate such as ATAN2 or a real-to-string cast can tell the two zeros apart. Other predicates still propagate as before. I found no remaining merge-blocking risk.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the core change: keeping +0 and -0 distinct when constant propagation substitutes equal real values. It matches the raw summary, which describes a guard that skips substitution for sig…
Description check Passed The description follows the repository template. It includes the problem summary, a linked issue (close #73686), a detailed explanation of the change, a checked unit test with the test name, and a rel…
Linked Issues check Passed Directly linked issue #73686 requires that x = y AND ATAN2(x, -1) > 0 return 1 row when x is +0 and generated y is -0. The issue attributes the failure to constant propagation that substitutes `…
Out of Scope Changes check Passed All changed hunks serve the signed-zero propagation fix for #73686. The changes are: the observesZeroSign, containsZeroSignObserver, and hasRealZero helpers in pkg/expression/constant_propagatio…
Full details: Title check

Explanation

The title names the core change: keeping +0 and -0 distinct when constant propagation substitutes equal real values. It matches the raw summary, which describes a guard that skips substitution for sign-sensitive functions when a real zero is propagated. The title is specific and follows the repository's package-prefix format.

Full details: Description check

Explanation

The description follows the repository template. It includes the problem summary, a linked issue (close #73686), a detailed explanation of the change, a checked unit test with the test name, and a release note. The integration and manual test boxes are unchecked, which is acceptable because at least one test type is checked. The Documentation and Side effects sections are left unchecked, which the template allows. The description also states the scope and the known gaps (decimal and JSON casts, json_array).

Full details: Linked Issues check

Explanation

Directly linked issue #73686 requires that x = y AND ATAN2(x, -1) &gt; 0 return 1 row when x is +0 and generated y is -0. The issue attributes the failure to constant propagation that substitutes y for x in ATAN2(x, -1) &gt; 0. The diff addresses both substitution paths:

  • tryToReplaceCond (constant_propagation.go) returns without substitution when src is ETReal and observesZeroSign is true. observesZeroSign returns true for ATAN2, two-argument ATAN, and a real-to-string CAST. This blocks the column-to-column replacement that produces ATAN2(y, -1) &gt; 0.
  • propagateConstantEQ (inner join) and propagateConstantEQ (outer join) skip ColumnSubstitute for conditions containing such a function when hasRealZero is true for the propagated constants. This covers constant equalities such as x = -'o'.

Other predicates still propagate. The change does not alter them, as shown by the x + 1 &gt; 0 case. TestSignedZeroPropagation covers the reproduced query (expected count 1), the join and outer-join variants, and the constant cases. I did not run the test. The PR reports that it fails on base and passes with this change. The linked issue has no non-coding requirements. Known gaps listed in the PR (decimal and JSON casts, json_array) are not in the issue's stated scope.

Full details: Out of Scope Changes check

Explanation

All changed hunks serve the signed-zero propagation fix for #73686. The changes are: the observesZeroSign, containsZeroSignObserver, and hasRealZero helpers in pkg/expression/constant_propagation.go, the guard in tryToReplaceCond, and the two guarded ColumnSubstitute calls in the inner and outer join solvers. The test change in constant_propagation_test.go adds only TestSignedZeroPropagation, which exercises that behavior. No unrelated files or refactors appear in the reviewed diff. The PR does not include the related PAD SPACE or ATAN2 pushdown reports, and it does not claim to fix them.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

golangci-lint (2.15.0)

golangci-lint (2.15.0) exceeded its 180-second time budget. Findings from completed work are retained; remaining checks are incomplete.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit checks each floating sign,
And keeps the zeroes in their line.
The filters hold the values true,
While joins keep signed-zero clues.
The query paths now stay in tune.
I nibble greens beneath the moon.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 37bcef2d-44f3-47c4-876e-5aed3caa2800
Commits

Reviewing files that changed from the base of the PR and between 2698c93 and 22d0b0b.

Files selected for processing: 2

  • pkg/expression/constant_propagation.go
  • pkg/expression/test/constantpropagation/constant_propagation_test.go

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

This branch has not been deployed

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

Labels

contribution This PR is from a community contributor. needs-cherry-pick-release-nextgen-202603 Should cherry pick this PR to release-nextgen-202603 branch. needs-cherry-pick-release-nextgen-202609 Should cherry pick this PR to release-nextgen-202609 branch. needs-ok-to-test Indicates a PR created by contributors and need ORG member send '/ok-to-test' to start testing. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

planner: propagating ATAN2 across equal DOUBLE columns drops rows for signed zero

1 participant