Search in sources :

Example 11 with CalciteSchema

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteSchema in project calcite by apache.

the class SqlDropObject method execute.

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);
    }
    final boolean existed;
    switch(getKind()) {
        case DROP_TABLE:
        case DROP_MATERIALIZED_VIEW:
            existed = schema.removeTable(name.getSimple());
            if (!existed && !ifExists) {
                throw SqlUtil.newContextException(name.getParserPosition(), RESOURCE.tableNotFound(name.getSimple()));
            }
            break;
        case DROP_VIEW:
            // Not quite right: removes any other functions with the same name
            existed = schema.removeFunction(name.getSimple());
            if (!existed && !ifExists) {
                throw SqlUtil.newContextException(name.getParserPosition(), RESOURCE.viewNotFound(name.getSimple()));
            }
            break;
        default:
            throw new AssertionError(getKind());
    }
}
Also used : CalciteSchema(org.apache.calcite.jdbc.CalciteSchema)

Example 12 with CalciteSchema

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteSchema in project calcite by apache.

the class CalciteCatalogReader method operatorTable.

/**
 * Creates an operator table that contains functions in the given class.
 *
 * @see ModelHandler#addFunctions
 */
public static SqlOperatorTable operatorTable(String className) {
    // Dummy schema to collect the functions
    final CalciteSchema schema = CalciteSchema.createRootSchema(false, false);
    ModelHandler.addFunctions(schema.plus(), null, ImmutableList.<String>of(), className, "*", true);
    // The following is technical debt; see [CALCITE-2082] Remove
    // RelDataTypeFactory argument from SqlUserDefinedAggFunction constructor
    final SqlTypeFactoryImpl typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
    final ListSqlOperatorTable table = new ListSqlOperatorTable();
    for (String name : schema.getFunctionNames()) {
        for (Function function : schema.getFunctions(name, true)) {
            final SqlIdentifier id = new SqlIdentifier(name, SqlParserPos.ZERO);
            table.add(toOp(typeFactory, id, function));
        }
    }
    return table;
}
Also used : ListSqlOperatorTable(org.apache.calcite.sql.util.ListSqlOperatorTable) SqlUserDefinedFunction(org.apache.calcite.sql.validate.SqlUserDefinedFunction) TableFunction(org.apache.calcite.schema.TableFunction) SqlUserDefinedAggFunction(org.apache.calcite.sql.validate.SqlUserDefinedAggFunction) Function(org.apache.calcite.schema.Function) AggregateFunction(org.apache.calcite.schema.AggregateFunction) SqlUserDefinedTableFunction(org.apache.calcite.sql.validate.SqlUserDefinedTableFunction) ScalarFunction(org.apache.calcite.schema.ScalarFunction) SqlTypeFactoryImpl(org.apache.calcite.sql.type.SqlTypeFactoryImpl) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier)

Example 13 with CalciteSchema

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteSchema in project calcite by apache.

the class CalciteMaterializer method useStar.

/**
 * Converts a relational expression to use a
 * {@link org.apache.calcite.schema.impl.StarTable} defined in {@code schema}.
 * Uses the first star table that fits.
 */
private Iterable<Callback> useStar(CalciteSchema schema, RelNode queryRel) {
    List<CalciteSchema.TableEntry> starTables = Schemas.getStarTables(schema.root());
    if (starTables.isEmpty()) {
        // Don't waste effort converting to leaf-join form.
        return ImmutableList.of();
    }
    final List<Callback> list = Lists.newArrayList();
    final RelNode rel2 = RelOptMaterialization.toLeafJoinForm(queryRel);
    for (CalciteSchema.TableEntry starTable : starTables) {
        final Table table = starTable.getTable();
        assert table instanceof StarTable;
        RelOptTableImpl starRelOptTable = RelOptTableImpl.create(catalogReader, table.getRowType(typeFactory), starTable, null);
        final RelNode rel3 = RelOptMaterialization.tryUseStar(rel2, starRelOptTable);
        if (rel3 != null) {
            list.add(new Callback(rel3, starTable, starRelOptTable));
        }
    }
    return list;
}
Also used : RelOptTable(org.apache.calcite.plan.RelOptTable) SqlRexConvertletTable(org.apache.calcite.sql2rel.SqlRexConvertletTable) Table(org.apache.calcite.schema.Table) StarTable(org.apache.calcite.schema.impl.StarTable) RelNode(org.apache.calcite.rel.RelNode) StarTable(org.apache.calcite.schema.impl.StarTable) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema)

