Search in sources :

Example 1 with CatalogManager

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

the class HiveParser method parse.

@Override
public List<Operation> parse(String statement) {
    CatalogManager catalogManager = getCatalogManager();
    Catalog currentCatalog = catalogManager.getCatalog(catalogManager.getCurrentCatalog()).orElse(null);
    if (!(currentCatalog instanceof HiveCatalog)) {
        LOG.warn("Current catalog is not HiveCatalog. Falling back to Flink's planner.");
        return super.parse(statement);
    }
    HiveConf hiveConf = new HiveConf(((HiveCatalog) currentCatalog).getHiveConf());
    hiveConf.setVar(HiveConf.ConfVars.DYNAMICPARTITIONINGMODE, "nonstrict");
    hiveConf.set("hive.allow.udf.load.on.demand", "false");
    hiveConf.setVar(HiveConf.ConfVars.HIVE_EXECUTION_ENGINE, "mr");
    HiveShim hiveShim = HiveShimLoader.loadHiveShim(((HiveCatalog) currentCatalog).getHiveVersion());
    try {
        // creates SessionState
        startSessionState(hiveConf, catalogManager);
        // We override Hive's grouping function. Refer to the implementation for more details.
        hiveShim.registerTemporaryFunction("grouping", HiveGenericUDFGrouping.class);
        return processCmd(statement, hiveConf, hiveShim, (HiveCatalog) currentCatalog);
    } finally {
        clearSessionState();
    }
}
Also used : HiveCatalog(org.apache.flink.table.catalog.hive.HiveCatalog) HiveConf(org.apache.hadoop.hive.conf.HiveConf) HiveShim(org.apache.flink.table.catalog.hive.client.HiveShim) CatalogManager(org.apache.flink.table.catalog.CatalogManager) HiveCatalog(org.apache.flink.table.catalog.hive.HiveCatalog) Catalog(org.apache.flink.table.catalog.Catalog)

Example 2 with CatalogManager

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

the class SessionContext method create.

// --------------------------------------------------------------------------------------------
// Helper method to create
// --------------------------------------------------------------------------------------------
public static SessionContext create(DefaultContext defaultContext, String sessionId) {
    // --------------------------------------------------------------------------------------------------------------
    // Init config
    // --------------------------------------------------------------------------------------------------------------
    Configuration configuration = defaultContext.getFlinkConfig().clone();
    // --------------------------------------------------------------------------------------------------------------
    // Init classloader
    // --------------------------------------------------------------------------------------------------------------
    URLClassLoader classLoader = ClientUtils.buildUserCodeClassLoader(defaultContext.getDependencies(), Collections.emptyList(), SessionContext.class.getClassLoader(), configuration);
    // --------------------------------------------------------------------------------------------------------------
    // Init session state
    // --------------------------------------------------------------------------------------------------------------
    ModuleManager moduleManager = new ModuleManager();
    final EnvironmentSettings settings = EnvironmentSettings.fromConfiguration(configuration);
    CatalogManager catalogManager = CatalogManager.newBuilder().classLoader(classLoader).config(configuration).defaultCatalog(settings.getBuiltInCatalogName(), new GenericInMemoryCatalog(settings.getBuiltInCatalogName(), settings.getBuiltInDatabaseName())).build();
    FunctionCatalog functionCatalog = new FunctionCatalog(configuration, catalogManager, moduleManager);
    SessionState sessionState = new SessionState(catalogManager, moduleManager, functionCatalog);
    // --------------------------------------------------------------------------------------------------------------
    // Init ExecutionContext
    // --------------------------------------------------------------------------------------------------------------
    ExecutionContext executionContext = new ExecutionContext(configuration, classLoader, sessionState);
    return new SessionContext(defaultContext, sessionId, configuration, classLoader, sessionState, executionContext);
}
Also used : FunctionCatalog(org.apache.flink.table.catalog.FunctionCatalog) EnvironmentSettings(org.apache.flink.table.api.EnvironmentSettings) Configuration(org.apache.flink.configuration.Configuration) URLClassLoader(java.net.URLClassLoader) ModuleManager(org.apache.flink.table.module.ModuleManager) CatalogManager(org.apache.flink.table.catalog.CatalogManager) GenericInMemoryCatalog(org.apache.flink.table.catalog.GenericInMemoryCatalog)

Example 3 with CatalogManager

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

the class DynamicTableSourceSpecSerdeTest method testDynamicTableSourceSpecSerde.

@ParameterizedTest
@MethodSource("testDynamicTableSinkSpecSerde")
void testDynamicTableSourceSpecSerde(DynamicTableSourceSpec 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 DynamicTableSourceSpec(ContextResolvedTable.permanent(spec.getContextResolvedTable().getIdentifier(), catalogManager.getCatalog(catalogManager.getCurrentCatalog()).get(), spec.getContextResolvedTable().getResolvedTable()), spec.getSourceAbilities());
    String actualJson = toJson(serdeCtx, spec);
    DynamicTableSourceSpec actual = toObject(serdeCtx, actualJson, DynamicTableSourceSpec.class);
    assertThat(actual.getContextResolvedTable()).isEqualTo(spec.getContextResolvedTable());
    assertThat(actual.getSourceAbilities()).isEqualTo(spec.getSourceAbilities());
    assertThat(actual.getScanTableSource(plannerMocks.getPlannerContext().getFlinkContext())).isNotNull();
}
Also used : JsonSerdeTestUtil.configuredSerdeContext(org.apache.flink.table.planner.plan.nodes.exec.serde.JsonSerdeTestUtil.configuredSerdeContext) DynamicTableSourceSpec(org.apache.flink.table.planner.plan.nodes.exec.spec.DynamicTableSourceSpec) 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 4 with CatalogManager

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

