Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ default boolean isColonFieldAccessAllowed() {
* fields of T if T is a STRUCT type.
*
* <p>Among the built-in conformance levels, true in
* {@link SqlConformanceEnum#BIG_QUERY},
* {@link SqlConformanceEnum#PRESTO};
* false otherwise.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ public enum SqlConformanceEnum implements SqlConformance {

@Override public boolean allowAliasUnnestItems() {
switch (this) {
case BIG_QUERY:
case PRESTO:
return true;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1939,6 +1939,32 @@ public static void checkActualAndReferenceFiles() {
sql(sql).withConformance(SqlConformanceEnum.PRESTO).ok();
}

/**
* Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-7547">[CALCITE-7547]
* BIG_QUERY conformance should allow field access on
* UNNEST(array_of_struct) AS alias</a>.
*/
@Test void testAliasUnnestArrayPlanWithSingleColumnBigQuery() {
final String sql = "select d.deptno, employee.empno\n"
+ "from dept_nested_expanded as d,\n"
+ " UNNEST(d.employees) as t(employee)";
sql(sql).withConformance(SqlConformanceEnum.BIG_QUERY).ok();
}

/**
* Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-7547">[CALCITE-7547]
* BIG_QUERY conformance should allow field access on
* UNNEST(array_of_struct) AS alias</a>.
*/
@Test void testAliasUnnestArrayPlanWithDoubleColumnBigQuery() {
final String sql = "select d.deptno, e, k.empno\n"
+ "from dept_nested_expanded as d CROSS JOIN\n"
+ " UNNEST(d.admins, d.employees) as t(e, k)";
sql(sql).withConformance(SqlConformanceEnum.BIG_QUERY).ok();
}

@Test void testArrayOfRecord() {
sql("select employees[1].detail.skills[2+3].desc from dept_nested").ok();
}
Expand Down
31 changes: 31 additions & 0 deletions core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8816,6 +8816,37 @@ void testGroupExpressionEquivalenceParams() {
+ "\\('EMPNO', 'ENAME', 'DETAIL'\\), whereas alias list has 1 columns");
}

/**
* Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-7547">[CALCITE-7547]
* BIG_QUERY conformance should allow field access on
* UNNEST(array_of_struct) AS alias</a>.
*/
@Test void testAliasUnnestMultipleArraysBigQuery() {
// for accessing a field in STRUCT type unnested from array
sql("select e.ENAME\n"
+ "from dept_nested_expanded as d CROSS JOIN\n"
+ " UNNEST(d.employees) as t(e)")
.withConformance(SqlConformanceEnum.BIG_QUERY).columnType("VARCHAR(10) NOT NULL");

// for unnesting multiple arrays at the same time
sql("select d.deptno, e, k.empno, l.\"unit\", l.\"X\" * l.\"Y\"\n"
+ "from dept_nested_expanded as d CROSS JOIN\n"
+ " UNNEST(d.admins, d.employees, d.offices) as t(e, k, l)")
.withConformance(SqlConformanceEnum.BIG_QUERY).ok();

// Make sure validation fails properly given illegal select items
sql("select d.deptno, ^e^.some_column, k.empno\n"
+ "from dept_nested_expanded as d CROSS JOIN\n"
+ " UNNEST(d.admins, d.employees) as t(e, k)")
.withConformance(SqlConformanceEnum.BIG_QUERY)
.fails("Table 'E' not found");
sql("select d.deptno, e.detail, ^unknown^.detail\n"
+ "from dept_nested_expanded as d CROSS JOIN\n"
+ " UNNEST(d.employees) as t(e)")
.withConformance(SqlConformanceEnum.BIG_QUERY).fails("Incompatible types");
}

@Test void testUnnestArray() {
sql("select*from unnest(array[1])")
.columnType("INTEGER NOT NULL");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,23 @@ LogicalProject(DEPTNO=[$0], E=[$5], EMPNO=[$6.EMPNO])
</Resource>
<Resource name="sql">
<![CDATA[select d.deptno, e, k.empno
from dept_nested_expanded as d CROSS JOIN
UNNEST(d.admins, d.employees) as t(e, k)]]>
</Resource>
</TestCase>
<TestCase name="testAliasUnnestArrayPlanWithDoubleColumnBigQuery">
<Resource name="plan">
<![CDATA[
LogicalProject(DEPTNO=[$0], E=[$5], EMPNO=[$6.EMPNO])
LogicalCorrelate(correlation=[$cor0], joinType=[inner], requiredColumns=[{2, 3}])
LogicalTableScan(table=[[CATALOG, SALES, DEPT_NESTED_EXPANDED]])
Uncollect
LogicalProject(ADMINS=[$cor0.ADMINS], EMPLOYEES=[$cor0.EMPLOYEES])
LogicalValues(tuples=[[{ 0 }]])
]]>
</Resource>
<Resource name="sql">
<![CDATA[select d.deptno, e, k.empno
from dept_nested_expanded as d CROSS JOIN
UNNEST(d.admins, d.employees) as t(e, k)]]>
</Resource>
Expand All @@ -358,6 +375,23 @@ LogicalProject(DEPTNO=[$0], EMPNO=[$5.EMPNO])
</Resource>
<Resource name="sql">
<![CDATA[select d.deptno, employee.empno
from dept_nested_expanded as d,
UNNEST(d.employees) as t(employee)]]>
</Resource>
</TestCase>
<TestCase name="testAliasUnnestArrayPlanWithSingleColumnBigQuery">
<Resource name="plan">
<![CDATA[
LogicalProject(DEPTNO=[$0], EMPNO=[$5.EMPNO])
LogicalCorrelate(correlation=[$cor0], joinType=[inner], requiredColumns=[{2}])
LogicalTableScan(table=[[CATALOG, SALES, DEPT_NESTED_EXPANDED]])
Uncollect
LogicalProject(EMPLOYEES=[$cor0.EMPLOYEES])
LogicalValues(tuples=[[{ 0 }]])
]]>
</Resource>
<Resource name="sql">
<![CDATA[select d.deptno, employee.empno
from dept_nested_expanded as d,
UNNEST(d.employees) as t(employee)]]>
</Resource>
Expand Down
Loading