gianm commented on code in PR #20702:
URL: https://github.com/apache/druid/pull/20702#discussion_r4362375612


##########
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:
   Updated.



-- 
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]

Reply via email to