gustavodemorais commented on code in PR #29967:
URL: https://github.com/apache/flink/pull/29967#discussion_r4585871720
##########
flink-python/docs/reference/pyflink.table/table.rst:
##########
@@ -110,6 +110,7 @@ Example:
Table.offset
Table.order_by
Table.over_window
+ Table.partition_by
Review Comment:
Are the Python changes needed in this PR? `partition_by`/`PartitionedTable`
are new public API that TO_CHANGELOG/FROM_CHANGELOG didn't add either. I'd keep
Python to a minimum and drop this together with `test_explain_deduplicate`.
##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/strategies/DeduplicateTypeStrategy.java:
##########
@@ -0,0 +1,373 @@
+/*
+ * 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.annotation.Internal;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.DataTypes.Field;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.connector.ChangelogMode;
+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.TypeStrategy;
+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.utils.LogicalTypeChecks;
+import org.apache.flink.table.types.utils.DataTypeUtils;
+import org.apache.flink.types.ColumnList;
+import org.apache.flink.types.RowKind;
+import org.apache.flink.util.TimeUtils;
+
+import java.time.Duration;
+import java.util.Arrays;
+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.types.inference.SystemTypeInference.PROCESS_TABLE_FUNCTION_ARG_ON_TIME_OFFSET;
+
+/**
+ * Type strategies for the {@code DEDUPLICATE} 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, without time attributes.
+ */
+@Internal
+public final class DeduplicateTypeStrategy {
+
+ 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;
+ public static final int ARG_RESET_TTL_ON_DUPLICATE = 4;
+ private static final int DECLARED_ARG_COUNT = ARG_RESET_TTL_ON_DUPLICATE +
1;
+
+ /** Which record of a key {@code DEDUPLICATE} keeps. */
+ @Internal
+ public enum Keep {
+ FIRST("first"),
+ LAST("last");
+
+ private final String value;
+
+ Keep(final String value) {
+ this.value = value;
+ }
+
+ /** Returns the mode for an argument value, or empty if the value
names no mode. */
+ public static Optional<Keep> fromValue(final String value) {
+ return Arrays.stream(values()).filter(keep ->
keep.value.equals(value)).findFirst();
+ }
+ }
+
+ public 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());
+ }
+ };
+
+ public static final TypeStrategy OUTPUT_TYPE_STRATEGY =
+ callContext ->
+
Optional.of(DataTypes.ROW(outputFields(tableSemantics(callContext))).notNull());
+
+ public static final ChangelogModeStrategy CHANGELOG_MODE_STRATEGY =
+ DeduplicateTypeStrategy::inferChangelogMode;
+
+ /** Returns the {@code keep} argument, or {@link Keep#FIRST} when it is
omitted. */
+ public static Keep keep(final CallContext callContext) {
+ return callContext
+ .getArgumentValue(ARG_KEEP, String.class)
+ .map(
+ value ->
+ Keep.fromValue(value)
+ .orElseThrow(
+ () ->
+ new
IllegalStateException(
+ "Validated
keep argument expected.")))
+ .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) {
Review Comment:
I like this chain. I'm just wondering whether some of these are already
taken care of by the framework (e.g. PARTITION BY for set-semantic tables), or
could be more generic and shared with DEDUPLICATE_WINDOW (literal checks,
state_ttl). Then we only add custom checks where they're really needed.
##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/BuiltInFunctionDefinitions.java:
##########
@@ -964,6 +965,31 @@ ANY, and(logical(LogicalTypeRoot.BOOLEAN), LITERAL)
"org.apache.flink.table.runtime.functions.ptf.DeduplicateKeepFirst")
.build();
+ public static final BuiltInFunctionDefinition DEDUPLICATE =
+ BuiltInFunctionDefinition.newBuilder()
+ .name("DEDUPLICATE")
+ .kind(PROCESS_TABLE)
+ .staticArguments(
+ StaticArgument.table(
+ "input",
+ Row.class,
+ false,
+ EnumSet.of(
+ StaticArgumentTrait.TABLE,
+
StaticArgumentTrait.SET_SEMANTIC_TABLE,
+
StaticArgumentTrait.SUPPORT_UPDATES)),
Review Comment:
Updating input isn't supported yet. If we drop SUPPORT_UPDATES for now, the
framework rejects it with a proper validation error instead of the internal
exception in `specialize()`.
##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/nodes/exec/stream/DeduplicateRestoreTestPrograms.java:
##########
@@ -0,0 +1,142 @@
+/*
+ * 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.planner.plan.nodes.exec.stream;
+
+import org.apache.flink.table.test.program.SinkTestStep;
+import org.apache.flink.table.test.program.SourceTestStep;
+import org.apache.flink.table.test.program.TableTestProgram;
+import org.apache.flink.types.Row;
+
+/** {@link TableTestProgram} definitions for restore tests of the DEDUPLICATE
PTF. */
+public class DeduplicateRestoreTestPrograms {
+
+ public static final TableTestProgram ARRIVAL_ORDER_RESTORE =
Review Comment:
One restore program per state layout should be enough. This one and
`SEQUENCE_BY_RESTORE` share the same state.
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/ptf/Deduplicate.java:
##########
@@ -0,0 +1,316 @@
+/*
+ * 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.ptf;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.dataview.ValueView;
+import org.apache.flink.table.connector.ChangelogMode;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.binary.BinaryRowData;
+import org.apache.flink.table.data.utils.JoinedRowData;
+import org.apache.flink.table.data.utils.ProjectedRowData;
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.table.functions.FunctionContext;
+import org.apache.flink.table.functions.TableSemantics;
+import org.apache.flink.table.functions.UserDefinedFunction;
+import org.apache.flink.table.runtime.functions.BuiltInSpecializedFunction;
+import
org.apache.flink.table.runtime.functions.ptf.DeduplicateUtils.SequenceComparator;
+import org.apache.flink.table.runtime.typeutils.RowDataSerializer;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.inference.CallContext;
+import org.apache.flink.table.types.inference.StateTypeStrategy;
+import
org.apache.flink.table.types.inference.strategies.ChangelogTypeStrategyUtils;
+import
org.apache.flink.table.types.inference.strategies.DeduplicateTypeStrategy;
+import
org.apache.flink.table.types.inference.strategies.DeduplicateTypeStrategy.Keep;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.types.ColumnList;
+import org.apache.flink.types.RowKind;
+
+import javax.annotation.Nullable;
+
+import java.time.Duration;
+import java.util.Arrays;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+import static
org.apache.flink.table.types.inference.strategies.DeduplicateTypeStrategy.ARG_INPUT;
+import static
org.apache.flink.table.types.inference.strategies.DeduplicateTypeStrategy.ARG_RESET_TTL_ON_DUPLICATE;
+import static
org.apache.flink.table.types.inference.strategies.DeduplicateTypeStrategy.ARG_STATE_TTL;
+
+/** Runtime implementation of {@link BuiltInFunctionDefinitions#DEDUPLICATE}.
*/
+@Internal
+public class Deduplicate extends BuiltInSpecializedFunction {
+
+ public Deduplicate() {
+ super(BuiltInFunctionDefinitions.DEDUPLICATE);
+ }
+
+ @Override
+ public UserDefinedFunction specialize(final SpecializedContext context) {
Review Comment:
Could we make this an explicit, small dispatch (which arguments lead to
which implementation) and move each variant to its own file? Also, `Sequenced`
covers `keep => 'last'` without `sequence_by` too, so a name that describes
what it keeps might be clearer.
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/ptf/Deduplicate.java:
##########
@@ -0,0 +1,316 @@
+/*
+ * 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.ptf;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.dataview.ValueView;
+import org.apache.flink.table.connector.ChangelogMode;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.binary.BinaryRowData;
+import org.apache.flink.table.data.utils.JoinedRowData;
+import org.apache.flink.table.data.utils.ProjectedRowData;
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.table.functions.FunctionContext;
+import org.apache.flink.table.functions.TableSemantics;
+import org.apache.flink.table.functions.UserDefinedFunction;
+import org.apache.flink.table.runtime.functions.BuiltInSpecializedFunction;
+import
org.apache.flink.table.runtime.functions.ptf.DeduplicateUtils.SequenceComparator;
+import org.apache.flink.table.runtime.typeutils.RowDataSerializer;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.inference.CallContext;
+import org.apache.flink.table.types.inference.StateTypeStrategy;
+import
org.apache.flink.table.types.inference.strategies.ChangelogTypeStrategyUtils;
+import
org.apache.flink.table.types.inference.strategies.DeduplicateTypeStrategy;
+import
org.apache.flink.table.types.inference.strategies.DeduplicateTypeStrategy.Keep;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.types.ColumnList;
+import org.apache.flink.types.RowKind;
+
+import javax.annotation.Nullable;
+
+import java.time.Duration;
+import java.util.Arrays;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+import static
org.apache.flink.table.types.inference.strategies.DeduplicateTypeStrategy.ARG_INPUT;
+import static
org.apache.flink.table.types.inference.strategies.DeduplicateTypeStrategy.ARG_RESET_TTL_ON_DUPLICATE;
+import static
org.apache.flink.table.types.inference.strategies.DeduplicateTypeStrategy.ARG_STATE_TTL;
+
+/** Runtime implementation of {@link BuiltInFunctionDefinitions#DEDUPLICATE}.
*/
+@Internal
+public class Deduplicate extends BuiltInSpecializedFunction {
+
+ public Deduplicate() {
+ super(BuiltInFunctionDefinitions.DEDUPLICATE);
+ }
+
+ @Override
+ public UserDefinedFunction specialize(final SpecializedContext context) {
+ final ChangelogMode inputMode =
+ tableSemantics(context)
+ .changelogMode()
+ .orElseThrow(() -> new
IllegalStateException("Changelog mode expected."));
+ if (!inputMode.containsOnly(RowKind.INSERT)) {
+ throw new IllegalStateException("Currently not implemented");
+ }
+ final CallContext callContext = context.getCallContext();
+ final boolean isFirstArrival =
+ DeduplicateTypeStrategy.keep(callContext) == Keep.FIRST
+ &&
DeduplicateTypeStrategy.sequenceByOutputIndices(callContext).length == 0;
+ return isFirstArrival ? new FirstArrival(context) : new
Sequenced(context);
+ }
+
+ private abstract static class Base extends
BuiltInProcessTableFunction<RowData> {
+
+ private static final long serialVersionUID = 1L;
+
+ protected final int[] outputIndices;
+
+ protected final boolean isResetTtlOnDuplicate;
+
+ Base(
+ final SpecializedContext context,
+ final LinkedHashMap<String, StateTypeStrategy>
stateStrategies) {
+ super(BuiltInFunctionDefinitions.DEDUPLICATE, context,
stateStrategies);
+ final CallContext callContext = context.getCallContext();
+ this.outputIndices =
+
ChangelogTypeStrategyUtils.computeOutputIndices(tableSemantics(context));
+ this.isResetTtlOnDuplicate =
Review Comment:
With no TTL configured (`state_ttl` omitted and `table.exec.state.ttl` = 0),
this is still true, so duplicates write state for nothing. Could we derive it
from the effective TTL? Worth checking the rest of the hot path for similar
per-record costs.
##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/strategies/DeduplicateTypeStrategy.java:
##########
@@ -0,0 +1,373 @@
+/*
+ * 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.annotation.Internal;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.DataTypes.Field;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.connector.ChangelogMode;
+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.TypeStrategy;
+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.utils.LogicalTypeChecks;
+import org.apache.flink.table.types.utils.DataTypeUtils;
+import org.apache.flink.types.ColumnList;
+import org.apache.flink.types.RowKind;
+import org.apache.flink.util.TimeUtils;
+
+import java.time.Duration;
+import java.util.Arrays;
+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.types.inference.SystemTypeInference.PROCESS_TABLE_FUNCTION_ARG_ON_TIME_OFFSET;
+
+/**
+ * Type strategies for the {@code DEDUPLICATE} 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, without time attributes.
+ */
+@Internal
+public final class DeduplicateTypeStrategy {
+
+ 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;
+ public static final int ARG_RESET_TTL_ON_DUPLICATE = 4;
+ private static final int DECLARED_ARG_COUNT = ARG_RESET_TTL_ON_DUPLICATE +
1;
+
+ /** Which record of a key {@code DEDUPLICATE} keeps. */
+ @Internal
+ public enum Keep {
+ FIRST("first"),
+ LAST("last");
+
+ private final String value;
+
+ Keep(final String value) {
+ this.value = value;
+ }
+
+ /** Returns the mode for an argument value, or empty if the value
names no mode. */
+ public static Optional<Keep> fromValue(final String value) {
+ return Arrays.stream(values()).filter(keep ->
keep.value.equals(value)).findFirst();
+ }
+ }
+
+ public 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());
+ }
+ };
+
+ public static final TypeStrategy OUTPUT_TYPE_STRATEGY =
+ callContext ->
+
Optional.of(DataTypes.ROW(outputFields(tableSemantics(callContext))).notNull());
+
+ public static final ChangelogModeStrategy CHANGELOG_MODE_STRATEGY =
+ DeduplicateTypeStrategy::inferChangelogMode;
+
+ /** Returns the {@code keep} argument, or {@link Keep#FIRST} when it is
omitted. */
+ public static Keep keep(final CallContext callContext) {
+ return callContext
+ .getArgumentValue(ARG_KEEP, String.class)
+ .map(
+ value ->
+ Keep.fromValue(value)
+ .orElseThrow(
+ () ->
+ new
IllegalStateException(
+ "Validated
keep argument expected.")))
+ .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 validatePartitionBy(tableSemantics)
+ .or(() -> validateNoOrderBy(tableSemantics))
+ .or(() -> validatePartitionByNoTimeAttribute(tableSemantics))
+ .or(() -> validateOnTime(callContext))
+ .or(() -> validateKeep(callContext))
+ .or(() -> validateSequenceBy(callContext, tableSemantics))
+ .or(() -> validateStateTtl(callContext))
+ .or(() -> validateResetTtlOnDuplicate(callContext));
+ }
+
+ private static Optional<String> validatePartitionBy(final TableSemantics
tableSemantics) {
+ if (tableSemantics.partitionByColumns().length > 0) {
+ return Optional.empty();
+ }
+ return Optional.of(
+ "DEDUPLICATE requires a key, but the input has no PARTITION
BY. Add PARTITION BY "
+ + "with the columns to deduplicate on, for example
TABLE t PARTITION BY "
+ + "id. To drop only rows that repeat in full,
partition by every column.");
+ }
+
+ private static Optional<String> validateNoOrderBy(final TableSemantics
tableSemantics) {
+ if (tableSemantics.orderByColumns().length == 0) {
+ return Optional.empty();
+ }
+ final List<String> fieldNames =
DataType.getFieldNames(tableSemantics.dataType());
+ final String columns =
+ Arrays.stream(tableSemantics.orderByColumns())
+ .mapToObj(fieldNames::get)
+ .collect(Collectors.joining(", "));
+ return Optional.of(
+ String.format(
+ "DEDUPLICATE does not support ORDER BY on its input,
but received "
+ + "ORDER BY %s. Use sequence_by =>
DESCRIPTOR(%s) to order the "
+ + "records of a key.",
+ columns, columns));
+ }
+
+ private static Optional<String> validatePartitionByNoTimeAttribute(
+ final TableSemantics tableSemantics) {
+ final List<Field> fields =
DataType.getFields(tableSemantics.dataType());
+ return Arrays.stream(tableSemantics.partitionByColumns())
+ .mapToObj(fields::get)
+ .filter(
+ field ->
+ LogicalTypeChecks.isTimeAttribute(
+ field.getDataType().getLogicalType()))
+ .findFirst()
+ .map(
+ field ->
+ String.format(
+ "DEDUPLICATE does not support a time
attribute as a "
+ + "PARTITION BY column, but
'%s' is one. Partition "
+ + "by a regular column, or
cast the column in a "
+ + "subquery first, for example
CAST(%s AS %s) AS %s.",
+ field.getName(),
+ field.getName(),
+
DataTypeUtils.removeTimeAttribute(field.getDataType())
+ .getLogicalType()
+ .asSummaryString(),
+ field.getName()));
+ }
+
+ private static Optional<String> validateOnTime(final CallContext
callContext) {
+ final int argCount = callContext.getArgumentDataTypes().size();
+ if (argCount == DECLARED_ARG_COUNT) {
+ return Optional.empty();
+ }
+ final int onTimePos = argCount - 1 -
PROCESS_TABLE_FUNCTION_ARG_ON_TIME_OFFSET;
+ return callContext
+ .getArgumentValue(onTimePos, ColumnList.class)
+ .map(ColumnList::getNames)
+ .filter(names -> !names.isEmpty())
+ .map(
+ names ->
+ String.format(
+ "DEDUPLICATE does not support on_time,
but received "
+ + "on_time => DESCRIPTOR(%s).
Use sequence_by => "
+ + "DESCRIPTOR(%s) to order by
timestamp, or "
+ + "DEDUPLICATE_WINDOW for
event-time windows."
+ + "ps.this function is not
currently implemented",
Review Comment:
Leftover text in the message. Could you check the other messages as well?
##########
flink-table/flink-table-runtime/src/test/java/org/apache/flink/table/runtime/functions/ptf/DeduplicateTest.java:
##########
@@ -0,0 +1,177 @@
+/*
+ * 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.ptf;
+
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.configuration.ReadableConfig;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.dataview.ValueView;
+import org.apache.flink.table.connector.ChangelogMode;
+import org.apache.flink.table.expressions.Expression;
+import org.apache.flink.table.functions.BuiltInFunctionDefinition;
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import
org.apache.flink.table.functions.SpecializedFunction.ExpressionEvaluator;
+import org.apache.flink.table.functions.SpecializedFunction.SpecializedContext;
+import org.apache.flink.table.functions.UserDefinedFunction;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.inference.CallContext;
+import org.apache.flink.table.types.inference.StateTypeStrategy;
+import org.apache.flink.table.types.inference.utils.CallContextMock;
+import org.apache.flink.table.types.inference.utils.TableSemanticsMock;
+import org.apache.flink.table.types.utils.DataTypeFactoryMock;
+import org.apache.flink.table.types.utils.DataTypeUtils;
+
+import org.junit.jupiter.api.Test;
+
+import javax.annotation.Nullable;
+
+import java.time.Duration;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/** Tests for the state that each specialization of {@link Deduplicate}
declares. */
+class DeduplicateTest {
Review Comment:
This mostly tests internals that the restore and semantic tests already
cover. Can we drop it, and look for similar overlaps across the other test
classes?
--
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]