@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 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 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); } } }