From 2f90d16fb7cc252d13601c8636c4eea1615b8836 Mon Sep 17 00:00:00 2001 From: Vas Shabu Date: Tue, 17 Nov 2026 16:28:19 +0000 Subject: [PATCH 1/2] [FLINK-41531][table] Add tests for disabling on_time while keeping uid --- .../inference/SystemTypeInferenceTest.java | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 flink-table/flink-table-common/src/test/java/org/apache/flink/table/types/inference/SystemTypeInferenceTest.java diff --git a/flink-table/flink-table-common/src/test/java/org/apache/flink/table/types/inference/SystemTypeInferenceTest.java b/flink-table/flink-table-common/src/test/java/org/apache/flink/table/types/inference/SystemTypeInferenceTest.java new file mode 100644 index 00000000000000..1f51fe8cdc520c --- /dev/null +++ b/flink-table/flink-table-common/src/test/java/org/apache/flink/table/types/inference/SystemTypeInferenceTest.java @@ -0,0 +1,87 @@ +/* + * 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; + +import org.apache.flink.table.api.DataTypes; +import org.apache.flink.table.api.ValidationException; +import org.apache.flink.table.functions.BuiltInFunctionDefinition; +import org.apache.flink.table.functions.FunctionKind; +import org.apache.flink.table.types.DataType; +import org.apache.flink.table.types.inference.utils.CallContextMock; +import org.apache.flink.types.ColumnList; + +import org.junit.jupiter.api.Test; + +import javax.annotation.Nullable; + +import java.util.List; +import java.util.Optional; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +/** Tests for {@link SystemTypeInference}. */ +class SystemTypeInferenceTest { + + @Test + void testOnTimeRejectedIfDisabled() { + assertThatThrownBy(() -> infer(true, ColumnList.of("ts"), null)) + .isInstanceOf(ValidationException.class) + .hasMessageContaining( + "The 'on_time' argument is not supported because function 'f' " + + "does not support time attributes."); + } + + @Test + void testUidAndEmptyOnTimeAllowedIfOnTimeDisabled() { + assertThat(infer(true, ColumnList.of(), "my_uid")).isPresent(); + } + + @Test + void testOnTimeAllowedByDefault() { + assertThat(infer(false, ColumnList.of("ts"), null)).isPresent(); + } + + private static Optional> infer( + boolean disableOnTime, ColumnList onTime, @Nullable String uid) { + final BuiltInFunctionDefinition.Builder builder = + BuiltInFunctionDefinition.newBuilder() + .name("f") + .kind(FunctionKind.PROCESS_TABLE) + .staticArguments(StaticArgument.scalar("i", DataTypes.INT(), false)) + .outputTypeStrategy(TypeStrategies.explicit(DataTypes.INT())); + final BuiltInFunctionDefinition definition = + builder.disableOnTimeArgument(disableOnTime).build(); + + final TypeInference inference = + SystemTypeInference.of(definition, definition.getTypeInference(null)); + + final CallContextMock callContext = new CallContextMock(); + callContext.name = "f"; + callContext.functionDefinition = definition; + callContext.argumentDataTypes = + List.of(DataTypes.INT(), DataTypes.DESCRIPTOR(), DataTypes.STRING()); + callContext.argumentValues = + List.of(Optional.empty(), Optional.of(onTime), Optional.ofNullable(uid)); + callContext.argumentNulls = List.of(false, false, uid == null); + callContext.argumentLiterals = List.of(false, true, uid != null); + + return inference.getInputTypeStrategy().inferInputTypes(callContext, true); + } +} From 64e1f07e91b16cbd0c1d999e017c8f25e83dd001 Mon Sep 17 00:00:00 2001 From: Vas Shabu Date: Tue, 17 Nov 2026 16:59:43 +0000 Subject: [PATCH 2/2] [FLINK-41531][table] Let a built-in PTF disable the on_time system argument while keeping uid --- .../rules/ResolveCallByArgumentsRule.java | 2 +- .../functions/BuiltInFunctionDefinition.java | 17 +++++++ .../types/inference/SystemTypeInference.java | 49 +++++++++++++++++-- .../bridging/BridgingSqlFunction.java | 2 +- 4 files changed, 64 insertions(+), 6 deletions(-) diff --git a/flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/expressions/resolver/rules/ResolveCallByArgumentsRule.java b/flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/expressions/resolver/rules/ResolveCallByArgumentsRule.java index 07930a9787507f..4fff869ca5af19 100644 --- a/flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/expressions/resolver/rules/ResolveCallByArgumentsRule.java +++ b/flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/expressions/resolver/rules/ResolveCallByArgumentsRule.java @@ -256,7 +256,7 @@ private List flattenLegacyCompositeType(ResolvedExpression c final TypeInference inference = definition.getTypeInference(resolutionContext.typeFactory()); if (inference.getOutputTypeStrategy() != TypeStrategies.MISSING) { - return SystemTypeInference.of(definition.getKind(), inference); + return SystemTypeInference.of(definition, inference); } else { return null; } diff --git a/flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/BuiltInFunctionDefinition.java b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/BuiltInFunctionDefinition.java index 03e4780e0183d0..9023d797c31493 100644 --- a/flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/BuiltInFunctionDefinition.java +++ b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/BuiltInFunctionDefinition.java @@ -70,6 +70,8 @@ public final class BuiltInFunctionDefinition implements SpecializedFunction { private final boolean isInternal; + private final boolean isOnTimeArgumentDisabled; + private final SqlCallSyntax sqlCallSyntax; private final @Nullable ChangelogModeStrategy changelogModeStrategy; @@ -87,6 +89,7 @@ private BuiltInFunctionDefinition( boolean isRuntimeProvided, String runtimeClass, boolean isInternal, + boolean isOnTimeArgumentDisabled, @Nullable ChangelogModeStrategy changelogModeStrategy) { this.name = checkNotNull(name, "Name must not be null."); this.sqlName = sqlName; @@ -97,6 +100,7 @@ private BuiltInFunctionDefinition( this.isRuntimeProvided = isRuntimeProvided; this.runtimeClass = runtimeClass; this.isInternal = isInternal; + this.isOnTimeArgumentDisabled = isOnTimeArgumentDisabled; this.sqlCallSyntax = sqlCallSyntax; this.changelogModeStrategy = changelogModeStrategy; validateFunction(this.name, this.version, this.isInternal); @@ -135,6 +139,10 @@ public boolean isInternal() { return isInternal; } + public boolean isOnTimeArgumentDisabled() { + return isOnTimeArgumentDisabled; + } + /** * Returns the optional {@link ChangelogModeStrategy} for built-in PTFs that emit updates (e.g., * FROM_CHANGELOG). The planner uses this to determine the output changelog mode. @@ -263,6 +271,8 @@ public static final class Builder { private boolean isInternal = false; + private boolean isOnTimeArgumentDisabled = false; + private SqlCallSyntax sqlCallSyntax = SqlCallSyntax.FUNCTION; private @Nullable ChangelogModeStrategy changelogModeStrategy; @@ -317,6 +327,12 @@ public Builder disableSystemArguments(boolean disableSystemArguments) { return this; } + /** Rejects the {@code on_time} system argument of a PTF but keeps {@code uid}. */ + public Builder disableOnTimeArgument(boolean disableOnTimeArgument) { + this.isOnTimeArgumentDisabled = disableOnTimeArgument; + return this; + } + /** * @deprecated Use {@link #staticArguments(StaticArgument...)} instead. */ @@ -434,6 +450,7 @@ public BuiltInFunctionDefinition build() { isRuntimeProvided, runtimeClass, isInternal, + isOnTimeArgumentDisabled, changelogModeStrategy); } } diff --git a/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/SystemTypeInference.java b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/SystemTypeInference.java index 3e72cdc280fd05..0ea38ea7469737 100644 --- a/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/SystemTypeInference.java +++ b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/SystemTypeInference.java @@ -23,6 +23,7 @@ import org.apache.flink.table.api.DataTypes.Field; import org.apache.flink.table.api.ValidationException; import org.apache.flink.table.connector.Projection; +import org.apache.flink.table.functions.BuiltInFunctionDefinition; import org.apache.flink.table.functions.FunctionDefinition; import org.apache.flink.table.functions.FunctionKind; import org.apache.flink.table.functions.ProcessTableFunction; @@ -96,6 +97,18 @@ public class SystemTypeInference { Pattern.compile("^[a-zA-Z_][a-zA-Z-_0-9]*$").asPredicate(); public static TypeInference of(FunctionKind functionKind, TypeInference origin) { + return of(functionKind, origin, false); + } + + public static TypeInference of(FunctionDefinition definition, TypeInference origin) { + final boolean disableOnTimeArg = + definition instanceof BuiltInFunctionDefinition + && ((BuiltInFunctionDefinition) definition).isOnTimeArgumentDisabled(); + return of(definition.getKind(), origin, disableOnTimeArg); + } + + private static TypeInference of( + FunctionKind functionKind, TypeInference origin, boolean disableOnTimeArg) { final TypeInference.Builder builder = TypeInference.newBuilder(); final List systemArgs = @@ -111,7 +124,8 @@ public static TypeInference of(FunctionKind functionKind, TypeInference origin) functionKind, systemArgs, origin.getInputTypeStrategy(), - origin.disableSystemArguments())); + origin.disableSystemArguments(), + disableOnTimeArg)); if (functionKind == FunctionKind.PROCESS_TABLE) { checkBroadcastTraits(systemArgs, origin.getStateTypeStrategies()); } @@ -344,11 +358,13 @@ private static InputTypeStrategy deriveSystemInputStrategy( FunctionKind functionKind, @Nullable List staticArgs, InputTypeStrategy inputStrategy, - boolean disableSystemArgs) { + boolean disableSystemArgs, + boolean disableOnTimeArg) { if (functionKind != FunctionKind.PROCESS_TABLE) { return inputStrategy; } - return new SystemInputStrategy(staticArgs, inputStrategy, disableSystemArgs); + return new SystemInputStrategy( + staticArgs, inputStrategy, disableSystemArgs, disableOnTimeArg); } private static TypeStrategy deriveSystemOutputStrategy( @@ -661,14 +677,17 @@ private static class SystemInputStrategy implements InputTypeStrategy { private final List staticArgs; private final InputTypeStrategy origin; private final boolean disableSystemArgs; + private final boolean disableOnTimeArg; private SystemInputStrategy( List staticArgs, InputTypeStrategy origin, - boolean disableSystemArgs) { + boolean disableSystemArgs, + boolean disableOnTimeArg) { this.staticArgs = staticArgs; this.origin = origin; this.disableSystemArgs = disableSystemArgs; + this.disableOnTimeArg = disableOnTimeArg; } @Override @@ -700,6 +719,9 @@ public Optional> inferInputTypes( // Resolve once so the rest of validation iterates the effective signature. final List resolvedArgs = resolveStaticArgs(callContext, staticArgs); try { + if (!disableSystemArgs && disableOnTimeArg) { + checkNoOnTimeArg(callContext); + } checkTableArgs(resolvedArgs, callContext); if (!disableSystemArgs) { checkUidArg(callContext); @@ -718,6 +740,25 @@ public List getExpectedSignatures(FunctionDefinition definition) { return origin.getExpectedSignatures(definition); } + private static void checkNoOnTimeArg(CallContext callContext) { + final int onTimePos = + callContext.getArgumentDataTypes().size() + - 1 + - PROCESS_TABLE_FUNCTION_ARG_ON_TIME_OFFSET; + final boolean hasOnTime = + callContext + .getArgumentValue(onTimePos, ColumnList.class) + .map(columns -> !columns.getNames().isEmpty()) + .orElse(false); + if (hasOnTime) { + throw new ValidationException( + String.format( + "The 'on_time' argument is not supported because function '%s' " + + "does not support time attributes.", + callContext.getName())); + } + } + private static void checkUidArg(CallContext callContext) { final List args = callContext.getArgumentDataTypes(); diff --git a/flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/bridging/BridgingSqlFunction.java b/flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/bridging/BridgingSqlFunction.java index cbfa6e3e7da88d..1987a1f11cef85 100644 --- a/flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/bridging/BridgingSqlFunction.java +++ b/flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/bridging/BridgingSqlFunction.java @@ -143,7 +143,7 @@ public static BridgingSqlFunction of( "Scalar or table function kind expected."); final TypeInference systemTypeInference = - SystemTypeInference.of(functionKind, typeInference); + SystemTypeInference.of(resolvedFunction.getDefinition(), typeInference); if (functionKind == FunctionKind.TABLE || functionKind == FunctionKind.ASYNC_TABLE