Search in sources :

Example 26 with CalciteSchema

use of org.apache.calcite.jdbc.CalciteSchema in project calcite by apache.

the class EmptyScope method resolve_.

private void resolve_(final CalciteSchema rootSchema, List<String> names, List<String> schemaNames, SqlNameMatcher nameMatcher, Path path, Resolved resolved) {
    final List<String> concat = ImmutableList.<String>builder().addAll(schemaNames).addAll(names).build();
    CalciteSchema schema = rootSchema;
    SqlValidatorNamespace namespace = null;
    List<String> remainingNames = concat;
    for (String schemaName : concat) {
        if (schema == rootSchema && nameMatcher.matches(schemaName, schema.name)) {
            remainingNames = Util.skip(remainingNames);
            continue;
        }
        final CalciteSchema subSchema = schema.getSubSchema(schemaName, nameMatcher.isCaseSensitive());
        if (subSchema != null) {
            path = path.plus(null, -1, subSchema.name, StructKind.NONE);
            remainingNames = Util.skip(remainingNames);
            schema = subSchema;
            namespace = new SchemaNamespace(validator, ImmutableList.copyOf(path.stepNames()));
            continue;
        }
        CalciteSchema.TableEntry entry = schema.getTable(schemaName, nameMatcher.isCaseSensitive());
        if (entry == null) {
            entry = schema.getTableBasedOnNullaryFunction(schemaName, nameMatcher.isCaseSensitive());
        }
        if (entry != null) {
            path = path.plus(null, -1, entry.name, StructKind.NONE);
            remainingNames = Util.skip(remainingNames);
            final Table table = entry.getTable();
            SqlValidatorTable table2 = null;
            if (table instanceof Wrapper) {
                table2 = ((Wrapper) table).unwrap(Prepare.PreparingTable.class);
            }
            if (table2 == null) {
                final RelOptSchema relOptSchema = validator.catalogReader.unwrap(RelOptSchema.class);
                final RelDataType rowType = table.getRowType(validator.typeFactory);
                table2 = RelOptTableImpl.create(relOptSchema, rowType, entry, null);
            }
            namespace = new TableNamespace(validator, table2);
            resolved.found(namespace, false, this, path, remainingNames);
            return;
        }
        // neither sub-schema nor table
        if (namespace != null && !remainingNames.equals(names)) {
            resolved.found(namespace, false, this, path, remainingNames);
        }
        return;
    }
}
Also used : Wrapper(org.apache.calcite.schema.Wrapper) Table(org.apache.calcite.schema.Table) RelOptSchema(org.apache.calcite.plan.RelOptSchema) RelDataType(org.apache.calcite.rel.type.RelDataType) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema)

Example 27 with CalciteSchema

use of org.apache.calcite.jdbc.CalciteSchema in project calcite by apache.

the class SqlDropSchema 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 = schema.removeSubSchema(name.getSimple());
    if (!existed && !ifExists) {
        throw SqlUtil.newContextException(name.getParserPosition(), RESOURCE.schemaNotFound(name.getSimple()));
    }
}
Also used : CalciteSchema(org.apache.calcite.jdbc.CalciteSchema)

Example 28 with CalciteSchema

use of org.apache.calcite.jdbc.CalciteSchema in project calcite by apache.

the class SqlCreateForeignSchema method execute.

