Search in sources :

Example 11 with TableSchema

use of org.apache.flink.table.api.TableSchema in project flink by apache.

the class DescriptorProperties method getOptionalTableSchema.

/**
 * Returns a table schema under the given key if it exists.
 */
public Optional<TableSchema> getOptionalTableSchema(String key) {
    // filter for number of fields
    final int fieldCount = properties.keySet().stream().filter((k) -> k.startsWith(key) && // "key." is the prefix.
    SCHEMA_COLUMN_NAME_SUFFIX.matcher(k.substring(key.length() + 1)).matches()).mapToInt((k) -> 1).sum();
    if (fieldCount == 0) {
        return Optional.empty();
    }
    // validate fields and build schema
    final TableSchema.Builder schemaBuilder = TableSchema.builder();
    for (int i = 0; i < fieldCount; i++) {
        final String nameKey = key + '.' + i + '.' + NAME;
        final String legacyTypeKey = key + '.' + i + '.' + TYPE;
        final String typeKey = key + '.' + i + '.' + DATA_TYPE;
        final String exprKey = key + '.' + i + '.' + EXPR;
        final String metadataKey = key + '.' + i + '.' + METADATA;
        final String virtualKey = key + '.' + i + '.' + VIRTUAL;
        final String name = optionalGet(nameKey).orElseThrow(exceptionSupplier(nameKey));
        final DataType type;
        if (containsKey(typeKey)) {
            type = getDataType(typeKey);
        } else if (containsKey(legacyTypeKey)) {
            type = TypeConversions.fromLegacyInfoToDataType(getType(legacyTypeKey));
        } else {
            throw exceptionSupplier(typeKey).get();
        }
        final Optional<String> expr = optionalGet(exprKey);
        final Optional<String> metadata = optionalGet(metadataKey);
        final boolean virtual = getOptionalBoolean(virtualKey).orElse(false);
        // computed column
        if (expr.isPresent()) {
            schemaBuilder.add(TableColumn.computed(name, type, expr.get()));
        } else // metadata column
        if (metadata.isPresent()) {
            final String metadataAlias = metadata.get();
            if (metadataAlias.equals(name)) {
                schemaBuilder.add(TableColumn.metadata(name, type, virtual));
            } else {
                schemaBuilder.add(TableColumn.metadata(name, type, metadataAlias, virtual));
            }
        } else // physical column
        {
            schemaBuilder.add(TableColumn.physical(name, type));
        }
    }
    // extract watermark information
    // filter for number of fields
    String watermarkPrefixKey = key + '.' + WATERMARK;
    final int watermarkCount = properties.keySet().stream().filter((k) -> k.startsWith(watermarkPrefixKey) && k.endsWith('.' + WATERMARK_ROWTIME)).mapToInt((k) -> 1).sum();
    if (watermarkCount > 0) {
        for (int i = 0; i < watermarkCount; i++) {
            final String rowtimeKey = watermarkPrefixKey + '.' + i + '.' + WATERMARK_ROWTIME;
            final String exprKey = watermarkPrefixKey + '.' + i + '.' + WATERMARK_STRATEGY_EXPR;
            final String typeKey = watermarkPrefixKey + '.' + i + '.' + WATERMARK_STRATEGY_DATA_TYPE;
            final String rowtime = optionalGet(rowtimeKey).orElseThrow(exceptionSupplier(rowtimeKey));
            final String exprString = optionalGet(exprKey).orElseThrow(exceptionSupplier(exprKey));
            final String typeString = optionalGet(typeKey).orElseThrow(exceptionSupplier(typeKey));
            final DataType exprType = TypeConversions.fromLogicalToDataType(LogicalTypeParser.parse(typeString));
            schemaBuilder.watermark(rowtime, exprString, exprType);
        }
    }
    // Extract unique constraints.
    String pkConstraintNameKey = key + '.' + PRIMARY_KEY_NAME;
    final Optional<String> pkConstraintNameOpt = optionalGet(pkConstraintNameKey);
    if (pkConstraintNameOpt.isPresent()) {
        final String pkColumnsKey = key + '.' + PRIMARY_KEY_COLUMNS;
        final String columns = optionalGet(pkColumnsKey).orElseThrow(exceptionSupplier(pkColumnsKey));
        schemaBuilder.primaryKey(pkConstraintNameOpt.get(), columns.split(","));
    }
    return Optional.of(schemaBuilder.build());
}
Also used : DynamicTableFactory(org.apache.flink.table.factories.DynamicTableFactory) IntStream(java.util.stream.IntStream) DataType(org.apache.flink.table.types.DataType) Arrays(java.util.Arrays) EncodingUtils(org.apache.flink.table.utils.EncodingUtils) MetadataColumn(org.apache.flink.table.api.TableColumn.MetadataColumn) HashMap(java.util.HashMap) RowTypeInfo(org.apache.flink.api.java.typeutils.RowTypeInfo) Function(java.util.function.Function) Supplier(java.util.function.Supplier) MemorySize(org.apache.flink.configuration.MemorySize) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) BigDecimal(java.math.BigDecimal) InstantiationUtil(org.apache.flink.util.InstantiationUtil) Matcher(java.util.regex.Matcher) TableColumn(org.apache.flink.table.api.TableColumn) Duration(java.time.Duration) Map(java.util.Map) ConfigOption(org.apache.flink.configuration.ConfigOption) Preconditions.checkNotNull(org.apache.flink.util.Preconditions.checkNotNull) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) ComputedColumn(org.apache.flink.table.api.TableColumn.ComputedColumn) CatalogPropertiesUtil(org.apache.flink.table.catalog.CatalogPropertiesUtil) TableException(org.apache.flink.table.api.TableException) WatermarkSpec(org.apache.flink.table.api.WatermarkSpec) Set(java.util.Set) TableSchema(org.apache.flink.table.api.TableSchema) Preconditions(org.apache.flink.util.Preconditions) Collectors(java.util.stream.Collectors) TypeStringUtils(org.apache.flink.table.utils.TypeStringUtils) Objects(java.util.Objects) Consumer(java.util.function.Consumer) List(java.util.List) TimeUtils(org.apache.flink.util.TimeUtils) Stream(java.util.stream.Stream) LogicalType(org.apache.flink.table.types.logical.LogicalType) ValidationException(org.apache.flink.table.api.ValidationException) LogicalTypeParser(org.apache.flink.table.types.logical.utils.LogicalTypeParser) Optional(java.util.Optional) Internal(org.apache.flink.annotation.Internal) TypeConversions(org.apache.flink.table.types.utils.TypeConversions) Pattern(java.util.regex.Pattern) LogicalTypeRoot(org.apache.flink.table.types.logical.LogicalTypeRoot) Collections(java.util.Collections) TableSchema(org.apache.flink.table.api.TableSchema) DataType(org.apache.flink.table.types.DataType)

