You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by GitBox <gi...@apache.org> on 2022/04/19 04:22:29 UTC

[GitHub] [druid] gianm commented on a diff in pull request #12431: Validate select columns for insert statement

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


##########
sql/src/main/java/org/apache/druid/sql/calcite/planner/DruidPlanner.java:
##########
@@ -838,6 +843,31 @@ static ParsedNodes create(final SqlNode node) throws ValidationException
       return new ParsedNodes(explain, druidSqlInsert, query, ingestionGranularity);
     }
 
+    private static void validateSelectColumnForIngestion(SqlNode sqlNode) throws ValidationException
+    {
+      if (sqlNode instanceof SqlBasicCall) {
+        // If the sql node is a basic call, it could have select statements as operands.
+        // We need to recursivly check the operands for select statements.
+        SqlBasicCall sqlBasicCall = (SqlBasicCall) sqlNode;
+        for (SqlNode operand : sqlBasicCall.getOperandList()) {
+          validateSelectColumnForIngestion(operand);
+        }
+      } else if (sqlNode instanceof SqlSelect) {
+        // Validate that all columns which do some computation in the select statement are named.
+        // If the column is named, the top level operator should be "AS".
+        for (SqlNode column : ((SqlSelect) sqlNode).getSelectList().getList()) {
+          if (column instanceof SqlBasicCall) {
+            SqlBasicCall basicCall = (SqlBasicCall) column;
+            if (!"AS".equalsIgnoreCase(basicCall.getOperator().getName())) {

Review Comment:
   Will this work if the alias is done without AS? Like `func(x) mycolumn`.



##########
sql/src/main/java/org/apache/druid/sql/calcite/planner/DruidPlanner.java:
##########
@@ -838,6 +843,31 @@ static ParsedNodes create(final SqlNode node) throws ValidationException
       return new ParsedNodes(explain, druidSqlInsert, query, ingestionGranularity);
     }
 
+    private static void validateSelectColumnForIngestion(SqlNode sqlNode) throws ValidationException
+    {
+      if (sqlNode instanceof SqlBasicCall) {
+        // If the sql node is a basic call, it could have select statements as operands.
+        // We need to recursivly check the operands for select statements.
+        SqlBasicCall sqlBasicCall = (SqlBasicCall) sqlNode;
+        for (SqlNode operand : sqlBasicCall.getOperandList()) {
+          validateSelectColumnForIngestion(operand);
+        }
+      } else if (sqlNode instanceof SqlSelect) {
+        // Validate that all columns which do some computation in the select statement are named.

Review Comment:
   What would happen with a query like this?
   
   ```
   INSERT INTO tbl
   SELECT TIME_PARSE(timestamp) AS __time, *
   FROM (SELECT timestamp, LOWER(x) FROM tbl)
   PARTITIONED BY DAY
   ```
   
   This query should be flagged as invalid. But I'm wondering if the structure would trick the validator, since it looks like the `*` is fine, but it's really going to get a funky name from `LOWER(x)`.



##########
sql/src/test/java/org/apache/druid/sql/calcite/CalciteInsertDmlTest.java:
##########
@@ -380,12 +380,12 @@ public void testInsertWithClusteredBy()
                                                   .add("__time", ColumnType.LONG)
                                                   .add("floor_m1", ColumnType.FLOAT)
                                                   .add("dim1", ColumnType.STRING)
-                                                  .add("EXPR$3", ColumnType.DOUBLE)
+                                                  .add("ciel_m2", ColumnType.DOUBLE)

Review Comment:
   `ceil_m2` (spelling)



-- 
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: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org