Search in sources :

Example 1 with CatalogPlanRestore

use of org.apache.flink.table.api.config.TableConfigOptions.CatalogPlanRestore in project flink by apache.

the class LogicalTypeJsonDeserializer method deserializeDistinctType.

private static LogicalType deserializeDistinctType(JsonNode logicalTypeNode, SerdeContext serdeContext) {
    final ObjectIdentifier identifier = ObjectIdentifierJsonDeserializer.deserialize(logicalTypeNode.get(FIELD_NAME_OBJECT_IDENTIFIER).asText(), serdeContext);
    final CatalogPlanRestore restoreStrategy = serdeContext.getConfiguration().get(TableConfigOptions.PLAN_RESTORE_CATALOG_OBJECTS);
    switch(restoreStrategy) {
        case ALL:
            if (logicalTypeNode.has(FIELD_NAME_SOURCE_TYPE)) {
                return deserializeDistinctTypeFromPlan(identifier, logicalTypeNode, serdeContext);
            }
            return deserializeUserDefinedTypeFromCatalog(identifier, serdeContext);
        case ALL_ENFORCED:
            return deserializeDistinctTypeFromPlan(identifier, logicalTypeNode, serdeContext);
        case IDENTIFIER:
            return deserializeUserDefinedTypeFromCatalog(identifier, serdeContext);
        default:
            throw new TableException("Unsupported catalog restore strategy.");
    }
}
Also used : TableException(org.apache.flink.table.api.TableException) CatalogPlanRestore(org.apache.flink.table.api.config.TableConfigOptions.CatalogPlanRestore) ObjectIdentifier(org.apache.flink.table.catalog.ObjectIdentifier)

Example 2 with CatalogPlanRestore

use of org.apache.flink.table.api.config.TableConfigOptions.CatalogPlanRestore in project flink by apache.

the class ContextResolvedTableJsonDeserializer method deserialize.

