Search in sources :

Example 21 with CatalogManager

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

the class LookupKeySerdeTest method testLookupKey.

@Test
public void testLookupKey() throws IOException {
    TableConfig tableConfig = TableConfig.getDefault();
    ModuleManager moduleManager = new ModuleManager();
    CatalogManager catalogManager = CatalogManager.newBuilder().classLoader(Thread.currentThread().getContextClassLoader()).config(tableConfig.getConfiguration()).defaultCatalog("default_catalog", new GenericInMemoryCatalog("default_db")).build();
    FlinkContext flinkContext = new FlinkContextImpl(false, tableConfig, moduleManager, new FunctionCatalog(tableConfig, catalogManager, moduleManager), catalogManager, null);
    SerdeContext serdeCtx = new SerdeContext(null, flinkContext, Thread.currentThread().getContextClassLoader(), FlinkTypeFactory.INSTANCE(), FlinkSqlOperatorTable.instance());
    ObjectReader objectReader = JsonSerdeUtil.createObjectReader(serdeCtx);
    ObjectWriter objectWriter = JsonSerdeUtil.createObjectWriter(serdeCtx);
    LookupJoinUtil.LookupKey[] lookupKeys = new LookupJoinUtil.LookupKey[] { new LookupJoinUtil.ConstantLookupKey(new BigIntType(), new RexBuilder(FlinkTypeFactory.INSTANCE()).makeLiteral("a")), new LookupJoinUtil.FieldRefLookupKey(3) };
    for (LookupJoinUtil.LookupKey lookupKey : lookupKeys) {
        LookupJoinUtil.LookupKey result = objectReader.readValue(objectWriter.writeValueAsString(lookupKey), LookupJoinUtil.LookupKey.class);
        assertEquals(lookupKey, result);
    }
}
Also used : FunctionCatalog(org.apache.flink.table.catalog.FunctionCatalog) ObjectWriter(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectWriter) BigIntType(org.apache.flink.table.types.logical.BigIntType) ModuleManager(org.apache.flink.table.module.ModuleManager) FlinkContext(org.apache.flink.table.planner.calcite.FlinkContext) CatalogManager(org.apache.flink.table.catalog.CatalogManager) GenericInMemoryCatalog(org.apache.flink.table.catalog.GenericInMemoryCatalog) LookupJoinUtil(org.apache.flink.table.planner.plan.utils.LookupJoinUtil) FlinkContextImpl(org.apache.flink.table.planner.calcite.FlinkContextImpl) TableConfig(org.apache.flink.table.api.TableConfig) RexBuilder(org.apache.calcite.rex.RexBuilder) ObjectReader(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectReader) Test(org.junit.Test)

Example 22 with CatalogManager

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

the class DynamicTableSinkSpecSerdeTest method testDynamicTableSinkSpecSerde.

@ParameterizedTest
@MethodSource("testDynamicTableSinkSpecSerde")
void testDynamicTableSinkSpecSerde(DynamicTableSinkSpec spec) throws IOException {
    PlannerMocks plannerMocks = PlannerMocks.create();
    CatalogManager catalogManager = plannerMocks.getCatalogManager();
    catalogManager.createTable(spec.getContextResolvedTable().getResolvedTable(), spec.getContextResolvedTable().getIdentifier(), false);
    SerdeContext serdeCtx = configuredSerdeContext(catalogManager, plannerMocks.getTableConfig());
    // Re-init the spec to be permanent with correct catalog
    spec = new DynamicTableSinkSpec(ContextResolvedTable.permanent(spec.getContextResolvedTable().getIdentifier(), catalogManager.getCatalog(catalogManager.getCurrentCatalog()).get(), spec.getContextResolvedTable().getResolvedTable()), spec.getSinkAbilities());
    String actualJson = toJson(serdeCtx, spec);
    DynamicTableSinkSpec actual = toObject(serdeCtx, actualJson, DynamicTableSinkSpec.class);
    assertThat(actual.getContextResolvedTable()).isEqualTo(spec.getContextResolvedTable());
    assertThat(actual.getSinkAbilities()).isEqualTo(spec.getSinkAbilities());
    assertThat(actual.getTableSink(plannerMocks.getPlannerContext().getFlinkContext())).isNotNull();
}
Also used : JsonSerdeTestUtil.configuredSerdeContext(org.apache.flink.table.planner.plan.nodes.exec.serde.JsonSerdeTestUtil.configuredSerdeContext) DynamicTableSinkSpec(org.apache.flink.table.planner.plan.nodes.exec.spec.DynamicTableSinkSpec) PlannerMocks(org.apache.flink.table.planner.utils.PlannerMocks) CatalogManager(org.apache.flink.table.catalog.CatalogManager) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 23 with CatalogManager

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