the class LogicalTypeJsonSerdeTest method testIdentifierSerde.

@Test
public void testIdentifierSerde() throws IOException {
    final DataTypeFactoryMock dataTypeFactoryMock = new DataTypeFactoryMock();
    final TableConfig tableConfig = TableConfig.getDefault();
    final Configuration config = tableConfig.getConfiguration();
    final CatalogManager catalogManager = preparedCatalogManager().dataTypeFactory(dataTypeFactoryMock).build();
    final SerdeContext serdeContext = configuredSerdeContext(catalogManager, tableConfig);
    // minimal plan content
    config.set(TableConfigOptions.PLAN_COMPILE_CATALOG_OBJECTS, IDENTIFIER);
    final String minimalJson = toJson(serdeContext, STRUCTURED_TYPE);
    assertThat(minimalJson).isEqualTo("\"`default_catalog`.`default_database`.`MyType`\"");
    // catalog lookup with miss
    config.set(TableConfigOptions.PLAN_RESTORE_CATALOG_OBJECTS, TableConfigOptions.CatalogPlanRestore.IDENTIFIER);
    dataTypeFactoryMock.logicalType = Optional.empty();
    assertThatThrownBy(() -> toObject(serdeContext, minimalJson, LogicalType.class)).satisfies(anyCauseMatches(ValidationException.class, "No type found."));
    // catalog lookup
    config.set(TableConfigOptions.PLAN_RESTORE_CATALOG_OBJECTS, TableConfigOptions.CatalogPlanRestore.IDENTIFIER);
    dataTypeFactoryMock.logicalType = Optional.of(STRUCTURED_TYPE);
    assertThat(toObject(serdeContext, minimalJson, LogicalType.class)).isEqualTo(STRUCTURED_TYPE);
    // maximum plan content
    config.set(TableConfigOptions.PLAN_COMPILE_CATALOG_OBJECTS, ALL);
    final String maximumJson = toJson(serdeContext, STRUCTURED_TYPE);
    final ObjectMapper mapper = new ObjectMapper();
    final JsonNode maximumJsonNode = mapper.readTree(maximumJson);
    assertThat(maximumJsonNode.get(LogicalTypeJsonSerializer.FIELD_NAME_ATTRIBUTES)).isNotNull();
    assertThat(maximumJsonNode.get(LogicalTypeJsonSerializer.FIELD_NAME_DESCRIPTION).asText()).isEqualTo("My original type.");
    // catalog lookup with miss
    config.set(TableConfigOptions.PLAN_RESTORE_CATALOG_OBJECTS, TableConfigOptions.CatalogPlanRestore.IDENTIFIER);
    dataTypeFactoryMock.logicalType = Optional.empty();
    assertThatThrownBy(() -> toObject(serdeContext, maximumJson, LogicalType.class)).satisfies(anyCauseMatches(ValidationException.class, "No type found."));
    // catalog lookup
    config.set(TableConfigOptions.PLAN_RESTORE_CATALOG_OBJECTS, TableConfigOptions.CatalogPlanRestore.IDENTIFIER);
    dataTypeFactoryMock.logicalType = Optional.of(UPDATED_STRUCTURED_TYPE);
    assertThat(toObject(serdeContext, maximumJson, LogicalType.class)).isEqualTo(UPDATED_STRUCTURED_TYPE);
    // no lookup
    config.set(TableConfigOptions.PLAN_RESTORE_CATALOG_OBJECTS, TableConfigOptions.CatalogPlanRestore.ALL);
    dataTypeFactoryMock.logicalType = Optional.of(UPDATED_STRUCTURED_TYPE);
    assertThat(toObject(serdeContext, maximumJson, LogicalType.class)).isEqualTo(STRUCTURED_TYPE);
}
Also used : ValidationException(org.apache.flink.table.api.ValidationException) Configuration(org.apache.flink.configuration.Configuration) JsonSerdeTestUtil.configuredSerdeContext(org.apache.flink.table.planner.plan.nodes.exec.serde.JsonSerdeTestUtil.configuredSerdeContext) DataTypeFactoryMock(org.apache.flink.table.types.utils.DataTypeFactoryMock) TableConfig(org.apache.flink.table.api.TableConfig) JsonNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode) CatalogManager(org.apache.flink.table.catalog.CatalogManager) CatalogManagerMocks.preparedCatalogManager(org.apache.flink.table.utils.CatalogManagerMocks.preparedCatalogManager) ObjectMapper(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with CatalogManager

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

the class StreamTableEnvironmentImplTest method getStreamTableEnvironment.

private StreamTableEnvironmentImpl getStreamTableEnvironment(StreamExecutionEnvironment env, DataStreamSource<Integer> elements) {
    TableConfig tableConfig = new TableConfig();
    CatalogManager catalogManager = CatalogManagerMocks.createEmptyCatalogManager();
    ModuleManager moduleManager = new ModuleManager();
    return new StreamTableEnvironmentImpl(catalogManager, moduleManager, new FunctionCatalog(tableConfig, catalogManager, moduleManager), tableConfig, env, new TestPlanner(elements.getTransformation()), new ExecutorMock(), true, this.getClass().getClassLoader());
}
Also used : FunctionCatalog(org.apache.flink.table.catalog.FunctionCatalog) TableConfig(org.apache.flink.table.api.TableConfig) ExecutorMock(org.apache.flink.table.utils.ExecutorMock) ModuleManager(org.apache.flink.table.module.ModuleManager) CatalogManager(org.apache.flink.table.catalog.CatalogManager)

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