Search in sources :

Example 31 with CalciteSchema

use of 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 32 with CalciteSchema

use of 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 33 with CalciteSchema

use of 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 34 with CalciteSchema

use of 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)

Example 35 with CalciteSchema

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

the class CalciteCatalogReader method getFunctionsFrom.

private Collection<Function> getFunctionsFrom(List<String> names) {
    final List<Function> functions2 = Lists.newArrayList();
    final List<List<String>> schemaNameList = new ArrayList<>();
    if (names.size() > 1) {
        // the last 2 items in the path.
        if (schemaPaths.size() > 1) {
            schemaNameList.addAll(Util.skip(schemaPaths));
        } else {
            schemaNameList.addAll(schemaPaths);
        }
    } else {
        for (List<String> schemaPath : schemaPaths) {
            CalciteSchema schema = SqlValidatorUtil.getSchema(rootSchema, schemaPath, nameMatcher);
            if (schema != null) {
                schemaNameList.addAll(schema.getPath());
            }
        }
    }
    for (List<String> schemaNames : schemaNameList) {
        CalciteSchema schema = SqlValidatorUtil.getSchema(rootSchema, Iterables.concat(schemaNames, Util.skipLast(names)), nameMatcher);
        if (schema != null) {
            final String name = Util.last(names);
            functions2.addAll(schema.getFunctions(name, true));
        }
    }
    return functions2;
}
Also used : 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) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList)

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