public void execute(CalcitePrepare.Context context) {
    final Pair<CalciteSchema, String> pair = SqlDdlNodes.schema(context, true, name);
    final SchemaPlus subSchema0 = pair.left.plus().getSubSchema(pair.right);
    if (subSchema0 != null) {
        if (!getReplace() && !ifNotExists) {
            throw SqlUtil.newContextException(name.getParserPosition(), RESOURCE.schemaExists(pair.right));
        }
    }
    final Schema subSchema;
    final String libraryName;
    if (type != null) {
        Preconditions.checkArgument(library == null);
        final String typeName = (String) value(this.type);
        final JsonSchema.Type type = Util.enumVal(JsonSchema.Type.class, typeName.toUpperCase(Locale.ROOT));
        if (type != null) {
            switch(type) {
                case JDBC:
                    libraryName = JdbcSchema.Factory.class.getName();
                    break;
                default:
                    libraryName = null;
            }
        } else {
            libraryName = null;
        }
        if (libraryName == null) {
            throw SqlUtil.newContextException(this.type.getParserPosition(), RESOURCE.schemaInvalidType(typeName, Arrays.toString(JsonSchema.Type.values())));
        }
    } else {
        Preconditions.checkArgument(library != null);
        libraryName = (String) value(library);
    }
    final SchemaFactory schemaFactory = AvaticaUtils.instantiatePlugin(SchemaFactory.class, libraryName);
    final Map<String, Object> operandMap = new LinkedHashMap<>();
    for (Pair<SqlIdentifier, SqlNode> option : options(optionList)) {
        operandMap.put(option.left.getSimple(), value(option.right));
    }
    subSchema = schemaFactory.create(pair.left.plus(), pair.right, operandMap);
    pair.left.add(pair.right, subSchema);
}
Also used : SchemaFactory(org.apache.calcite.schema.SchemaFactory) JdbcSchema(org.apache.calcite.adapter.jdbc.JdbcSchema) Schema(org.apache.calcite.schema.Schema) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) JsonSchema(org.apache.calcite.model.JsonSchema) JsonSchema(org.apache.calcite.model.JsonSchema) SchemaPlus(org.apache.calcite.schema.SchemaPlus) SchemaFactory(org.apache.calcite.schema.SchemaFactory) NlsString(org.apache.calcite.util.NlsString) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) LinkedHashMap(java.util.LinkedHashMap) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) SqlNode(org.apache.calcite.sql.SqlNode)

Example 29 with CalciteSchema

use of org.apache.calcite.jdbc.CalciteSchema in project calcite by apache.

the class SqlCreateView method execute.

public void execute(CalcitePrepare.Context context) {
    final Pair<CalciteSchema, String> pair = SqlDdlNodes.schema(context, true, name);
    final SchemaPlus schemaPlus = pair.left.plus();
    for (Function function : schemaPlus.getFunctions(pair.right)) {
        if (function.getParameters().isEmpty()) {
            if (!getReplace()) {
                throw SqlUtil.newContextException(name.getParserPosition(), RESOURCE.viewExists(pair.right));
            }
            pair.left.removeFunction(pair.right);
        }
    }
    final SqlNode q = SqlDdlNodes.renameColumns(columnList, query);
    final String sql = q.toSqlString(CalciteSqlDialect.DEFAULT).getSql();
    final ViewTableMacro viewTableMacro = ViewTable.viewMacro(schemaPlus, sql, pair.left.path(null), context.getObjectPath(), false);
    final TranslatableTable x = viewTableMacro.apply(ImmutableList.of());
    Util.discard(x);
    schemaPlus.add(pair.right, viewTableMacro);
}
Also used : Function(org.apache.calcite.schema.Function) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) ViewTableMacro(org.apache.calcite.schema.impl.ViewTableMacro) SchemaPlus(org.apache.calcite.schema.SchemaPlus) TranslatableTable(org.apache.calcite.schema.TranslatableTable) SqlNode(org.apache.calcite.sql.SqlNode)

Example 30 with CalciteSchema

use of org.apache.calcite.jdbc.CalciteSchema in project calcite by apache.

the class RelOptLattice method getAggregate.

/**
 * Retrieves a materialized table that will satisfy an aggregate query on
 * the star table.
 *
 * <p>The current implementation creates a materialization and populates it,
 * provided that {@link Lattice#auto} is true.
 *
 * <p>Future implementations might return materializations at a different
 * level of aggregation, from which the desired result can be obtained by
 * rolling up.
 *
 * @param planner Current planner
 * @param groupSet Grouping key
 * @param measureList Calls to aggregate functions
 * @return Materialized table
 */
public Pair<CalciteSchema.TableEntry, TileKey> getAggregate(RelOptPlanner planner, ImmutableBitSet groupSet, List<Lattice.Measure> measureList) {
    final CalciteConnectionConfig config = planner.getContext().unwrap(CalciteConnectionConfig.class);
    if (config == null) {
        return null;
    }
    final MaterializationService service = MaterializationService.instance();
    boolean create = lattice.auto && config.createMaterializations();
    final CalciteSchema schema = starRelOptTable.unwrap(CalciteSchema.class);
    return service.defineTile(lattice, groupSet, measureList, schema, create, false);
}
Also used : CalciteConnectionConfig(org.apache.calcite.config.CalciteConnectionConfig) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) MaterializationService(org.apache.calcite.materialize.MaterializationService)

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 Schema (org.apache.calcite.schema.Schema)4 Table (org.apache.calcite.schema.Table)4 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)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 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 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 JdbcSchema (org.apache.calcite.adapter.jdbc.JdbcSchema)2 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)2