Skip to content
Merged
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 @@ -713,9 +713,19 @@ public SqlNode toSql(@Nullable RexProgram program, RexNode rex) {
break;
case ROW:
case ITEM:
final SqlNode expr = toSql(program, referencedExpr);
sqlIdentifier = new SqlIdentifier(expr.toString(), POS);
break;
// The referenced expression (e.g. an array/map ITEM access) must be
// unparsed as its own SqlNode so that the target dialect quotes each
// part correctly. Combine it with the accessed field names using the
// DOT operator instead of collapsing everything into a single
// identifier name (which would get quoted as one token).
SqlNode dotNode = toSql(program, referencedExpr);
RexFieldAccess dotAccess;
while ((dotAccess = accesses.pollLast()) != null) {
dotNode =
SqlStdOperatorTable.DOT.createCall(POS, dotNode,
new SqlIdentifier(dotAccess.getField().getName(), POS));
}
return dotNode;
default:
sqlIdentifier = (SqlIdentifier) toSql(program, referencedExpr);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2998,14 +2998,26 @@ private SqlDialect nonOrdinalDialect() {
* SqlItemOperator fails in RelToSqlConverter</a>. */
@Test void testSqlItemOperator() {
sql("SELECT foo[0].\"EXPR$1\" FROM (SELECT ARRAY[ROW('a', 'b')] AS foo)")
.ok("SELECT \"ARRAY[ROW('a', 'b')][0]\".\"EXPR$1\"\n"

@xuzifu666 xuzifu666 Jul 1, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

SQL statements like this will cause an error in PostgreSQL, we can the last sql in https://onecompiler.com/postgresql/44tvwz86e

.ok("SELECT ARRAY[ROW('a', 'b')][0].\"EXPR$1\"\n"
+ "FROM (VALUES (0)) AS \"t\" (\"ZERO\")");
sql("SELECT foo['k'].\"EXPR$1\" FROM (SELECT MAP['k', ROW('a', 'b')] AS foo)")
.ok("SELECT \"MAP['k', ROW('a', 'b')]['k']\".\"EXPR$1\"\n"
.ok("SELECT MAP['k', ROW('a', 'b')]['k'].\"EXPR$1\"\n"
+ "FROM (VALUES (0)) AS \"t\" (\"ZERO\")");
sql("select\"books\"[0].\"title\" from \"authors\"")
.schema(CalciteAssert.SchemaSpec.BOOKSTORE)
.ok("SELECT \"`books`[0]\".\"title\"\n"
.ok("SELECT \"books\"[0].\"title\"\n"
+ "FROM \"bookstore\".\"authors\"");
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6344">[CALCITE-6344]
* RelToSqlConverter invalid quotation for arrays and item operator
* (ansi dialect)</a>. */
@Test void testSqlItemOperator2() {
sql("SELECT \"books\"[0].\"title\" from \"bookstore\".\"authors\"")
.schema(CalciteAssert.SchemaSpec.BOOKSTORE)
.withPostgresql()
.ok("SELECT \"books\"[0].\"title\"\n"
+ "FROM \"bookstore\".\"authors\"");
}

Expand Down
Loading