Search in sources :

Example 11 with MergingStrategy

use of org.apache.flink.sql.parser.ddl.SqlTableLike.MergingStrategy in project flink by apache.

the class MergeTableLikeUtilTest method mergeOverwritingMetadataColumnsDuplicate.

@Test
public void mergeOverwritingMetadataColumnsDuplicate() {
    TableSchema sourceSchema = TableSchema.builder().add(TableColumn.physical("one", DataTypes.INT())).add(TableColumn.metadata("two", DataTypes.INT())).build();
    List<SqlNode> derivedColumns = Collections.singletonList(metadataColumn("two", DataTypes.BOOLEAN(), true));
    Map<FeatureOption, MergingStrategy> mergingStrategies = getDefaultMergingStrategies();
    mergingStrategies.put(FeatureOption.METADATA, MergingStrategy.OVERWRITING);
    TableSchema mergedSchema = util.mergeTables(mergingStrategies, sourceSchema, derivedColumns, Collections.emptyList(), null);
    TableSchema expectedSchema = TableSchema.builder().add(TableColumn.physical("one", DataTypes.INT())).add(TableColumn.metadata("two", DataTypes.BOOLEAN(), true)).build();
    assertThat(mergedSchema, equalTo(expectedSchema));
}
Also used : FeatureOption(org.apache.flink.sql.parser.ddl.SqlTableLike.FeatureOption) TableSchema(org.apache.flink.table.api.TableSchema) MergingStrategy(org.apache.flink.sql.parser.ddl.SqlTableLike.MergingStrategy) SqlNode(org.apache.calcite.sql.SqlNode) Test(org.junit.Test)

Example 12 with MergingStrategy

use of org.apache.flink.sql.parser.ddl.SqlTableLike.MergingStrategy in project flink by apache.

the class MergeTableLikeUtilTest method mergeOverwritingGeneratedColumnsDuplicate.

@Test
public void mergeOverwritingGeneratedColumnsDuplicate() {
    TableSchema sourceSchema = TableSchema.builder().add(TableColumn.physical("one", DataTypes.INT())).add(TableColumn.computed("two", DataTypes.INT(), "one + 1")).build();
    List<SqlNode> derivedColumns = Collections.singletonList(computedColumn("two", plus("one", "3")));
    Map<FeatureOption, MergingStrategy> mergingStrategies = getDefaultMergingStrategies();
    mergingStrategies.put(FeatureOption.GENERATED, MergingStrategy.OVERWRITING);
    TableSchema mergedSchema = util.mergeTables(mergingStrategies, sourceSchema, derivedColumns, Collections.emptyList(), null);
    TableSchema expectedSchema = TableSchema.builder().add(TableColumn.physical("one", DataTypes.INT())).add(TableColumn.computed("two", DataTypes.INT(), "`one` + 3")).build();
    assertThat(mergedSchema, equalTo(expectedSchema));
}
Also used : FeatureOption(org.apache.flink.sql.parser.ddl.SqlTableLike.FeatureOption) TableSchema(org.apache.flink.table.api.TableSchema) MergingStrategy(org.apache.flink.sql.parser.ddl.SqlTableLike.MergingStrategy) SqlNode(org.apache.calcite.sql.SqlNode) Test(org.junit.Test)

Example 13 with MergingStrategy

use of org.apache.flink.sql.parser.ddl.SqlTableLike.MergingStrategy in project flink by apache.

the class MergeTableLikeUtil method computeMergingStrategies.

/**
 * Calculates merging strategies for all options. It applies options given by a user to the
 * {@link #defaultMergingStrategies}. The {@link MergingStrategy} specified for {@link
 * FeatureOption#ALL} overwrites all the default options. Those can be further changed with a
 * specific {@link FeatureOption}.
 */
