Search in sources :

Example 1 with Context

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

the class PlannerContext method createRelBuilder.

/**
 * Creates a configured {@link FlinkRelBuilder} for a planning session.
 *
 * @param currentCatalog the current default catalog to look for first during planning.
 * @param currentDatabase the current default database to look for first during planning.
 * @return configured rel builder
 */
public FlinkRelBuilder createRelBuilder(String currentCatalog, String currentDatabase) {
    FlinkCalciteCatalogReader relOptSchema = createCatalogReader(false, currentCatalog, currentDatabase);
    Context chain = Contexts.of(context, // Sets up the ViewExpander explicitly for FlinkRelBuilder.
    createFlinkPlanner(currentCatalog, currentDatabase).createToRelContext());
    return new FlinkRelBuilder(chain, cluster, relOptSchema);
}
Also used : Context(org.apache.calcite.plan.Context) FlinkContext(org.apache.flink.table.planner.calcite.FlinkContext) FlinkRelBuilder(org.apache.flink.table.planner.calcite.FlinkRelBuilder) FlinkCalciteCatalogReader(org.apache.flink.table.planner.plan.FlinkCalciteCatalogReader)

Example 2 with Context

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

the class SqlCreateFunction method execute.

@Override
public void execute(CalcitePrepare.Context context) {
    final Pair<CalciteSchema, String> pair = SqlDdlNodes.schema(context, true, functionName);
    SchemaPlus schema = pair.left.plus();
    String lastName = pair.right;
    if (!schema.getFunctions(lastName).isEmpty()) {
        throw SqlUtil.newContextException(functionName.getParserPosition(), RESOURCE.internal(String.format("Function %s is already defined.", lastName)));
    }
    JavaUdfLoader udfLoader = new JavaUdfLoader();
    // TODO(BEAM-12355) Support qualified function names.
    List<String> functionPath = ImmutableList.of(lastName);
    if (!(jarPath instanceof SqlCharStringLiteral)) {
        throw SqlUtil.newContextException(jarPath.getParserPosition(), RESOURCE.internal("Jar path is not instanceof SqlCharStringLiteral."));
    }
    String unquotedJarPath = ((SqlCharStringLiteral) jarPath).getNlsString().getValue();
    if (isAggregate) {
        // Try loading the aggregate function just to make sure it exists. LazyAggregateCombineFn will
        // need to fetch it again at runtime.
        udfLoader.loadAggregateFunction(functionPath, unquotedJarPath);
        LazyAggregateCombineFn<?, ?, ?> combineFn = new LazyAggregateCombineFn<>(functionPath, unquotedJarPath);
        schema.add(lastName, combineFn.getUdafImpl());
    } else {
        ScalarFn scalarFn = udfLoader.loadScalarFunction(functionPath, unquotedJarPath);
        Method method = ScalarFnReflector.getApplyMethod(scalarFn);
        Function function = ScalarFunctionImpl.create(method, unquotedJarPath);
        schema.add(lastName, function);
    }
}
Also used : Function(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Function) ScalarFn(org.apache.beam.sdk.extensions.sql.udf.ScalarFn) CalciteSchema(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteSchema) SchemaPlus(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus) LazyAggregateCombineFn(org.apache.beam.sdk.extensions.sql.impl.LazyAggregateCombineFn) JavaUdfLoader(org.apache.beam.sdk.extensions.sql.impl.JavaUdfLoader) SqlCharStringLiteral(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlCharStringLiteral) Method(java.lang.reflect.Method)

Example 3 with Context

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

the class SqlDropObject method execute.

@Override
public void execute(CalcitePrepare.Context context) {
    final List<String> path = context.getDefaultSchemaPath();
    CalciteSchema schema = 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));
        }
    }
    final boolean existed;
    switch(getKind()) {
        case DROP_TABLE:
            if (schema.schema instanceof BeamCalciteSchema) {
                BeamCalciteSchema beamSchema = (BeamCalciteSchema) schema.schema;
                beamSchema.getTableProvider().dropTable(name.getSimple());
                existed = true;
            } else {
                existed = schema.removeTable(name.getSimple());
            }
            if (!existed && !ifExists) {
                throw SqlUtil.newContextException(name.getParserPosition(), RESOURCE.tableNotFound(name.getSimple()));
            }
            break;
        default:
            throw new AssertionError(getKind());
    }
}
Also used : BeamCalciteSchema(org.apache.beam.sdk.extensions.sql.impl.BeamCalciteSchema) CalciteSchema(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteSchema) BeamCalciteSchema(org.apache.beam.sdk.extensions.sql.impl.BeamCalciteSchema)

Example 4 with Context

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

the class SqlSetOptionBeam method execute.

@Override
public void execute(CalcitePrepare.Context context) {
    final SqlIdentifier name = getName();
    final SqlNode value = getValue();
    final Pair<CalciteSchema, String> pair = SqlDdlNodes.schema(context, true, name);
    if (!(pair.left.schema instanceof BeamCalciteSchema)) {
        throw SqlUtil.newContextException(name.getParserPosition(), RESOURCE.internal("Schema is not instanceof BeamCalciteSchema"));
    }
    BeamCalciteSchema schema = (BeamCalciteSchema) pair.left.schema;
    if (value != null) {
        schema.setPipelineOption(pair.right, SqlDdlNodes.getString(value));
    } else if ("ALL".equals(pair.right)) {
        schema.removeAllPipelineOptions();
    } else {
        schema.removePipelineOption(pair.right);
    }
}
Also used : CalciteSchema(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteSchema) BeamCalciteSchema(org.apache.beam.sdk.extensions.sql.impl.BeamCalciteSchema) SqlIdentifier(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier) BeamCalciteSchema(org.apache.beam.sdk.extensions.sql.impl.BeamCalciteSchema) SqlNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode)

Example 5 with Context

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

the class SqlCreateExternalTable method execute.

@Override
public void execute(CalcitePrepare.Context context) {
    final Pair<CalciteSchema, String> pair = SqlDdlNodes.schema(context, true, name);
    if (pair.left.plus().getTable(pair.right) != null) {
        // Table exists.
        if (!ifNotExists) {
            // They did not specify IF NOT EXISTS, so give error.
            throw SqlUtil.newContextException(name.getParserPosition(), RESOURCE.tableExists(pair.right));
        }
        return;
    }
    // Table does not exist. Create it.
    if (!(pair.left.schema instanceof BeamCalciteSchema)) {
        throw SqlUtil.newContextException(name.getParserPosition(), RESOURCE.internal("Schema is not instanceof BeamCalciteSchema"));
    }
    BeamCalciteSchema schema = (BeamCalciteSchema) pair.left.schema;
    schema.getTableProvider().createTable(toTable());
}
Also used : BeamCalciteSchema(org.apache.beam.sdk.extensions.sql.impl.BeamCalciteSchema) CalciteSchema(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteSchema) BeamCalciteSchema(org.apache.beam.sdk.extensions.sql.impl.BeamCalciteSchema)

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