Search in sources :

Example 1 with Hintable

use of org.apache.calcite.rel.hint.Hintable in project calcite by apache.

the class SqlToRelConverter method convertSelectImpl.

/**
 * Implementation of {@link #convertSelect(SqlSelect, boolean)};
 * derived class may override.
 */
protected void convertSelectImpl(final Blackboard bb, SqlSelect select) {
    convertFrom(bb, select.getFrom());
    // semantics. It is not a 'pure order'.
    if (RelOptUtil.isPureOrder(castNonNull(bb.root)) && config.isRemoveSortInSubQuery()) {
        // sub-query.
        if (!bb.top || validator().isAggregate(select) || select.isDistinct() || select.hasOrderBy() || select.getFetch() != null || select.getOffset() != null) {
            bb.setRoot(castNonNull(bb.root).getInput(0), true);
        }
    }
    convertWhere(bb, select.getWhere());
    final List<SqlNode> orderExprList = new ArrayList<>();
    final List<RelFieldCollation> collationList = new ArrayList<>();
    gatherOrderExprs(bb, select, select.getOrderList(), orderExprList, collationList);
    final RelCollation collation = cluster.traitSet().canonize(RelCollations.of(collationList));
    if (validator().isAggregate(select)) {
        convertAgg(bb, select, orderExprList);
    } else {
        convertSelectList(bb, select, orderExprList);
    }
    if (select.isDistinct()) {
        distinctify(bb, true);
    }
    convertOrder(select, bb, collation, orderExprList, select.getOffset(), select.getFetch());
    if (select.hasHints()) {
        final List<RelHint> hints = SqlUtil.getRelHint(hintStrategies, select.getHints());
        // Attach the hints to the first Hintable node we found from the root node.
        bb.setRoot(bb.root().accept(new RelShuttleImpl() {

            boolean attached = false;

            @Override
            public RelNode visitChild(RelNode parent, int i, RelNode child) {
                if (parent instanceof Hintable && !attached) {
                    attached = true;
                    return ((Hintable) parent).attachHints(hints);
                } else {
                    return super.visitChild(parent, i, child);
                }
            }
        }), true);
    } else {
        bb.setRoot(bb.root(), true);
    }
}
Also used : RelCollation(org.apache.calcite.rel.RelCollation) RelNode(org.apache.calcite.rel.RelNode) ArrayList(java.util.ArrayList) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) RelShuttleImpl(org.apache.calcite.rel.RelShuttleImpl) Hintable(org.apache.calcite.rel.hint.Hintable) RelHint(org.apache.calcite.rel.hint.RelHint) SqlNode(org.apache.calcite.sql.SqlNode)

Example 2 with Hintable

use of org.apache.calcite.rel.hint.Hintable in project calcite by apache.

the class RelBuilder method hints.

/**
 * Attaches multiple hints to the stack top relational expression.
 *
 * <p>The redundant hints would be eliminated.
 *
 * @param hints Hints
 *
 * @throws AssertionError if the top relational expression does not implement
 * {@link org.apache.calcite.rel.hint.Hintable}
 */
public RelBuilder hints(Iterable<RelHint> hints) {
    requireNonNull(hints, "hints");
    final List<RelHint> relHintList = hints instanceof List ? (List<RelHint>) hints : Lists.newArrayList(hints);
    if (relHintList.isEmpty()) {
        return this;
    }
    final Frame frame = peek_();
    assert frame != null : "There is no relational expression to attach the hints";
    assert frame.rel instanceof Hintable : "The top relational expression is not a Hintable";
    Hintable hintable = (Hintable) frame.rel;
    replaceTop(hintable.attachHints(relHintList));
    return this;
}
Also used : Hintable(org.apache.calcite.rel.hint.Hintable) ArrayList(java.util.ArrayList) AbstractList(java.util.AbstractList) RelOptPredicateList(org.apache.calcite.plan.RelOptPredicateList) ImmutableIntList(org.apache.calcite.util.ImmutableIntList) ImmutableNullableList(org.apache.calcite.util.ImmutableNullableList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) RelHint(org.apache.calcite.rel.hint.RelHint)

Aggregations

ArrayList (java.util.ArrayList)2 Hintable (org.apache.calcite.rel.hint.Hintable)2 RelHint (org.apache.calcite.rel.hint.RelHint)2 ImmutableList (com.google.common.collect.ImmutableList)1 AbstractList (java.util.AbstractList)1 List (java.util.List)1 RelOptPredicateList (org.apache.calcite.plan.RelOptPredicateList)1 RelCollation (org.apache.calcite.rel.RelCollation)1 RelFieldCollation (org.apache.calcite.rel.RelFieldCollation)1 RelNode (org.apache.calcite.rel.RelNode)1 RelShuttleImpl (org.apache.calcite.rel.RelShuttleImpl)1 SqlNode (org.apache.calcite.sql.SqlNode)1 ImmutableIntList (org.apache.calcite.util.ImmutableIntList)1 ImmutableNullableList (org.apache.calcite.util.ImmutableNullableList)1