org.apache.calcite.interpreter.Bindables Java Examples
The following examples show how to use
org.apache.calcite.interpreter.Bindables.
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: MycatCalcitePlanner.java From Mycat2 with GNU General Public License v3.0 | 6 votes |
public static RelNode toPhysical(RelNode rel, Consumer<RelOptPlanner> setting) { final RelOptPlanner planner = rel.getCluster().getPlanner(); planner.clear(); setting.accept(planner); planner.addRule(new RelOptRule(operand(MycatTransientSQLTableScan.class, none()), RelFactories.LOGICAL_BUILDER, "MycatTransientSQLTableScan") { @Override public void onMatch(RelOptRuleCall call) { final MycatTransientSQLTableScan scan = call.rel(0); final RelOptTable table = scan.getTable(); if (Bindables.BindableTableScan.canHandle(table)) { call.transformTo( Bindables.BindableTableScan.create(scan.getCluster(), table)); } } }); final Program program = Programs.of(RuleSets.ofList(planner.getRules())); return program.run(planner, rel, rel.getTraitSet().replace(EnumerableConvention.INSTANCE), ImmutableList.of(), ImmutableList.of()); }
Example #2
Source File: PushDownLogicTableRule.java From Mycat2 with GNU General Public License v3.0 | 6 votes |
@NotNull private RelNode global(RelOptCluster cluster, Bindables.BindableTableScan bindableTableScan, RelOptSchema relOptSchema, MycatLogicTable logicTable) { final HashSet<String> context = new HashSet<>(); RelNode logicalTableScan; MycatPhysicalTable mycatPhysicalTable = logicTable.getMycatGlobalPhysicalTable(context); RelOptTable dataNode = RelOptTableImpl.create( relOptSchema, logicTable.getRowType(cluster.getTypeFactory()),//这里使用logicTable,避免类型不一致 mycatPhysicalTable, ImmutableList.of(mycatPhysicalTable.getBackendTableInfo().getUniqueName())); logicalTableScan = LogicalTableScan.create(cluster, dataNode, ImmutableList.of()); return RelOptUtil.createProject(RelOptUtil.createFilter(logicalTableScan, bindableTableScan.filters), bindableTableScan.projects); }
Example #3
Source File: PushDownLogicTableRule.java From Mycat2 with GNU General Public License v3.0 | 6 votes |
private RelNode shardingTable(RelBuilder builder, Bindables.BindableTableScan bindableTableScan, RelOptCluster cluster, RelOptSchema relOptSchema, MycatLogicTable logicTable) { RelNode value; ArrayList<RexNode> filters = new ArrayList<>(bindableTableScan.filters == null ? Collections.emptyList() : bindableTableScan.filters); List<DataNode> backendTableInfos = CalciteUtls.getBackendTableInfos((ShardingTableHandler) logicTable.logicTable(), filters); //////////////////////////////////////////////////////////////////////////////////////////////// //视图优化 //////////////////////////////////////////////////////////////////////////////////////////////// builder.clear(); for (DataNode backendTableInfo : backendTableInfos) { builder.push(getBindableTableScan(bindableTableScan, cluster, relOptSchema, backendTableInfo)); } value = builder.union(true, backendTableInfos.size()).build(); return value; }
Example #4
Source File: FilterTableScanRule.java From calcite with Apache License 2.0 | 6 votes |
protected void apply(RelOptRuleCall call, Filter filter, TableScan scan) { final ImmutableIntList projects; final ImmutableList.Builder<RexNode> filters = ImmutableList.builder(); if (scan instanceof Bindables.BindableTableScan) { final Bindables.BindableTableScan bindableScan = (Bindables.BindableTableScan) scan; filters.addAll(bindableScan.filters); projects = bindableScan.projects; } else { projects = scan.identity(); } final Mapping mapping = Mappings.target(projects, scan.getTable().getRowType().getFieldCount()); filters.add( RexUtil.apply(mapping.inverse(), filter.getCondition())); call.transformTo( Bindables.BindableTableScan.create(scan.getCluster(), scan.getTable(), filters.build(), projects)); }
Example #5
Source File: PushDownLogicTableRule.java From Mycat2 with GNU General Public License v3.0 | 5 votes |
public RelNode toPhyTable(RelBuilder builder, TableScan judgeObject) { Bindables.BindableTableScan bindableTableScan; if (judgeObject instanceof Bindables.BindableTableScan) { bindableTableScan = (Bindables.BindableTableScan) judgeObject; } else { bindableTableScan = Bindables.BindableTableScan.create(judgeObject.getCluster(), judgeObject.getTable()); } RelOptCluster cluster = bindableTableScan.getCluster();//工具类 RelMetadataQuery metadataQuery = cluster.getMetadataQuery(); RelOptTable relOptTable = bindableTableScan.getTable();//包装表 RelOptSchema relOptSchema = bindableTableScan.getTable().getRelOptSchema();//schema信息 MycatLogicTable logicTable = relOptTable.unwrap(MycatLogicTable.class); RelNode value = null; if (logicTable != null) { switch (logicTable.getTable().getType()) { case SHARDING: value = shardingTable(builder, bindableTableScan, cluster, relOptSchema, logicTable); break; case GLOBAL: value = global(cluster, bindableTableScan, relOptSchema, logicTable); } } else { value = bindableTableScan; } return value; }
Example #6
Source File: PushDownLogicTableRule.java From Mycat2 with GNU General Public License v3.0 | 5 votes |
@NotNull private static RelNode getBindableTableScan(Bindables.BindableTableScan bindableTableScan, RelOptCluster cluster, RelOptSchema relOptSchema, DataNode backendTableInfo) { String uniqueName = backendTableInfo.getUniqueName(); MycatLogicTable unwrap = bindableTableScan.getTable().unwrap(MycatLogicTable.class); MycatPhysicalTable mycatPhysicalTable = new MycatPhysicalTable(unwrap,backendTableInfo); RelOptTable dataNode = RelOptTableImpl.create( relOptSchema, mycatPhysicalTable.getRowType(cluster.getTypeFactory()), mycatPhysicalTable, ImmutableList.of(uniqueName)); RelNode logicalTableScan = LogicalTableScan.create(cluster, dataNode, ImmutableList.of()); return RelOptUtil.createProject(RelOptUtil.createFilter(logicalTableScan, bindableTableScan.filters), bindableTableScan.projects); }
Example #7
Source File: HBTQueryConvertor.java From Mycat2 with GNU General Public License v3.0 | 5 votes |
public RelNode filterFromTable(FilterFromTableSchema input) { List<String> names = input.getNames(); relBuilder.scan(names); TableScan tableScan = (TableScan) relBuilder.peek(); RelOptTable table = tableScan.getTable(); relBuilder.as(names.get(names.size() - 1)); relBuilder.filter(toRex(input.getFilter())); Filter build = (Filter) relBuilder.build(); Bindables.BindableTableScan bindableTableScan = Bindables.BindableTableScan.create(build.getCluster(), table, build.getChildExps(), TableScan.identity(table)); relBuilder.clear(); return PushDownLogicTableRule.BindableTableScan.toPhyTable(relBuilder, bindableTableScan); }
Example #8
Source File: SqlWorker.java From quark with Apache License 2.0 | 5 votes |
public RelNode run(RelOptPlanner planner, RelNode rel, RelTraitSet requiredOutputTraits, List<RelOptMaterialization> materializations, List<RelOptLattice> lattices) { planner.clear(); planner.addRelTraitDef(ConventionTraitDef.INSTANCE); planner.addRelTraitDef(RelCollationTraitDef.INSTANCE); //((VolcanoPlanner) planner).registerAbstractRelationalRules(); RelOptUtil.registerAbstractRels(planner); for (RelOptRule rule : ruleSet) { planner.addRule(rule); } planner.addRule(Bindables.BINDABLE_TABLE_SCAN_RULE); planner.addRule(ProjectTableScanRule.INSTANCE); planner.addRule(ProjectTableScanRule.INTERPRETER); planner.addRule(EnumerableInterpreterRule.INSTANCE); final CalciteSchema rootSchema = CalciteSchema.from(context.getRootSchema()); planner.setExecutor(new RexExecutorImpl(null)); planner.setRoot(rel); MaterializationService.setThreadLocal(materializationService); plannerHolder.setPlanner(planner); populateMaterializationsAndLattice(plannerHolder, rootSchema); if (!rel.getTraitSet().equals(requiredOutputTraits)) { rel = planner.changeTraits(rel, requiredOutputTraits); planner.setRoot(rel); } RelOptPlanner planner2 = planner.chooseDelegate(); return planner2.findBestExp(); }
Example #9
Source File: EnumerableJoinTest.java From calcite with Apache License 2.0 | 4 votes |
/** Test case for * <a href="https://issues.apache.org/jira/browse/CALCITE-3820">[CALCITE-3820] * EnumerableDefaults#orderBy should be lazily computed + support enumerator * re-initialization</a>. */ @Test void testRepeatUnionWithMergeJoin() { tester(false, new HierarchySchema()) .query("?") .withHook(Hook.PLANNER, (Consumer<RelOptPlanner>) planner -> { planner.addRule(Bindables.BINDABLE_TABLE_SCAN_RULE); planner.addRule(EnumerableRules.ENUMERABLE_MERGE_JOIN_RULE); planner.removeRule(EnumerableRules.ENUMERABLE_JOIN_RULE); }) // Note: explicit sort is used so EnumerableMergeJoin can actually work .withRel(builder -> builder // WITH RECURSIVE delta(empid, name) as ( // SELECT empid, name FROM emps WHERE empid = 2 // UNION ALL // SELECT e.empid, e.name FROM delta d // JOIN hierarchies h ON d.empid = h.managerid // JOIN emps e ON h.subordinateid = e.empid // ) // SELECT empid, name FROM delta .scan("s", "emps") .filter( builder.equals( builder.field("empid"), builder.literal(2))) .project( builder.field("emps", "empid"), builder.field("emps", "name")) .transientScan("#DELTA#") .sort(builder.field("empid")) .scan("s", "hierarchies") .sort(builder.field("managerid")) .join( JoinRelType.INNER, builder.equals( builder.field(2, "#DELTA#", "empid"), builder.field(2, "hierarchies", "managerid"))) .sort(builder.field("subordinateid")) .scan("s", "emps") .sort(builder.field("empid")) .join( JoinRelType.INNER, builder.equals( builder.field(2, "hierarchies", "subordinateid"), builder.field(2, "emps", "empid"))) .project( builder.field("emps", "empid"), builder.field("emps", "name")) .repeatUnion("#DELTA#", true) .build() ) .explainHookMatches("" // It is important to have MergeJoin + EnumerableSort in the plan + "EnumerableRepeatUnion(all=[true])\n" + " EnumerableTableSpool(readType=[LAZY], writeType=[LAZY], table=[[#DELTA#]])\n" + " EnumerableCalc(expr#0..4=[{inputs}], expr#5=[2], expr#6=[=($t0, $t5)], empid=[$t0], name=[$t2], $condition=[$t6])\n" + " EnumerableTableScan(table=[[s, emps]])\n" + " EnumerableTableSpool(readType=[LAZY], writeType=[LAZY], table=[[#DELTA#]])\n" + " EnumerableCalc(expr#0..8=[{inputs}], empid=[$t4], name=[$t6])\n" + " EnumerableMergeJoin(condition=[=($3, $4)], joinType=[inner])\n" + " EnumerableSort(sort0=[$3], dir0=[ASC])\n" + " EnumerableMergeJoin(condition=[=($0, $2)], joinType=[inner])\n" + " EnumerableSort(sort0=[$0], dir0=[ASC])\n" + " EnumerableInterpreter\n" + " BindableTableScan(table=[[#DELTA#]])\n" + " EnumerableSort(sort0=[$0], dir0=[ASC])\n" + " EnumerableTableScan(table=[[s, hierarchies]])\n" + " EnumerableSort(sort0=[$0], dir0=[ASC])\n" + " EnumerableTableScan(table=[[s, emps]])\n") .returnsUnordered("" + "empid=2; name=Emp2\n" + "empid=3; name=Emp3\n" + "empid=5; name=Emp5"); }