use of org.apache.flink.table.catalog.CatalogTable 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);
}
use of org.apache.flink.table.catalog.CatalogTable 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");
}
use of org.apache.flink.table.catalog.CatalogTable in project flink by apache.
the class CatalogITCase method testGetTablesFromGivenCatalogDatabase.
@Test
public void testGetTablesFromGivenCatalogDatabase() throws Exception {
final Catalog c1 = new GenericInMemoryCatalog("c1", "default");
final Catalog c2 = new GenericInMemoryCatalog("c2", "d2");
final CatalogManager catalogManager = CatalogManagerMocks.preparedCatalogManager().defaultCatalog("c2", c2).build();
catalogManager.registerCatalog("c1", c1);
final CatalogTable catalogTable = CatalogTable.of(Schema.newBuilder().build(), null, new ArrayList<>(), new HashMap<>());
c1.createDatabase("d1", new CatalogDatabaseImpl(new HashMap<>(), null), true);
c1.createTable(new ObjectPath("d1", "t1"), catalogTable, true);
c2.createTable(new ObjectPath(catalogManager.getCurrentDatabase(), "t2"), catalogTable, true);
assertThat(catalogManager.getCurrentCatalog()).isEqualTo("c2");
assertThat(catalogManager.getCurrentDatabase()).isEqualTo("d2");
assertThat(catalogManager.listTables()).containsExactlyInAnyOrder("t2");
assertThat(catalogManager.listTables("c1", "d1")).containsExactlyInAnyOrder("t1");
}
use of org.apache.flink.table.catalog.CatalogTable in project flink by apache.
the class PlannerMocks method registerTemporaryTable.
public PlannerMocks registerTemporaryTable(String tableName, Schema tableSchema) {
final CatalogTable table = CatalogTable.of(tableSchema, null, Collections.emptyList(), Collections.emptyMap());
this.getCatalogManager().createTemporaryTable(table, ObjectIdentifier.of(this.getCatalogManager().getCurrentCatalog(), this.getCatalogManager().getCurrentDatabase(), tableName), false);
return this;
}
use of org.apache.flink.table.catalog.CatalogTable in project flink by apache.
the class HiveCatalogUdfITCase method testFlinkUdf.
@Test
public void testFlinkUdf() throws Exception {
final TableSchema schema = TableSchema.builder().field("name", DataTypes.STRING()).field("age", DataTypes.INT()).build();
final Map<String, String> sourceOptions = new HashMap<>();
sourceOptions.put("connector.type", "filesystem");
sourceOptions.put("connector.path", getClass().getResource("/csv/test.csv").getPath());
sourceOptions.put("format.type", "csv");
CatalogTable source = new CatalogTableImpl(schema, sourceOptions, "Comment.");
hiveCatalog.createTable(new ObjectPath(HiveCatalog.DEFAULT_DB, sourceTableName), source, false);
hiveCatalog.createFunction(new ObjectPath(HiveCatalog.DEFAULT_DB, "myudf"), new CatalogFunctionImpl(TestHiveSimpleUDF.class.getCanonicalName()), false);
hiveCatalog.createFunction(new ObjectPath(HiveCatalog.DEFAULT_DB, "mygenericudf"), new CatalogFunctionImpl(TestHiveGenericUDF.class.getCanonicalName()), false);
hiveCatalog.createFunction(new ObjectPath(HiveCatalog.DEFAULT_DB, "myudtf"), new CatalogFunctionImpl(TestHiveUDTF.class.getCanonicalName()), false);
hiveCatalog.createFunction(new ObjectPath(HiveCatalog.DEFAULT_DB, "myudaf"), new CatalogFunctionImpl(GenericUDAFSum.class.getCanonicalName()), false);
testUdf(true);
testUdf(false);
}
Aggregations