[CALCITE-7826] IS NOT DISTINCT FROM gives wrong result in Enumerable convention when applied to DECIMAL values of different scales - #5296
Closed
julianhyde wants to merge 2 commits into
Conversation
…in Enumerable convention when applied to DECIMAL values of different scales In the Enumerable convention, IS NOT DISTINCT FROM and IS DISTINCT FROM are implemented by RexImpTable.DistinctFromImplementor, which compares non-null operands using Objects.equals and does not harmonize the operand types. BigDecimal.equals is sensitive to scale, so 1.10 (DECIMAL(3, 2)) and 1.1 (DECIMAL(2, 1)) are distinct, whereas '=' compares them numerically and finds them equal. The test evaluates both operators, and '=', on those two values, and gets: a=1.10; b=1.1; eq=true; indf=false; idf=true rather than the correct: a=1.10; b=1.1; eq=true; indf=true; idf=false A query parsed from SQL is not affected, because StandardConvertletTable.convertIsDistinctFrom expands IS [NOT] DISTINCT FROM (using RelOptUtil.isDistinctFrom) into IS NULL and '=' on operands that have been cast to a common type. RelBuilder .isNotDistinctFrom expands it in the same way. So the test calls the operators directly, using RelBuilder.call, as a plan built by an application may do. (The problem was found in Morel, which does this.)
…convention when applied to DECIMAL values of different scales Fix the problem reproduced by the previous commit. RexImpTable.DistinctFromImplementor, which implements IS NOT DISTINCT FROM and IS DISTINCT FROM in the Enumerable convention, compared two non-null operands using Objects.equals. For BigDecimal values that is sensitive to scale, so 1.10 and 1.1 were distinct. Now, if both operands are BigDecimal values, it compares them using SqlFunctions.eq(BigDecimal, BigDecimal) (via the new BuiltInMethod.EQ_DECIMAL), which the '=' operator also uses, and which ignores scale. Other values are still compared using Objects.equals. (Harmonizing the operand types, as other implementors do, would not fix the problem: both operands already have Java class BigDecimal, and the conversion does not change their scale.)
|
xuzifu666
approved these changes
Sep 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



CALCITE-7826
The branch has two commits: a reproducer, and a fix. I'll squash after review.