davidradl commented on code in PR #28970: URL: https://github.com/apache/flink/pull/28970#discussion_r3880054722
########## flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/MapContainsKeyFunction.java: ########## @@ -0,0 +1,97 @@ +/* + * 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.flink.table.runtime.functions.scalar; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.table.api.DataTypes; +import org.apache.flink.table.data.ArrayData; +import org.apache.flink.table.data.MapData; +import org.apache.flink.table.functions.BuiltInFunctionDefinitions; +import org.apache.flink.table.functions.FunctionContext; +import org.apache.flink.table.functions.SpecializedFunction.ExpressionEvaluator; +import org.apache.flink.table.functions.SpecializedFunction.SpecializedContext; +import org.apache.flink.table.types.DataType; +import org.apache.flink.table.types.KeyValueDataType; +import org.apache.flink.util.FlinkRuntimeException; + +import javax.annotation.Nullable; + +import java.lang.invoke.MethodHandle; + +import static org.apache.flink.table.api.Expressions.$; + +/** Implementation of {@link BuiltInFunctionDefinitions#MAP_CONTAINS_KEY}. */ +@Internal +public class MapContainsKeyFunction extends BuiltInScalarFunction { + + private final ArrayData.ElementGetter keyElementGetter; + private final ExpressionEvaluator equalityEvaluator; + private transient MethodHandle equalityHandle; + + public MapContainsKeyFunction(SpecializedContext context) { + super(BuiltInFunctionDefinitions.MAP_CONTAINS_KEY, context); + final DataType mapDataType = context.getCallContext().getArgumentDataTypes().get(0); + final DataType keyDataType = ((KeyValueDataType) mapDataType).getKeyDataType(); + + keyElementGetter = ArrayData.createElementGetter(keyDataType.getLogicalType()); + equalityEvaluator = + context.createEvaluator( + $("key").isEqual($("needle")), + DataTypes.BOOLEAN(), + DataTypes.FIELD("key", keyDataType.notNull().toInternal()), + DataTypes.FIELD("needle", keyDataType.notNull().toInternal())); + } + + @Override + public void open(FunctionContext context) throws Exception { + equalityHandle = equalityEvaluator.open(context); + } + + public @Nullable Boolean eval(@Nullable MapData map, @Nullable Object needle) { + if (map == null) { + return null; + } + final ArrayData keys = map.keyArray(); + final int size = map.size(); + for (int pos = 0; pos < size; pos++) { + final Object key = keyElementGetter.getElementOrNull(keys, pos); + // NULL matches NULL here, unlike the SQL `NULL = NULL` the evaluator would apply + if (needle == null && key == null) { + return true; + } + if (needle != null && key != null && isEqual(key, needle)) { + return true; + } + } + return false; + } + + private boolean isEqual(final Object key, final Object needle) { + try { + return (boolean) equalityHandle.invoke(key, needle); + } catch (Throwable t) { + throw new FlinkRuntimeException(t); Review Comment: Hi there, As this is a function is a test to say does the map contain a key function - if it fails I assume that it doesn't contain the key function, so false would seem reasonable. What sort of errors can we get here, that we would reasonably want to surface? At the same time I am keen we should follow the convention of the other functions. Though we could argue ARRAY_CONTAINS is a test also, so should not error, but ARRAY_REMOVE is a mutation , which if it fails should reasonably be an error. -- 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]
