-
Notifications
You must be signed in to change notification settings - Fork 13.9k
[FLINK-14925][table] Support precision-aware TO_TIMESTAMP with format-based inference #27793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
raminqaf
wants to merge
6
commits into
apache:master
Choose a base branch
from
raminqaf:FLINK-14925
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+534
−123
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
edd47b2
[FLINK-14925][table] Support precision-aware TO_TIMESTAMP with format…
raminqaf ba30e5f
[FLINK-14925][table] Add tests for other precisions, and updated docs
raminqaf 9da5466
[FLINK-14925][table] Fix broken CI tests
raminqaf 2165c0e
[FLINK-14925][table] Fix more tests
raminqaf afe6ef9
[FLINK-14925][table] Return null if the first paramter is invalid
raminqaf b798f98
[FLINK-14925][table] Fix link in chinese doc
raminqaf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
.../java/org/apache/flink/table/types/inference/strategies/ToTimestampInputTypeStrategy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /* | ||
| * 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.functions.BuiltInFunctionDefinitions; | ||
| import org.apache.flink.table.functions.FunctionDefinition; | ||
| import org.apache.flink.table.types.DataType; | ||
| import org.apache.flink.table.types.inference.ArgumentCount; | ||
| import org.apache.flink.table.types.inference.ArgumentTypeStrategy; | ||
| 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.logical.LogicalTypeFamily; | ||
|
|
||
| import java.time.format.DateTimeFormatter; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| import static org.apache.flink.table.types.inference.InputTypeStrategies.logical; | ||
|
|
||
| /** | ||
| * Input type strategy for {@link BuiltInFunctionDefinitions#TO_TIMESTAMP} that validates the format | ||
| * pattern at compile time when provided as a literal. | ||
| */ | ||
| @Internal | ||
| public class ToTimestampInputTypeStrategy implements InputTypeStrategy { | ||
|
|
||
| private static final ArgumentTypeStrategy CHARACTER_STRING_FAMILY_ARG = | ||
| logical(LogicalTypeFamily.CHARACTER_STRING); | ||
|
|
||
| @Override | ||
| public ArgumentCount getArgumentCount() { | ||
| return ConstantArgumentCount.between(1, 2); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<List<DataType>> inferInputTypes( | ||
| CallContext callContext, boolean throwOnFailure) { | ||
| final List<DataType> result = new ArrayList<>(); | ||
| final int numberOfArguments = callContext.getArgumentDataTypes().size(); | ||
|
|
||
| final Optional<DataType> timestampArg = | ||
| CHARACTER_STRING_FAMILY_ARG.inferArgumentType(callContext, 0, throwOnFailure); | ||
| if (timestampArg.isEmpty()) { | ||
| return Optional.empty(); | ||
| } | ||
| result.add(timestampArg.get()); | ||
|
|
||
| if (numberOfArguments > 1) { | ||
| final Optional<DataType> patternArg = | ||
| validatePatternArgument(callContext, throwOnFailure); | ||
| if (patternArg.isEmpty()) { | ||
| return Optional.empty(); | ||
| } | ||
| result.add(patternArg.get()); | ||
| } | ||
|
|
||
| return Optional.of(result); | ||
| } | ||
|
|
||
| private Optional<DataType> validatePatternArgument( | ||
| final CallContext callContext, final boolean throwOnFailure) { | ||
| final Optional<DataType> patternArg = | ||
| CHARACTER_STRING_FAMILY_ARG.inferArgumentType(callContext, 1, throwOnFailure); | ||
| if (patternArg.isEmpty()) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| if (callContext.isArgumentLiteral(1)) { | ||
| final Optional<String> patternOpt = callContext.getArgumentValue(1, String.class); | ||
| if (patternOpt.isEmpty()) { | ||
| return callContext.fail(throwOnFailure, "Pattern can not be a null literal"); | ||
| } | ||
| try { | ||
| DateTimeFormatter.ofPattern(patternOpt.get()); | ||
| } catch (IllegalArgumentException e) { | ||
| return callContext.fail( | ||
| throwOnFailure, | ||
| "Invalid pattern for parsing TIMESTAMP: %s", | ||
| e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| return patternArg; | ||
| } | ||
|
|
||
| @Override | ||
| public List<Signature> getExpectedSignatures(FunctionDefinition definition) { | ||
| return List.of( | ||
| Signature.of(Argument.ofGroup(LogicalTypeFamily.CHARACTER_STRING)), | ||
| Signature.of( | ||
| Argument.ofGroup(LogicalTypeFamily.CHARACTER_STRING), | ||
| Argument.ofGroup("pattern", LogicalTypeFamily.CHARACTER_STRING))); | ||
| } | ||
| } |
63 changes: 63 additions & 0 deletions
63
.../main/java/org/apache/flink/table/types/inference/strategies/ToTimestampTypeStrategy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /* | ||
| * 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.types.DataType; | ||
| import org.apache.flink.table.types.inference.CallContext; | ||
| import org.apache.flink.table.types.inference.TypeStrategy; | ||
| import org.apache.flink.table.utils.DateTimeUtils; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| /** | ||
| * Type strategy of {@code TO_TIMESTAMP}. Returns {@code TIMESTAMP(3)} for the 1-arg variant and | ||
| * infers precision from the format pattern's trailing 'S' count for the 2-arg variant. | ||
| */ | ||
| @Internal | ||
| public class ToTimestampTypeStrategy implements TypeStrategy { | ||
|
|
||
| private static final int DEFAULT_PRECISION = 3; | ||
|
|
||
| @Override | ||
| public Optional<DataType> inferType(CallContext callContext) { | ||
| int outputPrecision = DEFAULT_PRECISION; | ||
|
|
||
| if (callContext.getArgumentDataTypes().size() == 2) { | ||
| outputPrecision = inferPrecisionFromFormat(callContext); | ||
| } | ||
|
|
||
| return Optional.of(DataTypes.TIMESTAMP(outputPrecision).nullable()); | ||
| } | ||
|
|
||
| /** | ||
| * Infers the output precision from a format string literal. Returns at least {@link | ||
| * #DEFAULT_PRECISION}. | ||
| */ | ||
| private static int inferPrecisionFromFormat(CallContext callContext) { | ||
| if (!callContext.isArgumentLiteral(1)) { | ||
| return DEFAULT_PRECISION; | ||
| } | ||
| return callContext | ||
| .getArgumentValue(1, String.class) | ||
| .map(DateTimeUtils::precisionFromFormat) | ||
| .orElse(DEFAULT_PRECISION); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...t/java/org/apache/flink/table/types/inference/strategies/ToTimestampTypeStrategyTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /* | ||
| * 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.types.inference.TypeStrategiesTestBase; | ||
|
|
||
| import java.util.stream.Stream; | ||
|
|
||
| /** Tests for {@link ToTimestampTypeStrategy}. */ | ||
| class ToTimestampTypeStrategyTest extends TypeStrategiesTestBase { | ||
|
|
||
| @Override | ||
| protected Stream<TestSpec> testData() { | ||
| return Stream.of( | ||
| // 1-arg: always TIMESTAMP(3) | ||
| TestSpec.forStrategy( | ||
| "TO_TIMESTAMP(<STRING>) returns TIMESTAMP(3)", | ||
| SpecificTypeStrategies.TO_TIMESTAMP) | ||
| .inputTypes(DataTypes.STRING()) | ||
| .expectDataType(DataTypes.TIMESTAMP(3).nullable()), | ||
| // 2-arg: non-literal format defaults to TIMESTAMP(3) | ||
| TestSpec.forStrategy( | ||
| "TO_TIMESTAMP(<STRING>, <STRING>) defaults to TIMESTAMP(3)", | ||
| SpecificTypeStrategies.TO_TIMESTAMP) | ||
| .inputTypes(DataTypes.STRING(), DataTypes.STRING()) | ||
| .expectDataType(DataTypes.TIMESTAMP(3).nullable()), | ||
| // Format-based precision: SSS → TIMESTAMP(3) | ||
| TestSpec.forStrategy( | ||
| "Format with SSS returns TIMESTAMP(3)", | ||
| SpecificTypeStrategies.TO_TIMESTAMP) | ||
| .inputTypes(DataTypes.STRING(), DataTypes.STRING()) | ||
| .calledWithLiteralAt(1, "yyyy-MM-dd HH:mm:ss.SSS") | ||
| .expectDataType(DataTypes.TIMESTAMP(3).nullable()), | ||
| // Format-based precision: SSSSSS → TIMESTAMP(6) | ||
| TestSpec.forStrategy( | ||
| "Format with SSSSSS returns TIMESTAMP(6)", | ||
| SpecificTypeStrategies.TO_TIMESTAMP) | ||
| .inputTypes(DataTypes.STRING(), DataTypes.STRING()) | ||
| .calledWithLiteralAt(1, "yyyy-MM-dd HH:mm:ss.SSSSSS") | ||
| .expectDataType(DataTypes.TIMESTAMP(6).nullable()), | ||
| // Format-based precision: SSSSSSSSS → TIMESTAMP(9) | ||
| TestSpec.forStrategy( | ||
| "Format with SSSSSSSSS returns TIMESTAMP(9)", | ||
| SpecificTypeStrategies.TO_TIMESTAMP) | ||
| .inputTypes(DataTypes.STRING(), DataTypes.STRING()) | ||
| .calledWithLiteralAt(1, "yyyy-MM-dd HH:mm:ss.SSSSSSSSS") | ||
| .expectDataType(DataTypes.TIMESTAMP(9).nullable()), | ||
| // Format without S → TIMESTAMP(3) | ||
| TestSpec.forStrategy( | ||
| "Format without S returns TIMESTAMP(3)", | ||
| SpecificTypeStrategies.TO_TIMESTAMP) | ||
| .inputTypes(DataTypes.STRING(), DataTypes.STRING()) | ||
| .calledWithLiteralAt(1, "yyyy-MM-dd HH:mm:ss") | ||
| .expectDataType(DataTypes.TIMESTAMP(3).nullable())); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.