-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FLINK-32261][table] Add built-in MAP_UNION function
- Loading branch information
1 parent
5f38a57
commit c2a6fee
Showing
14 changed files
with
676 additions
and
139 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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
119 changes: 119 additions & 0 deletions
119
.../org/apache/flink/table/types/inference/strategies/CommonCollectionInputTypeStrategy.java
This file contains 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,119 @@ | ||
/* | ||
* 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.FunctionDefinition; | ||
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.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.LegacyTypeInformationType; | ||
import org.apache.flink.table.types.logical.LogicalType; | ||
import org.apache.flink.table.types.logical.LogicalTypeRoot; | ||
import org.apache.flink.table.types.logical.utils.LogicalTypeMerging; | ||
import org.apache.flink.table.types.utils.TypeConversions; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
/** An {@link InputTypeStrategy} that expects that all arguments have a common type. */ | ||
@Internal | ||
public class CommonCollectionInputTypeStrategy implements InputTypeStrategy { | ||
|
||
private static final Argument COMMON_ARGUMENT = Argument.ofGroup("COMMON"); | ||
|
||
private final ArgumentCount argumentCount; | ||
|
||
private final LogicalTypeRoot logicalTypeRoot; | ||
private final String errorMessage; | ||
|
||
public CommonCollectionInputTypeStrategy( | ||
ArgumentCount argumentCount, String errorMessage, LogicalTypeRoot logicalTypeRoot) { | ||
this.argumentCount = argumentCount; | ||
this.errorMessage = errorMessage; | ||
this.logicalTypeRoot = logicalTypeRoot; | ||
} | ||
|
||
@Override | ||
public ArgumentCount getArgumentCount() { | ||
return argumentCount; | ||
} | ||
|
||
@Override | ||
public Optional<List<DataType>> inferInputTypes( | ||
CallContext callContext, boolean throwOnFailure) { | ||
List<DataType> argumentDataTypes = callContext.getArgumentDataTypes(); | ||
List<LogicalType> argumentTypes = | ||
argumentDataTypes.stream() | ||
.map(DataType::getLogicalType) | ||
.collect(Collectors.toList()); | ||
|
||
if (!argumentTypes.stream().allMatch(logicalType -> logicalType.is(logicalTypeRoot))) { | ||
return callContext.fail(throwOnFailure, errorMessage); | ||
} | ||
|
||
if (argumentTypes.stream().anyMatch(CommonCollectionInputTypeStrategy::isLegacyType)) { | ||
return Optional.of(argumentDataTypes); | ||
} | ||
|
||
Optional<LogicalType> commonType = LogicalTypeMerging.findCommonType(argumentTypes); | ||
|
||
if (!commonType.isPresent()) { | ||
return callContext.fail( | ||
throwOnFailure, | ||
"Could not find a common type for arguments: %s", | ||
argumentDataTypes); | ||
} | ||
|
||
return commonType.map( | ||
type -> | ||
Collections.nCopies( | ||
argumentTypes.size(), TypeConversions.fromLogicalToDataType(type))); | ||
} | ||
|
||
@Override | ||
public List<Signature> getExpectedSignatures(FunctionDefinition definition) { | ||
Optional<Integer> minCount = argumentCount.getMinCount(); | ||
Optional<Integer> maxCount = argumentCount.getMaxCount(); | ||
|
||
int numberOfMandatoryArguments = minCount.orElse(0); | ||
|
||
if (maxCount.isPresent()) { | ||
return IntStream.range(numberOfMandatoryArguments, maxCount.get() + 1) | ||
.mapToObj(count -> Signature.of(Collections.nCopies(count, COMMON_ARGUMENT))) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
List<Argument> arguments = | ||
new ArrayList<>(Collections.nCopies(numberOfMandatoryArguments, COMMON_ARGUMENT)); | ||
arguments.add(Argument.ofGroupVarying("COMMON")); | ||
return Collections.singletonList(Signature.of(arguments)); | ||
} | ||
|
||
private static boolean isLegacyType(LogicalType type) { | ||
return type instanceof LegacyTypeInformationType; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...in/java/org/apache/flink/table/types/inference/strategies/CommonMapInputTypeStrategy.java
This file contains 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,33 @@ | ||
/* | ||
* 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.types.inference.ArgumentCount; | ||
import org.apache.flink.table.types.inference.InputTypeStrategy; | ||
import org.apache.flink.table.types.logical.LogicalTypeRoot; | ||
|
||
/** An {@link InputTypeStrategy} that expects that all arguments have a common map type. */ | ||
@Internal | ||
public final class CommonMapInputTypeStrategy extends CommonCollectionInputTypeStrategy { | ||
|
||
public CommonMapInputTypeStrategy(ArgumentCount argumentCount) { | ||
super(argumentCount, "All arguments requires to be a MAP type", LogicalTypeRoot.MAP); | ||
} | ||
} |
Oops, something went wrong.