HappenLee commented on code in PR #66734: URL: https://github.com/apache/doris/pull/66734#discussion_r3776175945
########## fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/generator/Stack.java: ########## @@ -0,0 +1,146 @@ +// 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.trees.expressions.functions.generator; + +import org.apache.doris.catalog.FunctionSignature; +import org.apache.doris.nereids.exceptions.AnalysisException; +import org.apache.doris.nereids.rules.expression.rules.FoldConstantRuleOnFE; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.functions.AlwaysNullable; +import org.apache.doris.nereids.trees.expressions.functions.ComputePrecision; +import org.apache.doris.nereids.trees.expressions.functions.CustomSignature; +import org.apache.doris.nereids.trees.expressions.literal.IntegerLikeLiteral; +import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor; +import org.apache.doris.nereids.types.DataType; +import org.apache.doris.nereids.types.IntegerType; +import org.apache.doris.nereids.types.NullType; +import org.apache.doris.nereids.types.StructField; +import org.apache.doris.nereids.types.StructType; +import org.apache.doris.nereids.util.ExpressionUtils; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; + +import java.util.ArrayList; +import java.util.List; + +/** + * stack(n, expr1, ..., exprk) separates the expressions into n rows in row-major order. + * Missing values in the last row are padded with nulls. + */ +public class Stack extends TableGeneratingFunction implements CustomSignature, ComputePrecision, AlwaysNullable { + + /** constructor with two or more arguments. */ + public Stack(Expression numRows, Expression argument, Expression... otherArguments) { + super("stack", ExpressionUtils.mergeArguments(numRows, argument, otherArguments)); + } + + /** constructor for withChildren and reuse signature. */ + private Stack(GeneratorFunctionParams functionParams) { + super(functionParams); + } + + @Override + public Stack withChildren(List<Expression> children) { + Preconditions.checkArgument(children.size() >= 2); + return new Stack(getFunctionParams(children)); + } + + @Override + public void checkLegalityBeforeTypeCoercion() { + getColumnTypes(); + } + + @Override + public FunctionSignature computePrecision(FunctionSignature signature) { + return signature; + } + + @Override + public FunctionSignature searchSignature(List<FunctionSignature> signatures) { + return super.searchSignature(signatures); + } + + @Override + public FunctionSignature customSignature() { + List<DataType> columnTypes = getColumnTypes(); + List<DataType> argumentTypes = new ArrayList<>(arity()); + argumentTypes.add(IntegerType.INSTANCE); + for (int i = 1; i < arity(); i++) { + argumentTypes.add(columnTypes.get((i - 1) % columnTypes.size())); + } + + if (columnTypes.size() == 1) { + return FunctionSignature.of(columnTypes.get(0), argumentTypes); + } + ImmutableList.Builder<StructField> fields = ImmutableList.builder(); + for (int i = 0; i < columnTypes.size(); i++) { + fields.add(new StructField("col" + i, columnTypes.get(i), true, "")); + } + return FunctionSignature.of(new StructType(fields.build()), argumentTypes); Review Comment: Fixed in 2351b0fc7c6. Multi-column Stack now always enters STRUCT expansion and exact alias-count validation, including the previously bypassed one-alias case. A 3-column stack with 1, 2, or 4 aliases returns AnalysisException; exactly 3 aliases still analyzes successfully. Single-column Stack and the existing one-alias behavior of other struct-returning generators are unchanged. Added a direct Nereids analyzer unit test plus the one-alias regression case. ########## fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/generator/Stack.java: ########## @@ -0,0 +1,146 @@ +// 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.trees.expressions.functions.generator; + +import org.apache.doris.catalog.FunctionSignature; +import org.apache.doris.nereids.exceptions.AnalysisException; +import org.apache.doris.nereids.rules.expression.rules.FoldConstantRuleOnFE; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.functions.AlwaysNullable; +import org.apache.doris.nereids.trees.expressions.functions.ComputePrecision; +import org.apache.doris.nereids.trees.expressions.functions.CustomSignature; +import org.apache.doris.nereids.trees.expressions.literal.IntegerLikeLiteral; +import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor; +import org.apache.doris.nereids.types.DataType; +import org.apache.doris.nereids.types.IntegerType; +import org.apache.doris.nereids.types.NullType; +import org.apache.doris.nereids.types.StructField; +import org.apache.doris.nereids.types.StructType; +import org.apache.doris.nereids.util.ExpressionUtils; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; + +import java.util.ArrayList; +import java.util.List; + +/** + * stack(n, expr1, ..., exprk) separates the expressions into n rows in row-major order. + * Missing values in the last row are padded with nulls. + */ +public class Stack extends TableGeneratingFunction implements CustomSignature, ComputePrecision, AlwaysNullable { + + /** constructor with two or more arguments. */ + public Stack(Expression numRows, Expression argument, Expression... otherArguments) { + super("stack", ExpressionUtils.mergeArguments(numRows, argument, otherArguments)); + } + + /** constructor for withChildren and reuse signature. */ + private Stack(GeneratorFunctionParams functionParams) { + super(functionParams); + } + + @Override + public Stack withChildren(List<Expression> children) { + Preconditions.checkArgument(children.size() >= 2); + return new Stack(getFunctionParams(children)); + } + + @Override + public void checkLegalityBeforeTypeCoercion() { + getColumnTypes(); + } + + @Override + public FunctionSignature computePrecision(FunctionSignature signature) { + return signature; + } + + @Override + public FunctionSignature searchSignature(List<FunctionSignature> signatures) { + return super.searchSignature(signatures); + } + + @Override + public FunctionSignature customSignature() { + List<DataType> columnTypes = getColumnTypes(); + List<DataType> argumentTypes = new ArrayList<>(arity()); + argumentTypes.add(IntegerType.INSTANCE); + for (int i = 1; i < arity(); i++) { + argumentTypes.add(columnTypes.get((i - 1) % columnTypes.size())); + } + + if (columnTypes.size() == 1) { + return FunctionSignature.of(columnTypes.get(0), argumentTypes); + } + ImmutableList.Builder<StructField> fields = ImmutableList.builder(); + for (int i = 0; i < columnTypes.size(); i++) { + fields.add(new StructField("col" + i, columnTypes.get(i), true, "")); + } + return FunctionSignature.of(new StructType(fields.build()), argumentTypes); + } + + private int getNumRows() { + Expression numRowsArgument = getArgument(0); + Expression evaluated = FoldConstantRuleOnFE.evaluate(numRowsArgument, null); Review Comment: Fixed completely at the current head, including 2351b0fc7c6. Stack now rejects every row-count expression for which Expression.isConstant() is false before calling the null-context constant folder. The existing cardinality evaluator from the preceding fix still reduces cardinality([1, 2]), so valid foldable integer constants remain accepted. Added current_catalog() as a deterministic runtime-only counterexample: it previously reached a context-dependent fold visitor, but now returns the documented AnalysisException. StackTest passes 8/8. -- 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]
