Search in sources :

Example 1 with AnalyzerOptions

use of com.google.zetasql.AnalyzerOptions in project beam by apache.

the class BeamZetaSqlCatalogTest method rejectsCreateFunctionStmtWithUnsupportedReturnType.

@Test
public void rejectsCreateFunctionStmtWithUnsupportedReturnType() {
    JdbcConnection jdbcConnection = createJdbcConnection();
    AnalyzerOptions analyzerOptions = SqlAnalyzer.baseAnalyzerOptions();
    BeamZetaSqlCatalog beamCatalog = BeamZetaSqlCatalog.create(jdbcConnection.getCurrentSchemaPlus(), jdbcConnection.getTypeFactory(), analyzerOptions);
    String sql = "CREATE FUNCTION foo() RETURNS ARRAY<TIME> LANGUAGE java OPTIONS (path='/does/not/exist');";
    ResolvedNodes.ResolvedStatement resolvedStatement = Analyzer.analyzeStatement(sql, analyzerOptions, beamCatalog.getZetaSqlCatalog());
    ResolvedNodes.ResolvedCreateFunctionStmt createFunctionStmt = (ResolvedNodes.ResolvedCreateFunctionStmt) resolvedStatement;
    thrown.expect(UnsupportedOperationException.class);
    thrown.expectMessage("ZetaSQL type TYPE_TIME not allowed in function foo");
    beamCatalog.addFunction(createFunctionStmt);
}
Also used : JdbcConnection(org.apache.beam.sdk.extensions.sql.impl.JdbcConnection) ResolvedNodes(com.google.zetasql.resolvedast.ResolvedNodes) AnalyzerOptions(com.google.zetasql.AnalyzerOptions) Test(org.junit.Test)

Example 2 with AnalyzerOptions

use of com.google.zetasql.AnalyzerOptions in project beam by apache.

the class BeamZetaSqlCatalogTest method rejectsCreateFunctionStmtWithUnsupportedParameterType.

@Test
public void rejectsCreateFunctionStmtWithUnsupportedParameterType() {
    JdbcConnection jdbcConnection = createJdbcConnection();
    AnalyzerOptions analyzerOptions = SqlAnalyzer.baseAnalyzerOptions();
    BeamZetaSqlCatalog beamCatalog = BeamZetaSqlCatalog.create(jdbcConnection.getCurrentSchemaPlus(), jdbcConnection.getTypeFactory(), analyzerOptions);
    String sql = "CREATE FUNCTION foo(a ARRAY<TIME>) RETURNS INT64 LANGUAGE java OPTIONS (path='/does/not/exist');";
    ResolvedNodes.ResolvedStatement resolvedStatement = Analyzer.analyzeStatement(sql, analyzerOptions, beamCatalog.getZetaSqlCatalog());
    ResolvedNodes.ResolvedCreateFunctionStmt createFunctionStmt = (ResolvedNodes.ResolvedCreateFunctionStmt) resolvedStatement;
    thrown.expect(UnsupportedOperationException.class);
    thrown.expectMessage("ZetaSQL type TYPE_TIME not allowed in function foo");
    beamCatalog.addFunction(createFunctionStmt);
}
Also used : JdbcConnection(org.apache.beam.sdk.extensions.sql.impl.JdbcConnection) ResolvedNodes(com.google.zetasql.resolvedast.ResolvedNodes) AnalyzerOptions(com.google.zetasql.AnalyzerOptions) Test(org.junit.Test)

Example 3 with AnalyzerOptions

use of com.google.zetasql.AnalyzerOptions in project beam by apache.

the class ZetaSQLPlannerImpl method rel.

public RelRoot rel(String sql, QueryParameters params) {
    RelOptCluster cluster = RelOptCluster.create(planner, new RexBuilder(typeFactory));
    AnalyzerOptions options = SqlAnalyzer.getAnalyzerOptions(params, defaultTimezone);
    BeamZetaSqlCatalog catalog = BeamZetaSqlCatalog.create(defaultSchemaPlus, (JavaTypeFactory) cluster.getTypeFactory(), options);
    // Set up table providers that need to be pre-registered
    SqlAnalyzer analyzer = new SqlAnalyzer();
    List<List<String>> tables = analyzer.extractTableNames(sql, options);
    TableResolution.registerTables(this.defaultSchemaPlus, tables);
    QueryTrait trait = new QueryTrait();
    catalog.addTables(tables, trait);
    ResolvedQueryStmt statement = analyzer.analyzeQuery(sql, options, catalog);
    ExpressionConverter expressionConverter = new ExpressionConverter(cluster, params, catalog.getUserFunctionDefinitions());
    ConversionContext context = ConversionContext.of(config, expressionConverter, cluster, trait);
    RelNode convertedNode = QueryStatementConverter.convertRootQuery(context, statement);
    return RelRoot.of(convertedNode, SqlKind.ALL);
}
Also used : RelOptCluster(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelOptCluster) ConversionContext(org.apache.beam.sdk.extensions.sql.zetasql.translation.ConversionContext) RelNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode) RexBuilder(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexBuilder) ExpressionConverter(org.apache.beam.sdk.extensions.sql.zetasql.translation.ExpressionConverter) List(java.util.List) ImmutableList(org.apache.beam.vendor.calcite.v1_28_0.com.google.common.collect.ImmutableList) ResolvedQueryStmt(com.google.zetasql.resolvedast.ResolvedNodes.ResolvedQueryStmt) AnalyzerOptions(com.google.zetasql.AnalyzerOptions)

