org.apache.calcite.rel.core.Sort Java Examples

The following examples show how to use org.apache.calcite.rel.core.Sort. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RelMdMaxRowCount.java    From calcite with Apache License 2.0 6 votes vote down vote up
public Double getMaxRowCount(Sort rel, RelMetadataQuery mq) {
  Double rowCount = mq.getMaxRowCount(rel.getInput());
  if (rowCount == null) {
    rowCount = Double.POSITIVE_INFINITY;
  }
  final int offset = rel.offset == null ? 0 : RexLiteral.intValue(rel.offset);
  rowCount = Math.max(rowCount - offset, 0D);

  if (rel.fetch != null) {
    final int limit = RexLiteral.intValue(rel.fetch);
    if (limit < rowCount) {
      return (double) limit;
    }
  }
  return rowCount;
}
 
Example #2
Source File: OLAPLimitRule.java    From kylin with Apache License 2.0 6 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {
    final Sort sort = call.rel(0);
    if (sort.offset == null && sort.fetch == null) {
        return;
    }

    RelTraitSet origTraitSet = sort.getTraitSet();
    RelTraitSet traitSet = origTraitSet.replace(OLAPRel.CONVENTION).simplify();

    RelNode input = sort.getInput();
    if (!sort.getCollation().getFieldCollations().isEmpty()) {
        // Create a sort with the same sort key, but no offset or fetch.
        input = sort.copy(sort.getTraitSet(), input, sort.getCollation(), null, null);
    }
    RelNode x = convert(input, input.getTraitSet().replace(OLAPRel.CONVENTION));
    call.transformTo(new OLAPLimitRel(sort.getCluster(), traitSet, x, sort.offset, sort.fetch));
}
 
Example #3
Source File: DruidQuery.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Returns a string describing the operations inside this query.
 *
 * <p>For example, "sfpahol" means {@link TableScan} (s)
 * followed by {@link Filter} (f)
 * followed by {@link Project} (p)
 * followed by {@link Aggregate} (a)
 * followed by {@link Filter} (h)
 * followed by {@link Project} (o)
 * followed by {@link Sort} (l).
 *
 * @see #isValidSignature(String)
 */
String signature() {
  final StringBuilder b = new StringBuilder();
  boolean flag = false;
  for (RelNode rel : rels) {
    b.append(rel instanceof TableScan ? 's'
        : (rel instanceof Project && flag) ? 'o'
            : (rel instanceof Filter && flag) ? 'h'
                : rel instanceof Aggregate ? 'a'
                    : rel instanceof Filter ? 'f'
                        : rel instanceof Sort ? 'l'
                            : rel instanceof Project ? 'p'
                                : '!');
    flag = flag || rel instanceof Aggregate;
  }
  return b.toString();
}
 
Example #4
Source File: RelMdMinRowCount.java    From Bats with Apache License 2.0 6 votes vote down vote up
public Double getMinRowCount(Sort rel, RelMetadataQuery mq) {
  Double rowCount = mq.getMinRowCount(rel.getInput());
  if (rowCount == null) {
    rowCount = 0D;
  }
  final int offset = rel.offset == null ? 0 : RexLiteral.intValue(rel.offset);
  rowCount = Math.max(rowCount - offset, 0D);

  if (rel.fetch != null) {
    final int limit = RexLiteral.intValue(rel.fetch);
    if (limit < rowCount) {
      return (double) limit;
    }
  }
  return rowCount;
}
 
Example #5
Source File: DrillLimitRule.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {
  final Sort incomingSort = call.rel(0);
  final RelTraitSet incomingTraits = incomingSort.getTraitSet();
  RelNode input = incomingSort.getInput();

  // if the Optiq sort rel includes a collation and a limit, we need to create a copy the sort rel that excludes the
  // limit information.
  if (!incomingSort.getCollation().getFieldCollations().isEmpty()) {
    input = incomingSort.copy(incomingTraits, input, incomingSort.getCollation(), null, null);
  }

  RelNode convertedInput = convert(input, input.getTraitSet().plus(DrillRel.DRILL_LOGICAL).simplify());
  call.transformTo(new DrillLimitRel(
      incomingSort.getCluster(), convertedInput.getTraitSet().plus(DrillRel.DRILL_LOGICAL),
      convertedInput, incomingSort.offset, incomingSort.fetch));
}
 
