twalthr commented on code in PR #29967:
URL: https://github.com/apache/flink/pull/29967#discussion_r4694503655


##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/DefaultGeneratedComparator.java:
##########
@@ -0,0 +1,81 @@
+/*
+ * 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;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.functions.FunctionContext;
+import 
org.apache.flink.table.functions.SpecializedFunction.GeneratedComparator;
+import org.apache.flink.table.runtime.generated.GeneratedRecordComparator;
+import org.apache.flink.table.runtime.generated.RecordComparator;
+
+/**
+ * Default runtime implementation for {@link GeneratedComparator}, backed by a 
generated {@link
+ * RecordComparator}. A value that is not a row is compared as a row of one 
field.
+ */
+@Internal
+public final class DefaultGeneratedComparator implements GeneratedComparator {
+
+    private static final long serialVersionUID = 1L;
+
+    private final GeneratedRecordComparator generated;
+
+    private final boolean isComposite;
+
+    private final boolean isNullFirst;
+
+    private transient RecordComparator comparator;
+
+    private transient GenericRowData leftRow;
+
+    private transient GenericRowData rightRow;
+
+    public DefaultGeneratedComparator(
+            final GeneratedRecordComparator generated,
+            final boolean isComposite,
+            final boolean isNullFirst) {
+        this.generated = generated;
+        this.isComposite = isComposite;
+        this.isNullFirst = isNullFirst;
+    }
+
+    @Override
+    public void open(final FunctionContext context) {
+        comparator = generated.newInstance(context.getUserCodeClassLoader());
+        leftRow = new GenericRowData(1);
+        rightRow = new GenericRowData(1);
+    }
+
+    @Override
+    public int compare(final Object left, final Object right) {
+        if (isComposite) {
+            if (left == null || right == null) {
+                if (left == right) {
+                    return 0;
+                }
+                return (left == null) == isNullFirst ? -1 : 1;
+            }

Review Comment:
   Is this part needed? Top-level rows should never be null, no?



##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/definitions/DeduplicateDefinition.java:
##########
@@ -0,0 +1,233 @@
+/*
+ * 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.functions.definitions;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.DataTypes.Field;
+import org.apache.flink.table.connector.ChangelogMode;
+import org.apache.flink.table.functions.BuiltInFunctionDefinition;
+import org.apache.flink.table.functions.ChangelogFunction.ChangelogContext;
+import org.apache.flink.table.functions.ChangelogModeStrategy;
+import org.apache.flink.table.functions.TableSemantics;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.inference.CallContext;
+import org.apache.flink.table.types.inference.InputTypeStrategy;
+import org.apache.flink.table.types.inference.StaticArgument;
+import org.apache.flink.table.types.inference.StaticArgumentTrait;
+import org.apache.flink.table.types.inference.TypeStrategy;
+import 
org.apache.flink.table.types.inference.strategies.ChangelogTypeStrategyUtils;
+import 
org.apache.flink.table.types.inference.strategies.ValidationOnlyInputTypeStrategy;
+import org.apache.flink.table.types.inference.utils.CallContextUtils;
+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.types.ColumnList;
+import org.apache.flink.types.Row;
+import org.apache.flink.types.RowKind;
+
+import java.util.Arrays;
+import java.util.EnumSet;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import static org.apache.flink.table.functions.FunctionKind.PROCESS_TABLE;
+
+/**
+ * Definition of the {@code DEDUPLICATE} built-in process table function.
+ *
+ * <p>The output is updating and keyed by the PARTITION BY columns: the 
PARTITION BY columns
+ * followed by the remaining input columns.
+ */
+@Internal
+public final class DeduplicateDefinition {
+
+    public static final int ARG_INPUT = 0;
+    public static final int ARG_KEEP = 1;
+    public static final int ARG_SEQUENCE_BY = 2;
+    public static final int ARG_STATE_TTL = 3;
+
+    private static final String NAME = "DEDUPLICATE";
+
+    /** Which record of a key is kept. */
+    @Internal
+    public enum Keep {
+        FIRST,
+        LAST
+    }
+
+    static final InputTypeStrategy INPUT_TYPE_STRATEGY =
+            new ValidationOnlyInputTypeStrategy() {
+                @Override
+                public Optional<List<DataType>> inferInputTypes(
+                        final CallContext callContext, final boolean 
throwOnFailure) {
+                    final Optional<String> error = validate(callContext);
+                    if (error.isPresent()) {
+                        return callContext.fail(throwOnFailure, "%s", 
error.get());
+                    }
+                    return Optional.of(callContext.getArgumentDataTypes());
+                }
+            };
+
+    static final TypeStrategy OUTPUT_TYPE_STRATEGY =
+            callContext ->
+                    
Optional.of(DataTypes.ROW(outputFields(tableSemantics(callContext))).notNull());
+
+    static final ChangelogModeStrategy CHANGELOG_MODE_STRATEGY =
+            DeduplicateDefinition::inferChangelogMode;
+
+    public static final BuiltInFunctionDefinition INSTANCE =
+            BuiltInFunctionDefinition.newBuilder()
+                    .name(NAME)
+                    .kind(PROCESS_TABLE)
+                    .staticArguments(
+                            StaticArgument.table(
+                                    "input",
+                                    Row.class,
+                                    false,
+                                    EnumSet.of(
+                                            StaticArgumentTrait.TABLE,
+                                            
StaticArgumentTrait.SET_SEMANTIC_TABLE,
+                                            StaticArgumentTrait.NO_ORDER_BY)),
+                            StaticArgument.scalar("keep", DataTypes.STRING(), 
true),
+                            StaticArgument.scalar("sequence_by", 
DataTypes.DESCRIPTOR(), true),
+                            StaticArgument.scalar(
+                                    "state_ttl", 
DataTypes.INTERVAL(DataTypes.SECOND()), true))
+                    .changelogModeStrategy(CHANGELOG_MODE_STRATEGY)
+                    .inputTypeStrategy(INPUT_TYPE_STRATEGY)
+                    .outputTypeStrategy(OUTPUT_TYPE_STRATEGY)
+                    
.runtimeClass("org.apache.flink.table.runtime.functions.ptf.Deduplicate")
+                    .build();
+
+    /** Returns the {@code keep} argument, or {@link Keep#FIRST} when it is 
omitted. */
+    public static Keep keep(final CallContext callContext) {
+        return CallContextUtils.getEnumArgument(callContext, ARG_KEEP, 
Keep.class)
+                .orElse(Keep.FIRST);
+    }
+
+    /**
+     * Returns the positions of the {@code sequence_by} columns, in the listed 
order, within the
+     * output row without the PARTITION BY columns.
+     */
+    public static int[] sequenceByOutputIndices(final CallContext callContext) 
{
+        final TableSemantics tableSemantics = tableSemantics(callContext);
+        final List<String> fieldNames = 
DataType.getFieldNames(tableSemantics.dataType());
+        final List<Integer> outputIndices =
+                
Arrays.stream(ChangelogTypeStrategyUtils.computeOutputIndices(tableSemantics))
+                        .boxed()
+                        .collect(Collectors.toList());
+        return sequenceBy(callContext).stream()
+                .mapToInt(column -> 
outputIndices.indexOf(fieldNames.indexOf(column)))
+                .toArray();
+    }
+
+    private static Optional<String> validate(final CallContext callContext) {
+        final TableSemantics tableSemantics = tableSemantics(callContext);
+        return CallContextUtils.validateEnumArgument(
+                        callContext, ARG_KEEP, "keep", NAME, Keep.class)
+                .or(() -> validateSequenceBy(callContext, tableSemantics))
+                .or(() -> CallContextUtils.validateStateTtl(callContext, 
ARG_STATE_TTL));
+    }
+
+    private static ChangelogMode inferChangelogMode(final ChangelogContext 
changelogContext) {
+        final ChangelogMode requiredMode = 
changelogContext.getRequiredChangelogMode();
+        final ChangelogMode.Builder builder =
+                ChangelogMode.newBuilder()
+                        .addContainedKind(RowKind.INSERT)
+                        .addContainedKind(RowKind.UPDATE_AFTER);
+        if (requiredMode != null && 
requiredMode.contains(RowKind.UPDATE_BEFORE)) {
+            builder.addContainedKind(RowKind.UPDATE_BEFORE);
+        }
+        return builder.build();
+    }
+
+    private static TableSemantics tableSemantics(final CallContext 
callContext) {
+        return CallContextUtils.getTableSemanticsOrThrow(callContext, 
ARG_INPUT);
+    }
+
+    /** Returns the output columns after the PARTITION BY columns. */
+    private static List<Field> outputFields(final TableSemantics 
tableSemantics) {
+        final List<Field> inputFields = 
DataType.getFields(tableSemantics.dataType());
+        return 
Arrays.stream(ChangelogTypeStrategyUtils.computeOutputIndices(tableSemantics))

Review Comment:
   ```suggestion
           return 
Arrays.stream(CallContextUtils.computeOutputIndices(tableSemantics))
   ```



-- 
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