[ 
https://issues.apache.org/jira/browse/CALCITE-6617?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17887721#comment-17887721
 ] 

Mihai Budiu commented on CALCITE-6617:
--------------------------------------

The problem described by this issue is in TypeCoercion. When comparing two 
values with different types (e.g., int and Decimal) TypeCoercion is supposed to 
cast them to a common type. However, TypeCoercion didn't do that for many pairs 
of legal types, because the function that computes the common type 
TypeCoercionImpl.commonTypeForComparison would give up computing the common 
type if 
- neither type was a string, and 
- the types didn't have the same "name" (SqlTypeUtil.sameNamedType)

The fix is to rewrite the function to return the leastRestrictive for most 
cases.

The crash was happening because some simplification code for SARG expressions 
attempted to build a representation that was mixing different types for 
literals. 

That being said, the function TypeCoercion.commonTypeForBinaryComparison is 
broken by definition: either the JavaDoc is wrong, or the function name is 
wrong. But I don't know if I am allowed to change it.

> TypeCoercion is not applied correctly to comparisons
> ----------------------------------------------------
>
>                 Key: CALCITE-6617
>                 URL: https://issues.apache.org/jira/browse/CALCITE-6617
>             Project: Calcite
>          Issue Type: Bug
>          Components: core
>            Reporter: Claude Brisson
>            Assignee: Mihai Budiu
>            Priority: Blocker
>              Labels: pull-request-available
>             Fix For: 1.38.0
>
>
> The following statement:
> {code:java}
> SELECT * FROM (VALUES (1, 2, 3), (5E0::REAL, 5E0, NULL)) AS foo {code}
> will provoke a ClassCastException between a Double and a BigDecimal, while it 
> was working in 1.37.0.
> It's due to the fact that when trying to infer a collation on a Values 
> tuples, the comparator still assumes that all numbers are of the same class, 
> which changed with CALCITE-2067.
> Here is the full stack trace:
>  
> {code:java}
> Caused by: java.lang.ClassCastException: class java.lang.Double cannot be 
> cast to class java.math.BigDecimal (java.lang.Double and java.math.BigDecimal 
> are in module java.base of loader 'bootstrap')
>     at java.base/java.math.BigDecimal.compareTo(BigDecimal.java:330)
>     at 
> org.apache.calcite.rel.RelFieldCollation.compare(RelFieldCollation.java:45)
>     at 
> org.apache.calcite.rel.metadata.RelMdCollation$1.compare(RelMdCollation.java:458)
>     at 
> org.apache.calcite.rel.metadata.RelMdCollation$1.compare(RelMdCollation.java:454)
>     at com.google.common.collect.Ordering.isOrdered(Ordering.java:910)
>     at 
> org.apache.calcite.rel.metadata.RelMdCollation.values(RelMdCollation.java:434)
>     at 
> org.apache.calcite.rel.logical.LogicalValues.lambda$create$0(LogicalValues.java:111)
>     at org.apache.calcite.plan.RelTraitSet.replaceIfs(RelTraitSet.java:246)
>     at 
> org.apache.calcite.rel.logical.LogicalValues.create(LogicalValues.java:110)
>     at 
> org.apache.calcite.rel.core.RelFactories$ValuesFactoryImpl.createValues(RelFactories.java:521)
>     at org.apache.calcite.tools.RelBuilder.values(RelBuilder.java:3497)
>     at org.apache.calcite.tools.RelBuilder.setOp(RelBuilder.java:2924)
>     at org.apache.calcite.tools.RelBuilder.union(RelBuilder.java:2946)
>     at 
> org.apache.calcite.sql2rel.SqlToRelConverter.convertValuesImpl(SqlToRelConverter.java:5050)
>     at 
> org.apache.calcite.sql2rel.SqlToRelConverter.convertFrom(SqlToRelConverter.java:2488)
>     at 
> org.apache.calcite.sql2rel.SqlToRelConverter.convertFrom(SqlToRelConverter.java:2393)
>     at 
> org.apache.calcite.sql2rel.SqlToRelConverter.convertFrom(SqlToRelConverter.java:2355)
>     at 
> org.apache.calcite.sql2rel.SqlToRelConverter.convertSelectImpl(SqlToRelConverter.java:754)
>     at 
> org.apache.calcite.sql2rel.SqlToRelConverter.convertSelect(SqlToRelConverter.java:734)
>     at 
> org.apache.calcite.sql2rel.SqlToRelConverter.convertQueryRecursive(SqlToRelConverter.java:3905)
>     at 
> org.apache.calcite.sql2rel.SqlToRelConverter.convertQuery(SqlToRelConverter.java:627)
>  {code}
>  



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to