Skip to content

Commit

Permalink
[CALCITE-6413] SqlValidator does not invoke TypeCoercionImpl::binaryC…
Browse files Browse the repository at this point in the history
…omparisonCoercion for both NATURAL and USING join conditions

Signed-off-by: Mihai Budiu <[email protected]>
  • Loading branch information
mihaibudiu committed Jan 8, 2025
1 parent 71131ab commit 936acd7
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3433,7 +3433,8 @@ private RexNode convertUsing(SqlValidatorNamespace leftNamespace,
final SqlNameMatcher nameMatcher = catalogReader.nameMatcher();
final List<RexNode> list = new ArrayList<>();
for (String name : nameList) {
List<RexNode> operands = new ArrayList<>();
List<RexNode> operands = new ArrayList<>(2);
List<RelDataType> fieldTypes = new ArrayList<>(2);
int offset = 0;
for (SqlValidatorNamespace n : ImmutableList.of(leftNamespace,
rightNamespace)) {
Expand All @@ -3443,12 +3444,32 @@ private RexNode convertUsing(SqlValidatorNamespace leftNamespace,
throw new AssertionError("field " + name + " is not found in "
+ rowType + " with " + nameMatcher);
}

fieldTypes.add(field.getType());
operands.add(
rexBuilder.makeInputRef(field.getType(),
offset + field.getIndex()));
offset += rowType.getFieldList().size();
}
list.add(rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, operands));

RelDataType resultType =
validator().getTypeCoercion().commonTypeForBinaryComparison(
fieldTypes.get(0), fieldTypes.get(1));
if (resultType == null) {
throw new IllegalArgumentException("Cannot compute compatible row type: " + fieldTypes);
}

List<RexNode> castedOperands = new ArrayList<>();
for (int i = 0; i < operands.size(); i++) {
RexNode operand = operands.get(i);
RelDataType fieldType = fieldTypes.get(i);
RexNode expr = operand;
if (!fieldType.equals(resultType)) {
expr = rexBuilder.makeCast(resultType, operand, true, false);
}
castedOperands.add(expr);
}
list.add(rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, castedOperands));
}
return RexUtil.composeConjunction(rexBuilder, list);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4897,6 +4897,15 @@ void checkUserDefinedOrderByOver(NullCollation nullCollation) {
sql(sql).ok();
}

/** Test case for <a href="https://issues.apache.org/jira/browse/CALCITE-6413">[CALCITE-6413]
* SqlValidator does not invoke TypeCoercionImpl::binaryComparisonCoercion for both NATURAL
* and USING join conditions</a>. */
@Test void testNaturalJoinCast() {
final String sql = "WITH t1(x) AS (VALUES('x')), t2(x) AS (VALUES(0.0))\n"
+ "SELECT * FROM t1 NATURAL JOIN t2";
sql(sql).ok();
}

