morrySnow commented on code in PR #21855: URL: https://github.com/apache/doris/pull/21855#discussion_r1328310626
########## fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/PlaceholderExpression.java: ########## @@ -0,0 +1,88 @@ +// 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.analyzer; + +import org.apache.doris.nereids.parser.trino.TrinoFnCallTransformer.PlaceholderCollector; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +/** + * Expression placeHolder, the expression in PlaceHolderExpression will be collected by + * + * @see PlaceholderCollector + */ +public class PlaceholderExpression extends Expression { + + private final Class<? extends Expression> delegateClazz; + /** + * 1 based + */ + private final int position; + + public PlaceholderExpression(List<Expression> children, Class<? extends Expression> delegateClazz, int position) { + super(children); + this.delegateClazz = delegateClazz; + this.position = position; + } + + public static PlaceholderExpression of(Class<? extends Expression> delegateClazz, int position) { + return new PlaceholderExpression(new ArrayList<>(), delegateClazz, position); Review Comment: ```suggestion return new PlaceholderExpression(ImmutableList.of(), delegateClazz, position); ``` ########## fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/visitor/ExpressionVisitor.java: ########## @@ -496,4 +497,12 @@ public R visitUnboundStar(UnboundStar unboundStar, C context) { public R visitUnboundVariable(UnboundVariable unboundVariable, C context) { return visit(unboundVariable, context); } + + /* ******************************************************************************************** + * Placeholder expressions + * ********************************************************************************************/ + + public R visitPlaceholder(PlaceholderExpression placeholderExpression, C context) { Review Comment: ```suggestion public R visitPlaceholderExpression(PlaceholderExpression placeholderExpression, C context) { ``` ########## 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: all collections should use ImmmutableXXX in guava ########## fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/PlaceholderExpression.java: ########## @@ -0,0 +1,88 @@ +// 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.analyzer; + +import org.apache.doris.nereids.parser.trino.TrinoFnCallTransformer.PlaceholderCollector; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +/** + * Expression placeHolder, the expression in PlaceHolderExpression will be collected by + * + * @see PlaceholderCollector + */ +public class PlaceholderExpression extends Expression { + + private final Class<? extends Expression> delegateClazz; + /** + * 1 based + */ + private final int position; + + public PlaceholderExpression(List<Expression> children, Class<? extends Expression> delegateClazz, int position) { + super(children); + this.delegateClazz = delegateClazz; + this.position = position; + } + + public static PlaceholderExpression of(Class<? extends Expression> delegateClazz, int position) { + return new PlaceholderExpression(new ArrayList<>(), delegateClazz, position); + } + + @Override + public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) { + return visitor.visit(this, context); + } + + public Class<? extends Expression> getDelegateClazz() { + return delegateClazz; + } + + public int getPosition() { + return position; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + if (!super.equals(o)) { + return false; + } + PlaceholderExpression that = (PlaceholderExpression) o; + return position == that.position && Objects.equals(delegateClazz, that.delegateClazz); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), delegateClazz, position); + } + + @Override + public boolean nullable() { + return false; + } Review Comment: usw AlwaysNotNullable trait ########## 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); + } + + @Override + protected Expression visitSingleColumn(io.trino.sql.tree.SingleColumn node, ParserContext context) { + String alias = node.getAlias().map(io.trino.sql.tree.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(io.trino.sql.tree.Identifier node, ParserContext context) { + return new UnboundSlot(Lists.newArrayList(node.getValue())); + } + + /* ******************************************************************************************** + * visitFunction + * ******************************************************************************************** */ + + @Override + protected Function visitFunctionCall(io.trino.sql.tree.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); Review Comment: why not return new UnboundFunction in `TrinoFnCallTransformers.transform` directly when return value is null? ########## fe/fe-core/src/main/java/org/apache/doris/nereids/parser/trino/TrinoFnCallTransformers.java: ########## @@ -0,0 +1,136 @@ +// 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.PlaceholderExpression; +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 com.google.common.collect.Lists; + +import java.util.List; +import java.util.stream.Collectors; + +/** + * 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, AbstractFnCallTransformer> TRANSFORMER_MAP; + private static final ImmutableListMultimap<String, AbstractFnCallTransformer> COMPLEX_TRANSFORMER_MAP; + private static final ImmutableListMultimap.Builder<String, AbstractFnCallTransformer> transformerBuilder = + ImmutableListMultimap.builder(); + private static final ImmutableListMultimap.Builder<String, AbstractFnCallTransformer> complexTransformerBuilder = + ImmutableListMultimap.builder(); + + static { + registerTransformers(); + TRANSFORMER_MAP = transformerBuilder.build(); + registerComplexTransformers(); + COMPLEX_TRANSFORMER_MAP = complexTransformerBuilder.build(); + } + + private TrinoFnCallTransformers() { + } + + /** + * Function transform facade + */ + public static Function transform(String sourceFnName, List<Expression> sourceFnTransformedArguments, + ParserContext context) { + List<AbstractFnCallTransformer> transformers = getTransformers(sourceFnName, false); + Function function = doTransform(transformers, sourceFnName, sourceFnTransformedArguments, context); + if (function != null) { + return function; + } + transformers = getTransformers(sourceFnName, true); Review Comment: maybe we should return complex == false and == true togather, and let complex == false ahead of complex == true ########## fe/fe-core/src/main/java/org/apache/doris/nereids/parser/trino/ComplexTrinoFnCallTransformer.java: ########## @@ -0,0 +1,74 @@ +// 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 org.apache.doris.nereids.trees.expressions.literal.VarcharLiteral; + +import com.google.common.collect.Lists; + +import java.util.List; + +/** + * Trino complex function transformer + */ +public abstract class ComplexTrinoFnCallTransformer extends AbstractFnCallTransformer { + + protected abstract String getSourceFnName(); + + /** + * DateDiff complex function transformer + */ + public static final class DateDiffFnCallTransformer extends ComplexTrinoFnCallTransformer { Review Comment: put this class into a new file ########## fe/fe-core/src/main/java/org/apache/doris/nereids/parser/trino/ComplexTrinoFnCallTransformer.java: ########## @@ -0,0 +1,74 @@ +// 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 org.apache.doris.nereids.trees.expressions.literal.VarcharLiteral; + +import com.google.common.collect.Lists; + +import java.util.List; + +/** + * Trino complex function transformer + */ +public abstract class ComplexTrinoFnCallTransformer extends AbstractFnCallTransformer { + + protected abstract String getSourceFnName(); + + /** + * DateDiff complex function transformer + */ + public static final class DateDiffFnCallTransformer extends ComplexTrinoFnCallTransformer { + + private static final String SECOND = "second"; + private static final String HOUR = "hour"; + private static final String DAY = "day"; + private static final String MILLI_SECOND = "millisecond"; + + @Override + public String getSourceFnName() { + return "date_diff"; + } + + @Override + protected boolean check(String sourceFnName, List<Expression> sourceFnTransformedArguments, + ParserContext context) { + return getSourceFnName().equalsIgnoreCase(sourceFnName); + } + + @Override + protected Function transform(String sourceFnName, List<Expression> sourceFnTransformedArguments, + ParserContext context) { + if (sourceFnTransformedArguments.size() != 3) { + return null; + } + VarcharLiteral diffGranularity = (VarcharLiteral) sourceFnTransformedArguments.get(0); + if (SECOND.equals(diffGranularity.getValue())) { + return new UnboundFunction( + "seconds_diff", + Lists.newArrayList(sourceFnTransformedArguments.get(1), sourceFnTransformedArguments.get(2))); Review Comment: use ImmutableList ########## fe/fe-core/src/main/java/org/apache/doris/qe/SessionVariable.java: ########## @@ -1188,6 +1191,9 @@ public void setMaxJoinNumberOfReorder(int maxJoinNumberOfReorder) { flag = VariableMgr.GLOBAL) public String fullAutoAnalyzeEndTime = ""; + @VariableMgr.VarAttr(name = SQL_DIALECT, needForward = true) + public String sqlDialect = "doris"; Review Comment: u should add `checker` and `description` in Annotation. checker is used to avoid typo and only accept valid string ########## fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/PlaceholderExpression.java: ########## @@ -0,0 +1,88 @@ +// 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.analyzer; + +import org.apache.doris.nereids.parser.trino.TrinoFnCallTransformer.PlaceholderCollector; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +/** + * Expression placeHolder, the expression in PlaceHolderExpression will be collected by + * + * @see PlaceholderCollector + */ +public class PlaceholderExpression extends Expression { + + private final Class<? extends Expression> delegateClazz; + /** + * 1 based + */ + private final int position; + + public PlaceholderExpression(List<Expression> children, Class<? extends Expression> delegateClazz, int position) { + super(children); + this.delegateClazz = delegateClazz; Review Comment: ```suggestion this.delegateClazz = Objects.requireNonNull(delegateClazz, "delegateClazz should not be null"); ``` ########## 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); + } + + @Override + protected Expression visitSingleColumn(io.trino.sql.tree.SingleColumn node, ParserContext context) { + String alias = node.getAlias().map(io.trino.sql.tree.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(io.trino.sql.tree.Identifier node, ParserContext context) { + return new UnboundSlot(Lists.newArrayList(node.getValue())); + } + + /* ******************************************************************************************** + * visitFunction + * ******************************************************************************************** */ + + @Override + protected Function visitFunctionCall(io.trino.sql.tree.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 + * ******************************************************************************************** */ + + @Override + protected LogicalPlan visitTable(io.trino.sql.tree.Table node, ParserContext context) { + io.trino.sql.tree.QualifiedName name = node.getName(); + List<String> tableId = name.getParts(); + List<String> partitionNames = new ArrayList<>(); + // build table + return LogicalPlanBuilderAssistant.withCheckPolicy( + new UnboundRelation(StatementScopeIdGenerator.newRelationId(), tableId, + partitionNames, false)); + } + + /* ******************************************************************************************** + * visit buildIn function + * ******************************************************************************************** */ + + @Override + protected Expression visitCast(io.trino.sql.tree.Cast node, ParserContext context) { + Expression expr = visit(node.getExpression(), context, Expression.class); + DataType dataType = mappingType(node.getType()); + Expression cast = new Cast(expr, dataType); + if (dataType.isStringLikeType() && ((CharacterType) dataType).getLen() >= 0) { + List<Expression> args = ImmutableList.of( + cast, + new TinyIntLiteral((byte) 1), + Literal.of(((CharacterType) dataType).getLen()) + ); + return new UnboundFunction("substr", args); + } else { + return cast; + } + } + + /* ******************************************************************************************** + * visitLiteral + * ******************************************************************************************** */ + + @Override + protected Object visitLiteral(io.trino.sql.tree.Literal node, ParserContext context) { + return super.visitLiteral(node, context); + } + + @Override + protected Literal visitLongLiteral(io.trino.sql.tree.LongLiteral node, ParserContext context) { + return LogicalPlanBuilderAssistant.handleIntegerLiteral(String.valueOf(node.getValue())); + } + + @Override + protected Object visitDoubleLiteral(io.trino.sql.tree.DoubleLiteral node, ParserContext context) { + return super.visitDoubleLiteral(node, context); + } + + @Override + protected Object visitDecimalLiteral(io.trino.sql.tree.DecimalLiteral node, ParserContext context) { + return super.visitDecimalLiteral(node, context); + } + + @Override + protected Object visitTimestampLiteral(io.trino.sql.tree.TimestampLiteral node, ParserContext context) { + try { + String value = node.getValue(); + if (value.length() <= 10) { + value += " 00:00:00"; + } + return new DateTimeLiteral(value); + } catch (AnalysisException e) { + throw new DialectTransformException("transform timestamp literal"); + } + } + + @Override + protected Object visitGenericLiteral(io.trino.sql.tree.GenericLiteral node, ParserContext context) { + return super.visitGenericLiteral(node, context); + } + + @Override + protected Object visitTimeLiteral(io.trino.sql.tree.TimeLiteral node, ParserContext context) { + return super.visitTimeLiteral(node, context); + } + + @Override + protected Object visitCharLiteral(io.trino.sql.tree.CharLiteral node, ParserContext context) { + return super.visitCharLiteral(node, context); + } + + @Override + protected Expression visitStringLiteral(io.trino.sql.tree.StringLiteral node, ParserContext context) { + // TODO: add unescapeSQLString. + String txt = node.getValue(); + if (txt.length() <= 1) { + return new VarcharLiteral(txt); + } + return new VarcharLiteral(LogicalPlanBuilderAssistant.escapeBackSlash(txt.substring(0, txt.length()))); + } + + @Override + protected Object visitIntervalLiteral(io.trino.sql.tree.IntervalLiteral node, ParserContext context) { + return super.visitIntervalLiteral(node, context); + } + + @Override + protected Object visitBinaryLiteral(io.trino.sql.tree.BinaryLiteral node, ParserContext context) { + return super.visitBinaryLiteral(node, context); + } + + @Override + protected Object visitNullLiteral(io.trino.sql.tree.NullLiteral node, ParserContext context) { + return super.visitNullLiteral(node, context); + } + + @Override + protected Object visitBooleanLiteral(io.trino.sql.tree.BooleanLiteral node, ParserContext context) { + return BooleanLiteral.of(node.getValue()); + } + + private DataType mappingType(io.trino.sql.tree.DataType dataType) { + + if (dataType instanceof io.trino.sql.tree.GenericDataType) { + io.trino.sql.tree.GenericDataType genericDataType = (io.trino.sql.tree.GenericDataType) dataType; + String typeName = genericDataType.getName().getValue().toLowerCase(); + List<String> types = Lists.newArrayList(typeName); + + String length = null; + String precision = null; + String scale = null; + List<io.trino.sql.tree.DataTypeParameter> arguments = genericDataType.getArguments(); + if (!arguments.isEmpty()) { + if (arguments.get(0) instanceof io.trino.sql.tree.NumericParameter) { + precision = length = ((io.trino.sql.tree.NumericParameter) arguments.get(0)).getValue(); + } + if (arguments.size() > 1 && arguments.get(1) instanceof io.trino.sql.tree.NumericParameter) { + scale = ((io.trino.sql.tree.NumericParameter) arguments.get(1)).getValue(); + } + } + if ("decimal".equals(typeName)) { + if (precision != null) { + types.add(precision); + } + if (scale != null) { + types.add(scale); + } + } + if ("varchar".equals(typeName) || "char".equals(typeName)) { + if (length != null) { + types.add(length); + } + } + return DataType.convertPrimitiveFromStrings(types, true); Review Comment: why unsigned always true? ########## 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()); Review Comment: ```suggestion .collect(ImmutableList.toImmutableList()); ``` ########## 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); + } + + @Override + protected Expression visitSingleColumn(io.trino.sql.tree.SingleColumn node, ParserContext context) { + String alias = node.getAlias().map(io.trino.sql.tree.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(io.trino.sql.tree.Identifier node, ParserContext context) { + return new UnboundSlot(Lists.newArrayList(node.getValue())); + } + + /* ******************************************************************************************** + * visitFunction + * ******************************************************************************************** */ + + @Override + protected Function visitFunctionCall(io.trino.sql.tree.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 + * ******************************************************************************************** */ + + @Override + protected LogicalPlan visitTable(io.trino.sql.tree.Table node, ParserContext context) { + io.trino.sql.tree.QualifiedName name = node.getName(); + List<String> tableId = name.getParts(); + List<String> partitionNames = new ArrayList<>(); + // build table + return LogicalPlanBuilderAssistant.withCheckPolicy( + new UnboundRelation(StatementScopeIdGenerator.newRelationId(), tableId, + partitionNames, false)); + } + + /* ******************************************************************************************** + * visit buildIn function + * ******************************************************************************************** */ + + @Override + protected Expression visitCast(io.trino.sql.tree.Cast node, ParserContext context) { + Expression expr = visit(node.getExpression(), context, Expression.class); + DataType dataType = mappingType(node.getType()); + Expression cast = new Cast(expr, dataType); + if (dataType.isStringLikeType() && ((CharacterType) dataType).getLen() >= 0) { + List<Expression> args = ImmutableList.of( + cast, + new TinyIntLiteral((byte) 1), + Literal.of(((CharacterType) dataType).getLen()) + ); + return new UnboundFunction("substr", args); + } else { + return cast; + } + } + + /* ******************************************************************************************** + * visitLiteral + * ******************************************************************************************** */ + + @Override + protected Object visitLiteral(io.trino.sql.tree.Literal node, ParserContext context) { + return super.visitLiteral(node, context); + } + + @Override + protected Literal visitLongLiteral(io.trino.sql.tree.LongLiteral node, ParserContext context) { + return LogicalPlanBuilderAssistant.handleIntegerLiteral(String.valueOf(node.getValue())); + } + + @Override + protected Object visitDoubleLiteral(io.trino.sql.tree.DoubleLiteral node, ParserContext context) { + return super.visitDoubleLiteral(node, context); + } + + @Override + protected Object visitDecimalLiteral(io.trino.sql.tree.DecimalLiteral node, ParserContext context) { + return super.visitDecimalLiteral(node, context); + } + + @Override + protected Object visitTimestampLiteral(io.trino.sql.tree.TimestampLiteral node, ParserContext context) { + try { + String value = node.getValue(); + if (value.length() <= 10) { + value += " 00:00:00"; + } + return new DateTimeLiteral(value); + } catch (AnalysisException e) { + throw new DialectTransformException("transform timestamp literal"); + } + } + + @Override + protected Object visitGenericLiteral(io.trino.sql.tree.GenericLiteral node, ParserContext context) { + return super.visitGenericLiteral(node, context); + } + + @Override + protected Object visitTimeLiteral(io.trino.sql.tree.TimeLiteral node, ParserContext context) { + return super.visitTimeLiteral(node, context); + } + + @Override + protected Object visitCharLiteral(io.trino.sql.tree.CharLiteral node, ParserContext context) { + return super.visitCharLiteral(node, context); + } + + @Override + protected Expression visitStringLiteral(io.trino.sql.tree.StringLiteral node, ParserContext context) { + // TODO: add unescapeSQLString. + String txt = node.getValue(); + if (txt.length() <= 1) { + return new VarcharLiteral(txt); + } + return new VarcharLiteral(LogicalPlanBuilderAssistant.escapeBackSlash(txt.substring(0, txt.length()))); + } + + @Override + protected Object visitIntervalLiteral(io.trino.sql.tree.IntervalLiteral node, ParserContext context) { + return super.visitIntervalLiteral(node, context); + } + + @Override + protected Object visitBinaryLiteral(io.trino.sql.tree.BinaryLiteral node, ParserContext context) { + return super.visitBinaryLiteral(node, context); + } + + @Override + protected Object visitNullLiteral(io.trino.sql.tree.NullLiteral node, ParserContext context) { + return super.visitNullLiteral(node, context); Review Comment: forget to change? ########## fe/fe-core/src/main/java/org/apache/doris/nereids/parser/trino/TrinoFnCallTransformers.java: ########## @@ -0,0 +1,136 @@ +// 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.PlaceholderExpression; +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 com.google.common.collect.Lists; + +import java.util.List; +import java.util.stream.Collectors; + +/** + * 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, AbstractFnCallTransformer> TRANSFORMER_MAP; + private static final ImmutableListMultimap<String, AbstractFnCallTransformer> COMPLEX_TRANSFORMER_MAP; + private static final ImmutableListMultimap.Builder<String, AbstractFnCallTransformer> transformerBuilder = + ImmutableListMultimap.builder(); + private static final ImmutableListMultimap.Builder<String, AbstractFnCallTransformer> complexTransformerBuilder = + ImmutableListMultimap.builder(); + + static { + registerTransformers(); + TRANSFORMER_MAP = transformerBuilder.build(); Review Comment: build should called in registerTransformers -- 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