Example 12 with TableSchema

use of org.apache.flink.table.api.TableSchema in project flink by apache.

the class DescriptorProperties method putTableSchema.

/**
 * Adds a table schema under the given key.
 */
public void putTableSchema(String key, TableSchema schema) {
    checkNotNull(key);
    checkNotNull(schema);
    final String[] fieldNames = schema.getFieldNames();
    final DataType[] fieldTypes = schema.getFieldDataTypes();
    final String[] fieldExpressions = schema.getTableColumns().stream().map(column -> {
        if (column instanceof ComputedColumn) {
            return ((ComputedColumn) column).getExpression();
        }
        return null;
    }).toArray(String[]::new);
    final String[] fieldMetadata = schema.getTableColumns().stream().map(column -> {
        if (column instanceof MetadataColumn) {
            return ((MetadataColumn) column).getMetadataAlias().orElse(column.getName());
        }
        return null;
    }).toArray(String[]::new);
    final String[] fieldVirtual = schema.getTableColumns().stream().map(column -> {
        if (column instanceof MetadataColumn) {
            return Boolean.toString(((MetadataColumn) column).isVirtual());
        }
        return null;
    }).toArray(String[]::new);
    final List<List<String>> values = new ArrayList<>();
    for (int i = 0; i < schema.getFieldCount(); i++) {
        values.add(Arrays.asList(fieldNames[i], fieldTypes[i].getLogicalType().asSerializableString(), fieldExpressions[i], fieldMetadata[i], fieldVirtual[i]));
    }
    putIndexedOptionalProperties(key, Arrays.asList(NAME, DATA_TYPE, EXPR, METADATA, VIRTUAL), values);
    if (!schema.getWatermarkSpecs().isEmpty()) {
        final List<List<String>> watermarkValues = new ArrayList<>();
        for (WatermarkSpec spec : schema.getWatermarkSpecs()) {
            watermarkValues.add(Arrays.asList(spec.getRowtimeAttribute(), spec.getWatermarkExpr(), spec.getWatermarkExprOutputType().getLogicalType().asSerializableString()));
        }
        putIndexedFixedProperties(key + '.' + WATERMARK, Arrays.asList(WATERMARK_ROWTIME, WATERMARK_STRATEGY_EXPR, WATERMARK_STRATEGY_DATA_TYPE), watermarkValues);
    }
    schema.getPrimaryKey().ifPresent(pk -> {
        putString(key + '.' + PRIMARY_KEY_NAME, pk.getName());
        putString(key + '.' + PRIMARY_KEY_COLUMNS, String.join(",", pk.getColumns()));
    });
}
Also used : DynamicTableFactory(org.apache.flink.table.factories.DynamicTableFactory) IntStream(java.util.stream.IntStream) DataType(org.apache.flink.table.types.DataType) Arrays(java.util.Arrays) EncodingUtils(org.apache.flink.table.utils.EncodingUtils) MetadataColumn(org.apache.flink.table.api.TableColumn.MetadataColumn) HashMap(java.util.HashMap) RowTypeInfo(org.apache.flink.api.java.typeutils.RowTypeInfo) Function(java.util.function.Function) Supplier(java.util.function.Supplier) MemorySize(org.apache.flink.configuration.MemorySize) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) BigDecimal(java.math.BigDecimal) InstantiationUtil(org.apache.flink.util.InstantiationUtil) Matcher(java.util.regex.Matcher) TableColumn(org.apache.flink.table.api.TableColumn) Duration(java.time.Duration) Map(java.util.Map) ConfigOption(org.apache.flink.configuration.ConfigOption) Preconditions.checkNotNull(org.apache.flink.util.Preconditions.checkNotNull) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) ComputedColumn(org.apache.flink.table.api.TableColumn.ComputedColumn) CatalogPropertiesUtil(org.apache.flink.table.catalog.CatalogPropertiesUtil) TableException(org.apache.flink.table.api.TableException) WatermarkSpec(org.apache.flink.table.api.WatermarkSpec) Set(java.util.Set) TableSchema(org.apache.flink.table.api.TableSchema) Preconditions(org.apache.flink.util.Preconditions) Collectors(java.util.stream.Collectors) TypeStringUtils(org.apache.flink.table.utils.TypeStringUtils) Objects(java.util.Objects) Consumer(java.util.function.Consumer) List(java.util.List) TimeUtils(org.apache.flink.util.TimeUtils) Stream(java.util.stream.Stream) LogicalType(org.apache.flink.table.types.logical.LogicalType) ValidationException(org.apache.flink.table.api.ValidationException) LogicalTypeParser(org.apache.flink.table.types.logical.utils.LogicalTypeParser) Optional(java.util.Optional) Internal(org.apache.flink.annotation.Internal) TypeConversions(org.apache.flink.table.types.utils.TypeConversions) Pattern(java.util.regex.Pattern) LogicalTypeRoot(org.apache.flink.table.types.logical.LogicalTypeRoot) Collections(java.util.Collections) MetadataColumn(org.apache.flink.table.api.TableColumn.MetadataColumn) WatermarkSpec(org.apache.flink.table.api.WatermarkSpec) ArrayList(java.util.ArrayList) DataType(org.apache.flink.table.types.DataType) ComputedColumn(org.apache.flink.table.api.TableColumn.ComputedColumn) ArrayList(java.util.ArrayList) List(java.util.List)

