dawidwys commented on code in PR #22951:
URL: https://github.com/apache/flink/pull/22951#discussion_r1484177231


##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/ArraySortFunction.java:
##########
@@ -0,0 +1,141 @@
+/*
+ * 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();
+        elementGetter = 
ArrayData.createElementGetter(elementDataType.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 || ascendingOrder.length > 0 && 
ascendingOrder[0] == null) {
+                return null;
+            }

Review Comment:
   This is not covered by the output strategy, is it? The output strategy does 
not depend on the second argument.



##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/ArraySortFunction.java:
##########
@@ -0,0 +1,141 @@
+/*
+ * 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();
+        elementGetter = 
ArrayData.createElementGetter(elementDataType.getLogicalType());

Review Comment:
   The types in the `elementGetter` and `greaterEvaluator` should use the same 
conversion class. I think it's best to operate on the internal types. So we 
should use:
   
   `elementDataType.toInternal()`



##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/strategies/ArrayComparableElementArgumentTypeStrategy.java:
##########
@@ -0,0 +1,125 @@
+/*
+ * 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.table.functions.FunctionDefinition;
+import org.apache.flink.table.types.CollectionDataType;
+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;
+import org.apache.flink.table.types.logical.LegacyTypeInformationType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.LogicalTypeFamily;
+import org.apache.flink.table.types.logical.LogicalTypeRoot;
+import org.apache.flink.table.types.logical.StructuredType;
+
+import java.util.List;
+import java.util.Optional;
+
+/**
+ * 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)}.
+ */
+public final class ArrayComparableElementArgumentTypeStrategy implements 
ArgumentTypeStrategy {

Review Comment:
   Why do we need this class? This basically a copy of 
https://github.com/apache/flink/blob/master/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/strategies/ArrayComparableElementTypeStrategy.java
   
   Something's not right here. We need to get rid of this code duplication.



##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/ArraySortFunction.java:
##########
@@ -0,0 +1,141 @@
+/*
+ * 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();
+        elementGetter = 
ArrayData.createElementGetter(elementDataType.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 || ascendingOrder.length > 0 && 
ascendingOrder[0] == 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);
+            }
+            if (isAscending) {
+                Comparator<Object> ascendingComparator = new 
MyComparator(true);
+                Arrays.sort(elements, ascendingComparator);
+            } else {
+                Comparator<Object> ascendingComparator = new 
MyComparator(false);
+                Arrays.sort(elements, ascendingComparator);
+            }

Review Comment:
   ```suggestion
   Comparator<Object> ascendingComparator = new MyComparator(isAscending);
   Arrays.sort(elements, ascendingComparator);
   ```



##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/ArraySortFunction.java:
##########
@@ -0,0 +1,141 @@
+/*
+ * 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();
+        elementGetter = 
ArrayData.createElementGetter(elementDataType.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 || ascendingOrder.length > 0 && 
ascendingOrder[0] == 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);
+            }
+            if (isAscending) {
+                Comparator<Object> ascendingComparator = new 
MyComparator(true);
+                Arrays.sort(elements, ascendingComparator);
+            } else {
+                Comparator<Object> ascendingComparator = new 
MyComparator(false);
+                Arrays.sort(elements, ascendingComparator);
+            }
+            return new GenericArrayData(elements);
+        } catch (Throwable t) {
+            throw new FlinkRuntimeException(t);
+        }
+    }
+
+    private class MyComparator implements Comparator<Object> {
+        private final boolean isAscending;
+
+        public MyComparator(boolean isAscending) {
+            this.isAscending = isAscending;
+        }
+
+        @Override
+        public int compare(Object o1, Object o2) {
+            try {
+                if (isAscending) {
+                    if (o1 == null) {
+                        return -1;
+                    }
+                    if (o2 == null) {
+                        return -1;
+                    }
+                    boolean found = (boolean) greaterHandle.invoke(o1, o2);

Review Comment:
   ```suggestion
                       boolean isGreater = (boolean) greaterHandle.invoke(o1, 
o2);
   ```



##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/ArraySortFunction.java:
##########
@@ -0,0 +1,141 @@
+/*
+ * 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();
+        elementGetter = 
ArrayData.createElementGetter(elementDataType.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 || ascendingOrder.length > 0 && 
ascendingOrder[0] == 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);
+            }
+            if (isAscending) {
+                Comparator<Object> ascendingComparator = new 
MyComparator(true);
+                Arrays.sort(elements, ascendingComparator);
+            } else {
+                Comparator<Object> ascendingComparator = new 
MyComparator(false);
+                Arrays.sort(elements, ascendingComparator);
+            }
+            return new GenericArrayData(elements);
+        } catch (Throwable t) {
+            throw new FlinkRuntimeException(t);
+        }
+    }
+
+    private class MyComparator implements Comparator<Object> {
+        private final boolean isAscending;
+
+        public MyComparator(boolean isAscending) {
+            this.isAscending = isAscending;
+        }
+
+        @Override
+        public int compare(Object o1, Object o2) {
+            try {
+                if (isAscending) {
+                    if (o1 == null) {
+                        return -1;
+                    }
+                    if (o2 == null) {
+                        return -1;
+                    }

Review Comment:
   Let's use `Comparator.nullsLast` and `Comparator.nullsFirst` instead.



##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/BuiltInFunctionDefinitions.java:
##########
@@ -231,6 +232,21 @@ 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(new 
ArrayComparableElementArgumentTypeStrategy()),
+                                    sequence(
+                                            new 
ArrayComparableElementArgumentTypeStrategy(),
+                                            logical(LogicalTypeRoot.BOOLEAN))))

Review Comment:
   Shouldn't the second argument be `NOT NULL`? What is the behaviour for 
`NULL` ascendingOrder?
   
   I believe you need `explicit(DataTypes.BOOLEAN().notNull())` 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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to