Example #6
Source File: RelToSqlConverter.java    From Bats with Apache License 2.0 6 votes vote down vote up
/** @see #dispatch */
public Result visit(Sort e) {
  Result x = visitChild(0, e.getInput());
  Builder builder = x.builder(e, Clause.ORDER_BY);
  List<SqlNode> orderByList = FluentListUtils.list();
  for (RelFieldCollation field : e.getCollation().getFieldCollations()) {
    builder.addOrderItem(orderByList, field);
  }
  if (!orderByList.isEmpty()) {
    builder.setOrderBy(new SqlNodeList(orderByList, POS));
    x = builder.result();
  }
  if (e.fetch != null) {
    builder = x.builder(e, Clause.FETCH);
    builder.setFetch(builder.context.toSql(null, e.fetch));
    x = builder.result();
  }
  if (e.offset != null) {
    builder = x.builder(e, Clause.OFFSET);
    builder.setOffset(builder.context.toSql(null, e.offset));
    x = builder.result();
  }
  return x;
}
 
Example #7
Source File: RelMdMinRowCount.java    From calcite with Apache License 2.0 6 votes vote down vote up
public Double getMinRowCount(Sort rel, RelMetadataQuery mq) {
  Double rowCount = mq.getMinRowCount(rel.getInput());
  if (rowCount == null) {
    rowCount = 0D;
  }
  final int offset = rel.offset == null ? 0 : RexLiteral.intValue(rel.offset);
  rowCount = Math.max(rowCount - offset, 0D);

  if (rel.fetch != null) {
    final int limit = RexLiteral.intValue(rel.fetch);
    if (limit < rowCount) {
      return (double) limit;
    }
  }
  return rowCount;
}
 
Example #8
Source File: EnumerableLimitRule.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public void onMatch(RelOptRuleCall call) {
  final Sort sort = call.rel(0);
  if (sort.offset == null && sort.fetch == null) {
    return;
  }
  RelNode input = sort.getInput();
  if (!sort.getCollation().getFieldCollations().isEmpty()) {
    // Create a sort with the same sort key, but no offset or fetch.
    input = sort.copy(
        sort.getTraitSet(),
        input,
        sort.getCollation(),
        null,
        null);
  }
  call.transformTo(
      EnumerableLimit.create(
          convert(input, input.getTraitSet().replace(EnumerableConvention.INSTANCE)),
          sort.offset,
          sort.fetch));
}
 
Example #9
Source File: RelMdMaxRowCount.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Double getMaxRowCount(RelSubset rel, RelMetadataQuery mq) {
  // FIXME This is a short-term fix for [CALCITE-1018]. A complete
  // solution will come with [CALCITE-1048].
  Util.discard(Bug.CALCITE_1048_FIXED);
  for (RelNode node : rel.getRels()) {
    if (node instanceof Sort) {
      Sort sort = (Sort) node;
      if (sort.fetch != null) {
        return (double) RexLiteral.intValue(sort.fetch);
      }
    }
  }

  return Double.POSITIVE_INFINITY;
}
 
Example #10
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testNodeTypeCountSort() {
  final String sql = "select * from emp order by ename";
  final Map<Class<? extends RelNode>, Integer> expected = new HashMap<>();
  expected.put(TableScan.class, 1);
  expected.put(Project.class, 1);
  expected.put(Sort.class, 1);
  checkNodeTypeCount(sql, expected);
}
 
Example #11
Source File: RelToSqlConverter.java    From calcite with Apache License 2.0 5 votes vote down vote up
public boolean hasTrickyRollup(Sort e, Aggregate aggregate) {
  return !dialect.supportsAggregateFunction(SqlKind.ROLLUP)
      && dialect.supportsGroupByWithRollup()
      && (aggregate.getGroupType() == Aggregate.Group.ROLLUP
          || aggregate.getGroupType() == Aggregate.Group.CUBE
              && aggregate.getGroupSet().cardinality() == 1)
      && e.collation.getFieldCollations().stream().allMatch(fc ->
          fc.getFieldIndex() < aggregate.getGroupSet().cardinality());
}
 
Example #12
Source File: SortUnionTransposeRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override public boolean matches(RelOptRuleCall call) {
  final Sort sort = call.rel(0);
  final Union union = call.rel(1);
  // We only apply this rule if Union.all is true and Sort.offset is null.
  // There is a flag indicating if this rule should be applied when
  // Sort.fetch is null.
  return union.all
      && sort.offset == null
      && (matchNullFetch || sort.fetch != null);
}
 
