Java Code Examples for org.apache.calcite.tools.Frameworks#withPlanner()
The following examples show how to use
org.apache.calcite.tools.Frameworks#withPlanner() .
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: RelWriterTest.java From calcite with Apache License 2.0 | 6 votes |
/** * Unit test for {@link org.apache.calcite.rel.externalize.RelJsonReader}. */ @Test void testReader() { String s = Frameworks.withPlanner((cluster, relOptSchema, rootSchema) -> { SchemaPlus schema = rootSchema.add("hr", new ReflectiveSchema(new JdbcTest.HrSchema())); final RelJsonReader reader = new RelJsonReader(cluster, relOptSchema, schema); RelNode node; try { node = reader.read(XX); } catch (IOException e) { throw TestUtil.rethrow(e); } return RelOptUtil.dumpPlan("", node, SqlExplainFormat.TEXT, SqlExplainLevel.EXPPLAN_ATTRIBUTES); }); assertThat(s, isLinux("LogicalAggregate(group=[{0}], c=[COUNT(DISTINCT $1)], d=[COUNT()])\n" + " LogicalFilter(condition=[=($1, 10)])\n" + " LogicalTableScan(table=[[hr, emps]])\n")); }
Example 2
Source File: RelWriterTest.java From calcite with Apache License 2.0 | 6 votes |
/** * Unit test for {@link org.apache.calcite.rel.externalize.RelJsonReader}. */ @Test void testReaderNull() { String s = Frameworks.withPlanner((cluster, relOptSchema, rootSchema) -> { SchemaPlus schema = rootSchema.add("hr", new ReflectiveSchema(new JdbcTest.HrSchema())); final RelJsonReader reader = new RelJsonReader(cluster, relOptSchema, schema); RelNode node; try { node = reader.read(XXNULL); } catch (IOException e) { throw TestUtil.rethrow(e); } return RelOptUtil.dumpPlan("", node, SqlExplainFormat.TEXT, SqlExplainLevel.EXPPLAN_ATTRIBUTES); }); assertThat(s, isLinux("LogicalAggregate(group=[{0}], agg#0=[COUNT(DISTINCT $1)], agg#1=[COUNT()])\n" + " LogicalFilter(condition=[=($1, null:INTEGER)])\n" + " LogicalTableScan(table=[[hr, emps]])\n")); }
Example 3
Source File: RelWriterTest.java From calcite with Apache License 2.0 | 6 votes |
/** * Deserialize a relnode from the json string by {@link RelJsonReader}, * and dump it to text format. */ private String deserializeAndDumpToTextFormat(RelOptSchema schema, String relJson) { String s = Frameworks.withPlanner((cluster, relOptSchema, rootSchema) -> { final RelJsonReader reader = new RelJsonReader( cluster, schema, rootSchema); RelNode node; try { node = reader.read(relJson); } catch (IOException e) { throw TestUtil.rethrow(e); } return RelOptUtil.dumpPlan("", node, SqlExplainFormat.TEXT, SqlExplainLevel.EXPPLAN_ATTRIBUTES); }); return s; }
Example 4
Source File: QuerySqlStatisticProvider.java From calcite with Apache License 2.0 | 5 votes |
private <R> R withBuilder(BuilderAction<R> action) { return Frameworks.withPlanner( (cluster, relOptSchema, rootSchema) -> { final RelBuilder relBuilder = RelBuilder.proto(Contexts.of()).create(cluster, relOptSchema); return action.apply(cluster, relOptSchema, relBuilder); }); }
Example 5
Source File: RelMetadataTest.java From calcite with Apache License 2.0 | 5 votes |
/** Unit test for * {@link org.apache.calcite.rel.metadata.RelMdCollation#project} * and other helper functions for deducing collations. */ @Test void testCollation() { final Project rel = (Project) convertSql("select * from emp, dept"); final Join join = (Join) rel.getInput(); final RelOptTable empTable = join.getInput(0).getTable(); final RelOptTable deptTable = join.getInput(1).getTable(); Frameworks.withPlanner((cluster, relOptSchema, rootSchema) -> { checkCollation(cluster, empTable, deptTable); return null; }); }
Example 6
Source File: RelMetadataTest.java From calcite with Apache License 2.0 | 5 votes |
/** Unit test for * {@link org.apache.calcite.rel.metadata.RelMetadataQuery#getAverageColumnSizes(org.apache.calcite.rel.RelNode)}, * {@link org.apache.calcite.rel.metadata.RelMetadataQuery#getAverageRowSize(org.apache.calcite.rel.RelNode)}. */ @Test void testAverageRowSize() { final Project rel = (Project) convertSql("select * from emp, dept"); final Join join = (Join) rel.getInput(); final RelOptTable empTable = join.getInput(0).getTable(); final RelOptTable deptTable = join.getInput(1).getTable(); Frameworks.withPlanner((cluster, relOptSchema, rootSchema) -> { checkAverageRowSize(cluster, empTable, deptTable); return null; }); }
Example 7
Source File: RelMetadataTest.java From calcite with Apache License 2.0 | 5 votes |
/** Unit test for * {@link org.apache.calcite.rel.metadata.RelMdPredicates#getPredicates(Join, RelMetadataQuery)}. */ @Test void testPredicates() { final Project rel = (Project) convertSql("select * from emp, dept"); final Join join = (Join) rel.getInput(); final RelOptTable empTable = join.getInput(0).getTable(); final RelOptTable deptTable = join.getInput(1).getTable(); Frameworks.withPlanner((cluster, relOptSchema, rootSchema) -> { checkPredicates(cluster, empTable, deptTable); return null; }); }
Example 8
Source File: RelMetadataTest.java From calcite with Apache License 2.0 | 5 votes |
@Test void testAllPredicates() { final Project rel = (Project) convertSql("select * from emp, dept"); final Join join = (Join) rel.getInput(); final RelOptTable empTable = join.getInput(0).getTable(); final RelOptTable deptTable = join.getInput(1).getTable(); Frameworks.withPlanner((cluster, relOptSchema, rootSchema) -> { checkAllPredicates(cluster, empTable, deptTable); return null; }); }
Example 9
Source File: SqlToRelConverterExtendedTest.java From calcite with Apache License 2.0 | 5 votes |
public static void foo(RelNode rel) { // Convert rel tree to JSON. final RelJsonWriter writer = new RelJsonWriter(); rel.explain(writer); final String json = writer.asString(); // Find the schema. If there are no tables in the plan, we won't need one. final RelOptSchema[] schemas = {null}; rel.accept(new RelShuttleImpl() { @Override public RelNode visit(TableScan scan) { schemas[0] = scan.getTable().getRelOptSchema(); return super.visit(scan); } }); // Convert JSON back to rel tree. Frameworks.withPlanner((cluster, relOptSchema, rootSchema) -> { final RelJsonReader reader = new RelJsonReader( cluster, schemas[0], rootSchema); try { RelNode x = reader.read(json); } catch (IOException e) { throw TestUtil.rethrow(e); } return null; }); }
Example 10
Source File: RelWriterTest.java From calcite with Apache License 2.0 | 5 votes |
/** * Unit test for {@link org.apache.calcite.rel.externalize.RelJsonWriter} on * a simple tree of relational expressions, consisting of a table and a * project including window expressions. */ @Test void testWriter() { String s = Frameworks.withPlanner((cluster, relOptSchema, rootSchema) -> { rootSchema.add("hr", new ReflectiveSchema(new JdbcTest.HrSchema())); LogicalTableScan scan = LogicalTableScan.create(cluster, relOptSchema.getTableForMember( Arrays.asList("hr", "emps")), ImmutableList.of()); final RexBuilder rexBuilder = cluster.getRexBuilder(); LogicalFilter filter = LogicalFilter.create(scan, rexBuilder.makeCall( SqlStdOperatorTable.EQUALS, rexBuilder.makeFieldAccess( rexBuilder.makeRangeReference(scan), "deptno", true), rexBuilder.makeExactLiteral(BigDecimal.TEN))); final RelJsonWriter writer = new RelJsonWriter(); final RelDataType bigIntType = cluster.getTypeFactory().createSqlType(SqlTypeName.BIGINT); LogicalAggregate aggregate = LogicalAggregate.create(filter, ImmutableList.of(), ImmutableBitSet.of(0), null, ImmutableList.of( AggregateCall.create(SqlStdOperatorTable.COUNT, true, false, false, ImmutableList.of(1), -1, RelCollations.EMPTY, bigIntType, "c"), AggregateCall.create(SqlStdOperatorTable.COUNT, false, false, false, ImmutableList.of(), -1, RelCollations.EMPTY, bigIntType, "d"))); aggregate.explain(writer); return writer.asString(); }); assertThat(s, is(XX)); }
Example 11
Source File: RelWriterTest.java From calcite with Apache License 2.0 | 5 votes |
/** * Unit test for {@link org.apache.calcite.rel.externalize.RelJsonReader}. */ @Test void testReader2() { String s = Frameworks.withPlanner((cluster, relOptSchema, rootSchema) -> { SchemaPlus schema = rootSchema.add("hr", new ReflectiveSchema(new JdbcTest.HrSchema())); final RelJsonReader reader = new RelJsonReader(cluster, relOptSchema, schema); RelNode node; try { node = reader.read(XX2); } catch (IOException e) { throw TestUtil.rethrow(e); } return RelOptUtil.dumpPlan("", node, SqlExplainFormat.TEXT, SqlExplainLevel.EXPPLAN_ATTRIBUTES); }); assertThat(s, isLinux("LogicalProject(field0=[$0]," + " field1=[COUNT($0) OVER (PARTITION BY $2 ORDER BY $1 NULLS LAST " + "ROWS UNBOUNDED PRECEDING)]," + " field2=[SUM($0) OVER (PARTITION BY $2 ORDER BY $1 NULLS LAST " + "RANGE BETWEEN CURRENT ROW AND 1 FOLLOWING)])\n" + " LogicalTableScan(table=[[hr, emps]])\n")); }
Example 12
Source File: RelWriterTest.java From calcite with Apache License 2.0 | 5 votes |
@Test void testCalc() { final FrameworkConfig config = RelBuilderTest.config().build(); final RelBuilder builder = RelBuilder.create(config); final RexBuilder rexBuilder = builder.getRexBuilder(); final LogicalTableScan scan = (LogicalTableScan) builder.scan("EMP").build(); final RexProgramBuilder programBuilder = new RexProgramBuilder(scan.getRowType(), rexBuilder); final RelDataTypeField field = scan.getRowType().getField("SAL", false, false); programBuilder.addIdentity(); programBuilder.addCondition( rexBuilder.makeCall(SqlStdOperatorTable.GREATER_THAN, new RexInputRef(field.getIndex(), field.getType()), builder.literal(10))); final LogicalCalc calc = LogicalCalc.create(scan, programBuilder.getProgram()); String relJson = RelOptUtil.dumpPlan("", calc, SqlExplainFormat.JSON, SqlExplainLevel.EXPPLAN_ATTRIBUTES); String s = Frameworks.withPlanner((cluster, relOptSchema, rootSchema) -> { final RelJsonReader reader = new RelJsonReader( cluster, getSchema(calc), rootSchema); RelNode node; try { node = reader.read(relJson); } catch (IOException e) { throw TestUtil.rethrow(e); } return RelOptUtil.dumpPlan("", node, SqlExplainFormat.TEXT, SqlExplainLevel.EXPPLAN_ATTRIBUTES); }); final String expected = "LogicalCalc(expr#0..7=[{inputs}], expr#8=[10], expr#9=[>($t5, $t8)]," + " proj#0..7=[{exprs}], $condition=[$t9])\n" + " LogicalTableScan(table=[[scott, EMP]])\n"; assertThat(s, isLinux(expected)); }
Example 13
Source File: RelMetadataTest.java From calcite with Apache License 2.0 | 4 votes |
/** Unit test for * {@link org.apache.calcite.rel.metadata.RelMdColumnUniqueness#areColumnsUnique} * applied to {@link Values}. */ @Test void testColumnUniquenessForValues() { Frameworks.withPlanner((cluster, relOptSchema, rootSchema) -> { final RexBuilder rexBuilder = cluster.getRexBuilder(); final RelMetadataQuery mq = cluster.getMetadataQuery(); final RelDataType rowType = cluster.getTypeFactory().builder() .add("a", SqlTypeName.INTEGER) .add("b", SqlTypeName.VARCHAR) .build(); final ImmutableList.Builder<ImmutableList<RexLiteral>> tuples = ImmutableList.builder(); addRow(tuples, rexBuilder, 1, "X"); addRow(tuples, rexBuilder, 2, "Y"); addRow(tuples, rexBuilder, 3, "X"); addRow(tuples, rexBuilder, 4, "X"); final LogicalValues values = LogicalValues.create(cluster, rowType, tuples.build()); final ImmutableBitSet colNone = ImmutableBitSet.of(); final ImmutableBitSet col0 = ImmutableBitSet.of(0); final ImmutableBitSet col1 = ImmutableBitSet.of(1); final ImmutableBitSet colAll = ImmutableBitSet.of(0, 1); assertThat(mq.areColumnsUnique(values, col0), is(true)); assertThat(mq.areColumnsUnique(values, col1), is(false)); assertThat(mq.areColumnsUnique(values, colAll), is(true)); assertThat(mq.areColumnsUnique(values, colNone), is(false)); // Repeat the above tests directly against the handler. final RelMdColumnUniqueness handler = (RelMdColumnUniqueness) RelMdColumnUniqueness.SOURCE .handlers(BuiltInMetadata.ColumnUniqueness.DEF) .get(BuiltInMethod.COLUMN_UNIQUENESS.method) .iterator().next(); assertThat(handler.areColumnsUnique(values, mq, col0, false), is(true)); assertThat(handler.areColumnsUnique(values, mq, col1, false), is(false)); assertThat(handler.areColumnsUnique(values, mq, colAll, false), is(true)); assertThat(handler.areColumnsUnique(values, mq, colNone, false), is(false)); return null; }); }
Example 14
Source File: RelWriterTest.java From calcite with Apache License 2.0 | 4 votes |
/** * Unit test for {@link org.apache.calcite.rel.externalize.RelJsonWriter} on * a simple tree of relational expressions, consisting of a table, a filter * and an aggregate node. */ @Test void testWriter2() { String s = Frameworks.withPlanner((cluster, relOptSchema, rootSchema) -> { rootSchema.add("hr", new ReflectiveSchema(new JdbcTest.HrSchema())); LogicalTableScan scan = LogicalTableScan.create(cluster, relOptSchema.getTableForMember( Arrays.asList("hr", "emps")), ImmutableList.of()); final RexBuilder rexBuilder = cluster.getRexBuilder(); final RelDataType bigIntType = cluster.getTypeFactory().createSqlType(SqlTypeName.BIGINT); LogicalProject project = LogicalProject.create(scan, ImmutableList.of(), ImmutableList.of( rexBuilder.makeInputRef(scan, 0), rexBuilder.makeOver(bigIntType, SqlStdOperatorTable.COUNT, ImmutableList.of(rexBuilder.makeInputRef(scan, 0)), ImmutableList.of(rexBuilder.makeInputRef(scan, 2)), ImmutableList.of( new RexFieldCollation( rexBuilder.makeInputRef(scan, 1), ImmutableSet.of())), RexWindowBounds.UNBOUNDED_PRECEDING, RexWindowBounds.CURRENT_ROW, true, true, false, false, false), rexBuilder.makeOver(bigIntType, SqlStdOperatorTable.SUM, ImmutableList.of(rexBuilder.makeInputRef(scan, 0)), ImmutableList.of(rexBuilder.makeInputRef(scan, 2)), ImmutableList.of( new RexFieldCollation( rexBuilder.makeInputRef(scan, 1), ImmutableSet.of())), RexWindowBounds.CURRENT_ROW, RexWindowBounds.following( rexBuilder.makeExactLiteral(BigDecimal.ONE)), false, true, false, false, false)), ImmutableList.of("field0", "field1", "field2")); final RelJsonWriter writer = new RelJsonWriter(); project.explain(writer); return writer.asString(); }); assertThat(s, is(XX2)); }