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

Julian Hyde commented on CALCITE-4713:
--------------------------------------

I'm not sure that this behavior is incorrect (even though it is not what you 
are expecting).

Calcite's primary type system is the SQL type system. If there are a variety of 
string-like types, then VARCHAR is the lowest common denominator.

If you change that, I think things will break.

> JavaTypeFactory#leastRestrictive returns wrong type for JavaType(String)
> ------------------------------------------------------------------------
>
>                 Key: CALCITE-4713
>                 URL: https://issues.apache.org/jira/browse/CALCITE-4713
>             Project: Calcite
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 1.27.0
>            Reporter: Zuozhi Wang
>            Priority: Minor
>
> When using JavaTypeFactoryImpl, calling `leastRestrictive` with two 
> `JavaType(String)` types gives a wrong return type of `VARCHAR` (SqlType). 
> The expected return type should still be `JavaType(String)` 
> See the following test case:
> {code:java}
>   @Test void testLeastRestrictiveWithWithJavaString() {
>     JavaTypeFactoryImpl javaTypeFactory = new JavaTypeFactoryImpl();
>     RelDataType javaStringType = javaTypeFactory.createJavaType(String.class);
>     // leastRestrictive of two same JavaType(String) types
>     RelDataType leastRestrictive = javaTypeFactory.leastRestrictive(
>         Arrays.asList(javaStringType, javaStringType));
>     // expect leastRestrictive to also be JavaType(String)
>     // but actual value is VARCHAR (BasicSqlType)
>     assertEquals(javaStringType, leastRestrictive);
>   }
> {code}
> We are currently investigating a fix of this issue.
> JavaTypeFactoryImpl extends SqlTypeFactoryImpl, and the `leastRestrictive` is 
> not overridden. In `SqlTypeFactoryImpl`, `leastRestrictive` will first try to 
> treat the input types as sql types, and call `leastRestrictiveSqlType`, 
> therefore `JavaType(String)` is treated as SqlType `VARCHAR`.
> One possible fix of this issue is to override the `leastRestrictive` function 
> in `JavaTypeFactoryImpl` to call `leastRestrictiveByCast` in 
> SqlTypeFactoryImpl instead. 
> {code:java}
>   // in JavaTypeFactoryImpl 
>   // also need to change SqlTypeFactoryImpl#leastRestrictiveByCast from 
> private to protected
>   @Override
>   public @Nullable RelDataType leastRestrictive(List<RelDataType> types) {
>     return super.leastRestrictiveByCast(types);
>   }
> {code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to