Java Code Examples for org.apache.calcite.materialize.Lattice#Measure

The following examples show how to use org.apache.calcite.materialize.Lattice#Measure . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AggregateStarTableRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
private static int find(ImmutableList<Lattice.Measure> measures,
    Pair<SqlAggFunction, List<Integer>> seek) {
  for (int i = 0; i < measures.size(); i++) {
    Lattice.Measure measure = measures.get(i);
    if (measure.agg.equals(seek.left)
        && measure.argOrdinals().equals(seek.right)) {
      return i;
    }
  }
  return -1;
}
 
Example 2
Source File: QuarkTileTable.java    From quark with Apache License 2.0 5 votes vote down vote up
public RelNode toRel(
    RelOptTable.ToRelContext context,
    RelOptTable relOptTable) {
  // Request all fields.
  RelNode rel = new QuarkTileScan(context.getCluster(),
      this.relOptTable, this.quarkTile, this.backingTable);

  //Create a filter

  RexBuilder rexBuilder = rel.getCluster().getRexBuilder();
  List<RexNode> filterArgs = Lists.newArrayList();
  filterArgs.add(rexBuilder.makeInputRef(rel, this.quarkTile.groupingColumn));
  filterArgs.add(rexBuilder.makeLiteral(bitSetToString(this.quarkTile.groupingValue)));

  rel = LogicalFilter.create(rel, rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, filterArgs));

  //Create a project list
  List<Integer> posList = Lists.newArrayList();
  for (QuarkTile.Column quarkColumn : this.quarkTile.cubeColumns) {
    posList.add(quarkColumn.cubeOrdinal);
  }

  for (Lattice.Measure measure : this.quarkTile.measures) {
    posList.add(((QuarkTile.Measure) measure).ordinal);
  }

  return RelOptUtil.createProject(rel, posList);

}
 
Example 3
Source File: QuarkTileTable.java    From quark with Apache License 2.0 5 votes vote down vote up
public RelDataType getRowType(RelDataTypeFactory typeFactory) {
  List<QuarkColumn> columns = this.backingTable.columns;
  final List<String> names = new ArrayList<>();
  final List<RelDataType> types = new ArrayList<>();

  for (QuarkTile.Column quarkColumn : this.quarkTile.cubeColumns) {
    addColumn(columns.get(quarkColumn.cubeOrdinal), names, types, typeFactory);
  }

  for (Lattice.Measure measure : this.quarkTile.measures) {
    addColumn(columns.get(((QuarkTile.Measure) measure).ordinal), names, types, typeFactory);
  }
  return typeFactory.createStructType(Pair.zip(names, types));
}
 
Example 4
Source File: QuarkTile.java    From quark with Apache License 2.0 5 votes vote down vote up
public QuarkTile(List<Lattice.Measure> measures,
                 List<Lattice.Column> dimensions,
                 List<QuarkTile.Column> cubeColumns,
                 int groupingColumn,
                 ImmutableBitSet groupingValue,
                 List<String> tableName, List<String> alias) {
  super(Ordering.natural().immutableSortedCopy(measures),
      Ordering.natural().immutableSortedCopy(dimensions));
  this.tableName = tableName;
  this.cubeColumns = Ordering.natural().immutableSortedCopy(cubeColumns);
  this.groupingColumn = groupingColumn;
  this.groupingValue = groupingValue;
  this.alias = alias;
}
 
Example 5
Source File: AggregateStarTableRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static int find(ImmutableList<Lattice.Measure> measures,
    Pair<SqlAggFunction, List<Integer>> seek) {
  for (int i = 0; i < measures.size(); i++) {
    Lattice.Measure measure = measures.get(i);
    if (measure.agg.equals(seek.left)
        && measure.argOrdinals().equals(seek.right)) {
      return i;
    }
  }
  return -1;
}
 
