Search in sources :

Example 16 with ResolvedCatalogTable

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

the class TableImpl method insertInto.

@Override
public TablePipeline insertInto(TableDescriptor descriptor, boolean overwrite) {
    final SchemaTranslator.ConsumingResult schemaTranslationResult = SchemaTranslator.createConsumingResult(tableEnvironment.getCatalogManager().getDataTypeFactory(), getResolvedSchema().toSourceRowDataType(), descriptor.getSchema().orElse(null), false);
    final TableDescriptor updatedDescriptor = descriptor.toBuilder().schema(schemaTranslationResult.getSchema()).build();
    final ResolvedCatalogTable resolvedCatalogBaseTable = tableEnvironment.getCatalogManager().resolveCatalogTable(updatedDescriptor.toCatalogTable());
    return insertInto(ContextResolvedTable.anonymous(resolvedCatalogBaseTable), overwrite);
}
Also used : SchemaTranslator(org.apache.flink.table.catalog.SchemaTranslator) ResolvedCatalogTable(org.apache.flink.table.catalog.ResolvedCatalogTable) TableDescriptor(org.apache.flink.table.api.TableDescriptor)

Example 17 with ResolvedCatalogTable

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

the class ResolvedCatalogTableJsonDeserializer method deserialize.

@Override
public ResolvedCatalogTable deserialize(JsonParser jsonParser, DeserializationContext ctx) throws IOException {
    ObjectNode jsonNode = jsonParser.readValueAsTree();
    ResolvedSchema resolvedSchema = ctx.readValue(traverse(jsonNode.required(RESOLVED_SCHEMA), jsonParser.getCodec()), ResolvedSchema.class);
    List<String> partitionKeys = ctx.readValue(traverse(jsonNode.required(PARTITION_KEYS), jsonParser.getCodec()), ctx.getTypeFactory().constructCollectionType(List.class, String.class));
    String comment = deserializeOptionalField(jsonNode, COMMENT, String.class, jsonParser.getCodec(), ctx).orElse(null);
    @SuppressWarnings("unchecked") Map<String, String> options = (Map<String, String>) deserializeOptionalField(jsonNode, OPTIONS, ctx.getTypeFactory().constructMapType(Map.class, String.class, String.class), jsonParser.getCodec(), ctx).orElse(Collections.emptyMap());
    return new ResolvedCatalogTable(CatalogTable.of(// reason, in case one tries to access the unresolved schema.
    Schema.newBuilder().fromResolvedSchema(resolvedSchema).build(), comment, partitionKeys, options), resolvedSchema);
}
Also used : ObjectNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ObjectNode) ResolvedCatalogTable(org.apache.flink.table.catalog.ResolvedCatalogTable) List(java.util.List) ResolvedSchema(org.apache.flink.table.catalog.ResolvedSchema) Map(java.util.Map)

Example 18 with ResolvedCatalogTable

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

the class DynamicTableSourceSpecSerdeTest method testDynamicTableSourceSpecSerdeWithEnrichmentOptions.

@Test
void testDynamicTableSourceSpecSerdeWithEnrichmentOptions() 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(PASSWORD.key(), "abc");
    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(), "abc");
    catalogOptions.put(PASSWORD.key(), "xyz");
    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());
    DynamicTableSourceSpec planSpec = new DynamicTableSourceSpec(ContextResolvedTable.permanent(identifier, catalogManager.getCatalog(catalogManager.getCurrentCatalog()).get(), planResolvedCatalogTable), Collections.emptyList());
    String actualJson = toJson(serdeCtx, planSpec);
    DynamicTableSourceSpec actual = toObject(serdeCtx, actualJson, DynamicTableSourceSpec.class);
    assertThat(actual.getContextResolvedTable()).isEqualTo(planSpec.getContextResolvedTable());
    assertThat(actual.getSourceAbilities()).isNull();
    TestDynamicTableFactory.DynamicTableSourceMock dynamicTableSource = (TestDynamicTableFactory.DynamicTableSourceMock) actual.getScanTableSource(plannerMocks.getPlannerContext().getFlinkContext());
    assertThat(dynamicTableSource.password).isEqualTo("xyz");
    assertThat(((TestFormatFactory.DecodingFormatMock) dynamicTableSource.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) DynamicTableSourceSpec(org.apache.flink.table.planner.plan.nodes.exec.spec.DynamicTableSourceSpec) 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 19 with ResolvedCatalogTable

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

the class ResolvedCatalogTableSerdeTest method testDontSerializeOptions.

@Test
void testDontSerializeOptions() throws IOException {
    SerdeContext serdeCtx = configuredSerdeContext();
    byte[] actualSerialized = JsonSerdeUtil.createObjectWriter(serdeCtx).withAttribute(ResolvedCatalogTableJsonSerializer.SERIALIZE_OPTIONS, false).writeValueAsBytes(FULL_RESOLVED_CATALOG_TABLE);
    JsonNode actualJson = JsonSerdeUtil.getObjectMapper().readTree(actualSerialized);
    assertThatJsonContains(actualJson, ResolvedCatalogTableJsonSerializer.RESOLVED_SCHEMA);
    assertThatJsonContains(actualJson, ResolvedCatalogTableJsonSerializer.PARTITION_KEYS);
    assertThatJsonDoesNotContain(actualJson, ResolvedCatalogTableJsonSerializer.OPTIONS);
    assertThatJsonDoesNotContain(actualJson, ResolvedCatalogTableJsonSerializer.COMMENT);
    ResolvedCatalogTable actual = JsonSerdeUtil.createObjectReader(serdeCtx).readValue(actualSerialized, ResolvedCatalogTable.class);
    assertThat(actual).isEqualTo(new ResolvedCatalogTable(CatalogTable.of(Schema.newBuilder().fromResolvedSchema(FULL_RESOLVED_SCHEMA).build(), null, Collections.singletonList("c"), Collections.emptyMap()), FULL_RESOLVED_SCHEMA));
}
Also used : ResolvedCatalogTable(org.apache.flink.table.catalog.ResolvedCatalogTable) JsonSerdeTestUtil.configuredSerdeContext(org.apache.flink.table.planner.plan.nodes.exec.serde.JsonSerdeTestUtil.configuredSerdeContext) JsonNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 20 with ResolvedCatalogTable

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

Aggregations

ResolvedCatalogTable (org.apache.flink.table.catalog.ResolvedCatalogTable)23 ResolvedSchema (org.apache.flink.table.catalog.ResolvedSchema)11 ObjectIdentifier (org.apache.flink.table.catalog.ObjectIdentifier)8 HashMap (java.util.HashMap)7 ValidationException (org.apache.flink.table.api.ValidationException)5 CatalogTable (org.apache.flink.table.catalog.CatalogTable)5 List (java.util.List)4 CatalogManager (org.apache.flink.table.catalog.CatalogManager)4 ContextResolvedTable (org.apache.flink.table.catalog.ContextResolvedTable)4 ExternalCatalogTable (org.apache.flink.table.catalog.ExternalCatalogTable)4 QueryOperation (org.apache.flink.table.operations.QueryOperation)4 Test (org.junit.Test)4 Optional (java.util.Optional)3 RelDataType (org.apache.calcite.rel.type.RelDataType)3 SchemaTranslator (org.apache.flink.table.catalog.SchemaTranslator)3 JsonSerdeTestUtil.configuredSerdeContext (org.apache.flink.table.planner.plan.nodes.exec.serde.JsonSerdeTestUtil.configuredSerdeContext)3 Test (org.junit.jupiter.api.Test)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2