lincoln-lil commented on code in PR #25163: URL: https://github.com/apache/flink/pull/25163#discussion_r1708428976
########## flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/EltFunction.java: ########## @@ -0,0 +1,48 @@ +/* + * 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.functions.BuiltInFunctionDefinitions; +import org.apache.flink.table.functions.SpecializedFunction.SpecializedContext; + +import javax.annotation.Nullable; + +/** Implementation of {@link BuiltInFunctionDefinitions#ELT}. */ +@Internal +public class EltFunction extends BuiltInScalarFunction { + + public EltFunction(SpecializedContext context) { + super(BuiltInFunctionDefinitions.ELT, context); + } + + public @Nullable Object eval(@Nullable Number index, Object... expr) { + if (index == null) { + return null; + } + + long idx = index.longValue(); + if (idx < 1 || idx > expr.length) { + throw new IndexOutOfBoundsException( Review Comment: Return null instead of throwing exception due to the 'exception to null' contract for most of the scalar functions. ########## flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/StringFunctionsITCase.java: ########## @@ -32,7 +32,90 @@ class StringFunctionsITCase extends BuiltInFunctionTestBase { @Override Stream<TestSetSpec> getTestSetSpecs() { - return Stream.concat(regexpExtractTestCases(), translateTestCases()); + return Stream.of(eltTestCases(), regexpExtractTestCases(), translateTestCases()) + .flatMap(s -> s); + } + + private Stream<TestSetSpec> eltTestCases() { + return Stream.of( + TestSetSpec.forFunction(BuiltInFunctionDefinitions.ELT) + .onFieldsWithData(null, null, null, new byte[] {1, 2, 3}) + .andDataTypes( + DataTypes.INT(), + DataTypes.STRING(), + DataTypes.BYTES(), + DataTypes.BYTES()) + // null input + .testResult( + $("f0").elt("a", "b"), "ELT(f0, 'a', 'b')", null, DataTypes.CHAR(1)) + .testResult(lit(1).elt($("f1")), "ELT(1, f1)", null, DataTypes.STRING()) + .testResult( + lit(1).elt("a", $("f1")), + "ELT(1, 'a', f1)", + "a", + DataTypes.STRING()) + // invalid index + .testTableApiRuntimeError( + lit(0).elt("a"), + IndexOutOfBoundsException.class, + "Index 0 is out of range [1, 1]") + .testSqlRuntimeError( + "ELT(0, 'a')", + IndexOutOfBoundsException.class, + "Index 0 is out of range [1, 1]") + .testTableApiRuntimeError( + lit(4).elt("a", "b", "c"), + IndexOutOfBoundsException.class, + "Index 4 is out of range [1, 3]") + .testSqlRuntimeError( + "ELT(4, 'a', 'b', 'c')", + IndexOutOfBoundsException.class, + "Index 4 is out of range [1, 3]") Review Comment: Also add indexes including negative integer/decimal/greater than int.max ########## flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/EltFunction.java: ########## @@ -0,0 +1,48 @@ +/* + * 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.functions.BuiltInFunctionDefinitions; +import org.apache.flink.table.functions.SpecializedFunction.SpecializedContext; + +import javax.annotation.Nullable; + +/** Implementation of {@link BuiltInFunctionDefinitions#ELT}. */ +@Internal +public class EltFunction extends BuiltInScalarFunction { + + public EltFunction(SpecializedContext context) { + super(BuiltInFunctionDefinitions.ELT, context); + } + + public @Nullable Object eval(@Nullable Number index, Object... expr) { Review Comment: Can we use a `Integer` instead of `Number` here? -- 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: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org