Example #13
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testNodeTypeCountSortLimitOffsetOnFinite() {
  final String sql = "select * from (select * from emp limit 12)\n"
      + "order by ename limit 20 offset 5";
  final Map<Class<? extends RelNode>, Integer> expected = new HashMap<>();
  expected.put(TableScan.class, 1);
  expected.put(Project.class, 2);
  expected.put(Sort.class, 2);
  checkNodeTypeCount(sql, expected);
}
 
Example #14
Source File: CassandraRules.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelNode convert(Sort sort, CassandraFilter filter) {
  final RelTraitSet traitSet =
      sort.getTraitSet().replace(CassandraRel.CONVENTION)
          .replace(sort.getCollation());
  return new CassandraSort(sort.getCluster(), traitSet,
      convert(sort.getInput(), traitSet.replace(RelCollations.EMPTY)),
      sort.getCollation());
}
 
Example #15
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testNodeTypeCountJoinEmptyEmpty() {
  final String sql = "select * from (select * from emp limit 0) as emp\n"
      + "inner join (select * from dept limit 0) as dept\n"
      + "on emp.deptno = dept.deptno";
  final Map<Class<? extends RelNode>, Integer> expected = new HashMap<>();
  expected.put(TableScan.class, 2);
  expected.put(Join.class, 1);
  expected.put(Project.class, 3);
  expected.put(Sort.class, 2);
  checkNodeTypeCount(sql, expected);
}
 
Example #16
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testNodeTypeCountSortLimitOffset() {
  final String sql = "select * from emp order by ename limit 10 offset 5";
  final Map<Class<? extends RelNode>, Integer> expected = new HashMap<>();
  expected.put(TableScan.class, 1);
  expected.put(Project.class, 1);
  expected.put(Sort.class, 1);
  checkNodeTypeCount(sql, expected);
}
 
Example #17
Source File: SortJoinTransposeRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a SortJoinTransposeRule. */
public SortJoinTransposeRule(Class<? extends Sort> sortClass,
    Class<? extends Join> joinClass, RelBuilderFactory relBuilderFactory) {
  super(
      operand(sortClass,
          operand(joinClass, any())),
      relBuilderFactory, null);
}
 
Example #18
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testNodeTypeCountJoinFinite() {
  final String sql = "select * from (select * from emp limit 14) as emp\n"
      + "inner join (select * from dept limit 4) as dept\n"
      + "on emp.deptno = dept.deptno";
  final Map<Class<? extends RelNode>, Integer> expected = new HashMap<>();
  expected.put(TableScan.class, 2);
  expected.put(Join.class, 1);
  expected.put(Project.class, 3);
  expected.put(Sort.class, 2);
  checkNodeTypeCount(sql, expected);
}
 
Example #19
Source File: SortRemoveConstantKeysRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override public void onMatch(RelOptRuleCall call) {
  final Sort sort = call.rel(0);
  final RelMetadataQuery mq = call.getMetadataQuery();
  final RelNode input = sort.getInput();
  final RelOptPredicateList predicates = mq.getPulledUpPredicates(input);
  if (predicates == null) {
    return;
  }

  final RexBuilder rexBuilder = sort.getCluster().getRexBuilder();
  final List<RelFieldCollation> collationsList =
      sort.getCollation().getFieldCollations().stream()
          .filter(fc ->
              !predicates.constantMap.containsKey(
                  rexBuilder.makeInputRef(input, fc.getFieldIndex())))
          .collect(Collectors.toList());

  if (collationsList.size() == sort.collation.getFieldCollations().size()) {
    return;
  }

  // No active collations. Remove the sort completely
  if (collationsList.isEmpty() && sort.offset == null && sort.fetch == null) {
    call.transformTo(input);
    call.getPlanner().setImportance(sort, 0.0);
    return;
  }

  final Sort result =
      sort.copy(sort.getTraitSet(), input, RelCollations.of(collationsList));
  call.transformTo(result);
  call.getPlanner().setImportance(sort, 0.0);
}
 
Example #20
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testNodeTypeCountSortLimit() {
  final String sql = "select * from emp order by ename limit 10";
  final Map<Class<? extends RelNode>, Integer> expected = new HashMap<>();
  expected.put(TableScan.class, 1);
  expected.put(Project.class, 1);
  expected.put(Sort.class, 1);
  checkNodeTypeCount(sql, expected);
}
 
