You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@calcite.apache.org by "Francesco Guardiani (Jira)" <ji...@apache.org> on 2022/03/15 15:46:00 UTC
[jira] [Commented] (CALCITE-5046) SqlTypeFactorylmpl#leastRestrictiveByCast incorrect results with MAP types
[ https://issues.apache.org/jira/browse/CALCITE-5046?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17506999#comment-17506999 ]
Francesco Guardiani commented on CALCITE-5046:
----------------------------------------------
After some investigation I found out that on master this bug does not reproduce. I tried to add this test:
{code:java}
/**
* Test case for CALCITE-5046.
*/
@Test void testLeastRestrictiveForMapsWithMixedCharVarChar() {
SqlTypeFixture f = new SqlTypeFixture();
// (CHAR(1), VARCHAR(6)) MAP
// (VARCHAR(2147483647), VARCHAR(2147483647)) MAP
// (CHAR(1), CHAR(1)) MAP
List<RelDataType> inputs = Lists.newArrayList(
f.typeFactory.createMapType(f.sqlChar1, f.typeFactory.createSqlType(SqlTypeName.VARCHAR, 6)),
f.typeFactory.createMapType(f.sqlVarchar, f.sqlVarchar),
f.typeFactory.createMapType(f.sqlChar1, f.sqlChar1)
);
RelDataType leastRestrictive = f.typeFactory.leastRestrictive(inputs);
assertThat(leastRestrictive.getSqlTypeName(), is(SqlTypeName.MAP));
assertThat(leastRestrictive.isNullable(), is(false));
assertThat(leastRestrictive.getKeyType().getSqlTypeName(), is(SqlTypeName.VARCHAR));
assertThat(leastRestrictive.getValueType().getSqlTypeName(), is(SqlTypeName.VARCHAR));
} {code}
And it passes fine, so I guess we can close this one. Sorry for the noise!
> SqlTypeFactorylmpl#leastRestrictiveByCast incorrect results with MAP types
> --------------------------------------------------------------------------
>
> Key: CALCITE-5046
> URL: https://issues.apache.org/jira/browse/CALCITE-5046
> Project: Calcite
> Issue Type: Bug
> Affects Versions: 1.26.0
> Reporter: Francesco Guardiani
> Priority: Major
>
> SqlTypeFactorylmpl#leastRestrictiveByCast returns incorrect results when the two input types are maps, with key and value types castable.
> For example, trying with this input:
> {code:sql}
> (CHAR(1), VARCHAR(6)) MAP
> (VARCHAR(2147483647), VARCHAR(2147483647)) MAP
> (CHAR(1), CHAR(1)) MAP
> {code}
> Returns the type {{(CHAR(1), CHAR(1)) MAP}}, which is incorrect as i would expect {{(VARCHAR(2147483647), VARCHAR(2147483647)) MAP}} as result.
> My analysis is that this is caused by {{SqlTypeUtil#canCastFrom}} which doesn't properly support MAP types, in particular when coerce = false. Adding this chunk of code here https://github.com/apache/calcite/blob/master/core/src/main/java/org/apache/calcite/sql/type/SqlTypeUtil.java#L877:
> {code:java}
> if (fromTypeName == toTypeName && fromTypeName == SqlTypeName.MAP) {
> return canCastFrom(toType.getKeyType(), fromType.getKeyType(), coerce)
> && canCastFrom(toType.getValueType(), fromType.getValueType(), coerce);
> }
> {code}
> Solves the issue, as now the MAP type casting check is not falling back to {{SqlTypeMappingRule}} anymore.
> I tested with 1.26, but looking on master, it sounds like the problem applies to later versions as well https://github.com/apache/calcite/blob/master/core/src/main/java/org/apache/calcite/sql/type/SqlTypeUtil.java#L814
--
This message was sent by Atlassian Jira
(v8.20.1#820001)