org.apache.calcite.materialize.Lattice Java Examples

The following examples show how to use org.apache.calcite.materialize.Lattice. 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: LatticeTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Tests some of the properties of the {@link Lattice} data structure. */
@Test void testLattice() throws Exception {
  modelWithLattice("star",
      "select 1 from \"foodmart\".\"sales_fact_1997\" as s\n"
          + "join \"foodmart\".\"product\" as p using (\"product_id\")\n"
          + "join \"foodmart\".\"time_by_day\" as t on t.\"time_id\" = s.\"time_id\"")
      .doWithConnection(c -> {
        final SchemaPlus schema = c.getRootSchema();
        final SchemaPlus adhoc = schema.getSubSchema("adhoc");
        assertThat(adhoc.getTableNames().contains("EMPLOYEES"), is(true));
        final Map.Entry<String, CalciteSchema.LatticeEntry> entry =
            adhoc.unwrap(CalciteSchema.class).getLatticeMap().firstEntry();
        final Lattice lattice = entry.getValue().getLattice();
        assertThat(lattice.firstColumn("S"), is(0));
        assertThat(lattice.firstColumn("P"), is(8));
        assertThat(lattice.firstColumn("T"), is(23));
        assertThat(lattice.firstColumn("PC"), is(-1));
        assertThat(lattice.defaultMeasures.size(), is(1));
        assertThat(lattice.rootNode.descendants.size(), is(3));
      });
}
 
Example #2
Source File: LatticeTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Tests that it's OK for a lattice to have the same name as a table in the
 * schema. */
@Test void testLatticeSql() throws Exception {
  modelWithLattice("EMPLOYEES", "select * from \"foodmart\".\"days\"")
      .doWithConnection(c -> {
        final SchemaPlus schema = c.getRootSchema();
        final SchemaPlus adhoc = schema.getSubSchema("adhoc");
        assertThat(adhoc.getTableNames().contains("EMPLOYEES"), is(true));
        final Map.Entry<String, CalciteSchema.LatticeEntry> entry =
            adhoc.unwrap(CalciteSchema.class).getLatticeMap().firstEntry();
        final Lattice lattice = entry.getValue().getLattice();
        final String sql = "SELECT \"days\".\"day\"\n"
            + "FROM \"foodmart\".\"days\" AS \"days\"\n"
            + "GROUP BY \"days\".\"day\"";
        assertThat(
            lattice.sql(ImmutableBitSet.of(0),
                ImmutableList.of()), is(sql));
        final String sql2 = "SELECT"
            + " \"days\".\"day\", \"days\".\"week_day\"\n"
            + "FROM \"foodmart\".\"days\" AS \"days\"";
        assertThat(
            lattice.sql(ImmutableBitSet.of(0, 1), false,
                ImmutableList.of()),
            is(sql2));
      });
}
 
Example #3
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 #4
Source File: Profiler.java    From calcite with Apache License 2.0 6 votes vote down vote up
public double cardinality(ImmutableBitSet columnOrdinals) {
  final ImmutableBitSet originalOrdinals = columnOrdinals;
  for (;;) {
    final Distribution distribution = distributionMap.get(columnOrdinals);
    if (distribution != null) {
      if (columnOrdinals == originalOrdinals) {
        return distribution.cardinality;
      } else {
        final List<Double> cardinalityList = new ArrayList<>();
        cardinalityList.add(distribution.cardinality);
        for (int ordinal : originalOrdinals.except(columnOrdinals)) {
          final Distribution d = singletonDistributionList.get(ordinal);
          cardinalityList.add(d.cardinality);
        }
        return Lattice.getRowCount(rowCount.rowCount, cardinalityList);
      }
    }
    // Clear the last bit and iterate.
    // Better would be to combine all of our nearest ancestors.
    final List<Integer> list = columnOrdinals.asList();
    columnOrdinals = columnOrdinals.clear(Util.last(list));
  }
}
 
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: FoodMartLatticeStatisticProvider.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public double cardinality(List<Lattice.Column> columns) {
  final List<Double> cardinalityList = new ArrayList<>();
  for (Lattice.Column column : columns) {
    cardinalityList.add((double) cardinality(column));
  }
  return Lattice.getRowCount(lattice.getFactRowCount(), cardinalityList);
}
 
Example #7
Source File: FoodMartLatticeStatisticProvider.java    From calcite with Apache License 2.0 5 votes vote down vote up
private int cardinality(Lattice.Column column) {
  final Integer integer = CARDINALITY_MAP.get(column.alias);
  if (integer != null && integer > 0) {
    return integer;
  }
  return column.alias.length();
}
 