Example 13 with TableSchema

use of org.apache.flink.table.api.TableSchema in project flink by apache.

the class TableSchemaUtilsTest method testBuilderWithGivenSchema.

@Test
public void testBuilderWithGivenSchema() {
    TableSchema oriSchema = TableSchema.builder().field("a", DataTypes.INT().notNull()).field("b", DataTypes.STRING()).field("c", DataTypes.INT(), "a + 1").field("t", DataTypes.TIMESTAMP(3)).primaryKey("ct1", new String[] { "a" }).watermark("t", "t", DataTypes.TIMESTAMP(3)).build();
    TableSchema newSchema = TableSchemaUtils.builderWithGivenSchema(oriSchema).build();
    assertEquals(oriSchema, newSchema);
}
Also used : TableSchema(org.apache.flink.table.api.TableSchema) Test(org.junit.Test)

Example 14 with TableSchema

use of org.apache.flink.table.api.TableSchema in project flink by apache.

the class TypeMappingUtilsTest method testCheckPhysicalLogicalTypeCompatible.

@Test
public void testCheckPhysicalLogicalTypeCompatible() {
    TableSchema tableSchema = TableSchema.builder().field("a", DataTypes.VARCHAR(2)).field("b", DataTypes.DECIMAL(20, 2)).build();
    TableSink tableSink = new TestTableSink(tableSchema);
    LegacyTypeInformationType legacyDataType = (LegacyTypeInformationType) tableSink.getConsumedDataType().getLogicalType();
    TypeInformation legacyTypeInfo = ((TupleTypeInfo) legacyDataType.getTypeInformation()).getTypeAt(1);
    DataType physicalType = TypeConversions.fromLegacyInfoToDataType(legacyTypeInfo);
    ResolvedSchema physicSchema = DataTypeUtils.expandCompositeTypeToSchema(physicalType);
    DataType[] logicalDataTypes = tableSchema.getFieldDataTypes();
    List<DataType> physicalDataTypes = physicSchema.getColumnDataTypes();
    for (int i = 0; i < logicalDataTypes.length; i++) {
        TypeMappingUtils.checkPhysicalLogicalTypeCompatible(physicalDataTypes.get(i).getLogicalType(), logicalDataTypes[i].getLogicalType(), "physicalField", "logicalField", false);
    }
}
Also used : TableSchema(org.apache.flink.table.api.TableSchema) DataType(org.apache.flink.table.types.DataType) TableSink(org.apache.flink.table.sinks.TableSink) ResolvedSchema(org.apache.flink.table.catalog.ResolvedSchema) LegacyTypeInformationType(org.apache.flink.table.types.logical.LegacyTypeInformationType) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) TupleTypeInfo(org.apache.flink.api.java.typeutils.TupleTypeInfo) Test(org.junit.Test)

