Skip to content

[CALCITE-7827] DECIMAL compared with FLOAT loses precision when narrowed - #5297

Open
sbroeder wants to merge 4 commits into
apache:mainfrom
sbroeder:7827
Open

sbroeder wants to merge 4 commits into
apache:mainfrom
sbroeder:7827

Conversation

@sbroeder

Copy link
Copy Markdown
Contributor

commonTypeForBinaryComparison() picked whichever operand was approximate as the common type for a comparison against DECIMAL, narrowing the DECIMAL side to that operand's precision.

A 32-bit REAL/FLOAT holds only ~7 significant digits, so a DECIMAL literal or column with more digits than that can silently collide with a different value after narrowing. For example, 59999943 and 59999945 both round to the same float.

By widening the common type to DOUBLE whenever the exact-numeric operand is a DECIMAL, we don't lose precision.

Updated ArrowAdapterTest's expected plan, which now casts a FLOAT column to DOUBLE when compared against a DECIMAL literal.

Jira Link

CALCITE-7827

…narrowed

commonTypeForBinaryComparison() picked whichever operand was
approximate as the common type for a comparison against DECIMAL,
narrowing the DECIMAL side to that operand's precision.

A 32-bit REAL/FLOAT holds only ~7 significant digits, so a
DECIMAL literal or column with more digits than that can silently
collide with a different value after narrowing.  For example,
59999943 and 59999945 both round to the same float.

By widening the common type to DOUBLE whenever the exact-numeric
operand is a DECIMAL, we don't lose precision.

Updated ArrowAdapterTest's expected plan, which now casts a FLOAT
column to DOUBLE when compared against a DECIMAL literal.
…ercion override

Updated the design based on input from the JIRA so that users could
opt in to thei behavior and avoid breaking changes for existing users.

Extract the decision into a new protected
AbstractTypeCoercion#approximateExactComparisonType(...) hook. A
system that needs the precision-safe behavior can opt in with a small
TypeCoercionFactory overriding this one method, the same pattern
already used by TypeCoercionImpl and demonstrated by
SqlToRelConverterTest#testNaturalJoinCastNoCoercion.

Reworked the tests accordingly.
@mihaibudiu

Copy link
Copy Markdown
Contributor

This looks like a reasonable approach, I will review this

Comment thread core/src/test/java/org/apache/calcite/test/TypeCoercionTest.java Outdated
@sonarqubecloud

Copy link
Copy Markdown

@mihaibudiu mihaibudiu added the LGTM-will-merge-soon Overall PR looks OK. Only minor things left. label Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

LGTM-will-merge-soon Overall PR looks OK. Only minor things left.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants