Search in sources :

Example 1 with TableEnvironmentMock

use of org.apache.flink.table.utils.TableEnvironmentMock in project flink by apache.

the class TableEnvironmentTest method innerTestManagedTableFromDescriptor.

private void innerTestManagedTableFromDescriptor(boolean ignoreIfExists, boolean isTemporary) {
    final TableEnvironmentMock tEnv = TableEnvironmentMock.getStreamingInstance();
    final String catalog = tEnv.getCurrentCatalog();
    final String database = tEnv.getCurrentDatabase();
    final Schema schema = Schema.newBuilder().column("f0", DataTypes.INT()).build();
    final String tableName = UUID.randomUUID().toString();
    ObjectIdentifier identifier = ObjectIdentifier.of(catalog, database, tableName);
    // create table
    MANAGED_TABLES.put(identifier, new AtomicReference<>());
    CreateTableOperation createOperation = new CreateTableOperation(identifier, TableDescriptor.forManaged().schema(schema).option("a", "Test").build().toCatalogTable(), ignoreIfExists, isTemporary);
    tEnv.executeInternal(createOperation);
    // test ignore: create again
    if (ignoreIfExists) {
        tEnv.executeInternal(createOperation);
    } else {
        assertThatThrownBy(() -> tEnv.executeInternal(createOperation), isTemporary ? "already exists" : "Could not execute CreateTable");
    }
    // lookup table
    boolean isInCatalog = tEnv.getCatalog(catalog).orElseThrow(AssertionError::new).tableExists(new ObjectPath(database, tableName));
    if (isTemporary) {
        assertThat(isInCatalog).isFalse();
    } else {
        assertThat(isInCatalog).isTrue();
    }
    final Optional<ContextResolvedTable> lookupResult = tEnv.getCatalogManager().getTable(identifier);
    assertThat(lookupResult.isPresent()).isTrue();
    final CatalogBaseTable catalogTable = lookupResult.get().getTable();
    assertThat(catalogTable instanceof CatalogTable).isTrue();
    assertThat(catalogTable.getUnresolvedSchema()).isEqualTo(schema);
    assertThat(catalogTable.getOptions().get("a")).isEqualTo("Test");
    assertThat(catalogTable.getOptions().get(ENRICHED_KEY)).isEqualTo(ENRICHED_VALUE);
    AtomicReference<Map<String, String>> reference = MANAGED_TABLES.get(identifier);
    assertThat(reference.get()).isNotNull();
    assertThat(reference.get().get("a")).isEqualTo("Test");
    assertThat(reference.get().get(ENRICHED_KEY)).isEqualTo(ENRICHED_VALUE);
    DropTableOperation dropOperation = new DropTableOperation(identifier, ignoreIfExists, isTemporary);
    tEnv.executeInternal(dropOperation);
    assertThat(MANAGED_TABLES.get(identifier).get()).isNull();
    // test ignore: drop again
    if (ignoreIfExists) {
        tEnv.executeInternal(dropOperation);
    } else {
        assertThatThrownBy(() -> tEnv.executeInternal(dropOperation), "does not exist");
    }
    MANAGED_TABLES.remove(identifier);
}
Also used : ObjectPath(org.apache.flink.table.catalog.ObjectPath) CatalogBaseTable(org.apache.flink.table.catalog.CatalogBaseTable) CreateTableOperation(org.apache.flink.table.operations.ddl.CreateTableOperation) CatalogTable(org.apache.flink.table.catalog.CatalogTable) DropTableOperation(org.apache.flink.table.operations.ddl.DropTableOperation) ContextResolvedTable(org.apache.flink.table.catalog.ContextResolvedTable) TableEnvironmentMock(org.apache.flink.table.utils.TableEnvironmentMock) Map(java.util.Map) ObjectIdentifier(org.apache.flink.table.catalog.ObjectIdentifier)

Example 2 with TableEnvironmentMock

use of org.apache.flink.table.utils.TableEnvironmentMock in project flink by apache.

the class TableEnvironmentTest method testCreateTemporaryTableFromDescriptor.

@Test
public void testCreateTemporaryTableFromDescriptor() {
    final TableEnvironmentMock tEnv = TableEnvironmentMock.getStreamingInstance();
    final String catalog = tEnv.getCurrentCatalog();
    final String database = tEnv.getCurrentDatabase();
    final Schema schema = Schema.newBuilder().column("f0", DataTypes.INT()).build();
    tEnv.createTemporaryTable("T", TableDescriptor.forConnector("fake").schema(schema).option("a", "Test").build());
    assertThat(tEnv.getCatalog(catalog).orElseThrow(AssertionError::new).tableExists(new ObjectPath(database, "T"))).isFalse();
    final Optional<ContextResolvedTable> lookupResult = tEnv.getCatalogManager().getTable(ObjectIdentifier.of(catalog, database, "T"));
    assertThat(lookupResult.isPresent()).isTrue();
    final CatalogBaseTable catalogTable = lookupResult.get().getTable();
    assertThat(catalogTable instanceof CatalogTable).isTrue();
    assertThat(catalogTable.getUnresolvedSchema()).isEqualTo(schema);
    assertThat(catalogTable.getOptions().get("connector")).isEqualTo("fake");
    assertThat(catalogTable.getOptions().get("a")).isEqualTo("Test");
}
Also used : ObjectPath(org.apache.flink.table.catalog.ObjectPath) CatalogBaseTable(org.apache.flink.table.catalog.CatalogBaseTable) ContextResolvedTable(org.apache.flink.table.catalog.ContextResolvedTable) CatalogTable(org.apache.flink.table.catalog.CatalogTable) TableEnvironmentMock(org.apache.flink.table.utils.TableEnvironmentMock) Test(org.junit.jupiter.api.Test)

Example 3 with TableEnvironmentMock

use of org.apache.flink.table.utils.TableEnvironmentMock 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 4 with TableEnvironmentMock

use of org.apache.flink.table.utils.TableEnvironmentMock in project flink by apache.

the class GenericInMemoryCatalogTest method testRegisterCatalog.

@Test
public void testRegisterCatalog() {
    final TableEnvironmentMock tableEnv = TableEnvironmentMock.getStreamingInstance();
    try {
        tableEnv.registerCatalog(TEST_CATALOG_NAME, new MyCatalog(TEST_CATALOG_NAME));
    } catch (CatalogException e) {
    }
    assertThat(tableEnv.getCatalog(TEST_CATALOG_NAME).isPresent(), equalTo(false));
}
Also used : CatalogException(org.apache.flink.table.catalog.exceptions.CatalogException) TableEnvironmentMock(org.apache.flink.table.utils.TableEnvironmentMock) Test(org.junit.Test)

Example 5 with TableEnvironmentMock

use of org.apache.flink.table.utils.TableEnvironmentMock in project flink by apache.

the class TableEnvironmentTest method testCreateTableFromDescriptor.

@Test
public void testCreateTableFromDescriptor() throws Exception {
    final TableEnvironmentMock tEnv = TableEnvironmentMock.getStreamingInstance();
    final String catalog = tEnv.getCurrentCatalog();
    final String database = tEnv.getCurrentDatabase();
    final Schema schema = Schema.newBuilder().column("f0", DataTypes.INT()).build();
    tEnv.createTable("T", TableDescriptor.forConnector("fake").schema(schema).option("a", "Test").build());
    final ObjectPath objectPath = new ObjectPath(database, "T");
    assertThat(tEnv.getCatalog(catalog).orElseThrow(AssertionError::new).tableExists(objectPath)).isTrue();
    final CatalogBaseTable catalogTable = tEnv.getCatalog(catalog).orElseThrow(AssertionError::new).getTable(objectPath);
    assertThat(catalogTable).isInstanceOf(CatalogTable.class);
    assertThat(catalogTable.getUnresolvedSchema()).isEqualTo(schema);
    assertThat(catalogTable.getOptions()).contains(entry("connector", "fake"), entry("a", "Test"));
}
Also used : ObjectPath(org.apache.flink.table.catalog.ObjectPath) CatalogBaseTable(org.apache.flink.table.catalog.CatalogBaseTable) TableEnvironmentMock(org.apache.flink.table.utils.TableEnvironmentMock) Test(org.junit.jupiter.api.Test)

Aggregations

TableEnvironmentMock (org.apache.flink.table.utils.TableEnvironmentMock)5 CatalogBaseTable (org.apache.flink.table.catalog.CatalogBaseTable)4 CatalogTable (org.apache.flink.table.catalog.CatalogTable)3 ContextResolvedTable (org.apache.flink.table.catalog.ContextResolvedTable)3 ObjectPath (org.apache.flink.table.catalog.ObjectPath)3 Test (org.junit.jupiter.api.Test)3 Map (java.util.Map)1 ObjectIdentifier (org.apache.flink.table.catalog.ObjectIdentifier)1 CatalogException (org.apache.flink.table.catalog.exceptions.CatalogException)1 SourceQueryOperation (org.apache.flink.table.operations.SourceQueryOperation)1 CreateTableOperation (org.apache.flink.table.operations.ddl.CreateTableOperation)1 DropTableOperation (org.apache.flink.table.operations.ddl.DropTableOperation)1 Test (org.junit.Test)1