Search in sources :

Example 11 with TableEnvironmentInternal

use of org.apache.flink.table.api.internal.TableEnvironmentInternal in project flink by apache.

the class LocalExecutor method completeStatement.

@Override
public List<String> completeStatement(String sessionId, String statement, int position) {
    final ExecutionContext context = getExecutionContext(sessionId);
    final TableEnvironmentInternal tableEnv = (TableEnvironmentInternal) context.getTableEnvironment();
    try {
        return context.wrapClassLoader(() -> Arrays.asList(tableEnv.getParser().getCompletionHints(statement, position)));
    } catch (Throwable t) {
        // catch everything such that the query does not crash the executor
        if (LOG.isDebugEnabled()) {
            LOG.debug("Could not complete statement at " + position + ":" + statement, t);
        }
        return Collections.emptyList();
    }
}
Also used : ExecutionContext(org.apache.flink.table.client.gateway.context.ExecutionContext) TableEnvironmentInternal(org.apache.flink.table.api.internal.TableEnvironmentInternal)

Example 12 with TableEnvironmentInternal

use of org.apache.flink.table.api.internal.TableEnvironmentInternal in project flink by apache.

the class BuiltInFunctionTestBase method testFunction.

@Test
public void testFunction() {
    final TableEnvironment env = TableEnvironment.create(EnvironmentSettings.newInstance().build());
    env.getConfig().addConfiguration(configuration());
    testSpec.functions.forEach(f -> env.createTemporarySystemFunction(f.getSimpleName(), f));
    final DataTypeFactory dataTypeFactory = ((TableEnvironmentInternal) env).getCatalogManager().getDataTypeFactory();
    final Table inputTable;
    if (testSpec.fieldDataTypes == null) {
        inputTable = env.fromValues(Row.of(testSpec.fieldData));
    } else {
        final DataTypes.UnresolvedField[] fields = IntStream.range(0, testSpec.fieldDataTypes.length).mapToObj(i -> DataTypes.FIELD("f" + i, testSpec.fieldDataTypes[i])).toArray(DataTypes.UnresolvedField[]::new);
        inputTable = env.fromValues(DataTypes.ROW(fields), Row.of(testSpec.fieldData));
    }
    for (TestItem testItem : testSpec.testItems) {
        try {
            if (testItem instanceof ResultTestItem<?>) {
                testResult(dataTypeFactory, env, inputTable, (ResultTestItem<?>) testItem);
            } else if (testItem instanceof ErrorTestItem<?>) {
                testError(env, inputTable, (ErrorTestItem<?>) testItem);
            }
        } catch (Throwable t) {
            throw new AssertionError("Failing test item: " + testItem, t);
        }
    }
}
Also used : DataTypeFactory(org.apache.flink.table.catalog.DataTypeFactory) IntStream(java.util.stream.IntStream) DataType(org.apache.flink.table.types.DataType) BuiltInFunctionDefinition(org.apache.flink.table.functions.BuiltInFunctionDefinition) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) Expression(org.apache.flink.table.expressions.Expression) AtomicReference(java.util.concurrent.atomic.AtomicReference) MiniClusterResourceConfiguration(org.apache.flink.runtime.testutils.MiniClusterResourceConfiguration) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) ClassRule(org.junit.ClassRule) FlinkAssertions.anyCauseMatches(org.apache.flink.core.testutils.FlinkAssertions.anyCauseMatches) Parameterized(org.junit.runners.Parameterized) Nullable(javax.annotation.Nullable) AbstractDataType(org.apache.flink.table.types.AbstractDataType) MiniClusterWithClientResource(org.apache.flink.test.util.MiniClusterWithClientResource) TableEnvironment(org.apache.flink.table.api.TableEnvironment) Iterator(java.util.Iterator) Parameter(org.junit.runners.Parameterized.Parameter) Configuration(org.apache.flink.configuration.Configuration) DataTypes(org.apache.flink.table.api.DataTypes) UserDefinedFunction(org.apache.flink.table.functions.UserDefinedFunction) Test(org.junit.Test) Table(org.apache.flink.table.api.Table) Preconditions(org.apache.flink.util.Preconditions) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) List(java.util.List) ValidationException(org.apache.flink.table.api.ValidationException) EnvironmentSettings(org.apache.flink.table.api.EnvironmentSettings) TableResult(org.apache.flink.table.api.TableResult) Row(org.apache.flink.types.Row) TableEnvironmentInternal(org.apache.flink.table.api.internal.TableEnvironmentInternal) Table(org.apache.flink.table.api.Table) TableEnvironment(org.apache.flink.table.api.TableEnvironment) DataTypeFactory(org.apache.flink.table.catalog.DataTypeFactory) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) DataTypes(org.apache.flink.table.api.DataTypes) Test(org.junit.Test)

Example 13 with TableEnvironmentInternal

use of org.apache.flink.table.api.internal.TableEnvironmentInternal in project zeppelin by apache.

the class Flink114Shims method parseSql.

/**
 * Parse it via flink SqlParser first, then fallback to regular expression matching.
 *
 * @param tableEnv
 * @param stmt
 * @return
 */
