Search in sources :

Example 11 with Context

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.Context in project calcite by apache.

the class RelOptRulesTest method testExtractYearMonthToRange.

@Test
public void testExtractYearMonthToRange() {
    final String sql = "select *\n" + "from sales.emp_b as e\n" + "where extract(year from birthdate) = 2014" + "and extract(month from birthdate) = 4";
    HepProgram program = new HepProgramBuilder().addRuleInstance(DateRangeRules.FILTER_INSTANCE).build();
    final Context context = Contexts.of(new CalciteConnectionConfigImpl(new Properties()));
    sql(sql).with(program).withContext(context).check();
}
Also used : Context(org.apache.calcite.plan.Context) CalciteConnectionConfigImpl(org.apache.calcite.config.CalciteConnectionConfigImpl) HepProgram(org.apache.calcite.plan.hep.HepProgram) HepProgramBuilder(org.apache.calcite.plan.hep.HepProgramBuilder) Properties(java.util.Properties) Test(org.junit.Test)

Example 12 with Context

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.Context in project calcite by apache.

the class RelOptRulesTest method testExtractYearToRange.

/**
 * Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-434">[CALCITE-434]
 * Converting predicates on date dimension columns into date ranges</a>,
 * specifically a rule that converts {@code EXTRACT(YEAR FROM ...) = constant}
 * to a range.
 */
@Test
public void testExtractYearToRange() {
    final String sql = "select *\n" + "from sales.emp_b as e\n" + "where extract(year from birthdate) = 2014";
    HepProgram program = new HepProgramBuilder().addRuleInstance(DateRangeRules.FILTER_INSTANCE).build();
    final Context context = Contexts.of(new CalciteConnectionConfigImpl(new Properties()));
    sql(sql).with(program).withContext(context).check();
}
Also used : Context(org.apache.calcite.plan.Context) CalciteConnectionConfigImpl(org.apache.calcite.config.CalciteConnectionConfigImpl) HepProgram(org.apache.calcite.plan.hep.HepProgram) HepProgramBuilder(org.apache.calcite.plan.hep.HepProgramBuilder) Properties(java.util.Properties) Test(org.junit.Test)

Example 13 with Context

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.Context in project beam by apache.

the class CalciteQueryPlanner method defaultConfig.

public FrameworkConfig defaultConfig(JdbcConnection connection, Collection<RuleSet> ruleSets) {
    final CalciteConnectionConfig config = connection.config();
    final SqlParser.ConfigBuilder parserConfig = SqlParser.configBuilder().setQuotedCasing(config.quotedCasing()).setUnquotedCasing(config.unquotedCasing()).setQuoting(config.quoting()).setConformance(config.conformance()).setCaseSensitive(config.caseSensitive());
    final SqlParserImplFactory parserFactory = config.parserFactory(SqlParserImplFactory.class, null);
    if (parserFactory != null) {
        parserConfig.setParserFactory(parserFactory);
    }
    final SchemaPlus schema = connection.getRootSchema();
    final SchemaPlus defaultSchema = connection.getCurrentSchemaPlus();
    final ImmutableList<RelTraitDef> traitDefs = ImmutableList.of(ConventionTraitDef.INSTANCE);
    final CalciteCatalogReader catalogReader = new CalciteCatalogReader(CalciteSchema.from(schema), ImmutableList.of(defaultSchema.getName()), connection.getTypeFactory(), connection.config());
    final SqlOperatorTable opTab0 = connection.config().fun(SqlOperatorTable.class, SqlStdOperatorTable.instance());
    return Frameworks.newConfigBuilder().parserConfig(parserConfig.build()).defaultSchema(defaultSchema).traitDefs(traitDefs).context(Contexts.of(connection.config())).ruleSets(ruleSets.toArray(new RuleSet[0])).costFactory(BeamCostModel.FACTORY).typeSystem(connection.getTypeFactory().getTypeSystem()).operatorTable(SqlOperatorTables.chain(opTab0, catalogReader)).build();
}
Also used : RuleSet(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.RuleSet) CalciteCatalogReader(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.prepare.CalciteCatalogReader) CalciteConnectionConfig(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.config.CalciteConnectionConfig) RelTraitDef(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelTraitDef) SqlOperatorTable(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperatorTable) SqlParser(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParser) SchemaPlus(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus) SqlParserImplFactory(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParserImplFactory)

Example 14 with Context

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.Context 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 15 with Context

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.Context in project beam by apache.

the class ZetaSQLPushDownTest method initializeCalciteEnvironmentWithContext.

private static void initializeCalciteEnvironmentWithContext(Context... extraContext) {
    JdbcConnection jdbcConnection = JdbcDriver.connect(tableProvider, PipelineOptionsFactory.create());
    SchemaPlus defaultSchemaPlus = jdbcConnection.getCurrentSchemaPlus();
    final ImmutableList<RelTraitDef> traitDefs = ImmutableList.of(ConventionTraitDef.INSTANCE);
    Object[] contexts = ImmutableList.<Context>builder().add(Contexts.of(jdbcConnection.config())).add(extraContext).build().toArray();
    config = Frameworks.newConfigBuilder().defaultSchema(defaultSchemaPlus).traitDefs(traitDefs).context(Contexts.of(contexts)).ruleSets(ZetaSQLQueryPlanner.getZetaSqlRuleSets().toArray(new RuleSet[0])).costFactory(BeamCostModel.FACTORY).typeSystem(jdbcConnection.getTypeFactory().getTypeSystem()).build();
}
Also used : Context(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.Context) RuleSet(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.RuleSet) RelTraitDef(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelTraitDef) SchemaPlus(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus) JdbcConnection(org.apache.beam.sdk.extensions.sql.impl.JdbcConnection)

Aggregations

CalciteSchema (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteSchema)5 Context (org.apache.calcite.plan.Context)5 Properties (java.util.Properties)3 BeamCalciteSchema (org.apache.beam.sdk.extensions.sql.impl.BeamCalciteSchema)3 CalciteConnectionConfigImpl (org.apache.calcite.config.CalciteConnectionConfigImpl)3 List (java.util.List)2 JdbcConnection (org.apache.beam.sdk.extensions.sql.impl.JdbcConnection)2 RelTraitDef (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelTraitDef)2 SchemaPlus (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus)2 SqlIdentifier (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier)2 SqlNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode)2 RuleSet (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.RuleSet)2 HepProgram (org.apache.calcite.plan.hep.HepProgram)2 HepProgramBuilder (org.apache.calcite.plan.hep.HepProgramBuilder)2 Test (org.junit.Test)2 AnalyzerOptions (com.google.zetasql.AnalyzerOptions)1 ResolvedQueryStmt (com.google.zetasql.resolvedast.ResolvedNodes.ResolvedQueryStmt)1 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 Method (java.lang.reflect.Method)1