raminqaf commented on code in PR #28970: URL: https://github.com/apache/flink/pull/28970#discussion_r3925618928
########## flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/MapContainsKeyFunction.java: ########## @@ -0,0 +1,96 @@ +/* + * 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 elementKey = keyElementGetter.getElementOrNull(keys, pos); + // A NULL needle matches a NULL key, unlike SQL `NULL = NULL` which yields UNKNOWN. + if (needle == null && elementKey == null) { + return true; + } else if (needle != null && elementKey != null && isEqual(elementKey, needle)) { + return true; + } Review Comment: How about? ```suggestion // A NULL needle matches a NULL key, unlike SQL `NULL = NULL` which yields UNKNOWN. if (keysEqual(elementKey, needle)) { return true; } ``` ```java private boolean keysEqual(@Nullable Object elementKey, @Nullable Object needle) { // A NULL needle matches a NULL key, unlike SQL `NULL = NULL` which yields UNKNOWN. return needle == null ? elementKey == null : elementKey != null && isEqual(elementKey, needle); } ``` ########## flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/strategies/MapKeyArgumentTypeStrategy.java: ########## @@ -0,0 +1,69 @@ +/* + * 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.types.inference.strategies; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.table.api.DataTypes; +import org.apache.flink.table.functions.BuiltInFunctionDefinitions; +import org.apache.flink.table.functions.FunctionDefinition; +import org.apache.flink.table.types.DataType; +import org.apache.flink.table.types.inference.ArgumentTypeStrategy; +import org.apache.flink.table.types.inference.CallContext; +import org.apache.flink.table.types.inference.Signature.Argument; +import org.apache.flink.table.types.logical.LogicalType; +import org.apache.flink.table.types.logical.MapType; + +import java.util.List; +import java.util.Optional; + +import static org.apache.flink.table.types.logical.utils.LogicalTypeCasts.supportsImplicitCast; + +/** + * Specific {@link ArgumentTypeStrategy} for {@link BuiltInFunctionDefinitions#MAP_CONTAINS_KEY}. + */ Review Comment: Maybe mention for which specific argument you are inferring ########## flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/strategies/MapKeyArgumentTypeStrategy.java: ########## @@ -0,0 +1,69 @@ +/* + * 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.types.inference.strategies; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.table.api.DataTypes; +import org.apache.flink.table.functions.BuiltInFunctionDefinitions; +import org.apache.flink.table.functions.FunctionDefinition; +import org.apache.flink.table.types.DataType; +import org.apache.flink.table.types.inference.ArgumentTypeStrategy; +import org.apache.flink.table.types.inference.CallContext; +import org.apache.flink.table.types.inference.Signature.Argument; +import org.apache.flink.table.types.logical.LogicalType; +import org.apache.flink.table.types.logical.MapType; + +import java.util.List; +import java.util.Optional; + +import static org.apache.flink.table.types.logical.utils.LogicalTypeCasts.supportsImplicitCast; + +/** + * Specific {@link ArgumentTypeStrategy} for {@link BuiltInFunctionDefinitions#MAP_CONTAINS_KEY}. + */ +@Internal +class MapKeyArgumentTypeStrategy implements ArgumentTypeStrategy { + + @Override + public Optional<DataType> inferArgumentType( + CallContext callContext, int argumentPos, boolean throwOnFailure) { + List<DataType> argumentTypes = callContext.getArgumentDataTypes(); + final MapType mapType = (MapType) argumentTypes.get(0).getLogicalType(); Review Comment: Extract `0` to a const and call it something like `MAP_ARGUMENT_POS` ########## docs/data/sql_functions.yml: ########## @@ -925,6 +925,23 @@ collection: - sql: MAP_ENTRIES(map) table: MAP.mapEntries() description: Returns an array of all entries in the given map. No order guaranteed. + - sql: MAP_CONTAINS_KEY(map, key) + table: MAP.mapContainsKey(key) + description: | + Returns TRUE if the given key exists in the map, FALSE otherwise. Returns NULL if the map is + NULL. + + If the search key is NULL, the function returns TRUE when the map contains a NULL key. + The given key is cast implicitly to the map's key type where Flink's implicit casting rules + allow it; otherwise the call fails validation. Review Comment: Can you please provide an example for this too? > The given key is cast implicitly to the map's key type where Flink's implicit casting rules allow it; otherwise the call fails validation. ########## 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: In practice it almost never fires. The caller only calls `isEqual` when both key and needle are non-null (the `needle != null && elementKey != null` guard), and generated equality on two internal values of the same type is exception-free. So it's mostly defensive. It would realistically only trigger on an internal codegen or runtime bug while comparing two keys, or a JVM Error bubbling up through invoke. -- 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]
