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


##########
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:
   The catch Throwable here mirrors other builtin function's implementation, 
functions like :
    - ARRAY_CONTAINS
    - ARRAY_POSITION
    - ARRAY_REMOVE
   
   I was also talking to my mentor about logging and how it is not great in 
streaming as we are processing millions of records and logs can get full really 
quickly and how you dont know easily what error causes a specific entry in a 
log.
   
   Please let me know your thoughts, intrested to hear more, I see that there 
are two options, either keep this implementation or alter other functions 
aswell to ensure that we are returning False as you say and logging. However if 
we decide to do this to the naked eye some invalid input that would cause an 
error would be hidden by a silent failure only to be discovered in a log.
   
   Would love to hear you thoughts regarding this.
   
   Thanks
   Vas.



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