raminqaf commented on code in PR #27901:
URL: https://github.com/apache/flink/pull/27901#discussion_r3071331156


##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/strategies/FromChangelogTypeStrategy.java:
##########
@@ -0,0 +1,263 @@
+/*
+ * 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;
+import org.apache.flink.table.functions.FunctionDefinition;
+import org.apache.flink.table.functions.TableSemantics;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.inference.ArgumentCount;
+import org.apache.flink.table.types.inference.CallContext;
+import org.apache.flink.table.types.inference.ConstantArgumentCount;
+import org.apache.flink.table.types.inference.InputTypeStrategy;
+import org.apache.flink.table.types.inference.Signature;
+import org.apache.flink.table.types.inference.Signature.Argument;
+import org.apache.flink.table.types.inference.TypeStrategy;
+import org.apache.flink.table.types.logical.LogicalTypeFamily;
+import org.apache.flink.types.ColumnList;
+import org.apache.flink.types.RowKind;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+/** Type strategies for the {@code FROM_CHANGELOG} process table function. */
+@Internal
+public final class FromChangelogTypeStrategy {
+
+    private static final String DEFAULT_OP_COLUMN_NAME = "op";
+
+    private static final Set<String> VALID_ROW_KIND_NAMES =
+            Set.of("INSERT", "UPDATE_BEFORE", "UPDATE_AFTER", "DELETE");
+
+    // 
--------------------------------------------------------------------------------------------
+    // Input validation
+    // 
--------------------------------------------------------------------------------------------
+
+    public static final InputTypeStrategy INPUT_TYPE_STRATEGY =
+            new InputTypeStrategy() {
+                @Override
+                public ArgumentCount getArgumentCount() {
+                    return ConstantArgumentCount.between(1, 3);
+                }
+
+                @Override
+                public Optional<List<DataType>> inferInputTypes(
+                        final CallContext callContext, final boolean 
throwOnFailure) {
+                    return validateInputs(callContext, throwOnFailure);
+                }
+
+                @Override
+                public List<Signature> getExpectedSignatures(final 
FunctionDefinition definition) {
+                    return List.of(
+                            Signature.of(Argument.of("input", "TABLE")),
+                            Signature.of(
+                                    Argument.of("input", "TABLE"), 
Argument.of("op", "DESCRIPTOR")),
+                            Signature.of(
+                                    Argument.of("input", "TABLE"),
+                                    Argument.of("op", "DESCRIPTOR"),
+                                    Argument.of("op_mapping", "MAP<STRING, 
STRING>")));
+                }
+            };
+
+    // 
--------------------------------------------------------------------------------------------
+    // Output type inference
+    // 
--------------------------------------------------------------------------------------------
+
+    public static final TypeStrategy OUTPUT_TYPE_STRATEGY =
+            callContext -> {
+                final TableSemantics tableSemantics =
+                        callContext
+                                .getTableSemantics(0)
+                                .orElseThrow(
+                                        () ->
+                                                new ValidationException(
+                                                        "First argument must 
be a table for FROM_CHANGELOG."));
+
+                final String opColumnName = resolveOpColumnName(callContext);
+
+                final List<Field> outputFields = 
buildOutputFields(tableSemantics, opColumnName);
+
+                return Optional.of(DataTypes.ROW(outputFields).notNull());
+            };
+
+    // 
--------------------------------------------------------------------------------------------
+    // Helpers
+    // 
--------------------------------------------------------------------------------------------
+
+    @SuppressWarnings("rawtypes")
+    private static Optional<List<DataType>> validateInputs(
+            final CallContext callContext, final boolean throwOnFailure) {
+        final boolean isMissingTableArg = 
callContext.getTableSemantics(0).isEmpty();
+        if (isMissingTableArg) {
+            return callContext.fail(
+                    throwOnFailure, "First argument must be a table for 
FROM_CHANGELOG.");
+        }
+
+        final Optional<ColumnList> opDescriptor = 
callContext.getArgumentValue(1, ColumnList.class);
+        final boolean hasInvalidOpDescriptor =
+                opDescriptor.isPresent() && 
opDescriptor.get().getNames().size() != 1;
+        if (hasInvalidOpDescriptor) {
+            return callContext.fail(
+                    throwOnFailure,
+                    "The descriptor for argument 'op' must contain exactly one 
column name.");
+        }
+
+        // Validate that the op column exists in the input schema and is of 
STRING type
+        final TableSemantics tableSemantics = 
callContext.getTableSemantics(0).get();
+        final String opColumnName = resolveOpColumnName(callContext);
+        final List<Field> inputFields = 
DataType.getFields(tableSemantics.dataType());
+        final Optional<Field> opField =
+                inputFields.stream().filter(f -> 
f.getName().equals(opColumnName)).findFirst();
+        if (opField.isEmpty()) {
+            return callContext.fail(
+                    throwOnFailure,
+                    String.format(
+                            "The op column '%s' does not exist in the input 
schema.",
+                            opColumnName));
+        }
+        if 
(!opField.get().getDataType().getLogicalType().is(LogicalTypeFamily.CHARACTER_STRING))
 {
+            return callContext.fail(
+                    throwOnFailure,
+                    String.format(
+                            "The op column '%s' must be of STRING type, but 
was '%s'.",
+                            opColumnName, 
opField.get().getDataType().getLogicalType()));
+        }
+
+        final boolean hasMappingArgProvided = !callContext.isArgumentNull(2);
+        final boolean isMappingArgLiteral = callContext.isArgumentLiteral(2);
+        if (hasMappingArgProvided && !isMappingArgLiteral) {
+            return callContext.fail(
+                    throwOnFailure, "The 'op_mapping' argument must be a 
constant MAP literal.");
+        }
+
+        final Optional<Map> opMapping = callContext.getArgumentValue(2, 
Map.class);
+        if (opMapping.isPresent()) {
+            final Optional<List<DataType>> validationError =
+                    validateOpMappingValues(callContext, opMapping.get(), 
throwOnFailure);
+            if (validationError.isPresent()) {
+                return validationError;
+            }
+        }
+
+        return Optional.of(callContext.getArgumentDataTypes());
+    }
+
+    /**
+     * Validates op_mapping values. Values must be valid RowKind names from 
{INSERT, UPDATE_AFTER,
+     * DELETE}. Keys are arbitrary user strings (e.g., 'c', 'u', 'd') and may 
be comma-separated to
+     * map multiple user codes to the same RowKind. Each RowKind name must 
appear at most once
+     * across all entries.
+     */
+    private static Optional<List<DataType>> validateOpMappingValues(
+            final CallContext callContext,
+            final Map<?, ?> opMapping,
+            final boolean throwOnFailure) {
+        final Set<String> allRowKindsSeen = new HashSet<>();
+
+        for (final Entry<?, ?> entry : opMapping.entrySet()) {
+            if (!(entry.getKey() instanceof String)) {
+                return callContext.fail(
+                        throwOnFailure, "Invalid target mapping for argument 
'op_mapping'.");
+            }
+            final Object value = entry.getValue();
+            if (!(value instanceof String)) {
+                return callContext.fail(
+                        throwOnFailure, "Invalid target mapping for argument 
'op_mapping'.");
+            }
+            final String rowKindName = ((String) value).trim();
+            if (!VALID_ROW_KIND_NAMES.contains(rowKindName)) {
+                return callContext.fail(
+                        throwOnFailure,
+                        String.format(
+                                "Invalid target mapping for argument 
'op_mapping'. "
+                                        + "Unknown change operation: '%s'. 
Valid values are: %s.",
+                                rowKindName, VALID_ROW_KIND_NAMES));
+            }
+            final boolean isDuplicate = !allRowKindsSeen.add(rowKindName);
+            if (isDuplicate) {
+                return callContext.fail(
+                        throwOnFailure,
+                        String.format(
+                                "Invalid target mapping for argument 
'op_mapping'. "
+                                        + "Duplicate change operation: '%s'.",

Review Comment:
   Good point! Improved the message!



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