Example 6
Source File: AggregateStarTableRule.java    From Bats with Apache License 2.0 4 votes vote down vote up
private static AggregateCall rollUp(int groupCount, RelBuilder relBuilder,
    AggregateCall aggregateCall, TileKey tileKey) {
  if (aggregateCall.isDistinct()) {
    return null;
  }
  final SqlAggFunction aggregation = aggregateCall.getAggregation();
  final Pair<SqlAggFunction, List<Integer>> seek =
      Pair.of(aggregation, aggregateCall.getArgList());
  final int offset = tileKey.dimensions.cardinality();
  final ImmutableList<Lattice.Measure> measures = tileKey.measures;

  // First, try to satisfy the aggregation by rolling up an aggregate in the
  // materialization.
  final int i = find(measures, seek);
tryRoll:
  if (i >= 0) {
    final SqlAggFunction roll = SubstitutionVisitor.getRollup(aggregation);
    if (roll == null) {
      break tryRoll;
    }
    return AggregateCall.create(roll, false,
        aggregateCall.isApproximate(), ImmutableList.of(offset + i), -1,
        aggregateCall.collation,
        groupCount, relBuilder.peek(), null, aggregateCall.name);
  }

  // Second, try to satisfy the aggregation based on group set columns.
tryGroup:
  {
    List<Integer> newArgs = new ArrayList<>();
    for (Integer arg : aggregateCall.getArgList()) {
      int z = tileKey.dimensions.indexOf(arg);
      if (z < 0) {
        break tryGroup;
      }
      newArgs.add(z);
    }
    return AggregateCall.create(aggregation, false,
        aggregateCall.isApproximate(), newArgs, -1, aggregateCall.collation,
        groupCount, relBuilder.peek(), null, aggregateCall.name);
  }

  // No roll up possible.
  return null;
}
 
Example 7
Source File: FilterAggStarRule.java    From quark with Apache License 2.0 4 votes vote down vote up
private RelNode constructTileRel(final StarTable.StarTableScan scan,
                                 final Aggregate mergedAggregate,
                                 final List<Lattice.Measure> measures,
                                 final RelOptTable aggregateRelOptTable,
                                 final RexNode filterConditionOnTile,
                                 final QuarkTile quarkTile) {

  RelNode rel = aggregateRelOptTable.toRel(RelOptUtil.getContext(scan.getCluster()));

  // Aggregate has finer granularity than we need. Roll up.
  if (CalcitePrepareImpl.DEBUG) {
    System.out.println("Using materialization "
        + aggregateRelOptTable.getQualifiedName()
        + ", rolling up " + quarkTile.bitSet() + " to "
        + mergedAggregate.getGroupSet());
  }
  assert quarkTile.bitSet().contains(mergedAggregate.getGroupSet());
  final List<AggregateCall> aggCalls = Lists.newArrayList();
  ImmutableBitSet.Builder groupSet = ImmutableBitSet.builder();
  for (int key : mergedAggregate.getGroupSet()) {
    groupSet.set(quarkTile.bitSet().indexOf(key));
  }

  //Create a filter on tile
  rel = LogicalFilter.create(rel, filterConditionOnTile);

  int columnCount = 0;
  //Create a project list to remove any unnecessary measures
  List<Integer> posList = Lists.newArrayList();
  for (QuarkTile.Column quarkColumn : quarkTile.cubeColumns) {
    posList.add(columnCount++);
  }

  for (Lattice.Measure measure : quarkTile.measures) {
    for (Lattice.Measure m : measures) {
      if (m.equals(measure)) {
        posList.add(columnCount);
      }
      columnCount++;
    }
  }

  rel = RelOptUtil.createProject(rel, posList);

  int index = quarkTile.cubeColumns.size();
  for (AggregateCall aggCall : mergedAggregate.getAggCallList()) {
    final AggregateCall copy = AggregateCall.create(aggCall.getAggregation(),
        false, ImmutableList.of(index), -1, groupSet.cardinality(), rel, null, aggCall.name);
    aggCalls.add(copy);
    index++;
  }

  return mergedAggregate.copy(mergedAggregate.getTraitSet(), rel, false,
      groupSet.build(), null, aggCalls);
}
 
Example 8
Source File: QuarkTile.java    From quark with Apache License 2.0 4 votes vote down vote up
Measure(Lattice.Measure measure, int ordinal) {
  super(measure.agg, measure.args);
  this.ordinal = ordinal;
}
 
