dawidwys commented on code in PR #22951: URL: https://github.com/apache/flink/pull/22951#discussion_r1490931709
########## flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/ArraySortFunction.java: ########## @@ -0,0 +1,127 @@ +/* + * 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) { + return eval(array, true, true); + } + + public @Nullable ArrayData eval(ArrayData array, Boolean ascendingOrder) { + return eval(array, ascendingOrder, ascendingOrder); + } + + public @Nullable ArrayData eval(ArrayData array, Boolean ascendingOrder, Boolean nullFirst) { + try { + if (array == null || ascendingOrder == null || nullFirst == null) { + return null; + } + if (array.size() == 0) { + return array; + } + 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(ascendingOrder); + Arrays.sort( + elements, + nullFirst + ? Comparator.nullsFirst(ascendingComparator) + : Comparator.nullsLast(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) { + if (o1 == null || o2 == null) { + return 0; + } Review Comment: Is this necessary? This should be handled by the `nullsFirst/nullsLast`, no? ########## flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/BuiltInFunctionDefinitions.java: ########## @@ -231,6 +231,28 @@ 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_FULLY_COMPARABLE), + sequence( + ARRAY_FULLY_COMPARABLE, + InputTypeStrategies.explicit( + DataTypes.BOOLEAN().nullable())), + sequence( + ARRAY_FULLY_COMPARABLE, + InputTypeStrategies.explicit( + DataTypes.BOOLEAN().nullable()), + InputTypeStrategies.explicit( + DataTypes.BOOLEAN().nullable())))) Review Comment: Let's use `logical(LogicalTypeRoot.BOOLEAN)` here. That way the output strategy have a chance of being `NOT NULL`. In the current state the output has always `NULLABLE` type, because 2nd and 3rd aguments are nullable. If you do: `logical(LogicalTypeRoot.BOOLEAN)` and pass e.g. literals: `ARRAY(1, 2, 3), true, true`, the output type will be `NOT NULL`, because all three arguments have `NOT NULL` types. The strategy here does not only perform validation, but it also does type coercion. -- 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]
