siddharthteotia commented on code in PR #9017:
URL: https://github.com/apache/pinot/pull/9017#discussion_r919124443


##########
pinot-common/src/main/java/org/apache/pinot/sql/parsers/CalciteSqlParser.java:
##########
@@ -105,66 +106,81 @@ private static String removeTerminatingSemicolon(String 
sql) {
   }
 
   public static SqlNodeAndOptions compileToSqlNodeAndOptions(String sql)
-      throws Exception {
+      throws SqlCompilationException {
     // Remove the comments from the query
     sql = removeComments(sql);
 
     // Remove the terminating semicolon from the query
     sql = removeTerminatingSemicolon(sql);
 
-    // Extract OPTION statements from sql as Calcite Parser doesn't parse it.
+    // extract and remove OPTIONS string
     List<String> options = extractOptionsFromSql(sql);
     if (!options.isEmpty()) {
       sql = removeOptionsFromSql(sql);
     }
 
     try (StringReader inStream = new StringReader(sql)) {
       SqlParserImpl sqlParser = newSqlParser(inStream);
-      return new SqlNodeAndOptions(sqlParser.parseSqlStmtEof(), options);
+      SqlNodeList sqlNodeList = sqlParser.SqlStmtsEof();
+      // Extract OPTION statements from sql.
+      SqlNodeAndOptions sqlNodeAndOptions = 
extractSqlNodeAndOptions(sqlNodeList);
+      // add legacy OPTIONS keyword-based options
+      if (options.size() > 0) {
+        sqlNodeAndOptions.setExtraOptions(extractOptionsMap(options));
+      }
+      return sqlNodeAndOptions;
     } catch (Throwable e) {
       throw new SqlCompilationException("Caught exception while parsing query: 
" + sql, e);
     }
   }
 
-  public static PinotSqlType extractSqlType(SqlNode sqlNode) {
-    switch (sqlNode.getKind()) {
-      case OTHER_DDL:
-        if (sqlNode instanceof SqlInsertFromFile) {
-          return PinotSqlType.DML;
+  public static SqlNodeAndOptions extractSqlNodeAndOptions(SqlNodeList 
sqlNodeList) {
+    PinotSqlType sqlType = null;
+    SqlNode statementNode = null;
+    Map<String, String> options = new HashMap<>();
+    for (SqlNode sqlNode : sqlNodeList) {
+      if (sqlNode instanceof SqlInsertFromFile) {
+        // extract insert statement (execution statement)
+        if (sqlType == null) {
+          sqlType = PinotSqlType.DML;
+          statementNode = sqlNode;
+        } else {
+          throw new SqlCompilationException("SqlNode with statement already 
exist with type: " + sqlType);
         }
-        throw new SqlCompilationException("Unsupported SqlNode type - " + 
sqlNode.getKind());
-      default:
-        return PinotSqlType.DQL;
+      } else if (sqlNode instanceof SqlSetOption) {
+        // extract options, these are non-execution statements
+        List<SqlNode> operandList = ((SqlSetOption) sqlNode).getOperandList();
+        SqlIdentifier key = (SqlIdentifier) operandList.get(1);
+        SqlLiteral value = (SqlLiteral) operandList.get(2);
+        options.put(key.getSimple(), value.toValue());
+      } else {
+        // default extract query statement (execution statement)
+        if (sqlType == null) {
+          sqlType = PinotSqlType.DQL;
+          statementNode = sqlNode;
+        } else {
+          throw new SqlCompilationException("SqlNode with statement already 
exist with type: " + sqlType);

Review Comment:
   (nit) suggest changing this to "SqlNode with query statement already ......."



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