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);
}
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);
}
}
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);
}
}
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;
}
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;
}
Aggregations