Example 14 with CalciteSchema

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteSchema in project calcite by apache.

the class CalcitePrepareImpl method populateMaterializations.

protected void populateMaterializations(Context context, RelOptPlanner planner, Prepare.Materialization materialization) {
    // not here?
    try {
        final CalciteSchema schema = materialization.materializedTable.schema;
        CalciteCatalogReader catalogReader = new CalciteCatalogReader(schema.root(), materialization.viewSchemaPath, context.getTypeFactory(), context.config());
        final CalciteMaterializer materializer = new CalciteMaterializer(this, context, catalogReader, schema, planner, createConvertletTable());
        materializer.populate(materialization);
    } catch (Exception e) {
        throw new RuntimeException("While populating materialization " + materialization.materializedTable.path(), e);
    }
}
Also used : CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) SqlParseException(org.apache.calcite.sql.parser.SqlParseException)

Example 15 with CalciteSchema

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteSchema in project calcite by apache.

the class CalcitePrepareImpl method perform.

/**
 * Executes a prepare action.
 */
public <R> R perform(CalciteServerStatement statement, Frameworks.PrepareAction<R> action) {
    final CalcitePrepare.Context prepareContext = statement.createPrepareContext();
    final JavaTypeFactory typeFactory = prepareContext.getTypeFactory();
    final CalciteSchema schema = action.getConfig().getDefaultSchema() != null ? CalciteSchema.from(action.getConfig().getDefaultSchema()) : prepareContext.getRootSchema();
    CalciteCatalogReader catalogReader = new CalciteCatalogReader(schema.root(), schema.path(null), typeFactory, prepareContext.config());
    final RexBuilder rexBuilder = new RexBuilder(typeFactory);
    final RelOptPlanner planner = createPlanner(prepareContext, action.getConfig().getContext(), action.getConfig().getCostFactory());
    final RelOptCluster cluster = createCluster(planner, rexBuilder);
    return action.apply(cluster, catalogReader, prepareContext.getRootSchema().plus(), statement);
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) CalcitePrepare(org.apache.calcite.jdbc.CalcitePrepare) RexBuilder(org.apache.calcite.rex.RexBuilder) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner)

Aggregations

CalciteSchema (org.apache.calcite.jdbc.CalciteSchema)31 SchemaPlus (org.apache.calcite.schema.SchemaPlus)8 CalciteSchema (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteSchema)6 ArrayList (java.util.ArrayList)5 RelDataType (org.apache.calcite.rel.type.RelDataType)5 Test (org.junit.Test)5 Method (java.lang.reflect.Method)4 SchemaPlus (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus)4 Schema (org.apache.calcite.schema.Schema)4 Table (org.apache.calcite.schema.Table)4 SqlNode (org.apache.calcite.sql.SqlNode)4 ImmutableList (com.google.common.collect.ImmutableList)3 BeamCalciteSchema (org.apache.beam.sdk.extensions.sql.impl.BeamCalciteSchema)3 JdbcConnection (org.apache.beam.sdk.extensions.sql.impl.JdbcConnection)3 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)3 TableFunction (org.apache.calcite.schema.TableFunction)3 TranslatableTable (org.apache.calcite.schema.TranslatableTable)3 AbstractSchema (org.apache.calcite.schema.impl.AbstractSchema)3 ViewTableMacro (org.apache.calcite.schema.impl.ViewTableMacro)3 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)3