@Override public void onMatch(RelOptRuleCall call) { final LimitPrel limit = (LimitPrel) call.rel(0); final UnionExchangePrel unionExchangePrel = (UnionExchangePrel) call.rel(1); RelNode child = unionExchangePrel.getInput(); final int offset = limit.getOffset() != null ? Math.max(0, RexLiteral.intValue(limit.getOffset())) : 0; final int fetch = Math.max(0, RexLiteral.intValue(limit.getFetch())); // child Limit uses conservative approach: use offset 0 and fetch = parent limit offset + parent limit fetch. final RexNode childFetch = limit.getCluster().getRexBuilder().makeExactLiteral(BigDecimal.valueOf(offset + fetch)); final RelNode limitUnderExchange = new LimitPrel(child.getCluster(), child.getTraitSet(), child, null, childFetch); final RelNode newUnionExch = new UnionExchangePrel(unionExchangePrel.getCluster(), unionExchangePrel.getTraitSet(), limitUnderExchange); final RelNode limitAboveExchange = new LimitPrel(limit.getCluster(), limit.getTraitSet(), newUnionExch, limit.getOffset(), limit.getFetch(), true); call.transformTo(limitAboveExchange); }
@Override public RelNode copy(RelTraitSet traitSet, List<RelNode> inputs) { return new LimitPrel(getCluster(), traitSet, sole(inputs), offset, fetch, isPushDown()); }
private PrelWithDictionaryInfo visitLimit(LimitPrel limitPrel, Void value) { final PrelWithDictionaryInfo newInput = ((Prel)limitPrel.getInput()).accept(this, value); if (limitPrel.getInput() == newInput.getPrel()) { return new PrelWithDictionaryInfo(limitPrel); } return new PrelWithDictionaryInfo((Prel)limitPrel.copy(limitPrel.getTraitSet(), Collections.<RelNode>singletonList(newInput.getPrel())), newInput.getFields()); }
@Override public boolean matches(RelOptRuleCall call) { final LimitPrel limit = (LimitPrel) call.rel(0); // Two situations we do not fire this rule: // 1) limit has been pushed down to its child, // 2) the fetch() is null (indicating we have to fetch all the remaining rows starting from offset. return !limit.isPushDown() && limit.getFetch() != null; }
public boolean isTrivial(){ Integer last = getLast(); if(last != null && last <= PrelUtil.getPlannerSettings(getCluster()).getSliceTarget() ){ return true; } else { return false; } }
@Override public boolean matches(RelOptRuleCall call) { final LimitPrel limit = call.rel(0); final ElasticsearchIntermediatePrel intermediatePrel = call.rel(1); if (intermediatePrel.hasTerminalPrel()) { return false; } // TODO: this can probably be supported in many cases. if (limit.getOffset() != null && RexLiteral.intValue(limit.getOffset()) != 0) { return false; } final PlannerSettings plannerSettings = PrelUtil.getPlannerSettings(limit.getCluster().getPlanner()); if (intermediatePrel.contains(ElasticsearchSample.class) && limit.getFetch() != null && RexLiteral.intValue(limit.getFetch()) >= SampleCrel.getSampleSizeAndSetMinSampleSize(plannerSettings, ElasticSampleRule.SAMPLE_SIZE_DENOMINATOR)) { return false; } return true; }
@Override public void onMatch(RelOptRuleCall call) { final LimitPrel limitPrel = call.rel(0); final ElasticsearchIntermediatePrel intermediatePrel = call.rel(1); final ElasticsearchLimit newLimit = new ElasticsearchLimit( intermediatePrel.getInput().getCluster(), intermediatePrel.getInput().getTraitSet(), intermediatePrel.getInput(), limitPrel.getOffset(), limitPrel.getFetch(), limitPrel.isPushDown(), intermediatePrel.getPluginId()); final ElasticsearchSample sample = intermediatePrel.getNoCheck(ElasticsearchSample.class); if(sample != null){ // we do not allow a sample and limit to coexist in the elastic tree, need to collapse them. final ElasticsearchIntermediatePrel withoutSample = intermediatePrel.filter(new Predicate<RelNode>(){ @Override public boolean apply(RelNode input) { return !(input instanceof ElasticsearchSample); }}); final ElasticsearchLimit mergedLimitSample = newLimit.merge(sample, withoutSample.getInput()); call.transformTo(intermediatePrel.withNewInput(mergedLimitSample)); } else { final ElasticsearchIntermediatePrel newInter = intermediatePrel.withNewInput(newLimit); call.transformTo(newInter); } } }
private Prel newLimit(int offset, int fetch, Prel child) { return new LimitPrel(cluster, traits, child, rexBuilder.makeBigintLiteral(new BigDecimal(offset)), rexBuilder.makeBigintLiteral(new BigDecimal(fetch)) ); }
@Override public PhysicalOperator getPhysicalOperator(PhysicalPlanCreator creator) throws IOException { Prel child = (Prel) this.getInput(); PhysicalOperator childPOP = child.getPhysicalOperator(creator); int first = getFirst(); Integer last = getLast(); Limit limit = new Limit(childPOP, first, last); return creator.addMetadata(this, limit); }
@Override public Prel visitPrel(Prel prel, Void value) throws IOException { if (prel instanceof LimitPrel) { LimitPrel limit = (LimitPrel) prel; if(isLimit0(limit.getFetch())){ PhysicalOperator op = PrelTransformer.convertToPop(config, prel); BatchSchema schema = op.getSchema(config.getContext().getFunctionRegistry()); // make sure to remove any selection vector modes since we're now the leaf node. schema = schema.clone(SelectionVectorMode.NONE); return new EmptyPrel(prel.getCluster(), prel.getTraitSet(), prel.getRowType(), schema); } } List<RelNode> children = new ArrayList<>(); for(Prel child : prel){ children.add(child.accept(this, null)); } return (Prel) prel.copy(prel.getTraitSet(), children); }
private Integer getLast(){ // Last offset to stop including into results (exclusive), translating fetch row counts into an offset. // Null value implies including entire remaining result set from first offset return fetch != null ? Math.max(0, RexLiteral.intValue(fetch)) + getFirst() : null; }
@Override public Iterator<Prel> iterator() { return PrelUtil.iter(getInput()); }
public static Prel addLimitPrel(Prel input, long fetchSize) { RexBuilder builder = input.getCluster().getRexBuilder(); return new LimitPrel( input.getCluster(), input.getTraitSet(), input, builder.makeBigintLiteral(BigDecimal.ZERO), builder.makeBigintLiteral(BigDecimal.valueOf(fetchSize))); }
@Override public void onMatch(RelOptRuleCall call) { final LimitPrel limit = (LimitPrel) call.rel(0); final SingleMergeExchangePrel smex = (SingleMergeExchangePrel) call.rel(1); final SortPrel sort = (SortPrel) call.rel(2); // First offset to include into results (inclusive). Null implies it is starting from offset 0 int offset = limit.getOffset() != null ? Math.max(0, RexLiteral.intValue(limit.getOffset())) : 0; int fetch = limit.getFetch() != null? Math.max(0, RexLiteral.intValue(limit.getFetch())) : 0; final TopNPrel topN = new TopNPrel(limit.getCluster(), sort.getTraitSet(), sort.getInput(), offset + fetch, sort.getCollation()); final LimitPrel newLimit = new LimitPrel(limit.getCluster(), limit.getTraitSet(), new SingleMergeExchangePrel(smex.getCluster(), smex.getTraitSet(), topN, sort.getCollation()), limit.getOffset(), limit.getFetch()); call.transformTo(newLimit); }
@Override public void onMatch(RelOptRuleCall call) { final SamplePrel sample = (SamplePrel) call.rel(0); final RelNode input = sample.getInput(); final RelNode convertedInput = convert(input, input.getTraitSet().plus(Prel.PHYSICAL)); final RexBuilder rexBuilder = input.getCluster().getRexBuilder(); final PlannerSettings plannerSettings = PrelUtil.getPlannerSettings(call.getPlanner()); final RexNode offset = rexBuilder.makeBigintLiteral(BigDecimal.ZERO); final RexNode limit = rexBuilder.makeBigintLiteral(BigDecimal.valueOf(SampleCrel.getSampleSizeAndSetMinSampleSize(plannerSettings, 1))); final LimitPrel sampleAsLimit = new LimitPrel(sample.getCluster(), sample.getTraitSet().plus(Prel.PHYSICAL), convertedInput, offset, limit); call.transformTo(sampleAsLimit); } }
@Override public void onMatch(RelOptRuleCall call) { final LimitRel limit = (LimitRel) call.rel(0); final RelNode input = limit.getInput(); final RelTraitSet traits = input.getTraitSet().plus(Prel.PHYSICAL).plus(DistributionTrait.SINGLETON); final RelNode convertedInput = convert(input, traits); LimitPrel newLimit = new LimitPrel(limit.getCluster(), limit.getTraitSet().plus(Prel.PHYSICAL).plus(DistributionTrait.SINGLETON), convertedInput, limit.getOffset(), limit.getFetch()); call.transformTo(newLimit); } }