Search in sources :

Example 1 with SqlTableLike

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

the class SqlCreateTableConverter method createCatalogTable.

private CatalogTable createCatalogTable(SqlCreateTable sqlCreateTable) {
    final TableSchema sourceTableSchema;
    final List<String> sourcePartitionKeys;
    final List<SqlTableLike.SqlTableLikeOption> likeOptions;
    final Map<String, String> sourceProperties;
    if (sqlCreateTable.getTableLike().isPresent()) {
        SqlTableLike sqlTableLike = sqlCreateTable.getTableLike().get();
        CatalogTable table = lookupLikeSourceTable(sqlTableLike);
        sourceTableSchema = TableSchema.fromResolvedSchema(table.getUnresolvedSchema().resolve(catalogManager.getSchemaResolver()));
        sourcePartitionKeys = table.getPartitionKeys();
        likeOptions = sqlTableLike.getOptions();
        sourceProperties = table.getOptions();
    } else {
        sourceTableSchema = TableSchema.builder().build();
        sourcePartitionKeys = Collections.emptyList();
        likeOptions = Collections.emptyList();
        sourceProperties = Collections.emptyMap();
    }
    Map<SqlTableLike.FeatureOption, SqlTableLike.MergingStrategy> mergingStrategies = mergeTableLikeUtil.computeMergingStrategies(likeOptions);
    Map<String, String> mergedOptions = mergeOptions(sqlCreateTable, sourceProperties, mergingStrategies);
    Optional<SqlTableConstraint> primaryKey = sqlCreateTable.getFullConstraints().stream().filter(SqlTableConstraint::isPrimaryKey).findAny();
    TableSchema mergedSchema = mergeTableLikeUtil.mergeTables(mergingStrategies, sourceTableSchema, sqlCreateTable.getColumnList().getList(), sqlCreateTable.getWatermark().map(Collections::singletonList).orElseGet(Collections::emptyList), primaryKey.orElse(null));
    List<String> partitionKeys = mergePartitions(sourcePartitionKeys, sqlCreateTable.getPartitionKeyList(), mergingStrategies);
    verifyPartitioningColumnsExist(mergedSchema, partitionKeys);
    String tableComment = sqlCreateTable.getComment().map(comment -> comment.getNlsString().getValue()).orElse(null);
    return new CatalogTableImpl(mergedSchema, partitionKeys, mergedOptions, tableComment);
}
Also used : CatalogManager(org.apache.flink.table.catalog.CatalogManager) Arrays(java.util.Arrays) ObjectIdentifier(org.apache.flink.table.catalog.ObjectIdentifier) UnresolvedIdentifier(org.apache.flink.table.catalog.UnresolvedIdentifier) SqlTableOption(org.apache.flink.sql.parser.ddl.SqlTableOption) CatalogTable(org.apache.flink.table.catalog.CatalogTable) HashMap(java.util.HashMap) Function(java.util.function.Function) SqlNode(org.apache.calcite.sql.SqlNode) Map(java.util.Map) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlCreateTable(org.apache.flink.sql.parser.ddl.SqlCreateTable) ContextResolvedTable(org.apache.flink.table.catalog.ContextResolvedTable) CatalogTableImpl(org.apache.flink.table.catalog.CatalogTableImpl) Operation(org.apache.flink.table.operations.Operation) SqlTableConstraint(org.apache.flink.sql.parser.ddl.constraint.SqlTableConstraint) TableSchema(org.apache.flink.table.api.TableSchema) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) SqlTableLike(org.apache.flink.sql.parser.ddl.SqlTableLike) List(java.util.List) ValidationException(org.apache.flink.table.api.ValidationException) FlinkCalciteSqlValidator(org.apache.flink.table.planner.calcite.FlinkCalciteSqlValidator) Optional(java.util.Optional) CreateTableOperation(org.apache.flink.table.operations.ddl.CreateTableOperation) SqlNodeList(org.apache.calcite.sql.SqlNodeList) Collections(java.util.Collections) TableSchema(org.apache.flink.table.api.TableSchema) CatalogTable(org.apache.flink.table.catalog.CatalogTable) CatalogTableImpl(org.apache.flink.table.catalog.CatalogTableImpl) SqlTableLike(org.apache.flink.sql.parser.ddl.SqlTableLike) SqlTableConstraint(org.apache.flink.sql.parser.ddl.constraint.SqlTableConstraint) Collections(java.util.Collections)

Aggregations

Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)1 SqlNode (org.apache.calcite.sql.SqlNode)1 SqlNodeList (org.apache.calcite.sql.SqlNodeList)1 SqlCreateTable (org.apache.flink.sql.parser.ddl.SqlCreateTable)1 SqlTableLike (org.apache.flink.sql.parser.ddl.SqlTableLike)1 SqlTableOption (org.apache.flink.sql.parser.ddl.SqlTableOption)1 SqlTableConstraint (org.apache.flink.sql.parser.ddl.constraint.SqlTableConstraint)1 TableSchema (org.apache.flink.table.api.TableSchema)1 ValidationException (org.apache.flink.table.api.ValidationException)1 CatalogManager (org.apache.flink.table.catalog.CatalogManager)1 CatalogTable (org.apache.flink.table.catalog.CatalogTable)1