Example 4 with AnalyzerOptions

use of com.google.zetasql.AnalyzerOptions in project beam by apache.

the class SqlAnalyzer method baseAnalyzerOptions.

static AnalyzerOptions baseAnalyzerOptions() {
    AnalyzerOptions options = new AnalyzerOptions();
    options.setErrorMessageMode(ErrorMessageMode.ERROR_MESSAGE_MULTI_LINE_WITH_CARET);
    options.getLanguageOptions().setProductMode(ProductMode.PRODUCT_EXTERNAL);
    options.getLanguageOptions().setEnabledLanguageFeatures(new HashSet<>(Arrays.asList(LanguageFeature.FEATURE_CREATE_AGGREGATE_FUNCTION, LanguageFeature.FEATURE_CREATE_TABLE_FUNCTION, LanguageFeature.FEATURE_DISALLOW_GROUP_BY_FLOAT, LanguageFeature.FEATURE_NUMERIC_TYPE, LanguageFeature.FEATURE_TABLE_VALUED_FUNCTIONS, LanguageFeature.FEATURE_TEMPLATE_FUNCTIONS, LanguageFeature.FEATURE_V_1_1_SELECT_STAR_EXCEPT_REPLACE, LanguageFeature.FEATURE_V_1_2_CIVIL_TIME, LanguageFeature.FEATURE_V_1_3_ADDITIONAL_STRING_FUNCTIONS)));
    options.getLanguageOptions().setSupportedStatementKinds(SUPPORTED_STATEMENT_KINDS);
    return options;
}
Also used : AnalyzerOptions(com.google.zetasql.AnalyzerOptions)

Example 5 with AnalyzerOptions

use of com.google.zetasql.AnalyzerOptions in project beam by apache.

the class SqlAnalyzer method getAnalyzerOptions.

static AnalyzerOptions getAnalyzerOptions(QueryParameters queryParams, String defaultTimezone) {
    AnalyzerOptions options = baseAnalyzerOptions();
    options.setDefaultTimezone(defaultTimezone);
    if (queryParams.getKind() == Kind.NAMED) {
        options.setParameterMode(ParameterMode.PARAMETER_NAMED);
        for (Map.Entry<String, Value> entry : ((Map<String, Value>) queryParams.named()).entrySet()) {
            options.addQueryParameter(entry.getKey(), entry.getValue().getType());
        }
    } else if (queryParams.getKind() == Kind.POSITIONAL) {
        options.setParameterMode(ParameterMode.PARAMETER_POSITIONAL);
        for (Value param : (List<Value>) queryParams.positional()) {
            options.addPositionalQueryParameter(param.getType());
        }
    }
    return options;
}
Also used : Value(com.google.zetasql.Value) Map(java.util.Map) AnalyzerOptions(com.google.zetasql.AnalyzerOptions)

Aggregations

AnalyzerOptions (com.google.zetasql.AnalyzerOptions)5 ResolvedNodes (com.google.zetasql.resolvedast.ResolvedNodes)2 JdbcConnection (org.apache.beam.sdk.extensions.sql.impl.JdbcConnection)2 Test (org.junit.Test)2 Value (com.google.zetasql.Value)1 ResolvedQueryStmt (com.google.zetasql.resolvedast.ResolvedNodes.ResolvedQueryStmt)1 List (java.util.List)1 Map (java.util.Map)1 ConversionContext (org.apache.beam.sdk.extensions.sql.zetasql.translation.ConversionContext)1 ExpressionConverter (org.apache.beam.sdk.extensions.sql.zetasql.translation.ExpressionConverter)1 ImmutableList (org.apache.beam.vendor.calcite.v1_28_0.com.google.common.collect.ImmutableList)1 RelOptCluster (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelOptCluster)1 RelNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode)1 RexBuilder (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexBuilder)1