use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot in project calcite by apache.
the class RelOptRulesTest method transitiveInference.
private void transitiveInference(RelOptRule... extraRules) throws Exception {
final DiffRepository diffRepos = getDiffRepos();
final String sql = diffRepos.expand(null, "${sql}");
final HepProgram program = new HepProgramBuilder().addRuleInstance(FilterJoinRule.DUMB_FILTER_ON_JOIN).addRuleInstance(FilterJoinRule.JOIN).addRuleInstance(FilterProjectTransposeRule.INSTANCE).addRuleInstance(FilterSetOpTransposeRule.INSTANCE).build();
final HepPlanner planner = new HepPlanner(program);
final RelRoot root = tester.convertSqlToRel(sql);
final RelNode relInitial = root.rel;
assertTrue(relInitial != null);
List<RelMetadataProvider> list = Lists.newArrayList();
list.add(DefaultRelMetadataProvider.INSTANCE);
planner.registerMetadataProviders(list);
RelMetadataProvider plannerChain = ChainedRelMetadataProvider.of(list);
relInitial.getCluster().setMetadataProvider(new CachingRelMetadataProvider(plannerChain, planner));
planner.setRoot(relInitial);
RelNode relBefore = planner.findBestExp();
String planBefore = NL + RelOptUtil.toString(relBefore);
diffRepos.assertEquals("planBefore", "${planBefore}", planBefore);
HepProgram program2 = new HepProgramBuilder().addMatchOrder(HepMatchOrder.BOTTOM_UP).addRuleInstance(FilterJoinRule.DUMB_FILTER_ON_JOIN).addRuleInstance(FilterJoinRule.JOIN).addRuleInstance(FilterProjectTransposeRule.INSTANCE).addRuleInstance(FilterSetOpTransposeRule.INSTANCE).addRuleInstance(JoinPushTransitivePredicatesRule.INSTANCE).addRuleCollection(Arrays.asList(extraRules)).build();
final HepPlanner planner2 = new HepPlanner(program2);
planner.registerMetadataProviders(list);
planner2.setRoot(relBefore);
RelNode relAfter = planner2.findBestExp();
String planAfter = NL + RelOptUtil.toString(relAfter);
diffRepos.assertEquals("planAfter", "${planAfter}", planAfter);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot in project calcite by apache.
the class RelMetadataTest method convertSql.
// ~ Methods ----------------------------------------------------------------
// ----------------------------------------------------------------------
// Tests for getPercentageOriginalRows
// ----------------------------------------------------------------------
private RelNode convertSql(String sql) {
final RelRoot root = tester.convertSqlToRel(sql);
root.rel.getCluster().setMetadataProvider(DefaultRelMetadataProvider.INSTANCE);
return root.rel;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot in project coral by linkedin.
the class HiveViewTable method toRel.
@Override
public RelNode toRel(RelOptTable.ToRelContext relContext, RelOptTable relOptTable) {
try {
RelRoot root = relContext.expandView(relOptTable.getRowType(), hiveTable.getViewExpandedText(), schemaPath, ImmutableList.of(hiveTable.getTableName()));
root = root.withRel(createCastRel(root.rel, relOptTable.getRowType(), RelFactories.DEFAULT_PROJECT_FACTORY));
// root = root.withRel(RelOptUtil.createCastRel(root.rel, relOptTable.getRowType()));
return root.rel;
} catch (Exception e) {
Throwables.propagateIfInstanceOf(e, RuntimeException.class);
throw new RuntimeException("Error while parsing view definition", e);
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot in project ignite-3 by apache.
the class PlannerTest method testJoinPushExpressionRule.
@Test
public void testJoinPushExpressionRule() throws Exception {
IgniteTypeFactory f = new IgniteTypeFactory(IgniteTypeSystem.INSTANCE);
TestTable emp = new TestTable(new RelDataTypeFactory.Builder(f).add("ID", f.createJavaType(Integer.class)).add("NAME", f.createJavaType(String.class)).add("DEPTNO", f.createJavaType(Integer.class)).build()) {
@Override
public IgniteDistribution distribution() {
return IgniteDistributions.broadcast();
}
};
TestTable dept = new TestTable(new RelDataTypeFactory.Builder(f).add("DEPTNO", f.createJavaType(Integer.class)).add("NAME", f.createJavaType(String.class)).build()) {
@Override
public IgniteDistribution distribution() {
return IgniteDistributions.broadcast();
}
};
IgniteSchema publicSchema = new IgniteSchema("PUBLIC");
publicSchema.addTable("EMP", emp);
publicSchema.addTable("DEPT", dept);
SchemaPlus schema = createRootSchema(false).add("PUBLIC", publicSchema);
String sql = "select d.deptno, e.deptno " + "from dept d, emp e " + "where d.deptno + e.deptno = 2";
PlanningContext ctx = PlanningContext.builder().parentContext(BaseQueryContext.builder().logger(log).frameworkConfig(newConfigBuilder(FRAMEWORK_CONFIG).defaultSchema(schema).costFactory(new IgniteCostFactory(1, 100, 1, 1)).build()).build()).query(sql).build();
RelRoot relRoot;
try (IgnitePlanner planner = ctx.planner()) {
assertNotNull(planner);
String qry = ctx.query();
assertNotNull(qry);
// Parse
SqlNode sqlNode = planner.parse(qry);
// Validate
sqlNode = planner.validate(sqlNode);
// Convert to Relational operators graph
relRoot = planner.rel(sqlNode);
RelNode rel = relRoot.rel;
assertNotNull(rel);
assertEquals("LogicalProject(DEPTNO=[$0], DEPTNO0=[$4])\n" + " LogicalFilter(condition=[=(CAST(+($0, $4)):INTEGER, 2)])\n" + " LogicalJoin(condition=[true], joinType=[inner])\n" + " IgniteLogicalTableScan(table=[[PUBLIC, DEPT]])\n" + " IgniteLogicalTableScan(table=[[PUBLIC, EMP]])\n", RelOptUtil.toString(rel));
// Transformation chain
RelTraitSet desired = rel.getCluster().traitSet().replace(IgniteConvention.INSTANCE).replace(IgniteDistributions.single()).replace(CorrelationTrait.UNCORRELATED).simplify();
IgniteRel phys = planner.transform(PlannerPhase.OPTIMIZATION, desired, rel);
assertNotNull(phys);
assertEquals("IgniteProject(DEPTNO=[$3], DEPTNO0=[$2])\n" + " IgniteCorrelatedNestedLoopJoin(condition=[=(CAST(+($3, $2)):INTEGER, 2)], joinType=[inner], " + "correlationVariables=[[$cor2]])\n" + " IgniteTableScan(table=[[PUBLIC, EMP]])\n" + " IgniteTableScan(table=[[PUBLIC, DEPT]], filters=[=(CAST(+($t0, $cor2.DEPTNO)):INTEGER, 2)])\n", RelOptUtil.toString(phys), "Invalid plan:\n" + RelOptUtil.toString(phys));
checkSplitAndSerialization(phys, publicSchema);
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot in project ignite-3 by apache.
the class PlannerHelper method optimize.
/**
* Optimize.
* TODO Documentation https://issues.apache.org/jira/browse/IGNITE-15859
*
* @param sqlNode Sql node.
* @param planner Planner.
*/
public static IgniteRel optimize(SqlNode sqlNode, IgnitePlanner planner) {
try {
// Convert to Relational operators graph
RelRoot root = planner.rel(sqlNode);
RelNode rel = root.rel;
if (HintUtils.containsDisabledRules(root.hints)) {
planner.setDisabledRules(HintUtils.disabledRules(root.hints));
}
// Transformation chain
rel = planner.transform(PlannerPhase.HEP_DECORRELATE, rel.getTraitSet(), rel);
rel = planner.trimUnusedFields(root.withRel(rel)).rel;
rel = planner.transform(PlannerPhase.HEP_FILTER_PUSH_DOWN, rel.getTraitSet(), rel);
rel = planner.transform(PlannerPhase.HEP_PROJECT_PUSH_DOWN, rel.getTraitSet(), rel);
RelTraitSet desired = rel.getCluster().traitSet().replace(IgniteConvention.INSTANCE).replace(IgniteDistributions.single()).replace(root.collation == null ? RelCollations.EMPTY : root.collation).simplify();
IgniteRel igniteRel = planner.transform(PlannerPhase.OPTIMIZATION, desired, rel);
if (!root.isRefTrivial()) {
final List<RexNode> projects = new ArrayList<>();
final RexBuilder rexBuilder = igniteRel.getCluster().getRexBuilder();
for (int field : Pair.left(root.fields)) {
projects.add(rexBuilder.makeInputRef(igniteRel, field));
}
igniteRel = new IgniteProject(igniteRel.getCluster(), desired, igniteRel, projects, root.validatedRowType);
}
if (sqlNode.isA(Set.of(SqlKind.INSERT, SqlKind.UPDATE, SqlKind.MERGE))) {
igniteRel = new FixDependentModifyNodeShuttle().visit(igniteRel);
}
return igniteRel;
} catch (Throwable ex) {
LOG.error("Unexpected error at query optimizer.", ex);
if (LOG.isDebugEnabled()) {
LOG.error(planner.dump());
}
throw ex;
}
}
Aggregations