You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@calcite.apache.org by "Dmitry Sysolyatin (Jira)" <ji...@apache.org> on 2024/10/24 16:57:00 UTC
[jira] [Commented] (CALCITE-5532) CompositeOperandTypeChecker should check operands without type coercion first
[ https://issues.apache.org/jira/browse/CALCITE-5532?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17892561#comment-17892561 ]
Dmitry Sysolyatin commented on CALCITE-5532:
--------------------------------------------
[~julianhyde]
After almost two years, it's time to fix the PR. Unfortunately, I cannot fully emulate PostgreSQL's {{concat}} at this point in time because Calcite does not have an implicit cast from array to string; however, it does have an implicit cast from numeric to string. Therefore, I introduced a mock function, {{COMPARE_STRINGS_OR_NUMERIC_VALUES.}}
Without the fix, it produces:
{code:java}
LogicalProject(EXPR$0=[COMPARE_STRINGS_OR_NUMERIC_VALUES('1':VARCHAR, '1':VARCHAR)])
LogicalValues(tuples=[[{ 0 }]])
{code}
With the fix, it produces:
{code:java}
LogicalProject(EXPR$0=[COMPARE_STRINGS_OR_NUMERIC_VALUES(1, 1)])
LogicalValues(tuples=[[{ 0 }]])
{code}
[~mbudiu], you might also be interested in this fix, as I see you want to improve TypeCoercion.
> CompositeOperandTypeChecker should check operands without type coercion first
> -----------------------------------------------------------------------------
>
> Key: CALCITE-5532
> URL: https://issues.apache.org/jira/browse/CALCITE-5532
> Project: Calcite
> Issue Type: Bug
> Components: core
> Affects Versions: 1.33.0
> Reporter: Dmitry Sysolyatin
> Assignee: Dmitry Sysolyatin
> Priority: Major
> Labels: pull-request-available
> Time Spent: 10m
> Remaining Estimate: 0h
>
> If define an operator with the following type checker:
> {code}
> SqlSingleOperandTypeChecker operandTypeChecker = OperandTypes.or(
> OperandTypes.family(SqlTypeFamily.STRING, SqlTypeFamily.STRING)
> .and(OperandTypes.SAME_SAME),
> OperandTypes.family(SqlTypeFamily.INTEGER, SqlTypeFamily.INTEGER)
> .and(OperandTypes.SAME_SAME));
> {code}
> and pass two operands with INTEGER type to this type checker. Then they will be wrapped into CAST operator which will cast them to VARCHAR. But they shouldn't be casted to VARCHAR.
> Testcase:
> {code:java}
> @Test void testCompositeOperandTypeWithoutCast() {
> SqlValidator validator = SqlTestFactory.INSTANCE.createValidator();
> SqlSingleOperandTypeChecker operandTypeChecker = OperandTypes.or(
> OperandTypes.family(SqlTypeFamily.STRING, SqlTypeFamily.STRING)
> .and(OperandTypes.SAME_SAME),
> OperandTypes.family(SqlTypeFamily.INTEGER, SqlTypeFamily.INTEGER)
> .and(OperandTypes.SAME_SAME));
> SqlBinaryOperator op = new SqlBinaryOperator(
> "~",
> SqlKind.OTHER,
> 60,
> true,
> null,
> null,
> null);
> List<SqlLiteral> args = ImmutableList.of(
> SqlLiteral.createExactNumeric("20", SqlParserPos.ZERO),
> SqlLiteral.createExactNumeric("30", SqlParserPos.ZERO));
> SqlCallBinding binding = new SqlCallBinding(
> validator,
> new EmptyScope((SqlValidatorImpl) validator),
> new SqlBasicCall(op, args, SqlParserPos.ZERO));
> List<RelDataType> typesBeforeChecking =
> ImmutableList.of(binding.getOperandType(0), binding.getOperandType(1));
> operandTypeChecker.checkOperandTypes(binding, false);
> # It fails
> assertEquals(typesBeforeChecking.get(0), binding.getOperandType(0));
> assertEquals(typesBeforeChecking.get(1), binding.getOperandType(1));
> }
> {code}
>
--
This message was sent by Atlassian Jira
(v8.20.10#820010)