Example #21
Source File: RelDecorrelator.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Rewrite Sort.
 *
 * @param rel Sort to be rewritten
 */
public Frame decorrelateRel(Sort rel) {
    //
    // Rewrite logic:
    //
    // 1. change the collations field to reference the new input.
    //

    // Sort itself should not reference corVars.
    assert !cm.mapRefRelToCorRef.containsKey(rel);

    // Sort only references field positions in collations field.
    // The collations field in the newRel now need to refer to the
    // new output positions in its input.
    // Its output does not change the input ordering, so there's no
    // need to call propagateExpr.

    final RelNode oldInput = rel.getInput();
    final Frame frame = getInvoke(oldInput, rel);
    if (frame == null) {
        // If input has not been rewritten, do not rewrite this rel.
        return null;
    }
    final RelNode newInput = frame.r;

    Mappings.TargetMapping mapping = Mappings.target(frame.oldToNewOutputs, oldInput.getRowType().getFieldCount(),
            newInput.getRowType().getFieldCount());

    RelCollation oldCollation = rel.getCollation();
    RelCollation newCollation = RexUtil.apply(mapping, oldCollation);

    final Sort newSort = LogicalSort.create(newInput, newCollation, rel.offset, rel.fetch);

    // Sort does not change input ordering
    return register(rel, newSort, frame.oldToNewOutputs, frame.corDefOutputs);
}
 
Example #22
Source File: ProjectSortTransposeRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a ProjectSortTransposeRule. */
private ProjectSortTransposeRule(Class<Project> projectClass,
    Class<Sort> sortClass, RelBuilderFactory relBuilderFactory) {
  this(
      operand(projectClass,
          operand(sortClass, any())),
      relBuilderFactory, null);
}
 
Example #23
Source File: RelOptUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Whether this node contains a limit specification.
 */
public static boolean isLimit(RelNode rel) {
    if ((rel instanceof Sort) && ((Sort) rel).fetch != null) {
        return true;
    }
    return false;
}
 
Example #24
Source File: RelOptUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Whether this node contains a sort specification.
 */
public static boolean isOrder(RelNode rel) {
    if ((rel instanceof Sort) && !((Sort) rel).getCollation().getFieldCollations().isEmpty()) {
        return true;
    }
    return false;
}
 
Example #25
Source File: SortJoinCopyRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a SortJoinCopyRule. */
public SortJoinCopyRule(Class<? extends Sort> sortClass,
    Class<? extends Join> joinClass, RelBuilderFactory relBuilderFactory) {
  super(
      operand(sortClass,
          operand(joinClass, any())),
      relBuilderFactory, null);
}
 
Example #26
Source File: PostOrderRelNodeVisitor.java    From streamline with Apache License 2.0 5 votes vote down vote up
public final T traverse(RelNode n) throws Exception {
  List<T> inputStreams = new ArrayList<>();
  for (RelNode input : n.getInputs()) {
    inputStreams.add(traverse(input));
  }

  if (n instanceof Aggregate) {
    return visitAggregate((Aggregate) n, inputStreams);
  } else if (n instanceof Calc) {
    return visitCalc((Calc) n, inputStreams);
  } else if (n instanceof Collect) {
    return visitCollect((Collect) n, inputStreams);
  } else if (n instanceof Correlate) {
    return visitCorrelate((Correlate) n, inputStreams);
  } else if (n instanceof Delta) {
    return visitDelta((Delta) n, inputStreams);
  } else if (n instanceof Exchange) {
    return visitExchange((Exchange) n, inputStreams);
  } else if (n instanceof Project) {
    return visitProject((Project) n, inputStreams);
  } else if (n instanceof Filter) {
    return visitFilter((Filter) n, inputStreams);
  } else if (n instanceof Sample) {
    return visitSample((Sample) n, inputStreams);
  } else if (n instanceof Sort) {
    return visitSort((Sort) n, inputStreams);
  } else if (n instanceof TableModify) {
    return visitTableModify((TableModify) n, inputStreams);
  } else if (n instanceof TableScan) {
    return visitTableScan((TableScan) n, inputStreams);
  } else if (n instanceof Uncollect) {
    return visitUncollect((Uncollect) n, inputStreams);
  } else if (n instanceof Window) {
    return visitWindow((Window) n, inputStreams);
  } else if (n instanceof Join) {
    return visitJoin((Join) n, inputStreams);
  } else {
    return defaultValue(n, inputStreams);
  }
}
 
