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);
}
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();
}
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;
}
Aggregations