public Map<FeatureOption, MergingStrategy> computeMergingStrategies(List<SqlTableLike.SqlTableLikeOption> mergingOptions) {
    Map<FeatureOption, MergingStrategy> result = new HashMap<>(defaultMergingStrategies);
    Optional<SqlTableLike.SqlTableLikeOption> maybeAllOption = mergingOptions.stream().filter(option -> option.getFeatureOption() == FeatureOption.ALL).findFirst();
    maybeAllOption.ifPresent((allOption) -> {
        MergingStrategy strategy = allOption.getMergingStrategy();
        for (FeatureOption featureOption : FeatureOption.values()) {
            if (featureOption != FeatureOption.ALL) {
                result.put(featureOption, strategy);
            }
        }
    });
    for (SqlTableLike.SqlTableLikeOption mergingOption : mergingOptions) {
        result.put(mergingOption.getFeatureOption(), mergingOption.getMergingStrategy());
    }
    return result;
}
Also used : DataType(org.apache.flink.table.types.DataType) MetadataColumn(org.apache.flink.table.api.TableColumn.MetadataColumn) HashMap(java.util.HashMap) PhysicalColumn(org.apache.flink.table.api.TableColumn.PhysicalColumn) SqlRegularColumn(org.apache.flink.sql.parser.ddl.SqlTableColumn.SqlRegularColumn) FlinkTypeFactory(org.apache.flink.table.planner.calcite.FlinkTypeFactory) SqlDataTypeSpec(org.apache.calcite.sql.SqlDataTypeSpec) Function(java.util.function.Function) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) SqlNode(org.apache.calcite.sql.SqlNode) SqlComputedColumn(org.apache.flink.sql.parser.ddl.SqlTableColumn.SqlComputedColumn) TableColumn(org.apache.flink.table.api.TableColumn) Map(java.util.Map) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) TypeConversions.fromLogicalToDataType(org.apache.flink.table.types.utils.TypeConversions.fromLogicalToDataType) SqlTableColumn(org.apache.flink.sql.parser.ddl.SqlTableColumn) SqlValidator(org.apache.calcite.sql.validate.SqlValidator) SqlCreateTable(org.apache.flink.sql.parser.ddl.SqlCreateTable) Nullable(javax.annotation.Nullable) ComputedColumn(org.apache.flink.table.api.TableColumn.ComputedColumn) RelDataType(org.apache.calcite.rel.type.RelDataType) MergingStrategy(org.apache.flink.sql.parser.ddl.SqlTableLike.MergingStrategy) SqlWatermark(org.apache.flink.sql.parser.ddl.SqlWatermark) FlinkTypeFactory.toLogicalType(org.apache.flink.table.planner.calcite.FlinkTypeFactory.toLogicalType) WatermarkSpec(org.apache.flink.table.api.WatermarkSpec) SqlTableConstraint(org.apache.flink.sql.parser.ddl.constraint.SqlTableConstraint) TableSchema(org.apache.flink.table.api.TableSchema) Collectors(java.util.stream.Collectors) SqlTableLike(org.apache.flink.sql.parser.ddl.SqlTableLike) List(java.util.List) LogicalType(org.apache.flink.table.types.logical.LogicalType) SqlMetadataColumn(org.apache.flink.sql.parser.ddl.SqlTableColumn.SqlMetadataColumn) ValidationException(org.apache.flink.table.api.ValidationException) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) Optional(java.util.Optional) TypeConversions(org.apache.flink.table.types.utils.TypeConversions) UniqueConstraint(org.apache.flink.table.api.constraints.UniqueConstraint) FeatureOption(org.apache.flink.sql.parser.ddl.SqlTableLike.FeatureOption) FeatureOption(org.apache.flink.sql.parser.ddl.SqlTableLike.FeatureOption) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MergingStrategy(org.apache.flink.sql.parser.ddl.SqlTableLike.MergingStrategy) SqlTableLike(org.apache.flink.sql.parser.ddl.SqlTableLike)

Aggregations

FeatureOption (org.apache.flink.sql.parser.ddl.SqlTableLike.FeatureOption)13 MergingStrategy (org.apache.flink.sql.parser.ddl.SqlTableLike.MergingStrategy)13 Test (org.junit.Test)12 TableSchema (org.apache.flink.table.api.TableSchema)10 SqlNode (org.apache.calcite.sql.SqlNode)7 SqlTableLikeOption (org.apache.flink.sql.parser.ddl.SqlTableLike.SqlTableLikeOption)3 SqlWatermark (org.apache.flink.sql.parser.ddl.SqlWatermark)3 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 Nullable (javax.annotation.Nullable)1 RelDataType (org.apache.calcite.rel.type.RelDataType)1 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)1 SqlDataTypeSpec (org.apache.calcite.sql.SqlDataTypeSpec)1 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)1