Search in sources :

Example 21 with Catalog

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

the class SqlToOperationConverter method convertAlterTableCompact.

/**
 * Convert `ALTER TABLE ... COMPACT` operation to {@link ModifyOperation} for Flink's managed
 * table to trigger a compaction batch job.
 */
private ModifyOperation convertAlterTableCompact(ObjectIdentifier tableIdentifier, ContextResolvedTable contextResolvedTable, SqlAlterTableCompact alterTableCompact) {
    Catalog catalog = catalogManager.getCatalog(tableIdentifier.getCatalogName()).orElse(null);
    ResolvedCatalogTable resolvedCatalogTable = contextResolvedTable.getResolvedTable();
    if (ManagedTableListener.isManagedTable(catalog, resolvedCatalogTable)) {
        Map<String, String> partitionKVs = alterTableCompact.getPartitionKVs();
        CatalogPartitionSpec partitionSpec = new CatalogPartitionSpec(Collections.emptyMap());
        if (partitionKVs != null) {
            List<String> partitionKeys = resolvedCatalogTable.getPartitionKeys();
            Set<String> validPartitionKeySet = new HashSet<>(partitionKeys);
            String exMsg = partitionKeys.isEmpty() ? String.format("Table %s is not partitioned.", tableIdentifier) : String.format("Available ordered partition columns: [%s]", partitionKeys.stream().collect(Collectors.joining("', '", "'", "'")));
            partitionKVs.forEach((partitionKey, partitionValue) -> {
                if (!validPartitionKeySet.contains(partitionKey)) {
                    throw new ValidationException(String.format("Partition column '%s' not defined in the table schema. %s", partitionKey, exMsg));
                }
            });
            partitionSpec = new CatalogPartitionSpec(partitionKVs);
        }
        Map<String, String> compactOptions = catalogManager.resolveCompactManagedTableOptions(resolvedCatalogTable, tableIdentifier, partitionSpec);
        QueryOperation child = new SourceQueryOperation(contextResolvedTable, compactOptions);
        return new SinkModifyOperation(contextResolvedTable, child, partitionSpec.getPartitionSpec(), false, compactOptions);
    }
    throw new ValidationException(String.format("ALTER TABLE COMPACT operation is not supported for non-managed table %s", tableIdentifier));
}
Also used : ValidationException(org.apache.flink.table.api.ValidationException) ResolvedCatalogTable(org.apache.flink.table.catalog.ResolvedCatalogTable) SinkModifyOperation(org.apache.flink.table.operations.SinkModifyOperation) SourceQueryOperation(org.apache.flink.table.operations.SourceQueryOperation) SqlShowCurrentCatalog(org.apache.flink.sql.parser.dql.SqlShowCurrentCatalog) Catalog(org.apache.flink.table.catalog.Catalog) SqlUseCatalog(org.apache.flink.sql.parser.ddl.SqlUseCatalog) SqlDropCatalog(org.apache.flink.sql.parser.ddl.SqlDropCatalog) SqlCreateCatalog(org.apache.flink.sql.parser.ddl.SqlCreateCatalog) CatalogPartitionSpec(org.apache.flink.table.catalog.CatalogPartitionSpec) HashSet(java.util.HashSet) QueryOperation(org.apache.flink.table.operations.QueryOperation) SourceQueryOperation(org.apache.flink.table.operations.SourceQueryOperation)

Example 22 with Catalog

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

the class DatabaseCalciteSchema method extractTableStats.

private TableStats extractTableStats(ContextResolvedTable lookupResult, ObjectIdentifier identifier) {
    if (lookupResult.isTemporary()) {
        return TableStats.UNKNOWN;
    }
    final Catalog catalog = lookupResult.getCatalog().orElseThrow(IllegalStateException::new);
    final ObjectPath tablePath = identifier.toObjectPath();
    try {
        final CatalogTableStatistics tableStatistics = catalog.getTableStatistics(tablePath);
        final CatalogColumnStatistics columnStatistics = catalog.getTableColumnStatistics(tablePath);
        return convertToTableStats(tableStatistics, columnStatistics);
    } catch (TableNotExistException e) {
        throw new ValidationException(format("Could not get statistic for table: [%s, %s, %s]", identifier.getCatalogName(), tablePath.getDatabaseName(), tablePath.getObjectName()), e);
    }
}
Also used : ObjectPath(org.apache.flink.table.catalog.ObjectPath) ValidationException(org.apache.flink.table.api.ValidationException) TableNotExistException(org.apache.flink.table.catalog.exceptions.TableNotExistException) Catalog(org.apache.flink.table.catalog.Catalog) CatalogColumnStatistics(org.apache.flink.table.catalog.stats.CatalogColumnStatistics) CatalogTableStatistics(org.apache.flink.table.catalog.stats.CatalogTableStatistics)

Example 23 with Catalog

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

the class FactoryUtilTest method testCreateCatalog.

@Test
public void testCreateCatalog() {
    final Map<String, String> options = new HashMap<>();
    options.put(CommonCatalogOptions.CATALOG_TYPE.key(), TestCatalogFactory.IDENTIFIER);
    options.put(TestCatalogFactory.DEFAULT_DATABASE.key(), "my-database");
    final Catalog catalog = FactoryUtil.createCatalog("my-catalog", options, null, Thread.currentThread().getContextClassLoader());
    assertThat(catalog).isInstanceOf(TestCatalogFactory.TestCatalog.class);
    final TestCatalogFactory.TestCatalog testCatalog = (TestCatalogFactory.TestCatalog) catalog;
    assertThat("my-catalog").isEqualTo(testCatalog.getName());
    assertThat("my-database").isEqualTo(testCatalog.getOptions().get(TestCatalogFactory.DEFAULT_DATABASE.key()));
}
Also used : HashMap(java.util.HashMap) Catalog(org.apache.flink.table.catalog.Catalog) Test(org.junit.jupiter.api.Test)

Aggregations

Catalog (org.apache.flink.table.catalog.Catalog)23 HashMap (java.util.HashMap)10 ValidationException (org.apache.flink.table.api.ValidationException)10 Test (org.junit.Test)9 TableNotExistException (org.apache.flink.table.catalog.exceptions.TableNotExistException)8 GenericInMemoryCatalog (org.apache.flink.table.catalog.GenericInMemoryCatalog)7 ObjectPath (org.apache.flink.table.catalog.ObjectPath)7 TableException (org.apache.flink.table.api.TableException)6 FunctionCatalog (org.apache.flink.table.catalog.FunctionCatalog)6 CatalogException (org.apache.flink.table.catalog.exceptions.CatalogException)6 DatabaseNotExistException (org.apache.flink.table.catalog.exceptions.DatabaseNotExistException)6 HiveCatalog (org.apache.flink.table.catalog.hive.HiveCatalog)6 CatalogTable (org.apache.flink.table.catalog.CatalogTable)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 ExecutionException (java.util.concurrent.ExecutionException)4 SqlParserException (org.apache.flink.table.api.SqlParserException)4 DatabaseAlreadyExistException (org.apache.flink.table.catalog.exceptions.DatabaseAlreadyExistException)4 DatabaseNotEmptyException (org.apache.flink.table.catalog.exceptions.DatabaseNotEmptyException)4