dawidwys commented on code in PR #22951: URL: https://github.com/apache/flink/pull/22951#discussion_r1485933655
########## flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/strategies/ArrayComparableElementArgumentTypeStrategy.java: ########## @@ -34,40 +32,33 @@ import org.apache.flink.table.types.logical.StructuredType.StructuredComparison; import org.apache.flink.util.Preconditions; -import java.util.Collections; import java.util.List; import java.util.Optional; /** - * An {@link InputTypeStrategy} that checks if the input argument is an ARRAY type and check whether - * its' elements are comparable. + * An {@link ArgumentTypeStrategy} that checks if the input argument is an ARRAY type and check + * whether its' elements are comparable. * * <p>It requires one argument. * * <p>For the rules which types are comparable with which types see {@link * #areComparable(LogicalType, LogicalType)}. */ @Internal -public final class ArrayComparableElementTypeStrategy implements InputTypeStrategy { +public final class ArrayComparableElementArgumentTypeStrategy implements ArgumentTypeStrategy { + private final StructuredComparison requiredComparison; - private final ConstantArgumentCount argumentCount; - public ArrayComparableElementTypeStrategy(StructuredComparison requiredComparison) { + public ArrayComparableElementArgumentTypeStrategy(StructuredComparison requiredComparison) { Preconditions.checkArgument(requiredComparison != StructuredComparison.NONE); this.requiredComparison = requiredComparison; - this.argumentCount = ConstantArgumentCount.of(1); - } - - @Override - public ArgumentCount getArgumentCount() { - return argumentCount; } @Override - public Optional<List<DataType>> inferInputTypes( - CallContext callContext, boolean throwOnFailure) { + public Optional<DataType> inferArgumentType( + CallContext callContext, int argumentPos, boolean throwOnFailure) { final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes(); - final DataType argumentType = argumentDataTypes.get(0); + final DataType argumentType = argumentDataTypes.get(argumentPos); if (!argumentType.getLogicalType().is(LogicalTypeRoot.ARRAY)) { return callContext.fail(throwOnFailure, "All arguments requires to be an ARRAY type"); Review Comment: please update the comment It's an argument type strategy now, so it applies to a single argument not `ALL` ########## flink-table/flink-table-common/src/test/java/org/apache/flink/table/types/inference/InputTypeStrategiesTest.java: ########## @@ -640,6 +640,28 @@ ANY, explicit(DataTypes.INT()) .expectArgumentTypes( DataTypes.ARRAY(DataTypes.INT().notNull()).notNull(), DataTypes.INT()), + TestSpec.forStrategy(sequence(SpecificInputTypeStrategies.ARRAY_COMPARABLE)) + .expectSignature("f(<ARRAY<COMPARABLE>>)") + .calledWithArgumentTypes(DataTypes.ARRAY(DataTypes.ROW())) + .expectErrorMessage( + "Invalid input arguments. Expected signatures are:\n" + + "f(<ARRAY<COMPARABLE>>)"), + TestSpec.forStrategy( + "Strategy fails if input argument type is not ARRAY", + sequence(SpecificInputTypeStrategies.ARRAY_COMPARABLE)) + .calledWithArgumentTypes(DataTypes.INT()) + .expectErrorMessage( + "Invalid input arguments. Expected signatures are:\n" + + "f(<ARRAY<COMPARABLE>>)"), + TestSpec.forStrategy( + "Strategy fails if the number of input arguments are not one", + sequence(SpecificInputTypeStrategies.ARRAY_COMPARABLE)) + .calledWithArgumentTypes( + DataTypes.ARRAY(DataTypes.INT()), + DataTypes.ARRAY(DataTypes.STRING())) + .expectErrorMessage( + "Invalid input arguments. Expected signatures are:\n" + + "f(<ARRAY<COMPARABLE>>)"), Review Comment: You test the `sequence` strategy here. We have tests for that already. ########## flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/BuiltInFunctionDefinitions.java: ########## @@ -231,6 +231,22 @@ ANY, and(logical(LogicalTypeRoot.BOOLEAN), LITERAL) "org.apache.flink.table.runtime.functions.scalar.ArrayContainsFunction") .build(); + public static final BuiltInFunctionDefinition ARRAY_SORT = + BuiltInFunctionDefinition.newBuilder() + .name("ARRAY_SORT") + .kind(SCALAR) + .inputTypeStrategy( + or( + sequence(ARRAY_COMPARABLE), + sequence( + ARRAY_COMPARABLE, + InputTypeStrategies.explicit( Review Comment: ```suggestion explicit( ``` ########## flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/ArraySortFunction.java: ########## @@ -0,0 +1,124 @@ +/* + * 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.GenericArrayData; +import org.apache.flink.table.functions.BuiltInFunctionDefinitions; +import org.apache.flink.table.functions.FunctionContext; +import org.apache.flink.table.functions.SpecializedFunction; +import org.apache.flink.table.types.CollectionDataType; +import org.apache.flink.table.types.DataType; +import org.apache.flink.util.FlinkRuntimeException; + +import javax.annotation.Nullable; + +import java.lang.invoke.MethodHandle; +import java.util.Arrays; +import java.util.Comparator; + +import static org.apache.flink.table.api.Expressions.$; + +/** Implementation of ARRAY_SORT function. */ +@Internal +public class ArraySortFunction extends BuiltInScalarFunction { + + private final ArrayData.ElementGetter elementGetter; + private final SpecializedFunction.ExpressionEvaluator greaterEvaluator; + + private transient MethodHandle greaterHandle; + + public ArraySortFunction(SpecializedFunction.SpecializedContext context) { + super(BuiltInFunctionDefinitions.ARRAY_SORT, context); + final DataType elementDataType = + ((CollectionDataType) context.getCallContext().getArgumentDataTypes().get(0)) + .getElementDataType() + .toInternal(); + elementGetter = + ArrayData.createElementGetter(elementDataType.toInternal().getLogicalType()); + greaterEvaluator = + context.createEvaluator( + $("element1").isGreater($("element2")), + DataTypes.BOOLEAN(), + DataTypes.FIELD("element1", elementDataType.notNull().toInternal()), + DataTypes.FIELD("element2", elementDataType.notNull().toInternal())); + } + + @Override + public void open(FunctionContext context) throws Exception { + greaterHandle = greaterEvaluator.open(context); + } + + public @Nullable ArrayData eval(ArrayData array, Boolean... ascendingOrder) { + try { + if (array == null) { + return null; + } + if (array.size() == 0) { + return array; + } + boolean isAscending = ascendingOrder.length > 0 ? ascendingOrder[0] : true; + Object[] elements = new Object[array.size()]; + for (int i = 0; i < array.size(); i++) { + elements[i] = elementGetter.getElementOrNull(array, i); + } + Comparator<Object> ascendingComparator = new ArraySortComparator(isAscending); + Arrays.sort(elements, ascendingComparator); + return new GenericArrayData(elements); + } catch (Throwable t) { + throw new FlinkRuntimeException(t); + } + } + + private class ArraySortComparator implements Comparator<Object> { + private final boolean isAscending; + + public ArraySortComparator(boolean isAscending) { + this.isAscending = isAscending; + } + + @Override + public int compare(Object o1, Object o2) { + Comparator<Object> baseComparator = + (a, b) -> { + try { + if (a == null || b == null) { + return 0; + } + boolean isGreater = (boolean) greaterHandle.invoke(a, b); + return isGreater ? 1 : -1; + } catch (Throwable e) { + throw new RuntimeException(e); + } + }; + Comparator<Object> comparator = + isAscending + ? Comparator.nullsFirst(baseComparator) + : Comparator.nullsLast(baseComparator.reversed()); Review Comment: Good that you used the `Comparator.nullsFirst`, but could you please move it outside the `ArraySortComparator` It does not make sense to create a new comparator for every single record in the array. e.g. ``` Comparator<Object> ascendingComparator = new ArraySortComparator(isAscending); Arrays.sort(elements, isAscending ? Comparator.nullsFirst(ascendingComparator) : Comparator.nullsLast(ascendingComparator)); ``` ########## flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/strategies/SpecificInputTypeStrategies.java: ########## @@ -89,6 +90,10 @@ public static InputTypeStrategy windowTimeIndicator() { public static final ArgumentTypeStrategy ARRAY_ELEMENT_ARG = new ArrayElementArgumentTypeStrategy(); + /** Argument type representing the array is comparable. */ + public static final ArgumentTypeStrategy ARRAY_COMPARABLE = Review Comment: ```suggestion public static final ArgumentTypeStrategy ARRAY_FULLY_COMPARABLE = ``` -- 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