Search in sources :

Example 6 with Context

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

the class SqlDdlNodes method schema.

/**
 * Returns the schema in which to create an object.
 */
static Pair<CalciteSchema, String> schema(CalcitePrepare.Context context, boolean mutable, SqlIdentifier id) {
    final List<String> path;
    if (id.isSimple()) {
        path = context.getDefaultSchemaPath();
    } else {
        path = Util.skipLast(id.names);
    }
    CalciteSchema schema = mutable ? context.getMutableRootSchema() : context.getRootSchema();
    for (String p : path) {
        schema = schema.getSubSchema(p, true);
        if (schema == null) {
            throw new AssertionError(String.format("Got null sub-schema for path '%s' in %s", p, path));
        }
    }
    return Pair.of(schema, name(id));
}
Also used : CalciteSchema(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteSchema) NlsString(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.NlsString)

Example 7 with Context

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

the class BigQueryTable method generateRowRestrictions.

private String generateRowRestrictions(Schema schema, List<RexNode> supported) {
    assert !supported.isEmpty();
    final IntFunction<SqlNode> field = i -> new SqlIdentifier(schema.getField(i).getName(), SqlParserPos.ZERO);
    // TODO: BigQuerySqlDialectWithTypeTranslation can be replaced with BigQuerySqlDialect after
    // updating vendor Calcite version.
    SqlImplementor.Context context = new BeamSqlUnparseContext(field);
    // Create a single SqlNode from a list of RexNodes
    SqlNode andSqlNode = null;
    for (RexNode node : supported) {
        SqlNode sqlNode = context.toSql(null, node);
        if (andSqlNode == null) {
            andSqlNode = sqlNode;
            continue;
        }
        // AND operator must have exactly 2 operands.
        andSqlNode = SqlStdOperatorTable.AND.createCall(SqlParserPos.ZERO, ImmutableList.of(andSqlNode, sqlNode));
    }
    return andSqlNode.toSqlString(BeamBigQuerySqlDialect.DEFAULT).getSql();
}
Also used : Arrays(java.util.Arrays) PBegin(org.apache.beam.sdk.values.PBegin) Experimental(org.apache.beam.sdk.annotations.Experimental) RexNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode) SqlIdentifier(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier) LoggerFactory(org.slf4j.LoggerFactory) ConversionOptions(org.apache.beam.sdk.io.gcp.bigquery.BigQueryUtils.ConversionOptions) BigQueryOptions(org.apache.beam.sdk.io.gcp.bigquery.BigQueryOptions) SqlImplementor(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.rel2sql.SqlImplementor) SqlParserPos(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParserPos) SelectHelpers(org.apache.beam.sdk.schemas.utils.SelectHelpers) DefaultTableFilter(org.apache.beam.sdk.extensions.sql.meta.DefaultTableFilter) ProjectSupport(org.apache.beam.sdk.extensions.sql.meta.ProjectSupport) Table(org.apache.beam.sdk.extensions.sql.meta.Table) FieldAccessDescriptor(org.apache.beam.sdk.schemas.FieldAccessDescriptor) BigInteger(java.math.BigInteger) Row(org.apache.beam.sdk.values.Row) PipelineOptions(org.apache.beam.sdk.options.PipelineOptions) SqlNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode) IntFunction(java.util.function.IntFunction) SchemaBaseBeamTable(org.apache.beam.sdk.extensions.sql.meta.SchemaBaseBeamTable) BigQueryHelpers(org.apache.beam.sdk.io.gcp.bigquery.BigQueryHelpers) Logger(org.slf4j.Logger) BigQueryIO(org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO) SchemaCoder(org.apache.beam.sdk.schemas.SchemaCoder) BeamSqlTableFilter(org.apache.beam.sdk.extensions.sql.meta.BeamSqlTableFilter) SqlStdOperatorTable(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.fun.SqlStdOperatorTable) IOException(java.io.IOException) Method(org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.TypedRead.Method) BigQueryUtils(org.apache.beam.sdk.io.gcp.bigquery.BigQueryUtils) PCollection(org.apache.beam.sdk.values.PCollection) Collectors(java.util.stream.Collectors) Schema(org.apache.beam.sdk.schemas.Schema) Serializable(java.io.Serializable) TypedRead(org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.TypedRead) VisibleForTesting(org.apache.beam.vendor.calcite.v1_28_0.com.google.common.annotations.VisibleForTesting) POutput(org.apache.beam.sdk.values.POutput) List(java.util.List) WriteDisposition(org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.Write.WriteDisposition) BeamTableStatistics(org.apache.beam.sdk.extensions.sql.impl.BeamTableStatistics) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) SqlIdentifier(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier) SqlImplementor(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.rel2sql.SqlImplementor) SqlNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode) RexNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode)

