use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Table in project calcite by apache.
the class AggregateStarTableRule method apply.
protected void apply(RelOptRuleCall call, Project postProject, final Aggregate aggregate, StarTable.StarTableScan scan) {
final RelOptCluster cluster = scan.getCluster();
final RelOptTable table = scan.getTable();
final RelOptLattice lattice = call.getPlanner().getLattice(table);
final List<Lattice.Measure> measures = lattice.lattice.toMeasures(aggregate.getAggCallList());
final Pair<CalciteSchema.TableEntry, TileKey> pair = lattice.getAggregate(call.getPlanner(), aggregate.getGroupSet(), measures);
if (pair == null) {
return;
}
final RelBuilder relBuilder = call.builder();
final CalciteSchema.TableEntry tableEntry = pair.left;
final TileKey tileKey = pair.right;
final RelMetadataQuery mq = call.getMetadataQuery();
final double rowCount = aggregate.estimateRowCount(mq);
final Table aggregateTable = tableEntry.getTable();
final RelDataType aggregateTableRowType = aggregateTable.getRowType(cluster.getTypeFactory());
final RelOptTable aggregateRelOptTable = RelOptTableImpl.create(table.getRelOptSchema(), aggregateTableRowType, tableEntry, rowCount);
relBuilder.push(aggregateRelOptTable.toRel(RelOptUtil.getContext(cluster)));
if (tileKey == null) {
if (CalcitePrepareImpl.DEBUG) {
System.out.println("Using materialization " + aggregateRelOptTable.getQualifiedName() + " (exact match)");
}
} else if (!tileKey.dimensions.equals(aggregate.getGroupSet())) {
// Aggregate has finer granularity than we need. Roll up.
if (CalcitePrepareImpl.DEBUG) {
System.out.println("Using materialization " + aggregateRelOptTable.getQualifiedName() + ", rolling up " + tileKey.dimensions + " to " + aggregate.getGroupSet());
}
assert tileKey.dimensions.contains(aggregate.getGroupSet());
final List<AggregateCall> aggCalls = Lists.newArrayList();
ImmutableBitSet.Builder groupSet = ImmutableBitSet.builder();
for (int key : aggregate.getGroupSet()) {
groupSet.set(tileKey.dimensions.indexOf(key));
}
for (AggregateCall aggCall : aggregate.getAggCallList()) {
final AggregateCall copy = rollUp(groupSet.cardinality(), relBuilder, aggCall, tileKey);
if (copy == null) {
return;
}
aggCalls.add(copy);
}
relBuilder.push(aggregate.copy(aggregate.getTraitSet(), relBuilder.build(), false, groupSet.build(), null, aggCalls));
} else if (!tileKey.measures.equals(measures)) {
if (CalcitePrepareImpl.DEBUG) {
System.out.println("Using materialization " + aggregateRelOptTable.getQualifiedName() + ", right granularity, but different measures " + aggregate.getAggCallList());
}
relBuilder.project(relBuilder.fields(new AbstractSourceMapping(tileKey.dimensions.cardinality() + tileKey.measures.size(), aggregate.getRowType().getFieldCount()) {
public int getSourceOpt(int source) {
assert aggregate.getIndicatorCount() == 0;
if (source < aggregate.getGroupCount()) {
int in = tileKey.dimensions.nth(source);
return aggregate.getGroupSet().indexOf(in);
}
Lattice.Measure measure = measures.get(source - aggregate.getGroupCount());
int i = tileKey.measures.indexOf(measure);
assert i >= 0;
return tileKey.dimensions.cardinality() + i;
}
}.inverse()));
}
if (postProject != null) {
relBuilder.push(postProject.copy(postProject.getTraitSet(), ImmutableList.of(relBuilder.peek())));
}
call.transformTo(relBuilder.build());
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Table in project calcite by apache.
the class SqlValidatorImpl method isRolledUpColumn.
// Returns true iff the given column is actually rolled up.
private boolean isRolledUpColumn(SqlIdentifier identifier, SqlValidatorScope scope) {
Pair<String, String> pair = findTableColumnPair(identifier, scope);
if (pair == null) {
return false;
}
String tableAlias = pair.left;
String columnName = pair.right;
Table table = findTable(tableAlias);
if (table != null) {
return table.isRolledUp(columnName);
}
return false;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Table in project calcite by apache.
the class SqlValidatorImpl method isRolledUpColumnAllowedInAgg.
// Returns true iff the given column is valid inside the given aggCall.
private boolean isRolledUpColumnAllowedInAgg(SqlIdentifier identifier, SqlValidatorScope scope, SqlCall aggCall, SqlNode parent) {
Pair<String, String> pair = findTableColumnPair(identifier, scope);
if (pair == null) {
return true;
}
String tableAlias = pair.left;
String columnName = pair.right;
Table table = findTable(tableAlias);
if (table != null) {
return table.rolledUpColumnValidInsideAgg(columnName, aggCall, parent, catalogReader.getConfig());
}
return true;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Table in project calcite by apache.
the class SqlDropMaterializedView method execute.
@Override
public void execute(CalcitePrepare.Context context) {
final Pair<CalciteSchema, String> pair = SqlDdlNodes.schema(context, true, name);
final Table table = pair.left.plus().getTable(pair.right);
if (table != null) {
// Materialized view exists.
super.execute(context);
if (table instanceof Wrapper) {
final MaterializationKey materializationKey = ((Wrapper) table).unwrap(MaterializationKey.class);
if (materializationKey != null) {
MaterializationService.instance().removeMaterialization(materializationKey);
}
}
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Table 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;
}
Aggregations