the class DynamicTableSinkSpecSerdeTest method testDynamicTableSinkSpecSerdeWithEnrichmentOptions.

@Test
void testDynamicTableSinkSpecSerdeWithEnrichmentOptions() throws Exception {
    // Test model
    ObjectIdentifier identifier = ObjectIdentifier.of(DEFAULT_BUILTIN_CATALOG, DEFAULT_BUILTIN_DATABASE, "my_table");
    String formatPrefix = FactoryUtil.getFormatPrefix(FORMAT, TestFormatFactory.IDENTIFIER);
    Map<String, String> planOptions = new HashMap<>();
    planOptions.put(CONNECTOR.key(), TestDynamicTableFactory.IDENTIFIER);
    planOptions.put(TARGET.key(), "abc");
    planOptions.put(BUFFER_SIZE.key(), "1000");
    planOptions.put(FORMAT.key(), TestFormatFactory.IDENTIFIER);
    planOptions.put(formatPrefix + DELIMITER.key(), "|");
    Map<String, String> catalogOptions = new HashMap<>();
    catalogOptions.put(CONNECTOR.key(), TestDynamicTableFactory.IDENTIFIER);
    catalogOptions.put(TARGET.key(), "xyz");
    catalogOptions.put(BUFFER_SIZE.key(), "2000");
    catalogOptions.put(FORMAT.key(), TestFormatFactory.IDENTIFIER);
    catalogOptions.put(formatPrefix + DELIMITER.key(), ",");
    ResolvedCatalogTable planResolvedCatalogTable = tableWithOnlyPhysicalColumns(planOptions);
    ResolvedCatalogTable catalogResolvedCatalogTable = tableWithOnlyPhysicalColumns(catalogOptions);
    // Create planner mocks
    PlannerMocks plannerMocks = PlannerMocks.create(new Configuration().set(PLAN_RESTORE_CATALOG_OBJECTS, CatalogPlanRestore.ALL).set(PLAN_COMPILE_CATALOG_OBJECTS, CatalogPlanCompilation.ALL));
    CatalogManager catalogManager = plannerMocks.getCatalogManager();
    catalogManager.createTable(catalogResolvedCatalogTable, identifier, false);
    // Mock the context
    SerdeContext serdeCtx = configuredSerdeContext(catalogManager, plannerMocks.getTableConfig());
    DynamicTableSinkSpec planSpec = new DynamicTableSinkSpec(ContextResolvedTable.permanent(identifier, catalogManager.getCatalog(catalogManager.getCurrentCatalog()).get(), planResolvedCatalogTable), Collections.emptyList());
    String actualJson = toJson(serdeCtx, planSpec);
    DynamicTableSinkSpec actual = toObject(serdeCtx, actualJson, DynamicTableSinkSpec.class);
    assertThat(actual.getContextResolvedTable()).isEqualTo(planSpec.getContextResolvedTable());
    assertThat(actual.getSinkAbilities()).isNull();
    TestDynamicTableFactory.DynamicTableSinkMock dynamicTableSink = (TestDynamicTableFactory.DynamicTableSinkMock) actual.getTableSink(plannerMocks.getPlannerContext().getFlinkContext());
    assertThat(dynamicTableSink.target).isEqualTo("abc");
    assertThat(dynamicTableSink.bufferSize).isEqualTo(2000);
    assertThat(((TestFormatFactory.EncodingFormatMock) dynamicTableSink.valueFormat).delimiter).isEqualTo(",");
}
Also used : Configuration(org.apache.flink.configuration.Configuration) HashMap(java.util.HashMap) JsonSerdeTestUtil.configuredSerdeContext(org.apache.flink.table.planner.plan.nodes.exec.serde.JsonSerdeTestUtil.configuredSerdeContext) DynamicTableSinkSpec(org.apache.flink.table.planner.plan.nodes.exec.spec.DynamicTableSinkSpec) TestDynamicTableFactory(org.apache.flink.table.factories.TestDynamicTableFactory) CatalogManager(org.apache.flink.table.catalog.CatalogManager) ResolvedCatalogTable(org.apache.flink.table.catalog.ResolvedCatalogTable) PlannerMocks(org.apache.flink.table.planner.utils.PlannerMocks) ObjectIdentifier(org.apache.flink.table.catalog.ObjectIdentifier) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 24 with CatalogManager

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

