morrySnow commented on code in PR #66968: URL: https://github.com/apache/doris/pull/66968#discussion_r3879385973
########## fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/MapApply.java: ########## @@ -0,0 +1,129 @@ +// 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.scalar; + +import org.apache.doris.catalog.FunctionSignature; +import org.apache.doris.nereids.exceptions.AnalysisException; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.PreferPushDownProject; +import org.apache.doris.nereids.trees.expressions.functions.CustomSignature; +import org.apache.doris.nereids.trees.expressions.functions.PropagateNullable; +import org.apache.doris.nereids.trees.expressions.functions.RewriteWhenAnalyze; +import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor; +import org.apache.doris.nereids.types.ArrayType; +import org.apache.doris.nereids.types.DataType; +import org.apache.doris.nereids.types.MapType; +import org.apache.doris.nereids.types.StructField; +import org.apache.doris.nereids.types.StructType; + +import com.google.common.base.Preconditions; + +import java.util.List; + +/** + * Scalar function map_apply. + * + * <p>The lambda produces a two-field Struct for each Map entry. After analysis, the mapped entry + * array is converted directly to Map: + * + * <pre> + * map_apply((mapKey, mapValue) -> struct(newKey, newValue), inputMap) + * -> + * map_from_entries(array_map( + * entry -> struct(newKey(entry[1], entry[2]), newValue(entry[1], entry[2])), + * map_entries(inputMap))) + * </pre> + */ +public class MapApply extends ScalarFunction + implements CustomSignature, PropagateNullable, PreferPushDownProject, + RewriteWhenAnalyze { + + public MapApply(Expression arg) { + this(MapLambdaFunctionUtils.requireLambda("map_apply", arg)); + } + + private MapApply(Lambda lambda) { + this(validateAndRewrite(lambda)); + } + + private MapApply(MapLambdaFunctionUtils.RewrittenMapLambda rewrittenLambda) { + super("map_apply", rewrittenLambda.getMapExpression(), rewrittenLambda.toArrayMap()); + } + + private MapApply(ScalarFunctionParams functionParams) { + super(functionParams); + } + + @Override + public FunctionSignature customSignature() { + DataType mappedEntriesType = getArgument(1).getDataType(); + if (!(mappedEntriesType instanceof ArrayType) + || !(((ArrayType) mappedEntriesType).getItemType() instanceof StructType)) { + throw invalidReturnType(); + } + StructType structType = (StructType) ((ArrayType) mappedEntriesType).getItemType(); + if (structType.getFields().size() != 2) { + throw invalidReturnType(); + } + MapType inputMapType = (MapType) getArgument(0).getDataType(); + List<StructField> fields = structType.getFields(); + MapType resultType = MapType.of(fields.get(0).getDataType(), fields.get(1).getDataType()); + resultType.validateDataType(); Review Comment: **[P1] Normalize lambda NullType before validating the result map** The mapped entry type can still contain NullType for valid lambda bodies such as null, [], or struct(k, null). Calling validateDataType() here rejects these expressions before the rewritten MapFromEntries can apply the standard recursive NullType-to-TinyInt normalization. The same issue exists in TransformKeys and TransformValues. Removing the previous inference from the input map type is reasonable, but please replace it with the generic normalization used by MapFromArrays.computeSignature() / MapFromEntries.customSignature(), and use the normalized mapped-entry type for both result-type derivation and argument coercion. The NullType FE/regression cases removed in 04d5459bbdb should also be restored with the generic TinyInt expectation. -- 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]
