Search in sources :

Example 66 with CatalogTable

use of org.apache.flink.table.catalog.CatalogTable 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)

Example 67 with CatalogTable

use of org.apache.flink.table.catalog.CatalogTable in project flink by apache.

the class SqlCreateTableConverter method lookupLikeSourceTable.

private CatalogTable lookupLikeSourceTable(SqlTableLike sqlTableLike) {
    UnresolvedIdentifier unresolvedIdentifier = UnresolvedIdentifier.of(sqlTableLike.getSourceTable().names);
    ObjectIdentifier identifier = catalogManager.qualifyIdentifier(unresolvedIdentifier);
    ContextResolvedTable lookupResult = catalogManager.getTable(identifier).orElseThrow(() -> new ValidationException(String.format("Source table '%s' of the LIKE clause not found in the catalog, at %s", identifier, sqlTableLike.getSourceTable().getParserPosition())));
    if (!(lookupResult.getTable() instanceof CatalogTable)) {
        throw new ValidationException(String.format("Source table '%s' of the LIKE clause can not be a VIEW, at %s", identifier, sqlTableLike.getSourceTable().getParserPosition()));
    }
    return lookupResult.getTable();
}
Also used : ValidationException(org.apache.flink.table.api.ValidationException) UnresolvedIdentifier(org.apache.flink.table.catalog.UnresolvedIdentifier) ContextResolvedTable(org.apache.flink.table.catalog.ContextResolvedTable) CatalogTable(org.apache.flink.table.catalog.CatalogTable) ObjectIdentifier(org.apache.flink.table.catalog.ObjectIdentifier)

Example 68 with CatalogTable

use of org.apache.flink.table.catalog.CatalogTable in project flink by apache.

the class CatalogSchemaTable method findAndCreateTableSource.

private Optional<TableSource<?>> findAndCreateTableSource() {
    Optional<TableSource<?>> tableSource = Optional.empty();
    try {
        if (contextResolvedTable.getTable() instanceof CatalogTable) {
            // Use an empty config for TableSourceFactoryContextImpl since we can't fetch the
            // actual TableConfig here. And currently the empty config do not affect the logic.
            ReadableConfig config = new Configuration();
            TableSourceFactory.Context context = new TableSourceFactoryContextImpl(contextResolvedTable.getIdentifier(), contextResolvedTable.getTable(), config, contextResolvedTable.isTemporary());
            TableSource<?> source = TableFactoryUtil.findAndCreateTableSource(context);
            if (source instanceof StreamTableSource) {
                if (!isStreamingMode && !((StreamTableSource<?>) source).isBounded()) {
                    throw new ValidationException("Cannot query on an unbounded source in batch mode, but " + contextResolvedTable.getIdentifier().asSummaryString() + " is unbounded.");
                }
                tableSource = Optional.of(source);
            } else {
                throw new ValidationException("Catalog tables only support " + "StreamTableSource and InputFormatTableSource.");
            }
        }
    } catch (Exception e) {
        tableSource = Optional.empty();
    }
    return tableSource;
}
Also used : TableSourceFactory(org.apache.flink.table.factories.TableSourceFactory) ReadableConfig(org.apache.flink.configuration.ReadableConfig) TableSource(org.apache.flink.table.sources.TableSource) StreamTableSource(org.apache.flink.table.sources.StreamTableSource) ValidationException(org.apache.flink.table.api.ValidationException) Configuration(org.apache.flink.configuration.Configuration) ConnectorCatalogTable(org.apache.flink.table.catalog.ConnectorCatalogTable) CatalogTable(org.apache.flink.table.catalog.CatalogTable) StreamTableSource(org.apache.flink.table.sources.StreamTableSource) ValidationException(org.apache.flink.table.api.ValidationException) TableSourceFactoryContextImpl(org.apache.flink.table.factories.TableSourceFactoryContextImpl)

Aggregations

CatalogTable (org.apache.flink.table.catalog.CatalogTable)68 Test (org.junit.Test)35 HashMap (java.util.HashMap)30 CatalogTableImpl (org.apache.flink.table.catalog.CatalogTableImpl)24 TableSchema (org.apache.flink.table.api.TableSchema)17 ObjectIdentifier (org.apache.flink.table.catalog.ObjectIdentifier)17 CreateTableOperation (org.apache.flink.table.operations.ddl.CreateTableOperation)14 ValidationException (org.apache.flink.table.api.ValidationException)13 ObjectPath (org.apache.flink.table.catalog.ObjectPath)13 CatalogBaseTable (org.apache.flink.table.catalog.CatalogBaseTable)12 Operation (org.apache.flink.table.operations.Operation)12 AlterTableAddConstraintOperation (org.apache.flink.table.operations.ddl.AlterTableAddConstraintOperation)12 AlterTableDropConstraintOperation (org.apache.flink.table.operations.ddl.AlterTableDropConstraintOperation)12 AlterTableOptionsOperation (org.apache.flink.table.operations.ddl.AlterTableOptionsOperation)12 AlterTableRenameOperation (org.apache.flink.table.operations.ddl.AlterTableRenameOperation)12 ExplainOperation (org.apache.flink.table.operations.ExplainOperation)11 LoadModuleOperation (org.apache.flink.table.operations.LoadModuleOperation)11 QueryOperation (org.apache.flink.table.operations.QueryOperation)11 ShowFunctionsOperation (org.apache.flink.table.operations.ShowFunctionsOperation)11 ShowModulesOperation (org.apache.flink.table.operations.ShowModulesOperation)11