gustavodemorais commented on code in PR #28970:
URL: https://github.com/apache/flink/pull/28970#discussion_r3932919191


##########
flink-table/flink-table-common/src/test/java/org/apache/flink/table/types/inference/InputTypeStrategiesTest.java:
##########
@@ -644,6 +644,43 @@ ANY, explicit(DataTypes.INT())
                         .expectArgumentTypes(
                                 
DataTypes.ARRAY(DataTypes.INT().notNull()).notNull(),
                                 DataTypes.INT()),
+                TestSpec.forStrategy(
+                                "MapKey argument type strategy implicitly 
casts the key",
+                                sequence(
+                                        logical(LogicalTypeRoot.MAP),
+                                        
SpecificInputTypeStrategies.MAP_KEY_ARG))
+                        .calledWithArgumentTypes(
+                                DataTypes.MAP(DataTypes.BIGINT().notNull(), 
DataTypes.STRING()),
+                                DataTypes.INT().notNull())
+                        .expectSignature("f(<MAP>, <MAP KEY>)")
+                        .expectArgumentTypes(
+                                DataTypes.MAP(DataTypes.BIGINT().notNull(), 
DataTypes.STRING()),
+                                DataTypes.BIGINT().notNull()),
+                TestSpec.forStrategy(
+                                "MapKey argument type strategy widens a NOT 
NULL key type "
+                                        + "for a nullable argument",
+                                sequence(
+                                        logical(LogicalTypeRoot.MAP),
+                                        
SpecificInputTypeStrategies.MAP_KEY_ARG))
+                        .calledWithArgumentTypes(
+                                DataTypes.MAP(DataTypes.BIGINT().notNull(), 
DataTypes.STRING())
+                                        .notNull(),
+                                DataTypes.BIGINT())
+                        .expectArgumentTypes(
+                                DataTypes.MAP(DataTypes.BIGINT().notNull(), 
DataTypes.STRING())
+                                        .notNull(),
+                                DataTypes.BIGINT()),
+                TestSpec.forStrategy(
+                                "MapKey argument type strategy rejects a key 
that cannot be cast",
+                                sequence(
+                                        logical(LogicalTypeRoot.MAP),
+                                        
SpecificInputTypeStrategies.MAP_KEY_ARG))
+                        .calledWithArgumentTypes(
+                                DataTypes.MAP(DataTypes.INT(), 
DataTypes.STRING()),
+                                DataTypes.BOOLEAN())
+                        .expectErrorMessage(
+                                "Invalid input arguments. Expected signatures 
are:\n"

Review Comment:
   Test should be failing since you updated the msg to "Unsupported argument 
type.."



##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/MapContainsKeyFunction.java:
##########
@@ -0,0 +1,102 @@
+/*
+ * 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();
+        if (needle == null) {
+            // A NULL needle matches a NULL key, unlike SQL `NULL = NULL` 
which yields UNKNOWN.
+            for (int pos = 0; pos < size; pos++) {
+                if (keyElementGetter.getElementOrNull(keys, pos) == null) {
+                    return true;
+                }
+            }
+        } else {
+            for (int pos = 0; pos < size; pos++) {
+                final Object key = keyElementGetter.getElementOrNull(keys, 
pos);
+                if (key != null && isEqual(key, needle)) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }

Review Comment:
   We have an additional if condition for each element in the smaller version. 
I think this is small performance diff since we also do things like 
"isEqual(elementKey, needle)" here. However, I agree it's a performance diff 
and Ramin seems to also be +1 to the previous version. I'd say @VasShabu can 
rollback to your suggestion and maybe only add a very short comment to explain 
why. Useful since there are other functions like ArrayContainsFunction that use 
the shorter version and one might wonder why we went with the lengthy version



-- 
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]

Reply via email to