Search in sources :

Example 21 with RelNode

use of org.apache.calcite.rel.RelNode in project hive by apache.

the class HiveSubQRemoveRelBuilder method project.

/** Creates a {@link org.apache.calcite.rel.core.Project} of the given list
   * of expressions, using the given names.
   *
   * <p>Names are deduced as follows:
   * <ul>
   *   <li>If the length of {@code fieldNames} is greater than the index of
   *     the current entry in {@code nodes}, and the entry in
   *     {@code fieldNames} is not null, uses it; otherwise
   *   <li>If an expression projects an input field,
   *     or is a cast an input field,
   *     uses the input field name; otherwise
   *   <li>If an expression is a call to
   *     {@link org.apache.calcite.sql.fun.SqlStdOperatorTable#AS}
   *     (see {@link #alias}), removes the call but uses the intended alias.
   * </ul>
   *
   * <p>After the field names have been inferred, makes the
   * field names unique by appending numeric suffixes.
   *
   * @param nodes Expressions
   * @param fieldNames Suggested field names
   * @param force create project even if it is identity
   */
public HiveSubQRemoveRelBuilder project(Iterable<? extends RexNode> nodes, Iterable<String> fieldNames, boolean force) {
    final List<String> names = new ArrayList<>();
    final List<RexNode> exprList = Lists.newArrayList(nodes);
    final Iterator<String> nameIterator = fieldNames.iterator();
    for (RexNode node : nodes) {
        final String name = nameIterator.hasNext() ? nameIterator.next() : null;
        final String name2 = inferAlias(exprList, node);
        names.add(Util.first(name, name2));
    }
    final RelDataType inputRowType = peek().getRowType();
    if (!force && RexUtil.isIdentity(exprList, inputRowType)) {
        if (names.equals(inputRowType.getFieldNames())) {
            // Do not create an identity project if it does not rename any fields
            return this;
        } else {
            // create "virtual" row type for project only rename fields
            final Frame frame = stack.pop();
            final RelDataType rowType = RexUtil.createStructType(cluster.getTypeFactory(), exprList, names, SqlValidatorUtil.F_SUGGESTER);
            stack.push(new Frame(frame.rel, ImmutableList.of(Pair.of(frame.right.get(0).left, rowType))));
            return this;
        }
    }
    final RelNode project = projectFactory.createProject(build(), ImmutableList.copyOf(exprList), names);
    push(project);
    return this;
}
Also used : RelNode(org.apache.calcite.rel.RelNode) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) NlsString(org.apache.calcite.util.NlsString) RexNode(org.apache.calcite.rex.RexNode)

Example 22 with RelNode

use of org.apache.calcite.rel.RelNode in project hive by apache.

the class HiveSubQRemoveRelBuilder method values.

/** Creates a {@link Values} with a specified row type.
   *
   * <p>This method can handle cases that {@link #values(String[], Object...)}
   * cannot, such as all values of a column being null, or there being zero
   * rows.
   *
   * @param tupleList Tuple list
   * @param rowType Row type
   */
public HiveSubQRemoveRelBuilder values(Iterable<? extends List<RexLiteral>> tupleList, RelDataType rowType) {
    RelNode values = valuesFactory.createValues(cluster, rowType, copy(tupleList));
    push(values);
    return this;
}
Also used : RelNode(org.apache.calcite.rel.RelNode)

Example 23 with RelNode

use of org.apache.calcite.rel.RelNode in project hive by apache.

the class HiveSubQRemoveRelBuilder method empty.

/**
   * Empty relationship can be expressed in many different ways, e.g.,
   * filter(cond=false), empty LogicalValues(), etc. Calcite default implementation
   * uses empty LogicalValues(); however, currently there is not an equivalent to
   * this expression in Hive. Thus, we use limit 0, since Hive already includes
   * optimizations that will do early pruning of the result tree when it is found,
   * e.g., GlobalLimitOptimizer.
   */
public HiveSubQRemoveRelBuilder empty() {
    final RelNode input = build();
    final RelNode sort = HiveRelFactories.HIVE_SORT_FACTORY.createSort(input, RelCollations.of(), null, literal(0));
    return this.push(sort);
}
Also used : RelNode(org.apache.calcite.rel.RelNode)

Example 24 with RelNode

use of org.apache.calcite.rel.RelNode in project hive by apache.

the class HiveRelBuilder method empty.

/**
   * Empty relationship can be expressed in many different ways, e.g.,
   * filter(cond=false), empty LogicalValues(), etc. Calcite default implementation
   * uses empty LogicalValues(); however, currently there is not an equivalent to
   * this expression in Hive. Thus, we use limit 0, since Hive already includes
   * optimizations that will do early pruning of the result tree when it is found,
   * e.g., GlobalLimitOptimizer.
   */
@Override
public RelBuilder empty() {
    final RelNode input = build();
    final RelNode sort = HiveRelFactories.HIVE_SORT_FACTORY.createSort(input, RelCollations.of(), null, literal(0));
    return this.push(sort);
}
Also used : RelNode(org.apache.calcite.rel.RelNode)

Example 25 with RelNode

use of org.apache.calcite.rel.RelNode in project hive by apache.

the class HiveRelShuttleImpl method visitChild.

/**
     * Visits a particular child of a parent.
     */
protected RelNode visitChild(RelNode parent, int i, RelNode child) {
    Stacks.push(stack, parent);
    try {
        RelNode child2 = child.accept(this);
        if (child2 != child) {
            final List<RelNode> newInputs = new ArrayList<RelNode>(parent.getInputs());
            newInputs.set(i, child2);
            return parent.copy(parent.getTraitSet(), newInputs);
        }
        return parent;
    } finally {
        Stacks.pop(stack, parent);
    }
}
Also used : RelNode(org.apache.calcite.rel.RelNode) ArrayList(java.util.ArrayList)

Aggregations

RelNode (org.apache.calcite.rel.RelNode)219 RexNode (org.apache.calcite.rex.RexNode)75 ArrayList (java.util.ArrayList)50 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)30 RelDataType (org.apache.calcite.rel.type.RelDataType)27 RelTraitSet (org.apache.calcite.plan.RelTraitSet)25 HashMap (java.util.HashMap)24 RexBuilder (org.apache.calcite.rex.RexBuilder)24 RexInputRef (org.apache.calcite.rex.RexInputRef)21 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)20 Prel (org.apache.drill.exec.planner.physical.Prel)20 AggregateCall (org.apache.calcite.rel.core.AggregateCall)16 Pair (org.apache.calcite.util.Pair)16 ImmutableList (com.google.common.collect.ImmutableList)15 Project (org.apache.calcite.rel.core.Project)14 RelOptCluster (org.apache.calcite.plan.RelOptCluster)13 HiveProject (org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveProject)13 TreeMap (java.util.TreeMap)11 HashSet (java.util.HashSet)10 RelCollation (org.apache.calcite.rel.RelCollation)10