/** Tests LEFT JOIN LATERAL with multiple columns from outer. */
@Test void testLeftJoinLateral4() {
final String sql = "select * from (values (4,5)) as t(c,d)\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3641,9 +3641,9 @@ NATURAL JOIN (SELECT deptno, name AS ename FROM dept) AS d]]>
<Resource name="plan">
<![CDATA[
LogicalProject(ENAME=[$1], DEPTNO=[$7], EMPNO=[$0], JOB=[$2], MGR=[$3], HIREDATE=[$4], SAL=[$5], COMM=[$6], SLACKER=[$8])
LogicalJoin(condition=[AND(=($1, $10), =($7, $9))], joinType=[inner])
LogicalJoin(condition=[AND(=($1, $11), =($7, $9))], joinType=[inner])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
LogicalProject(DEPTNO=[$0], ENAME=[$1])
LogicalProject(DEPTNO=[$0], ENAME=[$1], NAME=[CAST($1):VARCHAR(20) NOT NULL])
LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
]]>
</Resource>
Expand Down Expand Up @@ -3982,11 +3982,12 @@ JOIN (values ('Calcite', 200)) as s(ename, salary) using (ename)]]>
<Resource name="plan">
<![CDATA[
LogicalProject(DEPTNO=[$7], ENAME=[$1])
LogicalJoin(condition=[=($1, $11)], joinType=[inner])
LogicalJoin(condition=[=($1, $13)], joinType=[inner])
LogicalJoin(condition=[=($7, $9)], joinType=[inner])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
LogicalValues(tuples=[[{ 'Calcite', 200 }]])
LogicalProject(ENAME=[$0], SALARY=[$1], ENAME0=[CAST($0):VARCHAR(20) NOT NULL])
LogicalValues(tuples=[[{ 'Calcite', 200 }]])
]]>
</Resource>
</TestCase>
Expand All @@ -4013,11 +4014,11 @@ using (coord)]]>
</Resource>
<Resource name="plan">
<![CDATA[
LogicalProject(EXPR$0=[COALESCE(ROW($4, $5, $6), ROW($14, $15, $16)).X])
LogicalJoin(condition=[AND(AND(=($4, $14), =($5, $15)), =($6, $16))], joinType=[inner])
LogicalProject(CONTACTNO=[$0], FNAME=[$1], LNAME=[$2], EMAIL=[$3], X=[$4.X], Y=[$4.Y], unit=[$4.unit], M=[$5.M], A=[$5.SUB.A], B=[$5.SUB.B])
LogicalProject(EXPR$0=[COALESCE(ROW($4, $5, $6), ROW($17, $18, $19)).X])
LogicalJoin(condition=[AND(AND(=($10, $23), =($11, $24)), =($12, $25))], joinType=[inner])
LogicalProject(CONTACTNO=[$0], FNAME=[$1], LNAME=[$2], EMAIL=[$3], COORD=[$4.X], COORD5=[$4.Y], COORD6=[$4.unit], COORD_NE=[$5.M], COORD_NE8=[$5.SUB.A], COORD_NE9=[$5.SUB.B], COORD0$$0=[$4.X], COORD0$$011=[$4.Y], COORD0$$012=[$4.unit])
LogicalTableScan(table=[[CATALOG, CUSTOMER, CONTACT_PEEK]])
LogicalProject(CONTACTNO=[$0], FNAME=[$1], LNAME=[$2], EMAIL=[$3], X=[$4.X], Y=[$4.Y], unit=[$4.unit], M=[$5.M], A=[$5.SUB.A], B=[$5.SUB.B])
LogicalProject(CONTACTNO=[$0], FNAME=[$1], LNAME=[$2], EMAIL=[$3], COORD=[$4.X], COORD5=[$4.Y], COORD6=[$4.unit], COORD_NE=[$5.M], COORD_NE8=[$5.SUB.A], COORD_NE9=[$5.SUB.B], COORD0$$0=[$4.X], COORD0$$011=[$4.Y], COORD0$$012=[$4.unit])
LogicalTableScan(table=[[CATALOG, CUSTOMER, CONTACT_PEEK]])
]]>
</Resource>
Expand Down Expand Up @@ -5062,6 +5063,22 @@ LogicalProject(EXPR$0=[$1])
<![CDATA[select multiset(select deptno from dept) from (values(true))]]>
</Resource>
</TestCase>
<TestCase name="testNaturalJoinCast">
<Resource name="sql">
<![CDATA[WITH t1(x) AS (VALUES('x')), t2(x) AS (VALUES(0.0))
SELECT * FROM t1 NATURAL JOIN t2]]>
</Resource>
<Resource name="plan">
<![CDATA[
LogicalProject(X=[CAST($0):DECIMAL(2, 1) NOT NULL])
LogicalJoin(condition=[=($1, $3)], joinType=[inner])
LogicalProject(EXPR$0=[$0], EXPR$00=[CAST($0):DECIMAL(19, 9) NOT NULL])
LogicalValues(tuples=[[{ 'x' }]])
LogicalProject(EXPR$0=[$0], EXPR$00=[CAST($0):DECIMAL(19, 9) NOT NULL])
LogicalValues(tuples=[[{ 0.0 }]])
]]>
</Resource>
</TestCase>
<TestCase name="testNestedAggregates">
<Resource name="sql">
<![CDATA[SELECT
Expand Down

0 comments on commit 936acd7

Please sign in to comment.