Search in sources :

Example 31 with CalciteSchema

use of org.apache.beam.vendor.calcite.v1_28_0.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)

Example 32 with CalciteSchema

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

the class ModelHandler method visit.

public void visit(JsonMaterialization jsonMaterialization) {
    try {
        checkRequiredAttributes(jsonMaterialization, "sql");
        final SchemaPlus schema = currentSchema();
        if (!schema.isMutable()) {
            throw new RuntimeException("Cannot define materialization; parent schema '" + currentSchemaName() + "' is not a SemiMutableSchema");
        }
        CalciteSchema calciteSchema = CalciteSchema.from(schema);
        final String viewName;
        final boolean existing;
        if (jsonMaterialization.view == null) {
            // If the user did not supply a view name, that means the materialized
            // view is pre-populated. Generate a synthetic view name.
            viewName = "$" + schema.getTableNames().size();
            existing = true;
        } else {
            viewName = jsonMaterialization.view;
            existing = false;
        }
        List<String> viewPath = calciteSchema.path(viewName);
        schema.add(viewName, MaterializedViewTable.create(calciteSchema, jsonMaterialization.getSql(), jsonMaterialization.viewSchemaPath, viewPath, jsonMaterialization.table, existing));
    } catch (Exception e) {
        throw new RuntimeException("Error instantiating " + jsonMaterialization, e);
    }
}
Also used : CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) SchemaPlus(org.apache.calcite.schema.SchemaPlus) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 33 with CalciteSchema

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

the class ModelHandler method visit.

public void visit(JsonLattice jsonLattice) {
    try {
        checkRequiredAttributes(jsonLattice, "name", "sql");
        final SchemaPlus schema = currentSchema();
        if (!schema.isMutable()) {
            throw new RuntimeException("Cannot define lattice; parent schema '" + currentSchemaName() + "' is not a SemiMutableSchema");
        }
        CalciteSchema calciteSchema = CalciteSchema.from(schema);
        Lattice.Builder latticeBuilder = Lattice.builder(calciteSchema, jsonLattice.getSql()).auto(jsonLattice.auto).algorithm(jsonLattice.algorithm);
        if (jsonLattice.rowCountEstimate != null) {
            latticeBuilder.rowCountEstimate(jsonLattice.rowCountEstimate);
        }
        if (jsonLattice.statisticProvider != null) {
            latticeBuilder.statisticProvider(jsonLattice.statisticProvider);
        }
        populateLattice(jsonLattice, latticeBuilder);
        schema.add(jsonLattice.name, latticeBuilder.build());
    } catch (Exception e) {
        throw new RuntimeException("Error instantiating " + jsonLattice, e);
    }
}
Also used : CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) SchemaPlus(org.apache.calcite.schema.SchemaPlus) Lattice(org.apache.calcite.materialize.Lattice) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 34 with CalciteSchema

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

the class MaterializationService method defineMaterialization.

/**
 * Defines a new materialization. Returns its key.
 */
public MaterializationKey defineMaterialization(final CalciteSchema schema, TileKey tileKey, String viewSql, List<String> viewSchemaPath, String suggestedTableName, TableFactory tableFactory, boolean create, boolean existing) {
    final MaterializationActor.QueryKey queryKey = new MaterializationActor.QueryKey(viewSql, schema, viewSchemaPath);
    final MaterializationKey existingKey = actor.keyBySql.get(queryKey);
    if (existingKey != null) {
        return existingKey;
    }
    if (!create) {
        return null;
    }
    final CalciteConnection connection = CalciteMetaImpl.connect(schema.root(), null);
    CalciteSchema.TableEntry tableEntry;
    // with the name and if none can be found, lookup a view in the schema
    if (existing) {
        tableEntry = schema.getTable(suggestedTableName, true);
        if (tableEntry == null) {
            tableEntry = schema.getTableBasedOnNullaryFunction(suggestedTableName, true);
        }
    } else {
        tableEntry = null;
    }
    if (tableEntry == null) {
        tableEntry = schema.getTableBySql(viewSql);
    }
    RelDataType rowType = null;
    if (tableEntry == null) {
        Table table = tableFactory.createTable(schema, viewSql, viewSchemaPath);
        final String tableName = Schemas.uniqueTableName(schema, Util.first(suggestedTableName, "m"));
        tableEntry = schema.add(tableName, table, ImmutableList.of(viewSql));
        Hook.CREATE_MATERIALIZATION.run(tableName);
        rowType = table.getRowType(connection.getTypeFactory());
    }
    if (rowType == null) {
        // If we didn't validate the SQL by populating a table, validate it now.
        final CalcitePrepare.ParseResult parse = Schemas.parse(connection, schema, viewSchemaPath, viewSql);
        rowType = parse.rowType;
    }
    final MaterializationKey key = new MaterializationKey();
    final MaterializationActor.Materialization materialization = new MaterializationActor.Materialization(key, schema.root(), tableEntry, viewSql, rowType, viewSchemaPath);
    actor.keyMap.put(materialization.key, materialization);
    actor.keyBySql.put(queryKey, materialization.key);
    if (tileKey != null) {
        actor.keyByTile.put(tileKey, materialization.key);
    }
    return key;
}
Also used : Table(org.apache.calcite.schema.Table) CalcitePrepare(org.apache.calcite.jdbc.CalcitePrepare) RelDataType(org.apache.calcite.rel.type.RelDataType) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection)

Example 35 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 getAllSchemaObjectNames.

public List<SqlMoniker> getAllSchemaObjectNames(List<String> names) {
    final CalciteSchema schema = SqlValidatorUtil.getSchema(rootSchema, names, nameMatcher);
    if (schema == null) {
        return ImmutableList.of();
    }
    final List<SqlMoniker> result = new ArrayList<>();
    // Add root schema if not anonymous
    if (!schema.name.equals("")) {
        result.add(moniker(schema, null, SqlMonikerType.SCHEMA));
    }
    final Map<String, CalciteSchema> schemaMap = schema.getSubSchemaMap();
    for (String subSchema : schemaMap.keySet()) {
        result.add(moniker(schema, subSchema, SqlMonikerType.SCHEMA));
    }
    for (String table : schema.getTableNames()) {
        result.add(moniker(schema, table, SqlMonikerType.TABLE));
    }
    final NavigableSet<String> functions = schema.getFunctionNames();
    for (String function : functions) {
        // views are here as well
        result.add(moniker(schema, function, SqlMonikerType.FUNCTION));
    }
    return result;
}
Also used : SqlMoniker(org.apache.calcite.sql.validate.SqlMoniker) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) ArrayList(java.util.ArrayList)

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