Example #8
Source File: LatticeTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Unit test for {@link Lattice#getRowCount(double, List)}. */
@Test void testColumnCount() {
  assertThat(Lattice.getRowCount(10, 2, 3), within(5.03D, 0.01D));
  assertThat(Lattice.getRowCount(10, 9, 8), within(9.4D, 0.01D));
  assertThat(Lattice.getRowCount(100, 9, 8), within(54.2D, 0.1D));
  assertThat(Lattice.getRowCount(1000, 9, 8), within(72D, 0.01D));
  assertThat(Lattice.getRowCount(1000, 1, 1), is(1D));
  assertThat(Lattice.getRowCount(1, 3, 5), within(1D, 0.01D));
  assertThat(Lattice.getRowCount(1, 3, 5, 13, 4831), within(1D, 0.01D));
}
 
Example #9
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 #10
Source File: CalciteSchema.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a LatticeEntryImpl. */
public LatticeEntryImpl(CalciteSchema schema, String name,
    Lattice lattice) {
  super(schema, name);
  this.lattice = lattice;

  // Star table has same name as lattice and is in same schema.
  final StarTable starTable = lattice.createStarTable();
  starTableEntry = schema.add(name, starTable);
}
 
Example #11
Source File: CalciteSchema.java    From calcite with Apache License 2.0 5 votes vote down vote up
private LatticeEntry add(String name, Lattice lattice) {
  if (latticeMap.containsKey(name, false)) {
    throw new RuntimeException("Duplicate lattice '" + name + "'");
  }
  final LatticeEntryImpl entry = new LatticeEntryImpl(this, name, lattice);
  latticeMap.put(name, entry);
  return entry;
}
 
Example #12
Source File: CalciteConnectionImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Called after the constructor has completed and the model has been
 * loaded. */
void init() {
  final MaterializationService service = MaterializationService.instance();
  for (CalciteSchema.LatticeEntry e : Schemas.getLatticeEntries(rootSchema)) {
    final Lattice lattice = e.getLattice();
    for (Lattice.Tile tile : lattice.computeTiles()) {
      service.defineTile(lattice, tile.bitSet(), tile.measures, e.schema,
          true, true);
    }
  }
}
 
Example #13
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 #14
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 #15
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 #16
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 #17
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 #18
Source File: CalciteSchema.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Creates a LatticeEntryImpl. */
public LatticeEntryImpl(CalciteSchema schema, String name,
    Lattice lattice) {
  super(schema, name);
  this.lattice = lattice;

  // Star table has same name as lattice and is in same schema.
  final StarTable starTable = lattice.createStarTable();
  starTableEntry = schema.add(name, starTable);
}
 
Example #19
Source File: CalciteSchema.java    From Bats with Apache License 2.0 5 votes vote down vote up
private LatticeEntry add(String name, Lattice lattice) {
  if (latticeMap.containsKey(name, false)) {
    throw new RuntimeException("Duplicate lattice '" + name + "'");
  }
  final LatticeEntryImpl entry = new LatticeEntryImpl(this, name, lattice);
  latticeMap.put(name, entry);
  return entry;
}
 
Example #20
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 #21
Source File: Schemas.java    From Bats with Apache License 2.0 4 votes vote down vote up
/** Returns the lattices defined in a schema.
 *
 * @param schema Schema */
public static List<Lattice> getLattices(CalciteSchema schema) {
  final List<CalciteSchema.LatticeEntry> list = getLatticeEntries(schema);
  return Lists.transform(list, CalciteSchema.LatticeEntry::getLattice);
}
 
Example #22
Source File: FoodMartLatticeStatisticProvider.java    From calcite with Apache License 2.0 4 votes vote down vote up
private FoodMartLatticeStatisticProvider(Lattice lattice,
    LatticeStatisticProvider provider) {
  super(provider);
  this.lattice = lattice;
}
 
Example #23
Source File: SchemaPlus.java    From Bats with Apache License 2.0 4 votes vote down vote up
/** Adds a lattice to this schema. */
void add(String name, Lattice lattice);
 
Example #24
Source File: StarTable.java    From Bats with Apache License 2.0 4 votes vote down vote up
/** Creates a StarTable. */
private StarTable(Lattice lattice, ImmutableList<Table> tables) {
  this.lattice = Objects.requireNonNull(lattice);
  this.tables = tables;
}
 
Example #25
Source File: StarTable.java    From Bats with Apache License 2.0 4 votes vote down vote up
/** Creates a StarTable and registers it in a schema. */
public static StarTable of(Lattice lattice, List<Table> tables) {
  return new StarTable(lattice, ImmutableList.copyOf(tables));
}
 
Example #26
Source File: RelOptLattice.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RelOptLattice(Lattice lattice, RelOptTable starRelOptTable) {
  this.lattice = lattice;
  this.starRelOptTable = starRelOptTable;
}
 
Example #27
Source File: StarTable.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Creates a StarTable and registers it in a schema. */
public static StarTable of(Lattice lattice, List<Table> tables) {
  return new StarTable(lattice, ImmutableList.copyOf(tables));
}
 
Example #28
Source File: StarTable.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Creates a StarTable. */
private StarTable(Lattice lattice, ImmutableList<Table> tables) {
  this.lattice = Objects.requireNonNull(lattice);
  this.tables = tables;
}
 
Example #29
Source File: SchemaPlus.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Adds a lattice to this schema. */
void add(String name, Lattice lattice);
 
Example #30
Source File: Schemas.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Returns the lattices defined in a schema.
 *
 * @param schema Schema */
public static List<Lattice> getLattices(CalciteSchema schema) {
  final List<CalciteSchema.LatticeEntry> list = getLatticeEntries(schema);
  return Lists.transform(list, CalciteSchema.LatticeEntry::getLattice);
}