Example 15 with TableSchema

use of org.apache.flink.table.api.TableSchema in project flink by apache.

the class TableFormatFactoryBaseTest method testSchemaDerivationWithRowtime.

@Test
public void testSchemaDerivationWithRowtime() {
    final Map<String, String> properties = new HashMap<>();
    properties.put("schema.0.name", "otherField");
    properties.put("schema.0.type", "VARCHAR");
    properties.put("schema.0.from", "csvField");
    properties.put("schema.1.name", "abcField");
    properties.put("schema.1.type", "VARCHAR");
    properties.put("schema.2.name", "p");
    properties.put("schema.2.type", "TIMESTAMP");
    properties.put("schema.2.proctime", "true");
    properties.put("schema.3.name", "r");
    properties.put("schema.3.type", "TIMESTAMP");
    // from-field strategy
    properties.put("schema.3.rowtime.timestamps.type", "from-field");
    properties.put("schema.3.rowtime.timestamps.from", "myTime");
    properties.put("schema.3.rowtime.watermarks.type", "from-source");
    final TableSchema actualSchema = TableFormatFactoryBase.deriveSchema(properties);
    final TableSchema expectedSchema = TableSchema.builder().field("csvField", // aliased
    Types.STRING).field("abcField", Types.STRING).field("myTime", Types.SQL_TIMESTAMP).build();
    assertEquals(expectedSchema, actualSchema);
}
Also used : TableSchema(org.apache.flink.table.api.TableSchema) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

TableSchema (org.apache.flink.table.api.TableSchema)86 Test (org.junit.Test)54 HashMap (java.util.HashMap)26 CatalogTableImpl (org.apache.flink.table.catalog.CatalogTableImpl)21 SqlNode (org.apache.calcite.sql.SqlNode)19 ObjectPath (org.apache.flink.table.catalog.ObjectPath)19 CatalogTable (org.apache.flink.table.catalog.CatalogTable)18 DataType (org.apache.flink.table.types.DataType)16 ValidationException (org.apache.flink.table.api.ValidationException)14 TableColumn (org.apache.flink.table.api.TableColumn)10 UniqueConstraint (org.apache.flink.table.api.constraints.UniqueConstraint)10 ArrayList (java.util.ArrayList)9 List (java.util.List)9 Map (java.util.Map)9 FeatureOption (org.apache.flink.sql.parser.ddl.SqlTableLike.FeatureOption)9 MergingStrategy (org.apache.flink.sql.parser.ddl.SqlTableLike.MergingStrategy)9 CatalogBaseTable (org.apache.flink.table.catalog.CatalogBaseTable)8 ObjectIdentifier (org.apache.flink.table.catalog.ObjectIdentifier)8 Arrays (java.util.Arrays)7 Configuration (org.apache.flink.configuration.Configuration)7