use of org.apache.calcite.rel.type.RelDataType 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.type.RelDataType in project hive by apache.
the class HiveSubQRemoveRelBuilder method call.
/** Creates a call to a scalar operator. */
public RexNode call(SqlOperator operator, RexNode... operands) {
final RexBuilder builder = cluster.getRexBuilder();
final List<RexNode> operandList = ImmutableList.copyOf(operands);
final RelDataType type = builder.deriveReturnType(operator, operandList);
if (type == null) {
throw new IllegalArgumentException("cannot derive type: " + operator + "; operands: " + Lists.transform(operandList, FN_TYPE));
}
return builder.makeCall(type, operator, operandList);
}
use of org.apache.calcite.rel.type.RelDataType in project hive by apache.
the class HiveCalciteUtil method getTypeSafePred.
public static RexNode getTypeSafePred(RelOptCluster cluster, RexNode rex, RelDataType rType) {
RexNode typeSafeRex = rex;
if ((typeSafeRex instanceof RexCall) && HiveCalciteUtil.isComparisonOp((RexCall) typeSafeRex)) {
RexBuilder rb = cluster.getRexBuilder();
List<RexNode> fixedPredElems = new ArrayList<RexNode>();
RelDataType commonType = cluster.getTypeFactory().leastRestrictive(RexUtil.types(((RexCall) rex).getOperands()));
for (RexNode rn : ((RexCall) rex).getOperands()) {
fixedPredElems.add(rb.ensureType(commonType, rn, true));
}
typeSafeRex = rb.makeCall(((RexCall) typeSafeRex).getOperator(), fixedPredElems);
}
return typeSafeRex;
}
use of org.apache.calcite.rel.type.RelDataType in project hive by apache.
the class PlanModifierForASTConv method replaceEmptyGroupAggr.
private static void replaceEmptyGroupAggr(final RelNode rel, RelNode parent) {
// If this function is called, the parent should only include constant
List<RexNode> exps = parent.getChildExps();
for (RexNode rexNode : exps) {
if (!rexNode.accept(new HiveCalciteUtil.ConstantFinder())) {
throw new RuntimeException("We expect " + parent.toString() + " to contain only constants. However, " + rexNode.toString() + " is " + rexNode.getKind());
}
}
HiveAggregate oldAggRel = (HiveAggregate) rel;
RelDataTypeFactory typeFactory = oldAggRel.getCluster().getTypeFactory();
RelDataType longType = TypeConverter.convert(TypeInfoFactory.longTypeInfo, typeFactory);
RelDataType intType = TypeConverter.convert(TypeInfoFactory.intTypeInfo, typeFactory);
// Create the dummy aggregation.
SqlAggFunction countFn = SqlFunctionConverter.getCalciteAggFn("count", false, ImmutableList.of(intType), longType);
// TODO: Using 0 might be wrong; might need to walk down to find the
// proper index of a dummy.
List<Integer> argList = ImmutableList.of(0);
AggregateCall dummyCall = new AggregateCall(countFn, false, argList, longType, null);
Aggregate newAggRel = oldAggRel.copy(oldAggRel.getTraitSet(), oldAggRel.getInput(), oldAggRel.indicator, oldAggRel.getGroupSet(), oldAggRel.getGroupSets(), ImmutableList.of(dummyCall));
RelNode select = introduceDerivedTable(newAggRel);
parent.replaceInput(0, select);
}
use of org.apache.calcite.rel.type.RelDataType 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.getChildExps().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.getChildExps().subList(0, resultSchema.size()), obChild.getRowType().getFieldNames().subList(0, resultSchema.size()));
obRel.replaceInput(0, replacementProjectRel);
}
obRel.setInputRefToCallMap(inputRefToCallMap);
}
Aggregations