snuyanzin commented on code in PR #29365: URL: https://github.com/apache/flink/pull/29365#discussion_r4167272882
########## flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/strategies/DeduplicateKeepFirstTypeStrategy.java: ########## @@ -0,0 +1,188 @@ +/* + * 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.api.DataTypes; +import org.apache.flink.table.api.DataTypes.Field; +import org.apache.flink.table.api.ValidationException; +import org.apache.flink.table.api.dataview.ValueView; +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.StateTypeStrategy; +import org.apache.flink.table.types.inference.TypeStrategy; + +import java.time.Duration; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +public class DeduplicateKeepFirstTypeStrategy { + + public static final int ARG_INPUT = 0; + public static final int ARG_STATE_TTL = 1; + public static final int ARG_RESET_TTL_ON_DUPLICATE = 2; + + public static final InputTypeStrategy INPUT_TYPE_STRATEGY = + new ValidationOnlyInputTypeStrategy() { + @Override + public Optional<List<DataType>> inferInputTypes( + final CallContext callContext, final boolean throwOnFailure) { + final Optional<List<DataType>> stateTtlError = + validateStateTtl(callContext, throwOnFailure); + if (stateTtlError.isPresent()) { + return stateTtlError; + } + + final Optional<List<DataType>> resetTtlError = + validateResetTtlOnDuplicate(callContext, throwOnFailure); + if (resetTtlError.isPresent()) { + return resetTtlError; + } + + return Optional.of(callContext.getArgumentDataTypes()); + } + }; + + public static final TypeStrategy OUTPUT_TYPE_STRATEGY = + callContext -> { + final TableSemantics tableSemantics = + callContext + .getTableSemantics(ARG_INPUT) + .orElseThrow( + () -> + new ValidationException( + "First argument must be a table for DEDUPLICATE_KEEP_FIRST.")); + + final List<Field> inputFields = DataType.getFields(tableSemantics.dataType()); + final List<Field> outputFields = + Arrays.stream( + ChangelogTypeStrategyUtils.computeOutputIndices( + tableSemantics)) + .mapToObj(inputFields::get) + .collect(Collectors.toList()); + return Optional.of(DataTypes.ROW(outputFields).notNull()); + }; + + private static final String SEEN_STATE_NAME = "seen"; + + public static final StateTypeStrategy SEEN_STATE_TYPE_STRATEGY = + new StateTypeStrategy() { + @Override + public Optional<DataType> inferType(final CallContext callContext) { + return Optional.of( + ValueView.newValueViewDataType(DataTypes.BOOLEAN().notNull())); + } + + @Override + public Optional<Duration> getTimeToLive(final CallContext callContext) { + return callContext.getArgumentValue(ARG_STATE_TTL, Duration.class); + } + }; + + private static final String CANDIDATE_STATE_NAME = "candidate"; Review Comment: can we have one line constants at the very top? it will simplify reading -- 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]
