yashmayya commented on code in PR #20319:
URL: https://github.com/apache/pinot/pull/20319#discussion_r4646764211
##########
pinot-common/src/main/java/org/apache/pinot/sql/parsers/CalciteSqlParser.java:
##########
@@ -180,6 +184,101 @@ public static SqlNodeAndOptions
compileToSqlNodeAndOptions(String sql)
}
}
+ /// Extracts SQL `SET` statements and the legacy `OPTION(...)` suffix
without building the executable statement's AST.
+ /// Uses the same lexer and option parser as
[#compileToSqlNodeAndOptions(String)], including quoted identifiers,
+ /// literals, comments, and scoped `ALTER SESSION/SYSTEM SET` statements.
`SET` options take precedence over `OPTION`.
+ ///
+ /// This is not SQL validation: executable statements and broker
query-option policies must still be validated by the
+ /// broker. In particular, successfully extracting options does not mean the
SQL is executable.
+ public static Map<String, String> extractQueryOptions(String sql) {
+ sql = ParserUtils.sanitizeSql(sql);
+ List<String> legacyOptions = List.of();
+ Matcher matcher = OPTIONS_REGEX_PATTEN.matcher(sql);
+ if (matcher.find()) {
+ SqlOptionsMode legacyOptionSyntaxMode =
QueryOptionsUtils.getLegacyOptionSyntaxMode();
+ if (legacyOptionSyntaxMode != SqlOptionsMode.IGNORE) {
+ legacyOptions = List.of(matcher.group(1));
+ if (legacyOptionSyntaxMode == SqlOptionsMode.REJECT) {
+ throw new SqlCompilationException("Legacy OPTION(...) query options
are not allowed on this cluster, use "
+ + "'SET <key> = <value>;' statements instead: " + legacyOptions);
+ }
+ }
+ sql = stripLegacyOptionSuffix(sql, matcher.start());
+ }
+ try {
+ Map<String, String> options = new HashMap<>();
+ SqlParserImpl sqlParser = newSqlParser(sql);
+ SqlParserPos lastSeparator = findLastInternalSeparator(sql);
+ Token token = sqlParser.getToken(1);
+ while (token.kind != SqlParserImplConstants.EOF) {
+ if (token.kind == SqlParserImplConstants.SET || token.kind ==
SqlParserImplConstants.RESET
+ || (token.kind == SqlParserImplConstants.ALTER
+ && (sqlParser.getToken(2).kind ==
SqlParserImplConstants.SESSION
+ || sqlParser.getToken(2).kind ==
SqlParserImplConstants.SYSTEM)
+ && (sqlParser.getToken(3).kind == SqlParserImplConstants.SET
+ || sqlParser.getToken(3).kind ==
SqlParserImplConstants.RESET))) {
+ extractSetOption((SqlSetOption) sqlParser.SqlStmt(), options);
+ token = sqlParser.getToken(1);
+ if (token.kind != SqlParserImplConstants.EOF && token.kind !=
SqlParserImplConstants.SEMICOLON) {
+ throw new SqlCompilationException("Expected ';' after SET query
option");
+ }
+ } else {
+ // Skip the body if no later SET can follow. Otherwise use the lexer
to distinguish statement separators
+ // from semicolons in literals and comments.
+ if (token.beginLine > lastSeparator.getLineNum()
+ || (token.beginLine == lastSeparator.getLineNum()
+ && token.beginColumn > lastSeparator.getColumnNum())) {
+ break;
+ }
+ while (token.kind != SqlParserImplConstants.EOF && token.kind !=
SqlParserImplConstants.SEMICOLON) {
+ sqlParser.getNextToken();
+ token = sqlParser.getToken(1);
+ }
+ }
+ if (token.kind == SqlParserImplConstants.EOF) {
+ break;
+ }
+ sqlParser.getNextToken();
+ token = sqlParser.getToken(1);
+ }
+ Map<String, String> resolvedOptions =
QueryOptionsUtils.resolveCaseInsensitiveOptions(options);
+ if (!legacyOptions.isEmpty()) {
+
QueryOptionsUtils.resolveCaseInsensitiveOptions(extractOptionsMap(legacyOptions))
+ .forEach(resolvedOptions::putIfAbsent);
+ }
+ return resolvedOptions;
+ } catch (Throwable e) {
+ throw new SqlCompilationException(
+ "Caught exception while extracting query options: " + sql + ": " +
e.getMessage(), e);
+ }
+ }
+
+ private static SqlParserPos findLastInternalSeparator(String sql) {
+ // A terminal semicolon cannot introduce another statement.
+ int offset = sql.lastIndexOf(';', sql.length() - 2);
+ if (offset < 0) {
+ return SqlParserPos.ZERO;
+ }
+ // Match JavaCC's CR, LF and CRLF line endings. newSqlParser sets the tab
size to one.
Review Comment:
Nit: this works only while `newSqlParser` keeps tab size 1. As a test, I
changed it to `setTabSize(4)`. Then `"SET a=1;" + "\t".repeat(8) + "SELECT
'x;y' FROM t; SET b=2"` silently drops `b=2`, and all tests still pass.
Can you add this case to `testExtractQueryOptionsMatchesFullParser`? Then a
tab-size change fails a test.
##########
pinot-common/src/main/java/org/apache/pinot/sql/parsers/CalciteSqlParser.java:
##########
@@ -113,8 +116,9 @@ private CalciteSqlParser() {
//
// Multiple OPTIONs is also supported by:
// `OPTION (<k1> = <v1>, <k2> = <v2>, <k3> = <v3>)`
+ // Start at OPTION itself so the unanchored search does not rescan leading
whitespace.
private static final Pattern OPTIONS_REGEX_PATTEN =
- Pattern.compile("\\s*option\\s*\\(([^)]+)\\)\\s*;?\\s*\\Z",
Pattern.CASE_INSENSITIVE);
+ Pattern.compile("option\\s*+\\(([^)]++)\\)\\s*+;?\\s*+\\Z",
Pattern.CASE_INSENSITIVE);
Review Comment:
Nice side fix. On master, the leading `\s*` makes this scan quadratic on
long whitespace runs. A query with 200k spaces in the middle spends ~104 s in
this regex. The new pattern takes 0.5 ms.
Please mention this in the PR description.
--
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]