Skip to content

Commit

Permalink
Backport [CALCITE-4145]: Select nested field from aliased subquery (#86)
Browse files Browse the repository at this point in the history
* Backport [CALCITE-4145] select nested field from subquery

* modify UT
  • Loading branch information
aastha25 authored and rzhang10 committed Apr 12, 2023
1 parent 2973d0e commit e724e64
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,10 @@ public static boolean equalAsStructSansNullability(
* Returns the ordinal of a given field in a record type, or -1 if the field
* is not found.
*
* <p>The {@code fieldName} is always simple, if the field is nested within a record field,
* returns index of the outer field instead. i.g. for row type
* (a int, b (b1 bigint, b2 varchar(20) not null)), returns 1 for both simple name "b1" and "b2".
*
* @param type Record type
* @param fieldName Name of field
* @return Ordinal of field
Expand All @@ -1220,6 +1224,10 @@ public static int findField(RelDataType type, String fieldName) {
if (field.getName().equals(fieldName)) {
return i;
}
final RelDataType fieldType = field.getType();
if (fieldType.isStruct() && findField(fieldType, fieldName) != -1) {
return i;
}
}
return -1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3554,6 +3554,17 @@ private Tester getExtendedTester() {
sql(sql).ok();
}

/**
* Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-4145">[CALCITE-4145]
* Exception when nested field queried from subquery</a>.
*/
@Test public void testSelectNestedFromSubquery() {
final String sql = "select tmp.nested.\"EXPR$0\" as id from ("
+ " select (1, 2) as nested) tmp";
sql(sql).ok();
}

/**
* Visitor that checks that every {@link RelNode} in a tree is valid.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6269,6 +6269,18 @@ from emp window w as (order by empno)]]>
<![CDATA[
LogicalProject(EXPR$0=[IGNORE NULLS(LEAD($5, 4))], EXPR$1=[LEAD($5, 4) OVER (ORDER BY $0 RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
</TestCase>
<TestCase name="testSelectNestedFromSubquery">
<Resource name="sql">
<![CDATA[SELECT tmp.nested."EXPR$0" AS if FROM
(SELECT (1, 2) AS nested) tmp]]>
</Resource>
<Resource name="plan">
<![CDATA[
LogicalProject(ID=[ROW(1, 2).EXPR$0])
LogicalValues(tuples=[[{ 0 }]])
]]>
</Resource>
</TestCase>
Expand Down

0 comments on commit e724e64

Please sign in to comment.