the class TemporalTableSourceSpecSerdeTest method testTemporalTableSourceSpecSerde.

@ParameterizedTest
@MethodSource("testTemporalTableSourceSpecSerde")
public void testTemporalTableSourceSpecSerde(TemporalTableSourceSpec spec) throws IOException {
    CatalogManager catalogManager = CatalogManagerMocks.createEmptyCatalogManager();
    catalogManager.createTemporaryTable(spec.getTableSourceSpec().getContextResolvedTable().getResolvedTable(), spec.getTableSourceSpec().getContextResolvedTable().getIdentifier(), false);
    SerdeContext serdeCtx = JsonSerdeTestUtil.configuredSerdeContext(catalogManager, new TableConfig());
    String json = JsonSerdeTestUtil.toJson(serdeCtx, spec);
    TemporalTableSourceSpec actual = JsonSerdeTestUtil.toObject(serdeCtx, json, TemporalTableSourceSpec.class);
    assertThat(actual.getTableSourceSpec().getContextResolvedTable()).isEqualTo(spec.getTableSourceSpec().getContextResolvedTable());
    assertThat(actual.getTableSourceSpec().getSourceAbilities()).isEqualTo(spec.getTableSourceSpec().getSourceAbilities());
    assertThat(actual.getOutputType()).isEqualTo(spec.getOutputType());
}
Also used : TemporalTableSourceSpec(org.apache.flink.table.planner.plan.nodes.exec.spec.TemporalTableSourceSpec) TableConfig(org.apache.flink.table.api.TableConfig) CatalogManager(org.apache.flink.table.catalog.CatalogManager) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 25 with CatalogManager

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

the class DatabaseCalciteSchemaTest method testPermanentTableWithPrimaryKey.

@Test
public void testPermanentTableWithPrimaryKey() {
    final CatalogManager catalogManager = CatalogManagerMocks.createEmptyCatalogManager();
    final DatabaseCalciteSchema calciteSchema = new DatabaseCalciteSchema(DEFAULT_CATALOG, DEFAULT_DATABASE, catalogManager, true);
    catalogManager.createTable(createTable(), ObjectIdentifier.of(DEFAULT_CATALOG, DEFAULT_DATABASE, TABLE_NAME), false);
    final Table table = calciteSchema.getTable(TABLE_NAME);
    assertThat(table, instanceOf(CatalogSchemaTable.class));
    assertThat(((CatalogSchemaTable) table).getStatistic().getUniqueKeys().iterator().next(), containsInAnyOrder("a", "b"));
}
Also used : CatalogTable(org.apache.flink.table.catalog.CatalogTable) Table(org.apache.calcite.schema.Table) CatalogBaseTable(org.apache.flink.table.catalog.CatalogBaseTable) CatalogManager(org.apache.flink.table.catalog.CatalogManager) Test(org.junit.Test)

Aggregations

CatalogManager (org.apache.flink.table.catalog.CatalogManager)26 TableConfig (org.apache.flink.table.api.TableConfig)13 ModuleManager (org.apache.flink.table.module.ModuleManager)13 FunctionCatalog (org.apache.flink.table.catalog.FunctionCatalog)12 Executor (org.apache.flink.table.delegation.Executor)8 Planner (org.apache.flink.table.delegation.Planner)8 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)6 TableException (org.apache.flink.table.api.TableException)6 GenericInMemoryCatalog (org.apache.flink.table.catalog.GenericInMemoryCatalog)6 ObjectIdentifier (org.apache.flink.table.catalog.ObjectIdentifier)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 Constructor (java.lang.reflect.Constructor)5 Configuration (org.apache.flink.configuration.Configuration)5 TableEnvironment (org.apache.flink.table.api.TableEnvironment)5 ValidationException (org.apache.flink.table.api.ValidationException)5 JsonSerdeTestUtil.configuredSerdeContext (org.apache.flink.table.planner.plan.nodes.exec.serde.JsonSerdeTestUtil.configuredSerdeContext)5 HashMap (java.util.HashMap)4 CatalogTable (org.apache.flink.table.catalog.CatalogTable)4 ContextResolvedTable (org.apache.flink.table.catalog.ContextResolvedTable)4 ResolvedCatalogTable (org.apache.flink.table.catalog.ResolvedCatalogTable)4