clintropolis commented on code in PR #17894:
URL: https://github.com/apache/druid/pull/17894#discussion_r2053420690


##########
sql/src/main/java/org/apache/druid/sql/calcite/planner/DruidPlanner.java:
##########
@@ -283,6 +292,49 @@ public void close()
     planner.close();
   }
 
+  private SqlNode processStatementList(SqlNode root, String sql)
+  {
+    if (root instanceof SqlNodeList) {
+      final SqlNodeList nodeList = (SqlNodeList) root;
+      if (!allowSetStatementsToBuildContext && nodeList.size() > 1) {
+        throw InvalidSqlInput.exception("Multiple statements detected in SQL 
string[%s], but only a single statement is supported",
+                                        sql
+        );
+      }
+      final Map<String, Object> contextMap = new LinkedHashMap<>();
+      boolean isMissingDruidStatementNode = true;
+      // convert 0 or more SET statements into a Map of stuff to add to the 
query context
+      for (int i = 0; i < nodeList.size(); i++) {
+        SqlNode sqlNode = nodeList.get(i);
+        if (sqlNode instanceof SqlSetOption) {
+          final SqlSetOption sqlSetOption = (SqlSetOption) sqlNode;
+          if (!(sqlSetOption.getValue() instanceof SqlLiteral)) {
+            throw InvalidSqlInput.exception("Invalid sql SET statement[%s], 
value must be a literal", sqlSetOption);

Review Comment:
   switched to use toSqlString - I think its ok with the whole statement, but 
can switch to just identifier if you think it would be better. this and all 
other error messages can be seen in tests 
https://github.com/apache/druid/pull/17894/files#diff-c8b6119838f7153eb393d6c42f5be66179b47a2819494e7319afd962f4d2b333R15933



##########
sql/src/main/java/org/apache/druid/sql/calcite/planner/DruidPlanner.java:
##########
@@ -283,6 +292,49 @@ public void close()
     planner.close();
   }
 
+  private SqlNode processStatementList(SqlNode root, String sql)
+  {
+    if (root instanceof SqlNodeList) {
+      final SqlNodeList nodeList = (SqlNodeList) root;
+      if (!allowSetStatementsToBuildContext && nodeList.size() > 1) {
+        throw InvalidSqlInput.exception("Multiple statements detected in SQL 
string[%s], but only a single statement is supported",
+                                        sql
+        );
+      }
+      final Map<String, Object> contextMap = new LinkedHashMap<>();
+      boolean isMissingDruidStatementNode = true;
+      // convert 0 or more SET statements into a Map of stuff to add to the 
query context
+      for (int i = 0; i < nodeList.size(); i++) {
+        SqlNode sqlNode = nodeList.get(i);
+        if (sqlNode instanceof SqlSetOption) {
+          final SqlSetOption sqlSetOption = (SqlSetOption) sqlNode;
+          if (!(sqlSetOption.getValue() instanceof SqlLiteral)) {
+            throw InvalidSqlInput.exception("Invalid sql SET statement[%s], 
value must be a literal", sqlSetOption);
+          }
+          final SqlLiteral value = (SqlLiteral) sqlSetOption.getValue();
+          contextMap.put(sqlSetOption.getName().getSimple(), 
SqlResults.coerce(plannerContext.getJsonMapper(), 
SqlResults.Context.fromPlannerContext(plannerContext), value.getValue(), 
value.getTypeName(), "set"));
+        } else if (i < nodeList.size() - 1) {
+          // only SET statements can appear before the last statement
+          throw InvalidSqlInput.exception("Invalid sql statement list[%s] - 
only SET statements are permitted before the final statement",

Review Comment:
   done



##########
sql/src/main/java/org/apache/druid/sql/calcite/planner/DruidPlanner.java:
##########
@@ -283,6 +292,49 @@ public void close()
     planner.close();
   }
 
+  private SqlNode processStatementList(SqlNode root, String sql)
+  {
+    if (root instanceof SqlNodeList) {
+      final SqlNodeList nodeList = (SqlNodeList) root;
+      if (!allowSetStatementsToBuildContext && nodeList.size() > 1) {
+        throw InvalidSqlInput.exception("Multiple statements detected in SQL 
string[%s], but only a single statement is supported",
+                                        sql
+        );
+      }
+      final Map<String, Object> contextMap = new LinkedHashMap<>();
+      boolean isMissingDruidStatementNode = true;
+      // convert 0 or more SET statements into a Map of stuff to add to the 
query context
+      for (int i = 0; i < nodeList.size(); i++) {
+        SqlNode sqlNode = nodeList.get(i);
+        if (sqlNode instanceof SqlSetOption) {
+          final SqlSetOption sqlSetOption = (SqlSetOption) sqlNode;
+          if (!(sqlSetOption.getValue() instanceof SqlLiteral)) {
+            throw InvalidSqlInput.exception("Invalid sql SET statement[%s], 
value must be a literal", sqlSetOption);
+          }
+          final SqlLiteral value = (SqlLiteral) sqlSetOption.getValue();
+          contextMap.put(sqlSetOption.getName().getSimple(), 
SqlResults.coerce(plannerContext.getJsonMapper(), 
SqlResults.Context.fromPlannerContext(plannerContext), value.getValue(), 
value.getTypeName(), "set"));
+        } else if (i < nodeList.size() - 1) {
+          // only SET statements can appear before the last statement
+          throw InvalidSqlInput.exception("Invalid sql statement list[%s] - 
only SET statements are permitted before the final statement",
+                                          sql
+          );
+        } else {
+          // last SqlNode
+          root = sqlNode;
+          isMissingDruidStatementNode = false;
+        }
+      }
+      if (isMissingDruidStatementNode) {
+        throw InvalidSqlInput.exception(
+            "Invalid sql statement list[%s] - statement list must end with a 
statement that is not a SET",

Review Comment:
   done



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