morrySnow commented on code in PR #21855:
URL: https://github.com/apache/doris/pull/21855#discussion_r1287841922


##########
be/src/clucene:
##########


Review Comment:
   remove mod under this dir



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/exceptions/UnsupportedDialectException.java:
##########
@@ -0,0 +1,29 @@
+// 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.exceptions;
+
+/**
+ * UnsupportedDialectException when not match any in
+ * {@link org.apache.doris.nereids.parser.ParseDialect}.
+ */
+public class UnsupportedDialectException extends UnsupportedOperationException 
{
+
+    public UnsupportedDialectException(String msg) {
+        super(String.format("Unsupported dialect is %s", msg));

Review Comment:
   print dialect type, such as 'trino' or something



##########
fe/fe-core/src/main/java/org/apache/doris/qe/SessionVariable.java:
##########
@@ -1044,6 +1046,11 @@ public void setMaxJoinNumBushyTree(int 
maxJoinNumBushyTree) {
     })
     public boolean enableStrongConsistencyRead = false;
 
+    @VariableMgr.VarAttr(
+            name = SQL_DIALECT
+    )
+    public String sqlDialect = "doris";

Review Comment:
   need forward



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/parser/trino/TrinoParser.java:
##########
@@ -0,0 +1,35 @@
+// 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.trino;
+
+import io.trino.sql.parser.ParsingOptions;
+import io.trino.sql.parser.ParsingOptions.DecimalLiteralTreatment;
+import io.trino.sql.parser.SqlParser;
+import io.trino.sql.tree.Statement;
+
+/**
+ * Trino Parser, depends on 395 trino-parser, and 4.9.3 antlr-runtime

Review Comment:
   add comment to fe-core pom.xml's antlr dependency. We should not change 
antlr version since trino parser must use 4.9.3 antlr-runtime



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/parser/NereidsParser.java:
##########
@@ -84,6 +125,22 @@ private <T> T parse(String sql, Function<DorisParser, 
ParserRuleContext> parseFu
         return (T) logicalPlanBuilder.visit(tree);
     }
 
+    /**
+     * Parse dialect sql.
+     *
+     * @param sql sql string
+     * @param parserContext parse context
+     * @return logical plan
+     */
+    public <T> T parseSingleWithDialect(String sql, ParserContext 
parserContext) {
+        if (ParseDialect.TRINO_395.equals(parserContext.getParserDialect())) {
+            Statement statement = TrinoParser.parse(sql);
+            return (T) new LogicalPlanTrinoBuilder().visit(statement, 
parserContext);
+        } else {
+            throw new 
UnsupportedDialectException(parserContext.getParserDialect().name());
+        }
+    }

Review Comment:
   move these into TrinoParser



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/parser/trino/LogicalPlanTrinoBuilder.java:
##########


Review Comment:
   we shoud use the uniform import format. i think we should not import any 
trino class under `io.trino.sql.tree`, and use fully qualified class name in 
code.
   To avoid use it unguarded, we should add a checkstyle rule for it.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/parser/ParseDialect.java:
##########
@@ -0,0 +1,39 @@
+// 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;
+
+/**
+ * ParseDialect enum, maybe support other dialect.
+ */
+public enum ParseDialect {
+
+    /**
+     * Trino parser and it's version is 395.
+     */
+    TRINO_395("395");

Review Comment:
   we should also add a new attr to present engine name, since we may be add 
other engine dialect.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilderAssistant.java:
##########
@@ -0,0 +1,111 @@
+// 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.nereids.trees.expressions.literal.BigIntLiteral;
+import org.apache.doris.nereids.trees.expressions.literal.IntegerLiteral;
+import org.apache.doris.nereids.trees.expressions.literal.LargeIntLiteral;
+import org.apache.doris.nereids.trees.expressions.literal.Literal;
+import org.apache.doris.nereids.trees.expressions.literal.SmallIntLiteral;
+import org.apache.doris.nereids.trees.expressions.literal.TinyIntLiteral;
+import org.apache.doris.nereids.trees.plans.logical.LogicalCheckPolicy;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+
+import java.math.BigInteger;
+
+/**
+ * Logical plan builder assistant for buildIn dialect and other dialect.
+ * The same logical in {@link 
org.apache.doris.nereids.parser.LogicalPlanBuilder}
+ * and {@link org.apache.doris.nereids.parser.trino.LogicalPlanTrinoBuilder} 
can be
+ * extracted to here.
+ */
+public class LogicalPlanBuilderAssistant {
+
+    private LogicalPlanBuilderAssistant() {
+    }
+
+    /**
+     * EscapeBackSlash such \n, \t
+     */
+    public static String escapeBackSlash(String str) {
+        StringBuilder sb = new StringBuilder();
+        int strLen = str.length();
+        for (int i = 0; i < strLen; ++i) {
+            char c = str.charAt(i);
+            if (c == '\\' && (i + 1) < strLen) {
+                switch (str.charAt(i + 1)) {
+                    case 'n':
+                        sb.append('\n');
+                        break;
+                    case 't':
+                        sb.append('\t');
+                        break;
+                    case 'r':
+                        sb.append('\r');
+                        break;
+                    case 'b':
+                        sb.append('\b');
+                        break;
+                    case '0':
+                        sb.append('\0'); // Ascii null
+                        break;
+                    case 'Z': // ^Z must be escaped on Win32
+                        sb.append('\032');
+                        break;
+                    case '_':
+                    case '%':
+                        sb.append('\\'); // remember prefix for wildcard
+                        sb.append(str.charAt(i + 1));
+                        break;
+                    default:
+                        sb.append(str.charAt(i + 1));
+                        break;
+                }
+                i++;
+            } else {
+                sb.append(c);
+            }
+        }
+        return sb.toString();
+    }
+
+    /**
+     * Handle Integer literal by BigInteger.
+     */
+    public static Literal handleIntegerLiteral(String value) {

Review Comment:
   so trino is also parse integer as the smallest type?



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/parser/NereidsParser.java:
##########
@@ -56,6 +68,35 @@ public List<StatementBase> parseSQL(String originStr) {
         return statementBases;
     }
 
+    public List<StatementBase> parseSQL(String sql, SessionVariable 
sessionVariable) {
+        if ("trino".equalsIgnoreCase(sessionVariable.getSqlDialect())) {
+            return parseSQLWithDialect(sql, sessionVariable);
+        } else {
+            return parseSQL(sql);
+        }
+    }
+
+    private List<StatementBase> parseSQLWithDialect(String sql, 
SessionVariable sessionVariable) {
+        final List<StatementBase> logicalPlans = new ArrayList<>();
+        try {
+            StatementSplitter splitter = new StatementSplitter(sql);
+            ParserContext parserContext = new 
ParserContext(ParseDialect.TRINO_395);
+            StatementContext statementContext = new StatementContext();
+            for (StatementSplitter.Statement statement : 
splitter.getCompleteStatements()) {

Review Comment:
   do split in TrinoParser itself 



##########
fe/fe-core/src/main/java/org/apache/doris/qe/SessionVariable.java:
##########
@@ -1044,6 +1046,11 @@ public void setMaxJoinNumBushyTree(int 
maxJoinNumBushyTree) {
     })
     public boolean enableStrongConsistencyRead = false;
 
+    @VariableMgr.VarAttr(
+            name = SQL_DIALECT
+    )
+    public String sqlDialect = "doris";

Review Comment:
   use checker avoid typo, use setter to set it to normalize it to lowercase or 
uppercase.
   add a new getter to get it as a enum



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/parser/trino/LogicalPlanTrinoBuilder.java:
##########
@@ -0,0 +1,299 @@
+// 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.trino;
+
+import org.apache.doris.nereids.analyzer.UnboundAlias;
+import org.apache.doris.nereids.analyzer.UnboundFunction;
+import org.apache.doris.nereids.analyzer.UnboundRelation;
+import org.apache.doris.nereids.analyzer.UnboundSlot;
+import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.parser.LogicalPlanBuilderAssistant;
+import org.apache.doris.nereids.parser.ParserContext;
+import org.apache.doris.nereids.trees.expressions.Cast;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.functions.Function;
+import org.apache.doris.nereids.trees.expressions.literal.BooleanLiteral;
+import org.apache.doris.nereids.trees.expressions.literal.Literal;
+import org.apache.doris.nereids.trees.expressions.literal.TinyIntLiteral;
+import org.apache.doris.nereids.trees.expressions.literal.VarcharLiteral;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
+import org.apache.doris.nereids.types.DataType;
+import org.apache.doris.nereids.types.coercion.CharacterType;
+import org.apache.doris.nereids.util.RelationUtil;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+import io.trino.sql.tree.AstVisitor;
+import io.trino.sql.tree.DataTypeParameter;
+import io.trino.sql.tree.DateTimeDataType;
+import io.trino.sql.tree.FunctionCall;
+import io.trino.sql.tree.GenericDataType;
+import io.trino.sql.tree.Identifier;
+import io.trino.sql.tree.Node;
+import io.trino.sql.tree.NumericParameter;
+import io.trino.sql.tree.QualifiedName;
+import io.trino.sql.tree.Query;
+import io.trino.sql.tree.QueryBody;
+import io.trino.sql.tree.QuerySpecification;
+import io.trino.sql.tree.Relation;
+import io.trino.sql.tree.SelectItem;
+import io.trino.sql.tree.SingleColumn;
+import io.trino.sql.tree.Table;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+/**
+ * The actually planBuilder for Trino SQL to Doris logical plan.
+ * It depends on {@link io.trino.sql.tree.AstVisitor}
+ */
+public class LogicalPlanTrinoBuilder extends AstVisitor<Object, ParserContext> 
{
+
+    public Object visit(Node node, ParserContext context) {
+        return this.process(node, context);
+    }
+
+    public <T> T visit(Node node, ParserContext context, Class<T> clazz) {
+        return clazz.cast(this.process(node, context));
+    }
+
+    public <T> List<T> visit(List<? extends Node> nodes, ParserContext 
context, Class<T> clazz) {
+        return nodes.stream()
+                .map(node -> clazz.cast(this.process(node, context)))
+                .collect(Collectors.toList());
+    }
+
+    public Object processOptional(Optional<? extends Node> node, ParserContext 
context) {
+        return node.map(value -> this.process(value, context)).orElse(null);
+    }
+
+    public <T> T processOptional(Optional<? extends Node> node, ParserContext 
context, Class<T> clazz) {
+        return node.map(value -> clazz.cast(this.process(value, 
context))).orElse(null);
+    }
+
+    @Override
+    protected LogicalPlan visitQuery(Query node, ParserContext context) {
+        QueryBody queryBody = node.getQueryBody();
+        LogicalPlan logicalPlan = (LogicalPlan) visit(queryBody, context);
+        if (!(queryBody instanceof QuerySpecification)) {
+            // TODO: 2023/7/13

Review Comment:
   update the desc of TODO



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/parser/trino/TrinoFnCallTransformers.java:
##########
@@ -0,0 +1,84 @@
+// 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.trino;
+
+import org.apache.doris.nereids.analyzer.UnboundFunction;
+import org.apache.doris.nereids.parser.ParserContext;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.functions.Function;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableListMultimap;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * The builder and factory for {@link 
org.apache.doris.nereids.parser.trino.TrinoFnCallTransformer},
+ * and supply transform facade ability.
+ */
+public class TrinoFnCallTransformers {
+
+    private static final ImmutableListMultimap<String, TrinoFnCallTransformer> 
TRANSFORMER_MAP;
+    private static final ImmutableListMultimap.Builder<String, 
TrinoFnCallTransformer> transformerBuilder =
+            ImmutableListMultimap.builder();
+
+    static {
+        registerTransformers();
+        TRANSFORMER_MAP = transformerBuilder.build();
+    }
+
+    private TrinoFnCallTransformers() {
+    }
+
+    /**
+     * Function transform facade
+     */
+    public static Function transform(String sourceFnName, List<Expression> 
targetFnArgs, ParserContext context) {
+        List<TrinoFnCallTransformer> transformers = 
getTransformers(sourceFnName);
+        for (TrinoFnCallTransformer transformer : transformers) {
+            if (transformer.check(targetFnArgs, context)) {
+                return transformer.transform(targetFnArgs, context);
+            }
+        }
+        return null;
+    }
+
+    private static List<TrinoFnCallTransformer> getTransformers(String 
sourceFnName) {
+        ImmutableList<TrinoFnCallTransformer> trinoFunctionCallTransformers =
+                TRANSFORMER_MAP.get(sourceFnName);
+        return trinoFunctionCallTransformers == null ? ImmutableList.of() : 
trinoFunctionCallTransformers;
+    }
+
+    private static void registerTransformers() {
+        registerStringFunctionTransformer();
+        // TODO: add other type function transformer
+    }
+
+    private static void registerStringFunctionTransformer() {
+        doRegister("codepoint", 1, "ascii");
+    }
+
+    private static void doRegister(

Review Comment:
   hwo to register varArg function?



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/parser/NereidsParser.java:
##########
@@ -56,6 +68,35 @@ public List<StatementBase> parseSQL(String originStr) {
         return statementBases;
     }
 
+    public List<StatementBase> parseSQL(String sql, SessionVariable 
sessionVariable) {
+        if ("trino".equalsIgnoreCase(sessionVariable.getSqlDialect())) {
+            return parseSQLWithDialect(sql, sessionVariable);
+        } else {
+            return parseSQL(sql);
+        }
+    }
+
+    private List<StatementBase> parseSQLWithDialect(String sql, 
SessionVariable sessionVariable) {
+        final List<StatementBase> logicalPlans = new ArrayList<>();
+        try {
+            StatementSplitter splitter = new StatementSplitter(sql);
+            ParserContext parserContext = new 
ParserContext(ParseDialect.TRINO_395);
+            StatementContext statementContext = new StatementContext();
+            for (StatementSplitter.Statement statement : 
splitter.getCompleteStatements()) {
+                Object parsedPlan = 
parseSingleWithDialect(statement.statement(), parserContext);
+                logicalPlans.add(parsedPlan == null
+                        ? null : new LogicalPlanAdapter((LogicalPlan) 
parsedPlan, statementContext));
+            }
+        } catch (ParsingException | UnsupportedDialectException e) {
+            LOG.warn("Failed to parse logical plan from trino, sql is :{}", 
sql, e);

Review Comment:
   use debug level log, it could flush too many logs



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/parser/trino/AbstractFnCallTransformer.java:
##########
@@ -0,0 +1,42 @@
+// 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.trino;
+
+import org.apache.doris.nereids.parser.ParserContext;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.functions.Function;
+
+import java.util.List;
+
+/**
+ * Abstract function transformer, dialect function transformer should extend 
this.
+ */
+public abstract class AbstractFnCallTransformer {
+
+    /**
+     * Check source function signature is the same between function from SQL 
and
+     * definition in function call transformer.
+     * Check the targetArgs param matches the definition in function call 
transformer.
+     */
+    protected abstract boolean check(List<Expression> targetArgs, 
ParserContext context);

Review Comment:
   change targetArgs' type to `List<Object>`, since not all args is Expression. 
for example, `COUNT(DISTINCT col)`, the first arg is `boolean`



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/parser/trino/LogicalPlanTrinoBuilder.java:
##########
@@ -0,0 +1,299 @@
+// 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.trino;
+
+import org.apache.doris.nereids.analyzer.UnboundAlias;
+import org.apache.doris.nereids.analyzer.UnboundFunction;
+import org.apache.doris.nereids.analyzer.UnboundRelation;
+import org.apache.doris.nereids.analyzer.UnboundSlot;
+import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.parser.LogicalPlanBuilderAssistant;
+import org.apache.doris.nereids.parser.ParserContext;
+import org.apache.doris.nereids.trees.expressions.Cast;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.functions.Function;
+import org.apache.doris.nereids.trees.expressions.literal.BooleanLiteral;
+import org.apache.doris.nereids.trees.expressions.literal.Literal;
+import org.apache.doris.nereids.trees.expressions.literal.TinyIntLiteral;
+import org.apache.doris.nereids.trees.expressions.literal.VarcharLiteral;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
+import org.apache.doris.nereids.types.DataType;
+import org.apache.doris.nereids.types.coercion.CharacterType;
+import org.apache.doris.nereids.util.RelationUtil;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+import io.trino.sql.tree.AstVisitor;
+import io.trino.sql.tree.DataTypeParameter;
+import io.trino.sql.tree.DateTimeDataType;
+import io.trino.sql.tree.FunctionCall;
+import io.trino.sql.tree.GenericDataType;
+import io.trino.sql.tree.Identifier;
+import io.trino.sql.tree.Node;
+import io.trino.sql.tree.NumericParameter;
+import io.trino.sql.tree.QualifiedName;
+import io.trino.sql.tree.Query;
+import io.trino.sql.tree.QueryBody;
+import io.trino.sql.tree.QuerySpecification;
+import io.trino.sql.tree.Relation;
+import io.trino.sql.tree.SelectItem;
+import io.trino.sql.tree.SingleColumn;
+import io.trino.sql.tree.Table;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+/**
+ * The actually planBuilder for Trino SQL to Doris logical plan.
+ * It depends on {@link io.trino.sql.tree.AstVisitor}
+ */
+public class LogicalPlanTrinoBuilder extends AstVisitor<Object, ParserContext> 
{
+
+    public Object visit(Node node, ParserContext context) {
+        return this.process(node, context);
+    }
+
+    public <T> T visit(Node node, ParserContext context, Class<T> clazz) {
+        return clazz.cast(this.process(node, context));
+    }
+
+    public <T> List<T> visit(List<? extends Node> nodes, ParserContext 
context, Class<T> clazz) {
+        return nodes.stream()
+                .map(node -> clazz.cast(this.process(node, context)))
+                .collect(Collectors.toList());
+    }
+
+    public Object processOptional(Optional<? extends Node> node, ParserContext 
context) {
+        return node.map(value -> this.process(value, context)).orElse(null);
+    }
+
+    public <T> T processOptional(Optional<? extends Node> node, ParserContext 
context, Class<T> clazz) {
+        return node.map(value -> clazz.cast(this.process(value, 
context))).orElse(null);
+    }
+
+    @Override
+    protected LogicalPlan visitQuery(Query node, ParserContext context) {
+        QueryBody queryBody = node.getQueryBody();
+        LogicalPlan logicalPlan = (LogicalPlan) visit(queryBody, context);
+        if (!(queryBody instanceof QuerySpecification)) {
+            // TODO: 2023/7/13
+        }
+        return logicalPlan;
+    }
+
+    @Override
+    protected LogicalPlan visitQuerySpecification(QuerySpecification node, 
ParserContext context) {
+        // from -> where -> group by -> having -> select
+        Optional<Relation> from = node.getFrom();
+        LogicalPlan fromPlan = processOptional(from, context, 
LogicalPlan.class);
+        if (from == null) {
+            // TODO: support query values
+        }
+        // TODO: support predicate, aggregate, having, order by, limit
+        // TODO: support distinct
+        boolean isDistinct = node.getSelect().isDistinct();
+        List<SelectItem> selectItems = node.getSelect().getSelectItems();
+        return withProjection(selectItems, fromPlan, isDistinct, context);
+    }
+
+    private LogicalProject withProjection(List<SelectItem> selectItems,
+            LogicalPlan input,
+            boolean isDistinct,
+            ParserContext context) {
+        List<Expression> exprs = selectItems.stream()
+                .map(item -> visit(item, context, Expression.class))
+                .collect(Collectors.toList());
+        return new LogicalProject(exprs, Collections.emptyList(), input, 
isDistinct);
+    }
+
+    @Override
+    protected Expression visitSingleColumn(SingleColumn node, ParserContext 
context) {
+        String alias = node.getAlias().map(Identifier::getValue).orElse(null);
+        Expression expr = visit(node.getExpression(), context, 
Expression.class);
+        if (expr instanceof NamedExpression) {
+            return (NamedExpression) expr;
+        } else {
+            return alias == null ? new UnboundAlias(expr) : new 
UnboundAlias(expr, alias);
+        }
+    }
+
+    @Override
+    protected Object visitIdentifier(Identifier node, ParserContext context) {
+        return new UnboundSlot(Lists.newArrayList(node.getValue()));
+    }
+
+    //~ visitFunction 
-----------------------------------------------------------
+
+    @Override
+    protected Function visitFunctionCall(FunctionCall node, ParserContext 
context) {
+        List<Expression> exprs = visit(node.getArguments(), context, 
Expression.class);
+        Function transformedFn =
+                TrinoFnCallTransformers.transform(node.getName().toString(), 
exprs, context);
+        if (transformedFn == null) {
+            transformedFn = new UnboundFunction(node.getName().toString(), 
exprs);
+        }
+        return transformedFn;
+    }
+
+    //~ visitTable -----------------------------------------------------------

Review Comment:
   use the split comment format as LogicalPlanBuidler
   ```
       /* 
********************************************************************************************
        * visitTable
        * 
********************************************************************************************
 */
   ```



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/parser/trino/LogicalPlanTrinoBuilder.java:
##########
@@ -0,0 +1,299 @@
+// 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.trino;
+
+import org.apache.doris.nereids.analyzer.UnboundAlias;
+import org.apache.doris.nereids.analyzer.UnboundFunction;
+import org.apache.doris.nereids.analyzer.UnboundRelation;
+import org.apache.doris.nereids.analyzer.UnboundSlot;
+import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.parser.LogicalPlanBuilderAssistant;
+import org.apache.doris.nereids.parser.ParserContext;
+import org.apache.doris.nereids.trees.expressions.Cast;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.functions.Function;
+import org.apache.doris.nereids.trees.expressions.literal.BooleanLiteral;
+import org.apache.doris.nereids.trees.expressions.literal.Literal;
+import org.apache.doris.nereids.trees.expressions.literal.TinyIntLiteral;
+import org.apache.doris.nereids.trees.expressions.literal.VarcharLiteral;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
+import org.apache.doris.nereids.types.DataType;
+import org.apache.doris.nereids.types.coercion.CharacterType;
+import org.apache.doris.nereids.util.RelationUtil;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+import io.trino.sql.tree.AstVisitor;
+import io.trino.sql.tree.DataTypeParameter;
+import io.trino.sql.tree.DateTimeDataType;
+import io.trino.sql.tree.FunctionCall;
+import io.trino.sql.tree.GenericDataType;
+import io.trino.sql.tree.Identifier;
+import io.trino.sql.tree.Node;
+import io.trino.sql.tree.NumericParameter;
+import io.trino.sql.tree.QualifiedName;
+import io.trino.sql.tree.Query;
+import io.trino.sql.tree.QueryBody;
+import io.trino.sql.tree.QuerySpecification;
+import io.trino.sql.tree.Relation;
+import io.trino.sql.tree.SelectItem;
+import io.trino.sql.tree.SingleColumn;
+import io.trino.sql.tree.Table;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+/**
+ * The actually planBuilder for Trino SQL to Doris logical plan.
+ * It depends on {@link io.trino.sql.tree.AstVisitor}
+ */
+public class LogicalPlanTrinoBuilder extends AstVisitor<Object, ParserContext> 
{
+
+    public Object visit(Node node, ParserContext context) {
+        return this.process(node, context);
+    }
+
+    public <T> T visit(Node node, ParserContext context, Class<T> clazz) {
+        return clazz.cast(this.process(node, context));
+    }
+
+    public <T> List<T> visit(List<? extends Node> nodes, ParserContext 
context, Class<T> clazz) {
+        return nodes.stream()
+                .map(node -> clazz.cast(this.process(node, context)))
+                .collect(Collectors.toList());
+    }
+
+    public Object processOptional(Optional<? extends Node> node, ParserContext 
context) {
+        return node.map(value -> this.process(value, context)).orElse(null);
+    }
+
+    public <T> T processOptional(Optional<? extends Node> node, ParserContext 
context, Class<T> clazz) {
+        return node.map(value -> clazz.cast(this.process(value, 
context))).orElse(null);
+    }
+
+    @Override
+    protected LogicalPlan visitQuery(Query node, ParserContext context) {
+        QueryBody queryBody = node.getQueryBody();
+        LogicalPlan logicalPlan = (LogicalPlan) visit(queryBody, context);
+        if (!(queryBody instanceof QuerySpecification)) {
+            // TODO: 2023/7/13
+        }
+        return logicalPlan;
+    }
+
+    @Override
+    protected LogicalPlan visitQuerySpecification(QuerySpecification node, 
ParserContext context) {
+        // from -> where -> group by -> having -> select
+        Optional<Relation> from = node.getFrom();
+        LogicalPlan fromPlan = processOptional(from, context, 
LogicalPlan.class);
+        if (from == null) {
+            // TODO: support query values

Review Comment:
   throw exception on each TODO



-- 
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: commits-unsubscr...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org


Reply via email to