use of org.apache.calcite.rel.type.RelDataTypeFactory in project calcite by apache.
the class RelOptRulesTest method checkPlanning.
private void checkPlanning(String query) throws Exception {
final Tester tester1 = tester.withCatalogReaderFactory(new Function<RelDataTypeFactory, Prepare.CatalogReader>() {
public Prepare.CatalogReader apply(RelDataTypeFactory typeFactory) {
return new MockCatalogReader(typeFactory, true) {
@Override
public MockCatalogReader init() {
// CREATE SCHEMA abc;
// CREATE TABLE a(a INT);
// ...
// CREATE TABLE j(j INT);
MockSchema schema = new MockSchema("SALES");
registerSchema(schema);
final RelDataType intType = typeFactory.createSqlType(SqlTypeName.INTEGER);
for (int i = 0; i < 10; i++) {
String t = String.valueOf((char) ('A' + i));
MockTable table = MockTable.create(this, schema, t, false, 100);
table.addColumn(t, intType);
registerTable(table);
}
return this;
}
}.init();
}
});
HepProgram program = new HepProgramBuilder().addMatchOrder(HepMatchOrder.BOTTOM_UP).addRuleInstance(ProjectRemoveRule.INSTANCE).addRuleInstance(JoinToMultiJoinRule.INSTANCE).build();
checkPlanning(tester1, null, new HepPlanner(program), query);
}
use of org.apache.calcite.rel.type.RelDataTypeFactory in project calcite by apache.
the class SqlToRelConverterTest method getTesterWithDynamicTable.
private Tester getTesterWithDynamicTable() {
return tester.withCatalogReaderFactory(new Function<RelDataTypeFactory, Prepare.CatalogReader>() {
public Prepare.CatalogReader apply(RelDataTypeFactory typeFactory) {
return new MockCatalogReader(typeFactory, true) {
@Override
public MockCatalogReader init() {
// CREATE SCHEMA "SALES;
// CREATE DYNAMIC TABLE "NATION"
// CREATE DYNAMIC TABLE "CUSTOMER"
MockSchema schema = new MockSchema("SALES");
registerSchema(schema);
MockTable nationTable = new MockDynamicTable(this, schema.getCatalogName(), schema.getName(), "NATION", false, 100);
registerTable(nationTable);
MockTable customerTable = new MockDynamicTable(this, schema.getCatalogName(), schema.getName(), "CUSTOMER", false, 100);
registerTable(customerTable);
// CREATE TABLE "REGION" - static table with known schema.
final RelDataType intType = typeFactory.createSqlType(SqlTypeName.INTEGER);
final RelDataType varcharType = typeFactory.createSqlType(SqlTypeName.VARCHAR);
MockTable regionTable = MockTable.create(this, schema, "REGION", false, 100);
regionTable.addColumn("R_REGIONKEY", intType);
regionTable.addColumn("R_NAME", varcharType);
regionTable.addColumn("R_COMMENT", varcharType);
registerTable(regionTable);
return this;
}
}.init();
}
});
}
use of org.apache.calcite.rel.type.RelDataTypeFactory in project calcite by apache.
the class Smalls method multiplicationTable.
/**
* A function that generates multiplication table of {@code ncol} columns x
* {@code nrow} rows.
*/
public static QueryableTable multiplicationTable(final int ncol, final int nrow, Integer offset) {
final int offs = offset == null ? 0 : offset;
return new AbstractQueryableTable(Object[].class) {
public RelDataType getRowType(RelDataTypeFactory typeFactory) {
final RelDataTypeFactory.Builder builder = typeFactory.builder();
builder.add("row_name", typeFactory.createJavaType(String.class));
final RelDataType int_ = typeFactory.createJavaType(int.class);
for (int i = 1; i <= ncol; i++) {
builder.add("c" + i, int_);
}
return builder.build();
}
public Queryable<Object[]> asQueryable(QueryProvider queryProvider, SchemaPlus schema, String tableName) {
final List<Object[]> table = new AbstractList<Object[]>() {
@Override
public Object[] get(int index) {
Object[] cur = new Object[ncol + 1];
cur[0] = "row " + index;
for (int j = 1; j <= ncol; j++) {
cur[j] = j * (index + 1) + offs;
}
return cur;
}
@Override
public int size() {
return nrow;
}
};
return Linq4j.asEnumerable(table).asQueryable();
}
};
}
use of org.apache.calcite.rel.type.RelDataTypeFactory in project calcite by apache.
the class Smalls method oneThreePlus.
private static QueryableTable oneThreePlus(String s) {
List<Integer> items;
// Then the engine calls a function with null arguments to get getRowType.
if (s == null) {
items = ImmutableList.of();
} else {
Integer latest = Integer.parseInt(s.substring(1, s.length() - 1));
items = ImmutableList.of(1, 3, latest);
}
final Enumerable<Integer> enumerable = Linq4j.asEnumerable(items);
return new AbstractQueryableTable(Integer.class) {
public <E> Queryable<E> asQueryable(QueryProvider queryProvider, SchemaPlus schema, String tableName) {
// noinspection unchecked
return (Queryable<E>) enumerable.asQueryable();
}
public RelDataType getRowType(RelDataTypeFactory typeFactory) {
return typeFactory.builder().add("c", SqlTypeName.INTEGER).build();
}
};
}
use of org.apache.calcite.rel.type.RelDataTypeFactory in project calcite by apache.
the class FrameworksTest method testOptimize.
@Test
public void testOptimize() {
RelNode x = Frameworks.withPlanner(new Frameworks.PlannerAction<RelNode>() {
public RelNode apply(RelOptCluster cluster, RelOptSchema relOptSchema, SchemaPlus rootSchema) {
final RelDataTypeFactory typeFactory = cluster.getTypeFactory();
final Table table = new AbstractTable() {
public RelDataType getRowType(RelDataTypeFactory typeFactory) {
final RelDataType stringType = typeFactory.createJavaType(String.class);
final RelDataType integerType = typeFactory.createJavaType(Integer.class);
return typeFactory.builder().add("s", stringType).add("i", integerType).build();
}
};
// "SELECT * FROM myTable"
final RelOptAbstractTable relOptTable = new RelOptAbstractTable(relOptSchema, "myTable", table.getRowType(typeFactory)) {
};
final EnumerableTableScan tableRel = EnumerableTableScan.create(cluster, relOptTable);
// "WHERE i > 1"
final RexBuilder rexBuilder = cluster.getRexBuilder();
final RexNode condition = rexBuilder.makeCall(SqlStdOperatorTable.GREATER_THAN, rexBuilder.makeFieldAccess(rexBuilder.makeRangeReference(tableRel), "i", true), rexBuilder.makeExactLiteral(BigDecimal.ONE));
final LogicalFilter filter = LogicalFilter.create(tableRel, condition);
// Specify that the result should be in Enumerable convention.
final RelNode rootRel = filter;
final RelOptPlanner planner = cluster.getPlanner();
RelTraitSet desiredTraits = cluster.traitSet().replace(EnumerableConvention.INSTANCE);
final RelNode rootRel2 = planner.changeTraits(rootRel, desiredTraits);
planner.setRoot(rootRel2);
// Now, plan.
return planner.findBestExp();
}
});
String s = RelOptUtil.dumpPlan("", x, SqlExplainFormat.TEXT, SqlExplainLevel.DIGEST_ATTRIBUTES);
assertThat(Util.toLinux(s), equalTo("EnumerableFilter(condition=[>($1, 1)])\n" + " EnumerableTableScan(table=[[myTable]])\n"));
}
Aggregations