Francesco Guardiani created CALCITE-5046: --------------------------------------------
Summary: 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 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)