Example #27
Source File: SubQueryDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Rewrite Sort.
 *
 * <p>Rewrite logic:
 * change the collations field to reference the new input.
 *
 * @param rel Sort to be rewritten
 */
public Frame decorrelateRel(Sort rel) {
	// Sort itself should not reference corVars.
	assert !cm.mapRefRelToCorRef.containsKey(rel);

	// Sort only references field positions in collations field.
	// The collations field in the newRel now need to refer to the
	// new output positions in its input.
	// Its output does not change the input ordering, so there's no
	// need to call propagateExpr.
	final RelNode oldInput = rel.getInput();
	final Frame frame = getInvoke(oldInput);
	if (frame == null) {
		// If input has not been rewritten, do not rewrite this rel.
		return null;
	}
	final RelNode newInput = frame.r;

	Mappings.TargetMapping mapping =
			Mappings.target(frame.oldToNewOutputs,
					oldInput.getRowType().getFieldCount(),
					newInput.getRowType().getFieldCount());

	RelCollation oldCollation = rel.getCollation();
	RelCollation newCollation = RexUtil.apply(mapping, oldCollation);

	final Sort newSort = LogicalSort.create(newInput, newCollation, rel.offset, rel.fetch);

	// Sort does not change input ordering
	return new Frame(rel, newSort, frame.c, frame.oldToNewOutputs);
}
 
Example #28
Source File: SortUnionTransposeRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final Sort sort = call.rel(0);
  final Union union = call.rel(1);
  List<RelNode> inputs = new ArrayList<>();
  // Thus we use 'ret' as a flag to identify if we have finished pushing the
  // sort past a union.
  boolean ret = true;
  final RelMetadataQuery mq = call.getMetadataQuery();
  for (RelNode input : union.getInputs()) {
    if (!RelMdUtil.checkInputForCollationAndLimit(mq, input,
        sort.getCollation(), sort.offset, sort.fetch)) {
      ret = false;
      Sort branchSort = sort.copy(sort.getTraitSet(), input,
          sort.getCollation(), sort.offset, sort.fetch);
      inputs.add(branchSort);
    } else {
      inputs.add(input);
    }
  }
  // there is nothing to change
  if (ret) {
    return;
  }
  // create new union and sort
  Union unionCopy = (Union) union
      .copy(union.getTraitSet(), inputs, union.all);
  Sort result = sort.copy(sort.getTraitSet(), unionCopy, sort.getCollation(),
      sort.offset, sort.fetch);
  call.transformTo(result);
}
 
Example #29
Source File: RelDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Rewrite Sort.
 *
 * @param rel Sort to be rewritten
 */
public Frame decorrelateRel(Sort rel) {
  //
  // Rewrite logic:
  //
  // 1. change the collations field to reference the new input.
  //

  // Sort itself should not reference corVars.
  assert !cm.mapRefRelToCorRef.containsKey(rel);

  // Sort only references field positions in collations field.
  // The collations field in the newRel now need to refer to the
  // new output positions in its input.
  // Its output does not change the input ordering, so there's no
  // need to call propagateExpr.

  final RelNode oldInput = rel.getInput();
  final Frame frame = getInvoke(oldInput, rel);
  if (frame == null) {
    // If input has not been rewritten, do not rewrite this rel.
    return null;
  }
  final RelNode newInput = frame.r;

  Mappings.TargetMapping mapping =
      Mappings.target(frame.oldToNewOutputs,
          oldInput.getRowType().getFieldCount(),
          newInput.getRowType().getFieldCount());

  RelCollation oldCollation = rel.getCollation();
  RelCollation newCollation = RexUtil.apply(mapping, oldCollation);

  final Sort newSort =
      LogicalSort.create(newInput, newCollation, rel.offset, rel.fetch);

  // Sort does not change input ordering
  return register(rel, newSort, frame.oldToNewOutputs, frame.corDefOutputs);
}
 
Example #30
Source File: SortConvertPrule.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public RelNode convert(RelNode r) {
  Sort rel = (Sort) r;
  RelTraitSet traits = rel.getInput().getTraitSet().replace(Prel.DRILL_PHYSICAL);
  return new SortPrel(rel.getCluster(),
                      traits.plus(rel.getCollation()),
                      convert(rel.getInput(), traits.simplify()),
                      rel.getCollation());
}