[ 
https://issues.apache.org/jira/browse/CALCITE-7827?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Sean Broeder updated CALCITE-7827:
----------------------------------
    Description: 
AbstractTypeCoercion#commonTypeForBinaryComparison picks the approximate 
numeric operand's own type as the common type when an exact numeric (DECIMAL) 
is compared with an approximate numeric. The coercion layer then narrows the 
DECIMAL operand to that type, which can lose precision.

A 32-bit REAL has a 24-bit mantissa, about 7 significant decimal digits. Two 
distinct DECIMAL values can collapse onto the same REAL and compare as equal. 
For example, 59999943.000 and 59999945.000 both round to the same REAL. DOUBLE 
has a 53-bit mantissa, about 15 digits, so high-precision DECIMAL values can 
still collide after narrowing to DOUBLE. Integer types are affected the same 
way: INTEGER and BIGINT compared with REAL, and BIGINT compared with DOUBLE.

Changing the default would break existing users, so this issue does not change 
default behavior. Instead it:
- Documents which operators go through commonTypeForBinaryComparison: =, <>, <, 
<=, >, >=, IS [NOT] DISTINCT FROM, BETWEEN, IN, quantified comparisons, and the 
NATURAL JOIN / JOIN USING call from SqlToRelConverter.
- Documents the existing extension point: subclass TypeCoercionImpl, override 
commonTypeForBinaryComparison to return DOUBLE when the result is approximate 
and one operand is DECIMAL, and install the subclass via 
SqlValidator.Config#withTypeCoercionFactory.
- Adds tests showing the default and the overridden behavior.

  was:
AbstractTypeCoercion#commonTypeForBinaryComparison picks the approximate 
numeric operand's own type (FLOAT/REAL) as the common type for a comparison 
between an exact numeric DECIMAL and an approximate numeric operand, resulting 
in a loss of precision.

A 32-bit FLOAT/REAL carries only ~7 significant decimal digits. When the 
validator uses this common type to insert an implicit CAST on the DECIMAL 
operand, two genuinely different DECIMAL values can collapse onto the same 
float value and incorrectly compare equal.

commonTypeForBinaryComparison should consider the exact numeric's type before 
blindly returning the approximate numeric's type.


> Document precision loss in DECIMAL/REAL comparisons and how to widen them to 
> DOUBLE
> -----------------------------------------------------------------------------------
>
>                 Key: CALCITE-7827
>                 URL: https://issues.apache.org/jira/browse/CALCITE-7827
>             Project: Calcite
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 1.42.0
>            Reporter: Sean Broeder
>            Assignee: Sean Broeder
>            Priority: Major
>              Labels: pull-request-available
>
> AbstractTypeCoercion#commonTypeForBinaryComparison picks the approximate 
> numeric operand's own type as the common type when an exact numeric (DECIMAL) 
> is compared with an approximate numeric. The coercion layer then narrows the 
> DECIMAL operand to that type, which can lose precision.
> A 32-bit REAL has a 24-bit mantissa, about 7 significant decimal digits. Two 
> distinct DECIMAL values can collapse onto the same REAL and compare as equal. 
> For example, 59999943.000 and 59999945.000 both round to the same REAL. 
> DOUBLE has a 53-bit mantissa, about 15 digits, so high-precision DECIMAL 
> values can still collide after narrowing to DOUBLE. Integer types are 
> affected the same way: INTEGER and BIGINT compared with REAL, and BIGINT 
> compared with DOUBLE.
> Changing the default would break existing users, so this issue does not 
> change default behavior. Instead it:
> - Documents which operators go through commonTypeForBinaryComparison: =, <>, 
> <, <=, >, >=, IS [NOT] DISTINCT FROM, BETWEEN, IN, quantified comparisons, 
> and the NATURAL JOIN / JOIN USING call from SqlToRelConverter.
> - Documents the existing extension point: subclass TypeCoercionImpl, override 
> commonTypeForBinaryComparison to return DOUBLE when the result is approximate 
> and one operand is DECIMAL, and install the subclass via 
> SqlValidator.Config#withTypeCoercionFactory.
> - Adds tests showing the default and the overridden behavior.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to