forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature](nereids) Integrate nereids into current SQL process framewo…
…rk (apache#10304) To integrate the nereids optimizer with new SQLParser and Planner to existing SQL process framework, I abstract a interface which named "Planner" and let the Both planner from nereids and stale optimizer implement it, to disguish it with origin Planner, I rename the Planner to OriginalPlanner. As we don't want to impact the existing logic too much, I defined a LogicalPlanAdapter to adapt the logicalPlan that is the output of the new paser to the existing code. Besides, as the MySQL protocol supports sending multiple statements in one packet, so I add Nereids#SparseSQL method to handle this properly.
- Loading branch information
1 parent
8f2b2b4
commit e82d843
Showing
27 changed files
with
817 additions
and
599 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
fe/fe-core/src/main/java/org/apache/doris/nereids/parser/NereidsParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// 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.doris.nereids.parser; | ||
|
||
import org.apache.doris.analysis.StatementBase; | ||
import org.apache.doris.nereids.DorisLexer; | ||
import org.apache.doris.nereids.DorisParser; | ||
import org.apache.doris.nereids.exceptions.ParsingException; | ||
import org.apache.doris.nereids.trees.expressions.Expression; | ||
import org.apache.doris.nereids.trees.plans.logical.LogicalPlan; | ||
import org.apache.doris.nereids.trees.plans.logical.LogicalPlanAdapter; | ||
|
||
import org.antlr.v4.runtime.CharStreams; | ||
import org.antlr.v4.runtime.CommonTokenStream; | ||
import org.antlr.v4.runtime.ParserRuleContext; | ||
import org.antlr.v4.runtime.atn.PredictionMode; | ||
import org.antlr.v4.runtime.misc.ParseCancellationException; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* Sql parser, convert sql DSL to logical plan. | ||
*/ | ||
public class NereidsParser { | ||
|
||
/** | ||
* In MySQL protocol, client could send multi-statement in. | ||
* a single packet. | ||
* https://dev.mysql.com/doc/internals/en/com-set-option.html | ||
*/ | ||
public List<StatementBase> parseSQL(String originStr) throws Exception { | ||
List<LogicalPlan> logicalPlanList = parseMultiple(originStr); | ||
List<StatementBase> statementBaseList = new ArrayList<>(); | ||
for (LogicalPlan logicalPlan : logicalPlanList) { | ||
LogicalPlanAdapter logicalPlanAdapter = new LogicalPlanAdapter(logicalPlan); | ||
statementBaseList.add(logicalPlanAdapter); | ||
} | ||
return statementBaseList; | ||
} | ||
|
||
/** | ||
* parse sql DSL string. | ||
* | ||
* @param sql sql string | ||
* @return logical plan | ||
*/ | ||
public LogicalPlan parseSingle(String sql) throws Exception { | ||
return (LogicalPlan) parse(sql, DorisParser::singleStatement); | ||
} | ||
|
||
public List<LogicalPlan> parseMultiple(String sql) throws Exception { | ||
return (List<LogicalPlan>) parse(sql, DorisParser::multiStatements); | ||
} | ||
|
||
private Object parse(String sql, Function<DorisParser, ParserRuleContext> parseFunction) { | ||
try { | ||
ParserRuleContext tree = toAst(sql, parseFunction); | ||
LogicalPlanBuilder logicalPlanBuilder = new LogicalPlanBuilder(); | ||
return logicalPlanBuilder.visit(tree); | ||
} catch (StackOverflowError e) { | ||
throw new ParsingException(e.getMessage()); | ||
} | ||
} | ||
|
||
private ParserRuleContext toAst(String sql, Function<DorisParser, ParserRuleContext> parseFunction) { | ||
DorisLexer lexer = new DorisLexer(new CaseInsensitiveStream(CharStreams.fromString(sql))); | ||
CommonTokenStream tokenStream = new CommonTokenStream(lexer); | ||
DorisParser parser = new DorisParser(tokenStream); | ||
// parser.addParseListener(PostProcessor) | ||
// parser.removeErrorListeners() | ||
// parser.addErrorListener(ParseErrorListener) | ||
ParserRuleContext tree; | ||
try { | ||
// first, try parsing with potentially faster SLL mode | ||
parser.getInterpreter().setPredictionMode(PredictionMode.SLL); | ||
tree = parseFunction.apply(parser); | ||
} catch (ParseCancellationException ex) { | ||
// if we fail, parse with LL mode | ||
tokenStream.seek(0); // rewind input stream | ||
parser.reset(); | ||
|
||
parser.getInterpreter().setPredictionMode(PredictionMode.LL); | ||
tree = parseFunction.apply(parser); | ||
} | ||
return tree; | ||
} | ||
|
||
public Expression createExpression(String expression) { | ||
return (Expression) parse(expression, DorisParser::expression); | ||
} | ||
} |
85 changes: 0 additions & 85 deletions
85
fe/fe-core/src/main/java/org/apache/doris/nereids/parser/SqlParser.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.