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

The following examples show how to use org.apache.calcite.materialize.Lattice#Builder . 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: ModelHandler.java    From calcite with Apache License 2.0 6 votes vote down vote up
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);
  }
}
 
Example 2
Source File: QuarkCube.java    From quark with Apache License 2.0 5 votes vote down vote up
private void validateCubeLatticeFilter(Lattice.Builder latticeBuilder) {
  if (latticeBuilder.filter != null) {
    ImmutableBitSet rCols = RelOptUtil.InputFinder.bits(latticeBuilder.filter);
    Set<Integer> dimSet = new HashSet<>();
    for (Dimension dimension : dimensions) {
      dimSet.add(latticeBuilder.resolveColumn(dimension.qualifiedCol).ordinal);
    }
    ImmutableBitSet dims = ImmutableBitSet.of(dimSet);
    if (!dims.contains(rCols)) {
      throw new RuntimeException("Cube filter is only allowed on dimensions");
    }
  }
}
 
Example 3
Source File: ModelHandler.java    From calcite with Apache License 2.0 5 votes vote down vote up
private void populateLattice(JsonLattice jsonLattice,
    Lattice.Builder latticeBuilder) {
  // By default, the default measure list is just {count(*)}.
  if (jsonLattice.defaultMeasures == null) {
    final JsonMeasure countMeasure = new JsonMeasure();
    countMeasure.agg = "count";
    jsonLattice.defaultMeasures = ImmutableList.of(countMeasure);
  }
  assert this.latticeBuilder == null;
  this.latticeBuilder = latticeBuilder;
  jsonLattice.visitChildren(this);
  this.latticeBuilder = null;
}
 
Example 4
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();
}