@Override
public ContextResolvedTable deserialize(JsonParser jsonParser, DeserializationContext ctx) throws IOException {
    final CatalogPlanRestore planRestoreOption = SerdeContext.get(ctx).getConfiguration().get(PLAN_RESTORE_CATALOG_OBJECTS);
    final CatalogManager catalogManager = SerdeContext.get(ctx).getFlinkContext().getCatalogManager();
    final ObjectNode objectNode = jsonParser.readValueAsTree();
    // Deserialize the two fields, if available
    final ObjectIdentifier identifier = JsonSerdeUtil.deserializeOptionalField(objectNode, FIELD_NAME_IDENTIFIER, ObjectIdentifier.class, jsonParser.getCodec(), ctx).orElse(null);
    ResolvedCatalogTable resolvedCatalogTable = JsonSerdeUtil.deserializeOptionalField(objectNode, FIELD_NAME_CATALOG_TABLE, ResolvedCatalogTable.class, jsonParser.getCodec(), ctx).orElse(null);
    if (identifier == null && resolvedCatalogTable == null) {
        throw new ValidationException(String.format("The input JSON is invalid because it doesn't contain '%s', nor the '%s'.", FIELD_NAME_IDENTIFIER, FIELD_NAME_CATALOG_TABLE));
    }
    if (identifier == null) {
        if (isLookupForced(planRestoreOption)) {
            throw missingIdentifier();
        }
        return ContextResolvedTable.anonymous(resolvedCatalogTable);
    }
    Optional<ContextResolvedTable> contextResolvedTableFromCatalog = isLookupEnabled(planRestoreOption) ? catalogManager.getTable(identifier) : Optional.empty();
    // If we have a schema from the plan and from the catalog, we need to check they match.
    if (contextResolvedTableFromCatalog.isPresent() && resolvedCatalogTable != null) {
        ResolvedSchema schemaFromPlan = resolvedCatalogTable.getResolvedSchema();
        ResolvedSchema schemaFromCatalog = contextResolvedTableFromCatalog.get().getResolvedSchema();
        if (!areResolvedSchemasEqual(schemaFromPlan, schemaFromCatalog)) {
            throw schemaNotMatching(identifier, schemaFromPlan, schemaFromCatalog);
        }
    }
    if (resolvedCatalogTable == null || isLookupForced(planRestoreOption)) {
        if (!isLookupEnabled(planRestoreOption)) {
            throw lookupDisabled(identifier);
        }
        // We use what is stored inside the catalog
        return contextResolvedTableFromCatalog.orElseThrow(() -> missingTableFromCatalog(identifier, isLookupForced(planRestoreOption)));
    }
    if (contextResolvedTableFromCatalog.isPresent()) {
        // SCHEMA, so we just need to return the catalog query result
        if (objectNode.at("/" + FIELD_NAME_CATALOG_TABLE + "/" + OPTIONS).isMissingNode()) {
            return contextResolvedTableFromCatalog.get();
        }
        return contextResolvedTableFromCatalog.flatMap(ContextResolvedTable::getCatalog).map(c -> ContextResolvedTable.permanent(identifier, c, resolvedCatalogTable)).orElseGet(() -> ContextResolvedTable.temporary(identifier, resolvedCatalogTable));
    }
    return ContextResolvedTable.temporary(identifier, resolvedCatalogTable);
}
Also used : CatalogManager(org.apache.flink.table.catalog.CatalogManager) ObjectIdentifier(org.apache.flink.table.catalog.ObjectIdentifier) FIELD_NAME_IDENTIFIER(org.apache.flink.table.planner.plan.nodes.exec.serde.ContextResolvedTableJsonSerializer.FIELD_NAME_IDENTIFIER) Column(org.apache.flink.table.catalog.Column) ObjectNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ObjectNode) IDENTIFIER(org.apache.flink.table.api.config.TableConfigOptions.CatalogPlanRestore.IDENTIFIER) ResolvedSchema(org.apache.flink.table.catalog.ResolvedSchema) JsonParser(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser) IOException(java.io.IOException) PLAN_COMPILE_CATALOG_OBJECTS(org.apache.flink.table.api.config.TableConfigOptions.PLAN_COMPILE_CATALOG_OBJECTS) CatalogPlanRestore(org.apache.flink.table.api.config.TableConfigOptions.CatalogPlanRestore) CatalogPlanCompilation(org.apache.flink.table.api.config.TableConfigOptions.CatalogPlanCompilation) Objects(java.util.Objects) OPTIONS(org.apache.flink.table.planner.plan.nodes.exec.serde.ResolvedCatalogTableJsonSerializer.OPTIONS) DeserializationContext(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext) List(java.util.List) ValidationException(org.apache.flink.table.api.ValidationException) Optional(java.util.Optional) Internal(org.apache.flink.annotation.Internal) StdDeserializer(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.deser.std.StdDeserializer) PLAN_RESTORE_CATALOG_OBJECTS(org.apache.flink.table.api.config.TableConfigOptions.PLAN_RESTORE_CATALOG_OBJECTS) ResolvedCatalogTable(org.apache.flink.table.catalog.ResolvedCatalogTable) FIELD_NAME_CATALOG_TABLE(org.apache.flink.table.planner.plan.nodes.exec.serde.ContextResolvedTableJsonSerializer.FIELD_NAME_CATALOG_TABLE) ContextResolvedTable(org.apache.flink.table.catalog.ContextResolvedTable) ValidationException(org.apache.flink.table.api.ValidationException) ObjectNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ObjectNode) ResolvedCatalogTable(org.apache.flink.table.catalog.ResolvedCatalogTable) CatalogPlanRestore(org.apache.flink.table.api.config.TableConfigOptions.CatalogPlanRestore) ContextResolvedTable(org.apache.flink.table.catalog.ContextResolvedTable) ResolvedSchema(org.apache.flink.table.catalog.ResolvedSchema) CatalogManager(org.apache.flink.table.catalog.CatalogManager) ObjectIdentifier(org.apache.flink.table.catalog.ObjectIdentifier)

Example 3 with CatalogPlanRestore

use of org.apache.flink.table.api.config.TableConfigOptions.CatalogPlanRestore in project flink by apache.

the class RexNodeJsonDeserializer method deserializeCatalogFunction.

