Skip to content

Commit

Permalink
[CALCITE-5134] Queries with subquery inside select list does not work…
Browse files Browse the repository at this point in the history
… if subquery uses table from left join
  • Loading branch information
libenchao authored and rubenada committed Jun 17, 2022
1 parent e64a21e commit de41df4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
18 changes: 18 additions & 0 deletions core/src/main/java/org/apache/calcite/sql/validate/ListScope.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,24 @@ public List<SqlValidatorNamespace> getChildren() {
return Util.transform(children, scopeChild -> scopeChild.name);
}

/**
* Whether the ith child namespace produces nullable result.
*
* For example, in below query,
* <pre>
* SELECT *
* FROM EMPS
* LEFT OUTER JOIN DEPT
* </pre>
* the namespace which corresponding to 'DEPT' is nullable.
*
* @param i The child index.
* @return Whether it's nullable.
*/
public boolean isChildNullable(int i) {
return children.get(i).nullable;
}

private @Nullable ScopeChild findChild(List<String> names,
SqlNameMatcher nameMatcher) {
for (ScopeChild child : children) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4911,7 +4911,13 @@ void setRoot(List<RelNode> inputs) {
int i = 0;
int offset = 0;
for (SqlValidatorNamespace c : ancestorScope1.getChildren()) {
builder.addAll(c.getRowType().getFieldList());
if (ancestorScope1.isChildNullable(i)) {
for (final RelDataTypeField f : c.getRowType().getFieldList()) {
builder.add(f.getName(), typeFactory.createTypeWithNullability(f.getType(), true));
}
} else {
builder.addAll(c.getRowType().getFieldList());
}
if (i == resolve.path.steps().get(0).i) {
for (RelDataTypeField field : c.getRowType().getFieldList()) {
fields.put(field.getName(), field.getIndex() + offset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2639,6 +2639,14 @@ void checkCorrelatedMapSubQuery(boolean expand) {
sql(sql).withExpand(false).ok();
}

@Test void testCorrelatedForOuterFields() {
final String sql = "SELECT ARRAY(SELECT dept.deptno)\n"
+ "FROM emp\n"
+ "LEFT OUTER JOIN dept\n"
+ "ON emp.empno = dept.deptno";
sql(sql).ok();
}

/**
* Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-614">[CALCITE-614]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,26 @@ LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], SAL=[$
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
})])
LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
]]>
</Resource>
</TestCase>
<TestCase name="testCorrelatedForOuterFields">
<Resource name="sql">
<![CDATA[SELECT ARRAY(SELECT dept.deptno)
FROM emp
LEFT OUTER JOIN dept
ON emp.empno = dept.deptno]]>
</Resource>
<Resource name="plan">
<![CDATA[
LogicalProject(EXPR$0=[$11])
LogicalCorrelate(correlation=[$cor0], joinType=[inner], requiredColumns=[{9}])
LogicalJoin(condition=[=($0, $9)], joinType=[left])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
Collect(field=[EXPR$0])
LogicalProject(DEPTNO=[$cor0.DEPTNO0])
LogicalValues(tuples=[[{ 0 }]])
]]>
</Resource>
</TestCase>
Expand Down

0 comments on commit de41df4

Please sign in to comment.