Example 9
Source File: QuarkCube.java    From quark with Apache License 2.0 4 votes vote down vote up
public Lattice build(CalciteSchema calciteSchema, QuarkTable quarkTable) {
  Lattice.Builder latticeBuilder =
      Lattice.builder(calciteSchema, toString(this.sql))
          .auto(false)
          .algorithm(false);

  validateCubeLatticeFilter(latticeBuilder);

  List<Lattice.Measure> measures = new ArrayList<>();
  for (QuarkCube.Measure nzMeasure : this.measures) {
    final Lattice.Measure measure =
        latticeBuilder.resolveMeasure(nzMeasure.agg, nzMeasure.args);
    QuarkTile.Measure quarkMeasure = new QuarkTile.Measure(measure,
        quarkTable.getFieldOrdinal(nzMeasure.cubeColumn));
    measures.add(quarkMeasure);
  }

  final Set<Set<Dimension>> dimensionSets;
  if (groups == null || groups.isEmpty()) {
    dimensionSets = getDimensionSets(dimensions);
  } else {
    dimensionSets = ImmutableSet.<Set<Dimension>>builder()
        .addAll(groups) //Add all possible groups
        .add(Sets.<Dimension>newHashSet()) //Add an empty set
        .build();
  }

  for (Set<Dimension> set : dimensionSets) {
    List<Lattice.Column> columns = new ArrayList<>();
    List<QuarkTile.Column> cubeColumns = new ArrayList<>();
    ImmutableBitSet.Builder bitSetBuilder = ImmutableBitSet.builder();

    for (Dimension dimension : set) {
      final Lattice.Column column = latticeBuilder.resolveColumn(dimension.qualifiedCol);
      QuarkTile.Column quarkColumn = new QuarkTile.Column(column,
          quarkTable.getFieldOrdinal(dimension.cubeColumn));
      columns.add(column);
      cubeColumns.add(quarkColumn);
      bitSetBuilder.set(dimension.cubeOrdinal);
    }

    latticeBuilder.addTile(new QuarkTile(measures, columns, cubeColumns,
        quarkTable.getFieldOrdinal(this.groupingColumn), bitSetBuilder.build(),
        this.tableName, this.alias));
  }
  return latticeBuilder.build();
}
 
Example 10
Source File: AggregateStarTableRule.java    From calcite with Apache License 2.0 4 votes vote down vote up
private static AggregateCall rollUp(int groupCount, RelBuilder relBuilder,
    AggregateCall aggregateCall, TileKey tileKey) {
  if (aggregateCall.isDistinct()) {
    return null;
  }
  final SqlAggFunction aggregation = aggregateCall.getAggregation();
  final Pair<SqlAggFunction, List<Integer>> seek =
      Pair.of(aggregation, aggregateCall.getArgList());
  final int offset = tileKey.dimensions.cardinality();
  final ImmutableList<Lattice.Measure> measures = tileKey.measures;

  // First, try to satisfy the aggregation by rolling up an aggregate in the
  // materialization.
  final int i = find(measures, seek);
tryRoll:
  if (i >= 0) {
    final SqlAggFunction roll = SubstitutionVisitor.getRollup(aggregation);
    if (roll == null) {
      break tryRoll;
    }
    return AggregateCall.create(roll, false, aggregateCall.isApproximate(),
        aggregateCall.ignoreNulls(), ImmutableList.of(offset + i), -1,
        aggregateCall.collation,
        groupCount, relBuilder.peek(), null, aggregateCall.name);
  }

  // Second, try to satisfy the aggregation based on group set columns.
tryGroup:
  {
    List<Integer> newArgs = new ArrayList<>();
    for (Integer arg : aggregateCall.getArgList()) {
      int z = tileKey.dimensions.indexOf(arg);
      if (z < 0) {
        break tryGroup;
      }
      newArgs.add(z);
    }
    return AggregateCall.create(aggregation, false,
        aggregateCall.isApproximate(), aggregateCall.ignoreNulls(),
        newArgs, -1, aggregateCall.collation,
        groupCount, relBuilder.peek(), null, aggregateCall.name);
  }

  // No roll up possible.
  return null;
}
 
Example 11
Source File: RelOptLattice.java    From Bats with Apache License 2.0 3 votes vote down vote up
/** 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);
}
 
Example 12
Source File: RelOptLattice.java    From calcite with Apache License 2.0 3 votes vote down vote up
/** 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);
}