private static SqlOperator deserializeCatalogFunction(JsonNode jsonNode, SqlSyntax syntax, SerdeContext serdeContext) {
    final CatalogPlanRestore restoreStrategy = serdeContext.getConfiguration().get(PLAN_RESTORE_CATALOG_OBJECTS);
    final FunctionIdentifier identifier = FunctionIdentifier.of(ObjectIdentifierJsonDeserializer.deserialize(jsonNode.required(FIELD_NAME_CATALOG_NAME).asText(), serdeContext));
    switch(restoreStrategy) {
        case ALL:
            {
                final Optional<SqlOperator> lookupOperator = lookupOptionalSqlOperator(identifier, syntax, serdeContext, false);
                if (lookupOperator.isPresent()) {
                    return lookupOperator.get();
                } else if (jsonNode.has(FIELD_NAME_CLASS)) {
                    return deserializeFunctionClass(jsonNode, serdeContext);
                }
                throw missingFunctionFromCatalog(identifier, false);
            }
        case ALL_ENFORCED:
            {
                if (jsonNode.has(FIELD_NAME_CLASS)) {
                    return deserializeFunctionClass(jsonNode, serdeContext);
                }
                final Optional<SqlOperator> lookupOperator = lookupOptionalSqlOperator(identifier, syntax, serdeContext, false);
                if (lookupOperator.map(RexNodeJsonDeserializer::isTemporary).orElse(false)) {
                    return lookupOperator.get();
                }
                throw lookupDisabled(identifier);
            }
        case IDENTIFIER:
            final Optional<SqlOperator> lookupOperator = lookupOptionalSqlOperator(identifier, syntax, serdeContext, true);
            if (lookupOperator.isPresent()) {
                return lookupOperator.get();
            } else {
                throw missingFunctionFromCatalog(identifier, true);
            }
        default:
            throw new TableException("Unsupported restore strategy: " + restoreStrategy);
    }
}
Also used : FunctionIdentifier(org.apache.flink.table.functions.FunctionIdentifier) TableException(org.apache.flink.table.api.TableException) Optional(java.util.Optional) BuiltInSqlOperator(org.apache.flink.table.planner.functions.sql.BuiltInSqlOperator) SqlOperator(org.apache.calcite.sql.SqlOperator) CatalogPlanRestore(org.apache.flink.table.api.config.TableConfigOptions.CatalogPlanRestore)

Example 4 with CatalogPlanRestore

use of org.apache.flink.table.api.config.TableConfigOptions.CatalogPlanRestore in project flink by apache.

the class LogicalTypeJsonDeserializer method deserializeStructuredType.

private static LogicalType deserializeStructuredType(JsonNode logicalTypeNode, SerdeContext serdeContext) {
    // inline structured types have no object identifier
    if (!logicalTypeNode.has(FIELD_NAME_OBJECT_IDENTIFIER)) {
        return deserializeStructuredTypeFromPlan(logicalTypeNode, serdeContext);
    }
    // for catalog structured types
    final ObjectIdentifier identifier = ObjectIdentifierJsonDeserializer.deserialize(logicalTypeNode.get(FIELD_NAME_OBJECT_IDENTIFIER).asText(), serdeContext);
    final CatalogPlanRestore restoreStrategy = serdeContext.getConfiguration().get(TableConfigOptions.PLAN_RESTORE_CATALOG_OBJECTS);
    switch(restoreStrategy) {
        case ALL:
            if (logicalTypeNode.has(FIELD_NAME_ATTRIBUTES)) {
                return deserializeStructuredTypeFromPlan(logicalTypeNode, serdeContext);
            }
            return deserializeUserDefinedTypeFromCatalog(identifier, serdeContext);
        case ALL_ENFORCED:
            return deserializeStructuredTypeFromPlan(logicalTypeNode, serdeContext);
        case IDENTIFIER:
            return deserializeUserDefinedTypeFromCatalog(identifier, serdeContext);
        default:
            throw new TableException("Unsupported catalog restore strategy.");
    }
}
Also used : TableException(org.apache.flink.table.api.TableException) CatalogPlanRestore(org.apache.flink.table.api.config.TableConfigOptions.CatalogPlanRestore) ObjectIdentifier(org.apache.flink.table.catalog.ObjectIdentifier)

Aggregations

CatalogPlanRestore (org.apache.flink.table.api.config.TableConfigOptions.CatalogPlanRestore)4 TableException (org.apache.flink.table.api.TableException)3 ObjectIdentifier (org.apache.flink.table.catalog.ObjectIdentifier)3 Optional (java.util.Optional)2 IOException (java.io.IOException)1 List (java.util.List)1 Objects (java.util.Objects)1 SqlOperator (org.apache.calcite.sql.SqlOperator)1 Internal (org.apache.flink.annotation.Internal)1 JsonParser (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser)1 DeserializationContext (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext)1 StdDeserializer (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.deser.std.StdDeserializer)1 ObjectNode (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ObjectNode)1 ValidationException (org.apache.flink.table.api.ValidationException)1 CatalogPlanCompilation (org.apache.flink.table.api.config.TableConfigOptions.CatalogPlanCompilation)1 IDENTIFIER (org.apache.flink.table.api.config.TableConfigOptions.CatalogPlanRestore.IDENTIFIER)1 PLAN_COMPILE_CATALOG_OBJECTS (org.apache.flink.table.api.config.TableConfigOptions.PLAN_COMPILE_CATALOG_OBJECTS)1 PLAN_RESTORE_CATALOG_OBJECTS (org.apache.flink.table.api.config.TableConfigOptions.PLAN_RESTORE_CATALOG_OBJECTS)1 CatalogManager (org.apache.flink.table.catalog.CatalogManager)1 Column (org.apache.flink.table.catalog.Column)1