@Override public Double getRowCount(Sort rel, RelMetadataQuery mq) { final Double rowCount = mq.getRowCount(rel.getInput()); if (rowCount != null && rel.fetch != null) { final int offset = rel.offset == null ? 0 : RexLiteral.intValue(rel.offset); final int limit = RexLiteral.intValue(rel.fetch); final Double offsetLimit = new Double(offset + limit); // offsetLimit is smaller than rowCount of the input operator // thus, we return the offsetLimit if (offsetLimit < rowCount) { return offsetLimit; } } return rowCount; }
@Override public Double getRowCount(Sort rel, RelMetadataQuery mq) { final Double rowCount = mq.getRowCount(rel.getInput()); if (rowCount != null && rel.fetch != null) { final int offset = rel.offset == null ? 0 : RexLiteral.intValue(rel.offset); final int limit = RexLiteral.intValue(rel.fetch); final Double offsetLimit = new Double(offset + limit); // offsetLimit is smaller than rowCount of the input operator // thus, we return the offsetLimit if (offsetLimit < rowCount) { return offsetLimit; } } return rowCount; }
@Override public boolean matches(RelOptRuleCall call) { final HiveSortLimit sort = call.rel(0); final HiveUnion union = call.rel(1); // We only apply this rule if Union.all is true. // And Sort.fetch is not null and it is more than 0. return union.all && sort.fetch != null // Calite bug CALCITE-987 && RexLiteral.intValue(sort.fetch) > 0; }
@Override public boolean matches(RelOptRuleCall call) { final HiveSortLimit sort = call.rel(0); final HiveUnion union = call.rel(1); // We only apply this rule if Union.all is true. // And Sort.fetch is not null and it is more than 0. return union.all && sort.fetch != null // Calite bug CALCITE-987 && RexLiteral.intValue(sort.fetch) > 0; }
final int firstOffset = (first.offset != null ? RexLiteral.intValue(first.offset) : 0); final int secondOffset = (second.offset != null ? RexLiteral.intValue(second.offset) : 0); } else if (first.fetch == null) { fetch = RexLiteral.intValue(second.fetch); } else if (second.fetch == null) { fetch = Math.max(0, RexLiteral.intValue(first.fetch) - secondOffset); } else { fetch = Math.max( 0, Math.min( RexLiteral.intValue(first.fetch) - secondOffset, RexLiteral.intValue(second.fetch)
if (HiveCalciteUtil.limitRelNode(bottomSortLimit)) { final RexBuilder rexBuilder = topSortLimit.getCluster().getRexBuilder(); int topOffset = topSortLimit.offset == null ? 0 : RexLiteral.intValue(topSortLimit.offset); int topLimit = RexLiteral.intValue(topSortLimit.fetch); int bottomOffset = bottomSortLimit.offset == null ? 0 : RexLiteral.intValue(bottomSortLimit.offset); int bottomLimit = RexLiteral.intValue(bottomSortLimit.fetch);
if (HiveCalciteUtil.limitRelNode(bottomSortLimit)) { final RexBuilder rexBuilder = topSortLimit.getCluster().getRexBuilder(); int topOffset = topSortLimit.offset == null ? 0 : RexLiteral.intValue(topSortLimit.offset); int topLimit = RexLiteral.intValue(topSortLimit.fetch); int bottomOffset = bottomSortLimit.offset == null ? 0 : RexLiteral.intValue(bottomSortLimit.offset); int bottomLimit = RexLiteral.intValue(bottomSortLimit.fetch);
@Override public void onMatch(final RelOptRuleCall call) { final Sort sort = call.rel(0); final DruidUnionRel unionRel = call.rel(1); final int limit = RexLiteral.intValue(sort.fetch); final int offset = sort.offset != null ? RexLiteral.intValue(sort.offset) : 0; final DruidUnionRel newUnionRel = DruidUnionRel.create( unionRel.getQueryMaker(), unionRel.getRowType(), unionRel.getInputs(), unionRel.getLimit() >= 0 ? Math.min(limit + offset, unionRel.getLimit()) : limit + offset ); if (offset == 0) { call.transformTo(newUnionRel); } else { call.transformTo( call.builder() .push(newUnionRel) .sortLimit(offset, -1, Collections.emptyList()) .build() ); } } }
@Override public boolean matches(RelOptRuleCall call) { final HiveSortLimit sortLimit = call.rel(0); // If it is not created by HiveSortJoinReduceRule, we cannot remove it if (!sortLimit.isRuleCreated()) { return false; } // Finally, if we do not reduce the size input enough, we bail out int limit = RexLiteral.intValue(sortLimit.fetch); Double rowCount = call.getMetadataQuery().getRowCount(sortLimit.getInput()); if (rowCount != null && limit <= reductionProportion * rowCount && rowCount - limit >= reductionTuples) { return false; } return true; }
@Override public boolean matches(RelOptRuleCall call) { final HiveSortLimit sortLimit = call.rel(0); // If it is not created by HiveSortJoinReduceRule, we cannot remove it if (!sortLimit.isRuleCreated()) { return false; } // Finally, if we do not reduce the size input enough, we bail out int limit = RexLiteral.intValue(sortLimit.fetch); Double rowCount = RelMetadataQuery.instance().getRowCount(sortLimit.getInput()); if (rowCount != null && limit <= reductionProportion * rowCount && rowCount - limit >= reductionTuples) { return false; } return true; }
final int offset = sort.offset == null ? 0 : RexLiteral.intValue(sort.offset); for (RelNode input : union.getInputs()) { if (RexLiteral.intValue(sort.fetch) + offset < call.getMetadataQuery().getRowCount(input)) { finishPushSortPastUnion = false; .makeExactLiteral(BigDecimal.valueOf(RexLiteral.intValue(sort.fetch) + offset)); HiveSortLimit branchSort = sort.copy(sort.getTraitSet(), input, sort.getCollation(), null, fetchRN);
final Integer limit = sort.fetch != null ? RexLiteral.intValue(sort.fetch) : null; final List<OrderByColumnSpec> orderBys = new ArrayList<>(sort.getChildExps().size());
final int offset = sort.offset == null ? 0 : RexLiteral.intValue(sort.offset); for (RelNode input : union.getInputs()) { if (RexLiteral.intValue(sort.fetch) + offset < RelMetadataQuery.instance().getRowCount(input)) { finishPushSortPastUnion = false; .makeExactLiteral(BigDecimal.valueOf(RexLiteral.intValue(sort.fetch) + offset)); HiveSortLimit branchSort = sort.copy(sort.getTraitSet(), input, sort.getCollation(), null, fetchRN);
private BoundarySpec getWindowBound(RexWindowBound wb) { BoundarySpec boundarySpec; if (wb.isCurrentRow()) { boundarySpec = new BoundarySpec(Direction.CURRENT); } else { final Direction direction; final int amt; if (wb.isPreceding()) { direction = Direction.PRECEDING; } else { direction = Direction.FOLLOWING; } if (wb.isUnbounded()) { amt = BoundarySpec.UNBOUNDED_AMOUNT; } else { amt = RexLiteral.intValue(wb.getOffset()); } boundarySpec = new BoundarySpec(direction, amt); } return boundarySpec; }
); } else if (aggregateCall.getAggregation().getKind() == SqlKind.SUM && Calcites.isIntLiteral(arg1) && RexLiteral.intValue(arg1) == 1 && Calcites.isIntLiteral(arg2) && RexLiteral.intValue(arg2) == 0) { || (aggregateCall.getAggregation().getKind() == SqlKind.SUM && Calcites.isIntLiteral(arg2) && RexLiteral.intValue(arg2) == 0) /* Case A2 */) { newProjects.add(arg1); newProjects.add(filter);
private BoundarySpec getWindowBound(RexWindowBound wb) { BoundarySpec boundarySpec; if (wb.isCurrentRow()) { boundarySpec = new BoundarySpec(Direction.CURRENT); } else { final Direction direction; final int amt; if (wb.isPreceding()) { direction = Direction.PRECEDING; } else { direction = Direction.FOLLOWING; } if (wb.isUnbounded()) { amt = BoundarySpec.UNBOUNDED_AMOUNT; } else { amt = RexLiteral.intValue(wb.getOffset()); } boundarySpec = new BoundarySpec(direction, amt); } return boundarySpec; }
private GroupScan getGroupScanWithLimit(GroupScan groupScan, LimitPrel limit) { final int offset = limit.getOffset() != null ? Math.max(0, RexLiteral.intValue(limit.getOffset())) : 0; final int fetch = Math.max(0, RexLiteral.intValue(limit.getFetch())); // Scan Limit uses conservative approach: use offset 0 and fetch = parent limit offset + parent limit fetch. if (groupScan instanceof JsonTableGroupScan) { JsonTableGroupScan jsonTableGroupScan = (JsonTableGroupScan) groupScan; return (jsonTableGroupScan.clone(jsonTableGroupScan.getScanSpec()).applyLimit(offset + fetch)); } else if (groupScan instanceof BinaryTableGroupScan) { BinaryTableGroupScan binaryTableGroupScan = (BinaryTableGroupScan) groupScan; final HBaseScanSpec oldScanSpec = binaryTableGroupScan.getHBaseScanSpec(); final HBaseScanSpec newScanSpec = new HBaseScanSpec(oldScanSpec.getTableName(), oldScanSpec.getStartRow(), oldScanSpec.getStopRow(), oldScanSpec.getFilter()); return new BinaryTableGroupScan(binaryTableGroupScan.getUserName(), binaryTableGroupScan.getStoragePlugin(), binaryTableGroupScan.getFormatPlugin(), newScanSpec, binaryTableGroupScan.getColumns(), binaryTableGroupScan.getTableStats()).applyLimit(offset + fetch); } return null; }