Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -329,6 +329,9 @@ protected void addColumnNames(
final ResolvedImpl resolved = new ResolvedImpl();
int size = identifier.names.size();
int i = size - 1;
// Snapshot: resolution below may rewrite identifier's names in place [CALCITE-7614]
final SqlIdentifier identifierSnapshot =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would call this "originalIdentifier"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

(SqlIdentifier) identifier.clone(identifier.getParserPosition());
for (; i > 0; i--) {
final SqlIdentifier prefix = identifier.getComponent(0, i);
resolved.clear();
Expand Down Expand Up @@ -388,7 +391,9 @@ protected void addColumnNames(
fromNs = resolve.namespace;
fromPath = resolve.path;
fromRowType = resolve.rowType();
identifier = identifier
// Qualify the original (pre-resolution) names; see the comment
// on identifierSnapshot above ([CALCITE-7614]).
identifier = identifierSnapshot
.setName(0, columnName)
.add(0, tableName2, SqlParserPos.ZERO);
++i;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class SqlAdvisorTest extends SqlValidatorTestCase {
"TABLE(CATALOG.SALES.DOUBLE_PK)",
"TABLE(CATALOG.SALES.DEPT_NESTED)",
"TABLE(CATALOG.SALES.DEPT_NESTED_EXPANDED)",
"TABLE(CATALOG.SALES.DEPT_NESTED_PEEK)",
"TABLE(CATALOG.SALES.BONUS)",
"TABLE(CATALOG.SALES.ORDERS)",
"TABLE(CATALOG.SALES.SALGRADE)",
Expand Down
15 changes: 15 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 @@ -9339,6 +9339,21 @@ void testGroupExpressionEquivalenceParams() {
sql(sql3).fails("Table 'D' not found");
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-7614">[CALCITE-7614]
* UNNEST of an array field in a PEEK_FIELDS struct column fails when the table
* is not qualified</a>. */
@Test void testUnnestPeekFieldsArrayColumn() {
// Table-qualified form already works.
sql("select * from dept_nested_peek as r CROSS JOIN UNNEST(r.s.arr) as x").ok();
// Unqualified form must work too (used to fail with
// "Column 'S.S' not found in table 'DEPT_NESTED_PEEK'").
sql("select * from dept_nested_peek CROSS JOIN UNNEST(s.arr) as x").ok();
// Comma-join variants, for parity with testUnnestArrayColumn.
sql("select * from dept_nested_peek as r, UNNEST(r.s.arr) as x").ok();
sql("select * from dept_nested_peek, UNNEST(s.arr) as x").ok();
}

@Test void testUnnestWithOrdinality() {
sql("select*from unnest(array[1, 2]) with ordinality")
.type("RecordType(INTEGER NOT NULL EXPR$0, INTEGER NOT NULL ORDINALITY) NOT NULL");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ final class Fixture extends AbstractFixture {
final RelDataType varchar5ArrayType = array(varchar5Type);
final RelDataType intArrayArrayType = array(intArrayType);
final RelDataType varchar5ArrayArrayType = array(varchar5ArrayType);
// A "peek" struct that contains an array field, e.g. Row(ARR varchar(5) array)
// with StructKind.PEEK_FIELDS so that "ARR" can be referenced without the
// struct-column prefix.
final RelDataType peekArrayType = typeFactory.builder()
.add("ARR", varchar5ArrayType)
.kind(StructKind.PEEK_FIELDS)
.build();
final RelDataType intMultisetType = typeFactory.createMultisetType(intType, -1);
final RelDataType varchar5MultisetType = typeFactory.createMultisetType(varchar5Type, -1);
final RelDataType intMultisetArrayType = array(intMultisetType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ private void registerTableDeptNestedExpanded(MockSchema salesSchema, Fixture fix
registerTable(deptNestedExpandedTable);
}

private void registerTableDeptNestedPeek(MockSchema salesSchema, Fixture fixture) {
MockTable deptNestedPeekTable =
MockTable.create(this, salesSchema, "DEPT_NESTED_PEEK", false, 4);
deptNestedPeekTable.addColumn("DEPTNO", fixture.intType, true);
deptNestedPeekTable.addColumn("S", fixture.peekArrayType);
registerTable(deptNestedPeekTable);
}

private void registerTableBonus(MockSchema salesSchema, Fixture fixture) {
MockTable bonusTable =
MockTable.create(this, salesSchema, "BONUS", false, 0);
Expand Down Expand Up @@ -512,6 +520,10 @@ private void registerTableDoublePK(MockSchema salesSchema, Fixture fixture) {
// Register "DEPT_NESTED_EXPANDED" table.
registerTableDeptNestedExpanded(salesSchema, fixture);

// Register "DEPT_NESTED_PEEK" table, which has a PEEK_FIELDS struct column
// "S" containing an array field "ARR".
registerTableDeptNestedPeek(salesSchema, fixture);

// Register "BONUS" table.
registerTableBonus(salesSchema, fixture);

Expand Down
Loading