@Override
public Optional<SqlCommandParser.SqlCommandCall> parseSql(Object tableEnv, String stmt) {
    Parser sqlParser = ((TableEnvironmentInternal) tableEnv).getParser();
    SqlCommandCall sqlCommandCall = null;
    try {
        // parse statement via regex matching first
        Optional<SqlCommandCall> callOpt = parseByRegexMatching(stmt);
        if (callOpt.isPresent()) {
            sqlCommandCall = callOpt.get();
        } else {
            sqlCommandCall = parseBySqlParser(sqlParser, stmt);
        }
    } catch (Exception e) {
        return Optional.empty();
    }
    return Optional.of(sqlCommandCall);
}
Also used : TableEnvironmentInternal(org.apache.flink.table.api.internal.TableEnvironmentInternal) SqlCommandCall(org.apache.zeppelin.flink.sql.SqlCommandParser.SqlCommandCall) FlinkException(org.apache.flink.util.FlinkException) TableException(org.apache.flink.table.api.TableException) IOException(java.io.IOException) SqlCommandParser(org.apache.zeppelin.flink.sql.SqlCommandParser) Parser(org.apache.flink.table.delegation.Parser)

Example 14 with TableEnvironmentInternal

use of org.apache.flink.table.api.internal.TableEnvironmentInternal in project flink by apache.

the class HiveDynamicTableFactoryTest method getTableSource.

private DynamicTableSource getTableSource(String tableName) throws Exception {
    TableEnvironmentInternal tableEnvInternal = (TableEnvironmentInternal) tableEnv;
    ObjectIdentifier tableIdentifier = ObjectIdentifier.of(hiveCatalog.getName(), "default", tableName);
    CatalogTable catalogTable = (CatalogTable) hiveCatalog.getTable(tableIdentifier.toObjectPath());
    return FactoryUtil.createDynamicTableSource((DynamicTableSourceFactory) hiveCatalog.getFactory().orElseThrow(IllegalStateException::new), tableIdentifier, tableEnvInternal.getCatalogManager().resolveCatalogTable(catalogTable), tableEnv.getConfig().getConfiguration(), Thread.currentThread().getContextClassLoader(), false);
}
Also used : TableEnvironmentInternal(org.apache.flink.table.api.internal.TableEnvironmentInternal) CatalogTable(org.apache.flink.table.catalog.CatalogTable) ObjectIdentifier(org.apache.flink.table.catalog.ObjectIdentifier)

Example 15 with TableEnvironmentInternal

use of org.apache.flink.table.api.internal.TableEnvironmentInternal in project zeppelin by apache.

the class Flink113Shims method parseSql.

/**
 * Parse it via flink SqlParser first, then fallback to regular expression matching.
 *
 * @param tableEnv
 * @param stmt
 * @return
 */
@Override
public Optional<SqlCommandParser.SqlCommandCall> parseSql(Object tableEnv, String stmt) {
    Parser sqlParser = ((TableEnvironmentInternal) tableEnv).getParser();
    SqlCommandCall sqlCommandCall = null;
    try {
        // parse statement via regex matching first
        Optional<SqlCommandCall> callOpt = parseByRegexMatching(stmt);
        if (callOpt.isPresent()) {
            sqlCommandCall = callOpt.get();
        } else {
            sqlCommandCall = parseBySqlParser(sqlParser, stmt);
        }
    } catch (Exception e) {
        return Optional.empty();
    }
    return Optional.of(sqlCommandCall);
}
Also used : TableEnvironmentInternal(org.apache.flink.table.api.internal.TableEnvironmentInternal) SqlCommandCall(org.apache.zeppelin.flink.sql.SqlCommandParser.SqlCommandCall) FlinkException(org.apache.flink.util.FlinkException) TableException(org.apache.flink.table.api.TableException) IOException(java.io.IOException) SqlCommandParser(org.apache.zeppelin.flink.sql.SqlCommandParser) Parser(org.apache.flink.table.delegation.Parser)

Aggregations

TableEnvironmentInternal (org.apache.flink.table.api.internal.TableEnvironmentInternal)18 Parser (org.apache.flink.table.delegation.Parser)7 Test (org.junit.Test)5 TableEnvironment (org.apache.flink.table.api.TableEnvironment)4 CatalogTable (org.apache.flink.table.catalog.CatalogTable)4 ExecutionContext (org.apache.flink.table.client.gateway.context.ExecutionContext)4 IOException (java.io.IOException)3 ParameterTool (org.apache.flink.api.java.utils.ParameterTool)3 Table (org.apache.flink.table.api.Table)3 TableException (org.apache.flink.table.api.TableException)3 TableResult (org.apache.flink.table.api.TableResult)3 ObjectIdentifier (org.apache.flink.table.catalog.ObjectIdentifier)3 HiveParser (org.apache.flink.table.planner.delegation.hive.HiveParser)3 Row (org.apache.flink.types.Row)3 ArrayList (java.util.ArrayList)2 TypeInformation (org.apache.flink.api.common.typeinfo.TypeInformation)2 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)2 StreamTableEnvironment (org.apache.flink.table.api.bridge.java.StreamTableEnvironment)2 SqlExecutionException (org.apache.flink.table.client.gateway.SqlExecutionException)2 CsvTableSink (org.apache.flink.table.sinks.CsvTableSink)2