Example 8 with Context

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

the class ZetaSqlTestBase method initialize.

protected void initialize() {
    JdbcConnection jdbcConnection = JdbcDriver.connect(createBeamTableProvider(), PipelineOptionsFactory.create());
    this.config = Frameworks.newConfigBuilder().defaultSchema(jdbcConnection.getCurrentSchemaPlus()).traitDefs(ImmutableList.of(ConventionTraitDef.INSTANCE)).context(Contexts.of(jdbcConnection.config())).ruleSets(ZetaSQLQueryPlanner.getZetaSqlRuleSets().toArray(new RuleSet[0])).costFactory(BeamCostModel.FACTORY).typeSystem(jdbcConnection.getTypeFactory().getTypeSystem()).build();
}
Also used : RuleSet(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.RuleSet) JdbcConnection(org.apache.beam.sdk.extensions.sql.impl.JdbcConnection)

Example 9 with Context

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

the class HiveRelMdRuntimeRowCount method getRuntimeRowCount.

public Optional<Long> getRuntimeRowCount(RelNode rel) {
    RelOptCluster cluster = rel.getCluster();
    Context context = cluster.getPlanner().getContext();
    if (context instanceof HivePlannerContext) {
        StatsSource ss = ((HivePlannerContext) context).unwrap(StatsSource.class);
        if (ss.canProvideStatsFor(rel.getClass())) {
            Optional<OperatorStats> os = ss.lookup(RelTreeSignature.of(rel));
            if (os.isPresent()) {
                long outputRecords = os.get().getOutputRecords();
                return Optional.of(outputRecords);
            }
        }
    }
    return Optional.empty();
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) Context(org.apache.calcite.plan.Context) HivePlannerContext(org.apache.hadoop.hive.ql.optimizer.calcite.HivePlannerContext) HivePlannerContext(org.apache.hadoop.hive.ql.optimizer.calcite.HivePlannerContext) OperatorStats(org.apache.hadoop.hive.ql.stats.OperatorStats) StatsSource(org.apache.hadoop.hive.ql.plan.mapper.StatsSource)

Example 10 with Context

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

the class PlannerImpl method createCatalogReader.

// CalciteCatalogReader is stateless; no need to store one
private CalciteCatalogReader createCatalogReader() {
    final SchemaPlus rootSchema = rootSchema(defaultSchema);
    final Context context = config.getContext();
    final CalciteConnectionConfig connectionConfig;
    if (context != null) {
        connectionConfig = context.unwrap(CalciteConnectionConfig.class);
    } else {
        Properties properties = new Properties();
        properties.setProperty(CalciteConnectionProperty.CASE_SENSITIVE.camelName(), String.valueOf(parserConfig.caseSensitive()));
        connectionConfig = new CalciteConnectionConfigImpl(properties);
    }
    return new CalciteCatalogReader(CalciteSchema.from(rootSchema), CalciteSchema.from(defaultSchema).path(null), typeFactory, connectionConfig);
}
Also used : Context(org.apache.calcite.plan.Context) CalciteConnectionConfigImpl(org.apache.calcite.config.CalciteConnectionConfigImpl) CalciteConnectionConfig(org.apache.calcite.config.CalciteConnectionConfig) SchemaPlus(org.apache.calcite.schema.SchemaPlus) Properties(java.util.Properties)

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