JingDas commented on code in PR #21855: URL: https://github.com/apache/doris/pull/21855#discussion_r1329754959
########## fe/fe-core/src/main/java/org/apache/doris/nereids/parser/trino/LogicalPlanTrinoBuilder.java: ########## @@ -0,0 +1,312 @@ +// 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.UnboundOneRowRelation; +import org.apache.doris.nereids.analyzer.UnboundRelation; +import org.apache.doris.nereids.analyzer.UnboundResultSink; +import org.apache.doris.nereids.analyzer.UnboundSlot; +import org.apache.doris.nereids.exceptions.AnalysisException; +import org.apache.doris.nereids.exceptions.DialectTransformException; +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.StatementScopeIdGenerator; +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.DateTimeLiteral; +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 com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; + +import java.util.ArrayList; +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 io.trino.sql.tree.AstVisitor<Object, ParserContext> { + + public Object visit(io.trino.sql.tree.Node node, ParserContext context) { + return this.process(node, context); + } + + public <T> T visit(io.trino.sql.tree.Node node, ParserContext context, Class<T> clazz) { + return clazz.cast(this.process(node, context)); + } + + public <T> List<T> visit(List<? extends io.trino.sql.tree.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 io.trino.sql.tree.Node> node, ParserContext context) { + return node.map(value -> this.process(value, context)).orElse(null); + } + + public <T> T processOptional(Optional<? extends io.trino.sql.tree.Node> node, + ParserContext context, Class<T> clazz) { + return node.map(value -> clazz.cast(this.process(value, context))).orElse(null); + } + + @Override + protected LogicalPlan visitQuery(io.trino.sql.tree.Query node, ParserContext context) { + io.trino.sql.tree.QueryBody queryBody = node.getQueryBody(); + LogicalPlan logicalPlan = (LogicalPlan) visit(queryBody, context); + if (!(queryBody instanceof io.trino.sql.tree.QuerySpecification)) { + // TODO: need to handle orderBy and limit + throw new DialectTransformException("transform querySpecification"); + } + return logicalPlan; + } + + @Override + protected LogicalPlan visitQuerySpecification(io.trino.sql.tree.QuerySpecification node, + ParserContext context) { + // from -> where -> group by -> having -> select + Optional<io.trino.sql.tree.Relation> from = node.getFrom(); + LogicalPlan fromPlan = processOptional(from, context, LogicalPlan.class); + List<io.trino.sql.tree.SelectItem> selectItems = node.getSelect().getSelectItems(); + if (from == null || !from.isPresent()) { + // TODO: support query values + List<NamedExpression> expressions = selectItems.stream() + .map(item -> visit(item, context, NamedExpression.class)) + .collect(Collectors.toList()); + return new UnboundOneRowRelation(StatementScopeIdGenerator.newRelationId(), expressions); + } + // TODO: support predicate, aggregate, having, order by, limit + // TODO: support distinct + boolean isDistinct = node.getSelect().isDistinct(); + return new UnboundResultSink<>(withProjection(selectItems, fromPlan, isDistinct, context)); + } + + private LogicalProject withProjection(List<io.trino.sql.tree.SelectItem> selectItems, + LogicalPlan input, + boolean isDistinct, + ParserContext context) { + List<NamedExpression> expressions = selectItems.stream() + .map(item -> visit(item, context, NamedExpression.class)) + .collect(Collectors.toList()); + return new LogicalProject(expressions, new ArrayList<>(), isDistinct, input); Review Comment: Have fixed it -- 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]
