static HiveTableScan getTableScan(RelNode r, boolean traverseProject) { while (r != null && !(r instanceof HiveTableScan)) { if (r instanceof HepRelVertex) { r = ((HepRelVertex) r).getCurrentRel(); } else if (r instanceof Filter) { r = ((Filter) r).getInput(); } else if (traverseProject && r instanceof Project) { r = ((Project) r).getInput(); } else { r = null; } } return r == null ? null : (HiveTableScan) r; } }
static HiveTableScan getTableScan(RelNode r, boolean traverseProject) { while (r != null && !(r instanceof HiveTableScan)) { if (r instanceof HepRelVertex) { r = ((HepRelVertex) r).getCurrentRel(); } else if (r instanceof Filter) { r = ((Filter) r).getInput(); } else if (traverseProject && r instanceof Project) { r = ((Project) r).getInput(); } else { r = null; } } return r == null ? null : (HiveTableScan) r; }
public RelNode align(Project rel, List<RelFieldCollation> collations) { // 1) We extract the collations indices boolean containsWindowing = false; for (RexNode childExp : rel.getChildExps()) { if (childExp instanceof RexOver) { // TODO: support propagation for partitioning/ordering in windowing containsWindowing = true; break; } } ImmutableList.Builder<RelFieldCollation> propagateCollations = ImmutableList.builder(); if (!containsWindowing) { for (RelFieldCollation c : collations) { RexNode rexNode = rel.getChildExps().get(c.getFieldIndex()); if (rexNode instanceof RexInputRef) { int newIdx = ((RexInputRef) rexNode).getIndex(); propagateCollations.add(c.copy((newIdx))); } } } // 2) We propagate final RelNode child = dispatchAlign(rel.getInput(), propagateCollations.build()); // 3) Return new Project return rel.copy(rel.getTraitSet(), ImmutableList.of(child)); }
@Override public RelOptMaterialization apply(RelOptMaterialization materialization) { final RelNode viewScan = materialization.tableRel; final RelNode newViewScan; if (viewScan instanceof Project) { // There is a Project on top (due to nullability) final Project pq = (Project) viewScan; newViewScan = HiveProject.create(optCluster, copyNodeScan(pq.getInput()), pq.getChildExps(), pq.getRowType(), Collections.<RelCollation> emptyList()); } else { newViewScan = copyNodeScan(viewScan); } return new RelOptMaterialization(newViewScan, materialization.queryRel, null, materialization.qualifiedTableName); }
@Override public void onMatch(RelOptRuleCall call) { final Project project = call.rel(0); boolean changed = false; final RexBuilder rexBuilder = project.getCluster().getRexBuilder(); List<RexNode> newProjects = new ArrayList<>(); for (RexNode oldNode : project.getProjects()) { RexNode newNode = analyzeRexNode(rexBuilder, oldNode); if (!newNode.toString().equals(oldNode.toString())) { changed = true; newProjects.add(newNode); } else { newProjects.add(oldNode); } } if (!changed) { return; } Project newProject = project.copy(project.getTraitSet(), project.getInput(), newProjects, project.getRowType(), project.getFlags()); call.transformTo(newProject); }
private boolean isPartitionPredicate(RexNode expr, RelNode r) { if (r instanceof Project) { expr = RelOptUtil.pushFilterPastProject(expr, (Project) r); return isPartitionPredicate(expr, ((Project) r).getInput()); } else if (r instanceof Filter) { return isPartitionPredicate(expr, ((Filter) r).getInput()); } else if (r instanceof HiveTableScan) { RelOptHiveTable table = (RelOptHiveTable) ((HiveTableScan) r).getTable(); ImmutableBitSet cols = RelOptUtil.InputFinder.bits(expr); return table.containsPartitionColumnsOnly(cols); } return false; }
public Result translate(Project e) { // This variant is for the top project as we want to keep // the column aliases instead of producing STAR Result x = visitChild(0, e.getInput()); parseCorrelTable(e, x); final Builder builder = x.builder(e, Clause.SELECT); final List<SqlNode> selectList = new ArrayList<>(); for (RexNode ref : e.getChildExps()) { SqlNode sqlExpr = builder.context.toSql(null, ref); addSelect(selectList, sqlExpr, e.getRowType()); } builder.setSelect(new SqlNodeList(selectList, POS)); return builder.result(); }
@Override public RelNode convert(RelNode rel) { final Project project = (Project) rel; final RelNode input = project.getInput(); return new StreamsProjectRel(project.getCluster(), project.getTraitSet().replace(StreamsLogicalConvention.INSTANCE), convert(input, input.getTraitSet().replace(StreamsLogicalConvention.INSTANCE)), project.getProjects(), project.getRowType()); } }
private boolean isPartitionPredicate(RexNode expr, RelNode r) { if (r instanceof Project) { expr = RelOptUtil.pushFilterPastProject(expr, (Project) r); return isPartitionPredicate(expr, ((Project) r).getInput()); } else if (r instanceof Filter) { return isPartitionPredicate(expr, ((Filter) r).getInput()); } else if (r instanceof HiveTableScan) { RelOptHiveTable table = (RelOptHiveTable) ((HiveTableScan) r).getTable(); ImmutableBitSet cols = RelOptUtil.InputFinder.bits(expr); return table.containsPartitionColumnsOnly(cols); } return false; }
private static RelNode getNewProject(RexNode filterCondToPushBelowProj, RexNode unPushedFilCondAboveProj, Project oldProj, RelDataTypeFactory typeFactory, RelBuilder relBuilder) { // convert the filter to one that references the child of the project RexNode newPushedCondition = RelOptUtil.pushPastProject(filterCondToPushBelowProj, oldProj); // Remove cast of BOOLEAN NOT NULL to BOOLEAN or vice versa. Filter accepts // nullable and not-nullable conditions, but a CAST might get in the way of // other rewrites. if (RexUtil.isNullabilityCast(typeFactory, newPushedCondition)) { newPushedCondition = ((RexCall) newPushedCondition).getOperands().get(0); } RelNode newPushedFilterRel = relBuilder.push(oldProj.getInput()).filter(newPushedCondition).build(); RelNode newProjRel = relBuilder.push(newPushedFilterRel) .project(oldProj.getProjects(), oldProj.getRowType().getFieldNames()).build(); if (unPushedFilCondAboveProj != null) { // Remove cast of BOOLEAN NOT NULL to BOOLEAN or vice versa. Filter accepts // nullable and not-nullable conditions, but a CAST might get in the way of // other rewrites. if (RexUtil.isNullabilityCast(typeFactory, newPushedCondition)) { unPushedFilCondAboveProj = ((RexCall) unPushedFilCondAboveProj).getOperands().get(0); } newProjRel = relBuilder.push(newProjRel).filter(unPushedFilCondAboveProj).build(); } return newProjRel; }
input = project.getInput();
public RelNode align(Project rel, List<RelFieldCollation> collations) { // 1) We extract the collations indices boolean containsWindowing = false; for (RexNode childExp : rel.getChildExps()) { if (childExp instanceof RexOver) { // TODO: support propagation for partitioning/ordering in windowing containsWindowing = true; break; } } ImmutableList.Builder<RelFieldCollation> propagateCollations = ImmutableList.builder(); if (!containsWindowing) { for (RelFieldCollation c : collations) { RexNode rexNode = rel.getChildExps().get(c.getFieldIndex()); if (rexNode instanceof RexInputRef) { int newIdx = ((RexInputRef) rexNode).getIndex(); propagateCollations.add(c.copy((newIdx))); } } } // 2) We propagate final RelNode child = dispatchAlign(rel.getInput(), propagateCollations.build()); // 3) Return new Project return rel.copy(rel.getTraitSet(), ImmutableList.of(child)); }
public void onMatch(RelOptRuleCall call) { final Filter filter = call.rel(0); final Project project = call.rel(1); final List<RexNode> newProjects = new ArrayList<>(project.getProjects()); newProjects.add(filter.getCondition()); final RelOptCluster cluster = filter.getCluster(); RelDataType newRowType = cluster.getTypeFactory().builder() .addAll(project.getRowType().getFieldList()) .add("condition", Util.last(newProjects).getType()) .build(); final RelNode newProject = project.copy(project.getTraitSet(), project.getInput(), newProjects, newRowType); final RexInputRef newCondition = cluster.getRexBuilder().makeInputRef(newProject, newProjects.size() - 1); call.transformTo(filter.copy(filter.getTraitSet(), newProject, newCondition)); } }
private static RelNode getNewProject(RexNode filterCondToPushBelowProj, RexNode unPushedFilCondAboveProj, Project oldProj, RelDataTypeFactory typeFactory, RelBuilder relBuilder) { // convert the filter to one that references the child of the project RexNode newPushedCondition = RelOptUtil.pushPastProject(filterCondToPushBelowProj, oldProj); // Remove cast of BOOLEAN NOT NULL to BOOLEAN or vice versa. Filter accepts // nullable and not-nullable conditions, but a CAST might get in the way of // other rewrites. if (RexUtil.isNullabilityCast(typeFactory, newPushedCondition)) { newPushedCondition = ((RexCall) newPushedCondition).getOperands().get(0); } RelNode newPushedFilterRel = relBuilder.push(oldProj.getInput()).filter(newPushedCondition).build(); RelNode newProjRel = relBuilder.push(newPushedFilterRel) .project(oldProj.getProjects(), oldProj.getRowType().getFieldNames()).build(); if (unPushedFilCondAboveProj != null) { // Remove cast of BOOLEAN NOT NULL to BOOLEAN or vice versa. Filter accepts // nullable and not-nullable conditions, but a CAST might get in the way of // other rewrites. if (RexUtil.isNullabilityCast(typeFactory, newPushedCondition)) { unPushedFilCondAboveProj = ((RexCall) unPushedFilCondAboveProj).getOperands().get(0); } newProjRel = relBuilder.push(newProjRel).filter(unPushedFilCondAboveProj).build(); } return newProjRel; }
public void onMatch(RelOptRuleCall call) { final Project topProject = call.rel(0); final Project bottomProject = call.rel(1); // If top project does not reference any column at the bottom project, // we can just remove botton project final ImmutableBitSet topRefs = RelOptUtil.InputFinder.bits(topProject.getChildExps(), null); if (topRefs.isEmpty()) { RelBuilder relBuilder = call.builder(); relBuilder.push(bottomProject.getInput()); relBuilder.project(topProject.getChildExps()); call.transformTo(relBuilder.build()); return; } super.onMatch(call); }
final Join swappedJoin = (Join) bottomProject.getInput(0); call.transformTo(swappedJoin);
public void onMatch(RelOptRuleCall call) { final Project project = call.rel(0); //TODO: replace HiveSubQRemoveRelBuilder with calcite's once calcite 1.11.0 is released final HiveSubQRemoveRelBuilder builder = new HiveSubQRemoveRelBuilder(null, call.rel(0).getCluster(), null); final RexSubQuery e = RexUtil.SubQueryFinder.find(project.getProjects()); assert e != null; final RelOptUtil.Logic logic = LogicVisitor.find(RelOptUtil.Logic.TRUE_FALSE_UNKNOWN, project.getProjects(), e); builder.push(project.getInput()); final int fieldCount = builder.peek().getRowType().getFieldCount(); final RexNode target = apply(e, HiveFilter.getVariablesSet(e), logic, builder, 1, fieldCount, false); final RexShuttle shuttle = new ReplaceSubQueryShuttle(e, target); builder.project(shuttle.apply(project.getProjects()), project.getRowType().getFieldNames()); call.transformTo(builder.build()); } };
final RelNode swappedJoin = swappedProject.getInput(); assert(swappedJoin instanceof Join);