Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ private List<ResolvedExpression> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -434,6 +450,7 @@ public BuiltInFunctionDefinition build() {
isRuntimeProvided,
runtimeClass,
isInternal,
isOnTimeArgumentDisabled,
changelogModeStrategy);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<StaticArgument> systemArgs =
Expand All @@ -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());
}
Expand Down Expand Up @@ -344,11 +358,13 @@ private static InputTypeStrategy deriveSystemInputStrategy(
FunctionKind functionKind,
@Nullable List<StaticArgument> 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(
Expand Down Expand Up @@ -661,14 +677,17 @@ private static class SystemInputStrategy implements InputTypeStrategy {
private final List<StaticArgument> staticArgs;
private final InputTypeStrategy origin;
private final boolean disableSystemArgs;
private final boolean disableOnTimeArg;

private SystemInputStrategy(
List<StaticArgument> staticArgs,
InputTypeStrategy origin,
boolean disableSystemArgs) {
boolean disableSystemArgs,
boolean disableOnTimeArg) {
this.staticArgs = staticArgs;
this.origin = origin;
this.disableSystemArgs = disableSystemArgs;
this.disableOnTimeArg = disableOnTimeArg;
}

@Override
Expand Down Expand Up @@ -700,6 +719,9 @@ public Optional<List<DataType>> inferInputTypes(
// Resolve once so the rest of validation iterates the effective signature.
final List<StaticArgument> resolvedArgs = resolveStaticArgs(callContext, staticArgs);
try {
if (!disableSystemArgs && disableOnTimeArg) {
checkNoOnTimeArg(callContext);
}
checkTableArgs(resolvedArgs, callContext);
if (!disableSystemArgs) {
checkUidArg(callContext);
Expand All @@ -718,6 +740,25 @@ public List<Signature> 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<DataType> args = callContext.getArgumentDataTypes();

Expand Down
Original file line number Diff line number Diff line change
@@ -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<List<DataType>> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down