FrankChen021 commented on code in PR #20702:
URL: https://github.com/apache/druid/pull/20702#discussion_r4359554578
##########
sql/src/main/java/org/apache/druid/sql/calcite/planner/DruidSqlValidator.java:
##########
@@ -824,7 +826,46 @@ protected SqlNode performUnconditionalRewrites(SqlNode
node, final boolean under
}
}
- return super.performUnconditionalRewrites(node, underFrom);
+ if (node instanceof SqlSelect) {
+ aliasSelectItems((SqlSelect) node);
+ }
+
+ final SqlNode rewritten = super.performUnconditionalRewrites(node,
underFrom);
+
+ // Rewrite "x::TYPE" to standard CAST.
+ if (rewritten instanceof SqlCall cast
+ && SqlLibraryOperators.INFIX_CAST.equals(((SqlCall)
rewritten).getOperator())) {
+ return SqlStdOperatorTable.CAST.createCall(cast.getParserPosition(),
cast.getOperandList());
+ }
+
+ return rewritten;
+ }
+
+ /**
+ * Wraps select items in "AS" when {@link #deriveSelectItemAlias} has a
better name for them. This must run
+ * before other rewrites, since it looks at the syntax the user wrote.
+ */
+ private static void aliasSelectItems(final SqlSelect select)
+ {
+ final SqlNodeList selectList = select.getSelectList();
+ final List<String> aliases = new ArrayList<>(selectList.size());
+ final List<String> names = new ArrayList<>(selectList.size());
+ for (final SqlNode item : selectList) {
+ if (item instanceof SqlIdentifier && ((SqlIdentifier) item).isStar()) {
+ // Keep all defaults, since we can't tell which names the star expands
to.
+ return;
+ }
+ final String alias = deriveSelectItemAlias(item);
+ aliases.add(alias);
+ names.add(alias != null ? alias : SqlValidatorUtil.alias(item));
Review Comment:
[P2] Account for generated aliases in collision detection
**Finding:** `aliasSelectItems` uses the ordinal-less
`SqlValidatorUtil.alias(item)`, which returns null for literals and other
expressions even though Calcite later assigns them `EXPR$<ordinal>`. For
example, `SELECT 'x', EXPR$0::BIGINT FROM (SELECT 1 AS EXPR$0)` causes both
projected values to use `EXPR$0`; their differing STRING/LONG types can make
Druid row-signature construction fail, violating the documented duplicate-name
fallback.
**Suggestion:** Use ordinal-aware alias derivation, or otherwise reserve
Calcite's generated `EXPR$N` names, when building the collision set.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]