use of org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveSortLimit in project hive by apache.
the class PlanModifierUtil method fixTopOBSchema.
protected static void fixTopOBSchema(final RelNode rootRel, Pair<RelNode, RelNode> topSelparentPair, List<FieldSchema> resultSchema, boolean replaceProject) throws CalciteSemanticException {
if (!(topSelparentPair.getKey() instanceof Sort) || !HiveCalciteUtil.orderRelNode(topSelparentPair.getKey())) {
return;
}
HiveSortLimit obRel = (HiveSortLimit) topSelparentPair.getKey();
Project obChild = (Project) topSelparentPair.getValue();
if (obChild.getRowType().getFieldCount() <= resultSchema.size()) {
return;
}
RelDataType rt = obChild.getRowType();
@SuppressWarnings({ "unchecked", "rawtypes" }) Set<Integer> collationInputRefs = new HashSet(RelCollations.ordinals(obRel.getCollation()));
ImmutableMap.Builder<Integer, RexNode> inputRefToCallMapBldr = ImmutableMap.builder();
for (int i = resultSchema.size(); i < rt.getFieldCount(); i++) {
if (collationInputRefs.contains(i)) {
RexNode obyExpr = obChild.getProjects().get(i);
if (obyExpr instanceof RexCall) {
LOG.debug("Old RexCall : " + obyExpr);
obyExpr = adjustOBSchema((RexCall) obyExpr, obChild, resultSchema);
LOG.debug("New RexCall : " + obyExpr);
}
inputRefToCallMapBldr.put(i, obyExpr);
}
}
ImmutableMap<Integer, RexNode> inputRefToCallMap = inputRefToCallMapBldr.build();
if ((obChild.getRowType().getFieldCount() - inputRefToCallMap.size()) != resultSchema.size()) {
LOG.error(generateInvalidSchemaMessage(obChild, resultSchema, inputRefToCallMap.size()));
throw new CalciteSemanticException("Result Schema didn't match Optimized Op Tree Schema");
}
if (replaceProject) {
// This removes order-by only expressions from the projections.
HiveProject replacementProjectRel = HiveProject.create(obChild.getInput(), obChild.getProjects().subList(0, resultSchema.size()), obChild.getRowType().getFieldNames().subList(0, resultSchema.size()));
obRel.replaceInput(0, replacementProjectRel);
}
obRel.setInputRefToCallMap(inputRefToCallMap);
}
use of org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveSortLimit in project hive by apache.
the class JDBCSortPushDownRule method onMatch.
@Override
public void onMatch(RelOptRuleCall call) {
LOG.debug("JDBCSortPushDownRule has been called");
final HiveSortLimit sort = call.rel(0);
final HiveJdbcConverter converter = call.rel(1);
RelNode node = (sort.fetch != null && RexLiteral.intValue(sort.fetch) == 0) ? new JdbcFilter(sort.getCluster(), sort.getTraitSet().replace(converter.getJdbcConvention()), converter.getInput(), call.builder().literal(false)) : new JdbcSort(sort.getCluster(), sort.getTraitSet().replace(converter.getJdbcConvention()), converter.getInput(), sort.getCollation(), sort.offset, sort.fetch);
call.transformTo(converter.copy(converter.getTraitSet(), node));
}
use of org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveSortLimit in project hive by apache.
the class HiveSortProjectTransposeRule method onMatch.
// implement RelOptRule
public void onMatch(RelOptRuleCall call) {
final HiveSortLimit sort = call.rel(0);
final HiveProject project = call.rel(1);
// Determine mapping between project input and output fields. If sort
// relies on non-trivial expressions, we can't push.
final Mappings.TargetMapping map = RelOptUtil.permutation(project.getProjects(), project.getInput().getRowType());
for (RelFieldCollation fc : sort.getCollation().getFieldCollations()) {
if (map.getTargetOpt(fc.getFieldIndex()) < 0) {
return;
}
}
// Create new collation
final RelCollation newCollation = RelCollationTraitDef.INSTANCE.canonize(RexUtil.apply(map, sort.getCollation()));
// New operators
final HiveSortLimit newSort = sort.copy(sort.getTraitSet().replace(newCollation), project.getInput(), newCollation, sort.offset, sort.fetch);
final RelNode newProject = project.copy(sort.getTraitSet(), ImmutableList.<RelNode>of(newSort));
call.transformTo(newProject);
}
use of org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveSortLimit in project hive by apache.
the class HiveSortUnionReduceRule method matches.
// ~ Methods ----------------------------------------------------------------
@Override
public boolean matches(RelOptRuleCall call) {
final HiveSortLimit sort = call.rel(0);
final HiveUnion union = call.rel(1);
// 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;
}
use of org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveSortLimit in project hive by apache.
the class HiveSortLimitRemoveRule method matches.
@Override
public boolean matches(RelOptRuleCall call) {
final HiveSortLimit sortLimit = call.rel(0);
Double maxRowCount = call.getMetadataQuery().getMaxRowCount(sortLimit.getInput());
if (maxRowCount != null) {
if (HiveCalciteUtil.pureLimitRelNode(sortLimit)) {
// equal number of rows.
return maxRowCount <= RexLiteral.intValue(sortLimit.getFetchExpr());
} else if (HiveCalciteUtil.pureOrderRelNode(sortLimit)) {
// most one row.
return maxRowCount <= 1;
} else {
// rewrite the operator (rather than removing it).
return maxRowCount <= 1 && RexLiteral.intValue(sortLimit.getFetchExpr()) >= maxRowCount;
}
}
return false;
}
Aggregations