This is an automated email from the ASF dual-hosted git repository. rongr pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/pinot.git
The following commit(s) were added to refs/heads/master by this push: new 22843c6118 remove customized sql parser, use common one (#9166) 22843c6118 is described below commit 22843c6118eca91a369276977cc18da4c6bd25bb Author: Rong Rong <walterddr.walter...@gmail.com> AuthorDate: Thu Aug 4 15:14:30 2022 -0700 remove customized sql parser, use common one (#9166) Co-authored-by: Rong Rong <ro...@startree.ai> --- .../org/apache/pinot/query/QueryEnvironment.java | 7 +- .../pinot/query/parser/CalciteSqlParser.java | 148 --------------------- 2 files changed, 5 insertions(+), 150 deletions(-) diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/QueryEnvironment.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/QueryEnvironment.java index 02c66247b6..7f6d43274e 100644 --- a/pinot-query-planner/src/main/java/org/apache/pinot/query/QueryEnvironment.java +++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/QueryEnvironment.java @@ -46,7 +46,6 @@ import org.apache.calcite.sql2rel.StandardConvertletTable; import org.apache.calcite.tools.FrameworkConfig; import org.apache.calcite.tools.Frameworks; import org.apache.pinot.query.context.PlannerContext; -import org.apache.pinot.query.parser.CalciteSqlParser; import org.apache.pinot.query.planner.QueryPlan; import org.apache.pinot.query.planner.logical.LogicalPlanner; import org.apache.pinot.query.planner.logical.StagePlanner; @@ -54,6 +53,8 @@ import org.apache.pinot.query.routing.WorkerManager; import org.apache.pinot.query.rules.PinotQueryRuleSets; import org.apache.pinot.query.type.TypeFactory; import org.apache.pinot.query.validate.Validator; +import org.apache.pinot.sql.parsers.CalciteSqlParser; +import org.apache.pinot.sql.parsers.SqlNodeAndOptions; /** @@ -144,7 +145,9 @@ public class QueryEnvironment { protected SqlNode parse(String query, PlannerContext plannerContext) throws Exception { // 1. invoke CalciteSqlParser to parse out SqlNode; - return CalciteSqlParser.compile(query, plannerContext); + SqlNodeAndOptions sqlNodeAndOptions = CalciteSqlParser.compileToSqlNodeAndOptions(query); + plannerContext.setOptions(sqlNodeAndOptions.getOptions()); + return sqlNodeAndOptions.getSqlNode(); } protected SqlNode validate(SqlNode parsed) diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/parser/CalciteSqlParser.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/parser/CalciteSqlParser.java deleted file mode 100644 index d2fd054833..0000000000 --- a/pinot-query-planner/src/main/java/org/apache/pinot/query/parser/CalciteSqlParser.java +++ /dev/null @@ -1,148 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.pinot.query.parser; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.regex.Matcher; -import org.apache.calcite.sql.SqlNode; -import org.apache.calcite.sql.SqlOrderBy; -import org.apache.calcite.sql.SqlSelect; -import org.apache.calcite.sql.parser.SqlParseException; -import org.apache.calcite.sql.parser.SqlParser; -import org.apache.pinot.query.context.PlannerContext; -import org.apache.pinot.sql.parsers.SqlCompilationException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - - -/** - * This class provide API to parse a SQL string into Pinot query {@link SqlNode}. - * - * <p>This class is extracted from {@link org.apache.pinot.sql.parsers.CalciteSqlParser}. It contains the logic - * to parsed SQL string into {@link SqlNode} and use {@link QueryRewriter} to rewrite the query with Pinot specific - * contextual info. - */ -public class CalciteSqlParser { - private static final Logger LOGGER = LoggerFactory.getLogger(CalciteSqlParser.class); - - private CalciteSqlParser() { - // do not instantiate. - } - - /** - * entrypoint for Sql Parser. - */ - public static SqlNode compile(String sql, PlannerContext plannerContext) - throws SqlCompilationException { - // Extract OPTION statements from sql as Calcite Parser doesn't parse it. - // TODO: use parser syntax extension instead. - Map<String, String> options = parseOptions(extractOptionsFromSql(sql)); - plannerContext.setOptions(options); - if (!options.isEmpty()) { - sql = removeOptionsFromSql(sql); - } - // Compile Sql without OPTION statements. - SqlNode parsed = parse(sql); - - // query rewrite. - return QueryRewriter.rewrite(parsed, plannerContext); - } - - // ========================================================================== - // Static utils to parse the SQL. - // ========================================================================== - - private static Map<String, String> parseOptions(List<String> optionsStatements) { - if (optionsStatements.isEmpty()) { - return Collections.emptyMap(); - } - Map<String, String> options = new HashMap<>(); - for (String optionsStatement : optionsStatements) { - for (String option : optionsStatement.split(",")) { - final String[] splits = option.split("="); - if (splits.length != 2) { - throw new SqlCompilationException("OPTION statement requires two parts separated by '='"); - } - options.put(splits[0].trim(), splits[1].trim()); - } - } - return options; - } - - private static SqlNode parse(String sql) { - SqlParser sqlParser = SqlParser.create(sql, ParserUtils.PARSER_CONFIG); - SqlNode sqlNode; - try { - sqlNode = sqlParser.parseQuery(); - } catch (SqlParseException e) { - throw new SqlCompilationException("Caught exception while parsing query: " + sql, e); - } - - // This is a special rewrite, - // TODO: move it to planner later. - SqlSelect selectNode; - if (sqlNode instanceof SqlOrderBy) { - // Store order-by info into the select sql node - SqlOrderBy orderByNode = (SqlOrderBy) sqlNode; - selectNode = (SqlSelect) orderByNode.query; - selectNode.setOrderBy(orderByNode.orderList); - selectNode.setFetch(orderByNode.fetch); - selectNode.setOffset(orderByNode.offset); - } else { - selectNode = (SqlSelect) sqlNode; - } - return selectNode; - } - - private static List<String> extractOptionsFromSql(String sql) { - List<String> results = new ArrayList<>(); - Matcher matcher = ParserUtils.OPTIONS_REGEX_PATTEN.matcher(sql); - while (matcher.find()) { - results.add(matcher.group(1)); - } - return results; - } - - private static String removeOptionsFromSql(String sql) { - Matcher matcher = ParserUtils.OPTIONS_REGEX_PATTEN.matcher(sql); - return matcher.replaceAll(""); - } -} --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org