Search in sources :

Example 1 with SourceQueryOperation

use of org.apache.flink.table.operations.SourceQueryOperation in project flink by apache.

the class TableEnvironmentTest method testTableFromDescriptor.

@Test
public void testTableFromDescriptor() {
    final TableEnvironmentMock tEnv = TableEnvironmentMock.getStreamingInstance();
    final Schema schema = Schema.newBuilder().column("f0", DataTypes.INT()).build();
    final TableDescriptor descriptor = TableDescriptor.forConnector("fake").schema(schema).build();
    final Table table = tEnv.from(descriptor);
    assertThat(Schema.newBuilder().fromResolvedSchema(table.getResolvedSchema()).build()).isEqualTo(schema);
    assertThat(table.getQueryOperation()).asInstanceOf(type(SourceQueryOperation.class)).extracting(SourceQueryOperation::getContextResolvedTable).satisfies(crs -> {
        assertThat(crs.isAnonymous()).isTrue();
        assertThat(crs.getIdentifier().toList()).hasSize(1);
        assertThat(crs.getTable().getOptions()).containsEntry("connector", "fake");
    });
    assertThat(tEnv.getCatalogManager().listTables()).isEmpty();
}
Also used : CatalogTable(org.apache.flink.table.catalog.CatalogTable) CatalogBaseTable(org.apache.flink.table.catalog.CatalogBaseTable) ContextResolvedTable(org.apache.flink.table.catalog.ContextResolvedTable) SourceQueryOperation(org.apache.flink.table.operations.SourceQueryOperation) TableEnvironmentMock(org.apache.flink.table.utils.TableEnvironmentMock) Test(org.junit.jupiter.api.Test)

Example 2 with SourceQueryOperation

use of org.apache.flink.table.operations.SourceQueryOperation in project flink by apache.

the class TableEnvironmentImpl method from.

@Override
public Table from(TableDescriptor descriptor) {
    Preconditions.checkNotNull(descriptor, "Table descriptor must not be null.");
    final ResolvedCatalogTable resolvedCatalogBaseTable = catalogManager.resolveCatalogTable(descriptor.toCatalogTable());
    final QueryOperation queryOperation = new SourceQueryOperation(ContextResolvedTable.anonymous(resolvedCatalogBaseTable));
    return createTable(queryOperation);
}
Also used : ResolvedCatalogTable(org.apache.flink.table.catalog.ResolvedCatalogTable) TableSourceQueryOperation(org.apache.flink.table.operations.TableSourceQueryOperation) SourceQueryOperation(org.apache.flink.table.operations.SourceQueryOperation) QueryOperation(org.apache.flink.table.operations.QueryOperation) TableSourceQueryOperation(org.apache.flink.table.operations.TableSourceQueryOperation) SourceQueryOperation(org.apache.flink.table.operations.SourceQueryOperation)

Example 3 with SourceQueryOperation

use of org.apache.flink.table.operations.SourceQueryOperation in project flink by apache.

the class SqlToOperationConverterTest method checkAlterTableCompact.

private void checkAlterTableCompact(Operation operation, Map<String, String> staticPartitions) {
    assertThat(operation).isInstanceOf(SinkModifyOperation.class);
    SinkModifyOperation modifyOperation = (SinkModifyOperation) operation;
    assertThat(modifyOperation.getStaticPartitions()).containsExactlyInAnyOrderEntriesOf(staticPartitions);
    assertThat(modifyOperation.isOverwrite()).isFalse();
    assertThat(modifyOperation.getDynamicOptions()).containsEntry(TestManagedTableFactory.ENRICHED_KEY, TestManagedTableFactory.ENRICHED_VALUE);
    ContextResolvedTable contextResolvedTable = modifyOperation.getContextResolvedTable();
    assertThat(contextResolvedTable.getIdentifier()).isEqualTo(ObjectIdentifier.of("cat1", "db1", "tb1"));
    assertThat(modifyOperation.getChild()).isInstanceOf(SourceQueryOperation.class);
    SourceQueryOperation child = (SourceQueryOperation) modifyOperation.getChild();
    assertThat(child.getChildren()).isEmpty();
    assertThat(child.getDynamicOptions()).containsEntry("k", "v");
    assertThat(child.getDynamicOptions()).containsEntry(TestManagedTableFactory.ENRICHED_KEY, TestManagedTableFactory.ENRICHED_VALUE);
}
Also used : SinkModifyOperation(org.apache.flink.table.operations.SinkModifyOperation) SourceQueryOperation(org.apache.flink.table.operations.SourceQueryOperation) ContextResolvedTable(org.apache.flink.table.catalog.ContextResolvedTable)

Example 4 with SourceQueryOperation

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

Aggregations

SourceQueryOperation (org.apache.flink.table.operations.SourceQueryOperation)4 ContextResolvedTable (org.apache.flink.table.catalog.ContextResolvedTable)2 ResolvedCatalogTable (org.apache.flink.table.catalog.ResolvedCatalogTable)2 QueryOperation (org.apache.flink.table.operations.QueryOperation)2 SinkModifyOperation (org.apache.flink.table.operations.SinkModifyOperation)2 HashSet (java.util.HashSet)1 SqlCreateCatalog (org.apache.flink.sql.parser.ddl.SqlCreateCatalog)1 SqlDropCatalog (org.apache.flink.sql.parser.ddl.SqlDropCatalog)1 SqlUseCatalog (org.apache.flink.sql.parser.ddl.SqlUseCatalog)1 SqlShowCurrentCatalog (org.apache.flink.sql.parser.dql.SqlShowCurrentCatalog)1 ValidationException (org.apache.flink.table.api.ValidationException)1 Catalog (org.apache.flink.table.catalog.Catalog)1 CatalogBaseTable (org.apache.flink.table.catalog.CatalogBaseTable)1 CatalogPartitionSpec (org.apache.flink.table.catalog.CatalogPartitionSpec)1 CatalogTable (org.apache.flink.table.catalog.CatalogTable)1 TableSourceQueryOperation (org.apache.flink.table.operations.TableSourceQueryOperation)1 TableEnvironmentMock (org.apache.flink.table.utils.TableEnvironmentMock)1 Test (org.junit.jupiter.api.Test)1