-
Notifications
You must be signed in to change notification settings - Fork 13.9k
[FLINK-38826][table] Add new ArgumentStrategy to support array with specified element type #27374
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
dylanhz
wants to merge
2
commits into
apache:master
Choose a base branch
from
dylanhz:FLINK-38826
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.
+786
−441
Open
Changes from all commits
Commits
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
178 changes: 178 additions & 0 deletions
178
.../org/apache/flink/table/types/inference/strategies/ArrayOfFamilyArgumentTypeStrategy.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,178 @@ | ||
| /* | ||
| * 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.ValidationException; | ||
| import org.apache.flink.table.functions.FunctionDefinition; | ||
| import org.apache.flink.table.types.CollectionDataType; | ||
| import org.apache.flink.table.types.DataType; | ||
| import org.apache.flink.table.types.inference.ArgumentTypeStrategy; | ||
| import org.apache.flink.table.types.inference.CallContext; | ||
| import org.apache.flink.table.types.inference.Signature.Argument; | ||
| import org.apache.flink.table.types.logical.LogicalType; | ||
| import org.apache.flink.table.types.logical.LogicalTypeFamily; | ||
| import org.apache.flink.table.types.logical.LogicalTypeRoot; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
||
| import java.util.Objects; | ||
| import java.util.Optional; | ||
|
|
||
| import static org.apache.flink.table.types.inference.strategies.FamilyArgumentTypeStrategy.FAMILY_TO_ROOT; | ||
| import static org.apache.flink.table.types.inference.strategies.StrategyUtils.findDataType; | ||
|
|
||
| /** | ||
| * Strategy for an argument that corresponds to an {@code ARRAY} with specified element {@link | ||
| * LogicalTypeFamily} and nullability. | ||
| * | ||
| * <p>Implicit casts for element will be inserted if possible. | ||
| */ | ||
| @Internal | ||
| public class ArrayOfFamilyArgumentTypeStrategy implements ArgumentTypeStrategy { | ||
|
|
||
| private final LogicalTypeFamily expectedElementFamily; | ||
| private final @Nullable Boolean expectedArrayNullability; | ||
| private final @Nullable Boolean expectedElementNullability; | ||
|
|
||
| public ArrayOfFamilyArgumentTypeStrategy( | ||
| LogicalTypeFamily expectedElementFamily, | ||
| @Nullable Boolean expectedArrayNullability, | ||
| @Nullable Boolean expectedElementNullability) { | ||
| this.expectedElementFamily = expectedElementFamily; | ||
| this.expectedArrayNullability = expectedArrayNullability; | ||
| this.expectedElementNullability = expectedElementNullability; | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<DataType> inferArgumentType( | ||
| CallContext callContext, int argumentPos, boolean throwOnFailure) { | ||
| DataType actualDataType = callContext.getArgumentDataTypes().get(argumentPos); | ||
| LogicalType actualLogicalType = actualDataType.getLogicalType(); | ||
|
|
||
| if (!actualLogicalType.is(LogicalTypeRoot.ARRAY) | ||
| || Boolean.FALSE.equals(expectedArrayNullability) | ||
| && actualLogicalType.isNullable()) { | ||
| return callContext.fail( | ||
| throwOnFailure, | ||
| "Unsupported argument type. Expected %stype of root 'ARRAY' but actual type was '%s'.", | ||
| Boolean.FALSE.equals(expectedArrayNullability) ? "NOT NULL " : "", | ||
| actualLogicalType); | ||
| } | ||
|
|
||
| return inferElementType( | ||
| callContext, | ||
| throwOnFailure, | ||
| ((CollectionDataType) actualDataType).getElementDataType()) | ||
| .map(DataTypes::ARRAY) | ||
| .map( | ||
| dataType -> | ||
| Boolean.FALSE.equals(expectedArrayNullability) | ||
| || expectedArrayNullability == null | ||
| && !actualLogicalType.isNullable() | ||
| ? dataType.notNull() | ||
| : dataType); | ||
| } | ||
|
|
||
| private Optional<DataType> inferElementType( | ||
| CallContext callContext, boolean throwOnFailure, DataType elementDataType) { | ||
| LogicalType elementLogicalType = elementDataType.getLogicalType(); | ||
|
|
||
| if (Boolean.FALSE.equals(expectedElementNullability) && elementLogicalType.isNullable()) { | ||
| return callContext.fail( | ||
| throwOnFailure, | ||
| "Unsupported argument type. Expected NOT NULL element type of family '%s' but actual type was '%s'.", | ||
| expectedElementFamily, | ||
| elementLogicalType); | ||
| } | ||
|
|
||
| // type is part of the family | ||
| if (elementLogicalType.getTypeRoot().getFamilies().contains(expectedElementFamily)) { | ||
| if (Boolean.TRUE.equals(expectedElementNullability) | ||
| && !elementLogicalType.isNullable()) { | ||
| return Optional.of(elementDataType.nullable()); | ||
| } | ||
| return Optional.of(elementDataType); | ||
| } | ||
|
|
||
| Optional<DataType> inferredElementDataType = Optional.empty(); | ||
| // use the smallest type in the family to find a common type | ||
| LogicalTypeRoot expectedElementRoot = FAMILY_TO_ROOT.get(expectedElementFamily); | ||
| if (expectedElementRoot != null) { | ||
| try { | ||
| inferredElementDataType = | ||
| findDataType( | ||
| callContext, | ||
| throwOnFailure, | ||
| elementDataType, | ||
| expectedElementRoot, | ||
| expectedElementNullability); | ||
| } catch (ValidationException t) { | ||
| // wrap inner exception to provide more context about element | ||
| } | ||
| } | ||
|
|
||
| return inferredElementDataType.or( | ||
| () -> | ||
| callContext.fail( | ||
| throwOnFailure, | ||
| "Unsupported argument type. Expected %selement type of family '%s' but actual type was '%s'.", | ||
| Boolean.FALSE.equals(expectedElementNullability) ? "NOT NULL " : "", | ||
| expectedElementFamily, | ||
| elementLogicalType)); | ||
| } | ||
|
|
||
| @Override | ||
| public Argument getExpectedArgument(FunctionDefinition def, int pos) { | ||
| String elementType = expectedElementFamily.toString(); | ||
| if (Boolean.TRUE.equals(expectedElementNullability)) { | ||
| elementType += " NULL"; | ||
| } else if (Boolean.FALSE.equals(expectedElementNullability)) { | ||
| elementType += " NOT NULL"; | ||
| } | ||
| String type = "ARRAY<<" + elementType + ">>"; | ||
| if (Boolean.TRUE.equals(expectedArrayNullability)) { | ||
| type += " NULL"; | ||
| } else if (Boolean.FALSE.equals(expectedArrayNullability)) { | ||
| type += " NOT NULL"; | ||
| } | ||
| return Argument.of(type); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
| ArrayOfFamilyArgumentTypeStrategy strategy = (ArrayOfFamilyArgumentTypeStrategy) o; | ||
| return expectedElementFamily == strategy.expectedElementFamily | ||
| && Objects.equals(expectedArrayNullability, strategy.expectedArrayNullability) | ||
| && Objects.equals(expectedElementNullability, strategy.expectedElementNullability); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash( | ||
| expectedElementFamily, expectedArrayNullability, expectedElementNullability); | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please could you update the Jira text with some examples on how this will effect the externals and add to the documentation if appropriate.