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;
}
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;
}
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);
}
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);
}
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);
}
}
Aggregations