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

Maksim Zhuravkov updated IGNITE-22358:
--------------------------------------
    Description: 
{noformat}
SELECT 1 UNION ALL SELECT '2000-01-01'::DATE
# returns: DATE, DATE
{noformat}

Although a cast from INT to DATE should not be possible according to type 
transformation rules:
{noformat}
SELECT 1::DATE 
# Error: Cast function cannot convert value of type INTEGER to type DATE
{noformat}

This query should also return an error because it is not possible to convert an 
integer into a date.


This happens because Calcite's SqlTypeFactoryImpl::leastRestrictiveSqlType 
contains explicit code that allows int to date conversion and that code 
completely ignores Calcite's TypeConversion rules.

{code:java}
 else if (SqlTypeUtil.isExactNumeric(type)) {
        if (SqlTypeUtil.isExactNumeric(resultType)) {
          // TODO: come up with a cleaner way to support
          // interval + datetime = datetime
          if (types.size() > (i + 1)) {
            RelDataType type1 = types.get(i + 1);
            if (SqlTypeUtil.isDatetime(type1)) {
              resultType = type1;
              return createTypeWithNullability(resultType,
                  nullCount > 0 || nullableCount > 0);
            }
          }
{code}



  was:
{noformat}
SELECT 1 UNION ALL SELECT '2000-01-01'::DATE
# returns: DATE, DATE
{noformat}

Although a cast from INT to DATE should not be possible according to type 
transformation rules:
{noformat}
SELECT 1::DATE 
# Error: Cast function cannot convert value of type INTEGER to type DATE
{noformat}

This query should also return an error because it is not possible to convert an 
integer into a date.


This happens because Calcite's SqlTypeFactoryImpl::leastRestrictiveSqlType 
contains explicit code that allows int to date conversion and that code 
completely ignores its own TypeConversion rules.

{code:java}
 else if (SqlTypeUtil.isExactNumeric(type)) {
        if (SqlTypeUtil.isExactNumeric(resultType)) {
          // TODO: come up with a cleaner way to support
          // interval + datetime = datetime
          if (types.size() > (i + 1)) {
            RelDataType type1 = types.get(i + 1);
            if (SqlTypeUtil.isDatetime(type1)) {
              resultType = type1;
              return createTypeWithNullability(resultType,
                  nullCount > 0 || nullableCount > 0);
            }
          }
{code}




> Sql. Results of TypeFactory::leastRestrictiveType are incompatible with legal 
> type transformations
> --------------------------------------------------------------------------------------------------
>
>                 Key: IGNITE-22358
>                 URL: https://issues.apache.org/jira/browse/IGNITE-22358
>             Project: Ignite
>          Issue Type: Improvement
>          Components: sql
>            Reporter: Maksim Zhuravkov
>            Priority: Major
>              Labels: ignite-3
>
> {noformat}
> SELECT 1 UNION ALL SELECT '2000-01-01'::DATE
> # returns: DATE, DATE
> {noformat}
> Although a cast from INT to DATE should not be possible according to type 
> transformation rules:
> {noformat}
> SELECT 1::DATE 
> # Error: Cast function cannot convert value of type INTEGER to type DATE
> {noformat}
> This query should also return an error because it is not possible to convert 
> an integer into a date.
> This happens because Calcite's SqlTypeFactoryImpl::leastRestrictiveSqlType 
> contains explicit code that allows int to date conversion and that code 
> completely ignores Calcite's TypeConversion rules.
> {code:java}
>  else if (SqlTypeUtil.isExactNumeric(type)) {
>         if (SqlTypeUtil.isExactNumeric(resultType)) {
>           // TODO: come up with a cleaner way to support
>           // interval + datetime = datetime
>           if (types.size() > (i + 1)) {
>             RelDataType type1 = types.get(i + 1);
>             if (SqlTypeUtil.isDatetime(type1)) {
>               resultType = type1;
>               return createTypeWithNullability(resultType,
>                   nullCount > 0 || nullableCount > 0);
>             }
>           }
> {code}



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

Reply via email to