private RexInputRef getNewForOldInputRef(RexInputRef oldInputRef) { assert currentRel != null; int oldOrdinal = oldInputRef.getIndex(); int newOrdinal = 0; for (RelNode oldInput0 : currentRel.getInputs()) { RelDataType oldInputType = oldInput0.getRowType(); int n = oldInputType.getFieldCount(); if (oldOrdinal < n) { oldInput = oldInput0; newOrdinal += newInput.getRowType().getFieldCount(); oldOrdinal -= n; return new RexInputRef(newOrdinal, frame.r.getRowType().getFieldList().get(newLocalOrdinal).getType());
@Override public RexNode visitInputRef(RexInputRef inputRef) { return RexInputRef.of(inputRef.getIndex(), input.getRowType()); } }
private RexInputRef getNewForOldInputRef(RexInputRef oldInputRef) { assert currentRel != null; int oldOrdinal = oldInputRef.getIndex(); int oldOrdinalNo = oldOrdinal; int newOrdinal = 0; for (RelNode oldInput0 : currentRel.getInputs()) { RelDataType oldInputType = oldInput0.getRowType(); int n = oldInputType.getFieldCount(); if (oldOrdinal < n) { oldInput = oldInput0; newOrdinal += newInput.getRowType().getFieldCount(); oldOrdinal -= n; final Frame newFrame = map.get(currentRel.getInput(0)); if(newFrame.r instanceof HiveSemiJoin) { int oldFieldSize = currentRel.getInput(0).getRowType().getFieldCount(); int newOrd = newFrame.r.getRowType().getFieldCount() + oldOrdinalNo - oldFieldSize; return new RexInputRef(newOrd, oldInputRef.getType()); return new RexInputRef(newOrdinal, frame.r.getRowType().getFieldList().get(newLocalOrdinal).getType());
@Override public RexNode visitInputRef(RexInputRef inputRef) { final RexInputRef ref = getNewForOldInputRef(inputRef); if (ref.getIndex() == inputRef.getIndex() && ref.getType() == inputRef.getType()) { return inputRef; // re-use old object, to prevent needless expr cloning } return ref; } private RexNode decorrFieldAccess(RexFieldAccess fieldAccess) {
@Override public RexNode visitInputRef(RexInputRef ref) { final Integer integer = map.get(ref.getName()); if (integer != null) { return new RexInputRef(integer, ref.getType()); } throw MatchFailed.INSTANCE; }
final List<RelDataTypeField> relOutput = rel.getRowType().getFieldList(); final List<Pair<RexNode, String>> projects = Lists.newArrayList(); Pair.of( decorrelateExpr(oldProjects.get(newPos)), relOutput.get(newPos).getName())); mapOldToNewOutputs.put(newPos, newPos); for (Map.Entry<CorDef, Integer> entry : frame.corDefOutputs.entrySet()) { projects.add( RexInputRef.of2(entry.getValue(), frame.r.getRowType().getFieldList())); corDefOutputs.put(entry.getKey(), newPos); newPos++;
Project project, int nullIndicatorPos) { final RelDataTypeFactory typeFactory = join.getCluster().getTypeFactory(); final RelNode left = join.getLeft(); final JoinRelType joinType = join.getJoinType(); new RexInputRef( nullIndicatorPos, typeFactory.createTypeWithNullability( join.getRowType().getFieldList().get(nullIndicatorPos) .getType(), true)); List<Pair<RexNode, String>> newProjExprs = Lists.newArrayList(); left.getRowType().getFieldList(); newProjExprs.add(RexInputRef.of2(i, leftInputFields));
List<RelDataTypeField> newFieldList = Lists.newLinkedList(); List<RexNode> newExpList = Lists.newLinkedList(); Map<Integer, Pair<RelDataTypeField, RexNode>> replaceFieldMap = Maps .newHashMapWithExpectedSize(this.context.dynamicFields.size()); int paramIndex = this.rowType.getFieldList().size(); for (Map.Entry<String, RelDataType> rewriteField : this.context.rewriteFields.entrySet()) { String rewriteFieldName = rewriteField.getKey(); newFieldList.add(newField); RelDataTypeField inputField = getInput().getRowType().getFieldList().get(inputIndex); RexInputRef newFieldRef = new RexInputRef(inputField.getIndex(), inputField.getType()); newExpList.add(newFieldRef); RelDataTypeField newField = new RelDataTypeFieldImpl(replaceFieldName, rowIndex, fieldType); RelDataTypeField inputField = getInput().getRowType().getFieldList().get(inputIndex); RexInputRef newFieldRef = new RexInputRef(inputField.getIndex(), inputField.getType()); List<RexNode> newProjects = Lists.newArrayList(this.rewriteProjects); List<RelDataTypeField> newFields = Lists.newArrayList(this.rowType.getFieldList()); for (int rowIndex : replaceFieldMap.keySet()) { FieldInfoBuilder fieldInfo = getCluster().getTypeFactory().builder(); fieldInfo.addAll(newFields); fieldInfo.addAll(newFieldList); this.rowType = getCluster().getTypeFactory().createStructType(fieldInfo);
final List<RelFieldCollation> fieldCollations = Lists.newArrayList(); int fieldIndex = 0; RexNodeConverter converter = new RexNodeConverter(cluster, srcRel.getRowType(), relToHiveColNameCalcitePosMap.get(srcRel), 0, false); int srcRelRecordSz = srcRel.getRowType().getFieldCount(); fieldIndex = ((RexInputRef) rnd).getIndex(); } else { fieldIndex = srcRelRecordSz + newVCLst.size(); List<RexNode> originalInputRefs = Lists.transform(srcRel.getRowType().getFieldList(), new Function<RelDataTypeField, RexNode>() { @Override public RexNode apply(RelDataTypeField input) { return new RexInputRef(input.getIndex(), input.getType()); RelTraitSet traitSet = cluster.traitSetOf(HiveRelNode.CONVENTION); RelCollation canonizedCollation = traitSet.canonize(RelCollationImpl.of(fieldCollations)); sortRel = new HiveSortLimit(cluster, traitSet, obInputRel, canonizedCollation, null, null); List<RexNode> originalInputRefs = Lists.transform(srcRel.getRowType().getFieldList(), new Function<RelDataTypeField, RexNode>() { @Override public RexNode apply(RelDataTypeField input) { return new RexInputRef(input.getIndex(), input.getType());
public void onMatch(RelOptRuleCall call) { final LogicalCorrelate correlate = call.rel(0); final RelNode left = call.rel(1); final Project aggOutputProject; final Aggregate aggregate; if (flavor) { aggOutputProject = call.rel(2); aggregate = call.rel(3); } else { aggregate = call.rel(2); // Create identity projection final List<Pair<RexNode, String>> projects = Lists.newArrayList(); final List<RelDataTypeField> fields = aggregate.getRowType().getFieldList(); for (int i = 0; i < fields.size(); i++) { projects.add(RexInputRef.of2(projects.size(), fields)); } aggOutputProject = (Project) RelOptUtil.createProject( aggregate, Pair.left(projects), Pair.right(projects), false, relBuilder); } onMatch2(call, correlate, left, aggOutputProject, aggregate); }
List<RexNode> rightJoinKeys = Lists.newArrayList(); List<RexNode> tmpCorrelatedJoinKeys = Lists.newArrayList(); HiveRelOptUtil.splitCorrelatedFilterCondition( filter, List<RexFieldAccess> correlatedJoinKeys = Lists.newArrayList(); List<RexInputRef> correlatedInputRefJoinKeys = Lists.newArrayList(); for (RexNode joinKey : tmpCorrelatedJoinKeys) { int nFields = left.getRowType().getFieldCount(); ImmutableBitSet allCols = ImmutableBitSet.range(nFields); int nullIndicatorPos = join.getRowType().getFieldCount() - 1; new RexInputRef( nullIndicatorPos, cluster.getTypeFactory().createTypeWithNullability( join.getRowType().getFieldList() .get(nullIndicatorPos).getType(), true)); joinOutputProjects.add( rexBuilder.makeInputRef( leftInputFieldType.getFieldList().get(i).getType(), i));
List<RexNode> tmpRightJoinKeys = Lists.newArrayList(); List<RexNode> correlatedJoinKeys = Lists.newArrayList(); HiveRelOptUtil.splitCorrelatedFilterCondition( filter, left.getRowType().getFieldCount() + rightJoinKeys.get(0).getIndex(); } else if (cm.mapRefRelToCorRef.containsKey(project)) { left.getRowType().getFieldCount() + right.getRowType().getFieldCount() - 1; } else { return;
private RelNode genLateralViewPlans(ASTNode lateralView, Map<String, RelNode> aliasToRel) throws SemanticException { final RexBuilder rexBuilder = this.cluster.getRexBuilder(); final RelDataTypeFactory dtFactory = this.cluster.getTypeFactory(); final String inlineFunctionName = GenericUDTFInline.class.getAnnotation(Description.class).name(); List<RexNode> inputRefs = Lists.transform(inputRel.getRowType().getFieldList(), input -> new RexInputRef(input.getIndex(), input.getType())); inputRefsTypes.add(inputRefs.get(i).getType()); List<String> outputFieldNames = new ArrayList<>(inputRel.getRowType().getFieldNames()); RexCall convertedOriginalValuesExpr = (RexCall) new RexNodeConverter(this.cluster, inputRel.getRowType(), inputPosMap, 0, false).convert(valuesExpr); RelDataType valuesRowType = ((ArraySqlType) convertedOriginalValuesExpr.getType()).getComponentType(); List<RexNode> newStructExprs = new ArrayList<>(); for (RexNode structExpr : convertedOriginalValuesExpr.getOperands()) { RexCall structCall = (RexCall) structExpr; List<RexNode> exprs = new ArrayList<>(inputRefs); exprs.addAll(structCall.getOperands()); newStructExprs.add(rexBuilder.makeCall(structCall.op, exprs)); columnAliases.size() != valuesRowType.getFieldCount()) {
RexNodeConverter rexNodeConv = new RexNodeConverter(cluster, srcRel.getRowType(), outerNameToPosMap, buildHiveColNameToInputPosMap(col_list, inputRR), relToHiveRR.get(srcRel), outerRR, 0, false, subqueryId); List<RexNode> originalInputRefs = Lists.transform(srcRel.getRowType().getFieldList(), new Function<RelDataTypeField, RexNode>() { @Override public RexNode apply(RelDataTypeField input) { return new RexInputRef(input.getIndex(), input.getType()); ImmutableBitSet groupSet = ImmutableBitSet.range(outputRel.getRowType().getFieldList().size()); outputRel = new HiveAggregate(cluster, cluster.traitSetOf(HiveRelNode.CONVENTION), outputRel, groupSet, null, new ArrayList<AggregateCall>()); RowResolver groupByOutputRowResolver = new RowResolver();
RelNode leftRel = inputRels[0]; RelNode rightRel = inputRels[1]; RexBuilder rexBuilder = leftRel.getCluster().getRexBuilder(); RexNode outJoinCond = null; int origLeftInputSize = leftRel.getRowType().getFieldCount(); int origRightInputSize = rightRel.getRowType().getFieldCount(); final RelDataTypeField field = leftRel.getRowType().getFieldList().get(i); newLeftFields.add(rexBuilder.makeInputRef(field.getType(), i)); newLeftFieldNames.add(field.getName()); final RelDataTypeField field = rightRel.getRowType().getFieldList().get(i); newRightFields.add(rexBuilder.makeInputRef(field.getType(), i)); newRightFieldNames.add(field.getName()); origColEqConds.add(Pair.of(((RexInputRef) leftKey).getIndex(), ((RexInputRef) rightKey).getIndex())); origColEqCondsPosBuilder.set(i); } else { rightKeys.add(origRightInputSize + i); RexNode cond = rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, rexBuilder.makeInputRef(newLeftFields.get(origLeftInputSize + i).getType(), newLeftOffset + i), rexBuilder.makeInputRef(newRightFields.get(origRightInputSize + i).getType(), newRightOffset + i)); if (outJoinCond == null) {
public static ExprNodeDesc getExprNode(Integer inputRefIndx, RelNode inputRel, ExprNodeConverter exprConv) { ExprNodeDesc exprNode = null; RexNode rexInputRef = new RexInputRef(inputRefIndx, inputRel.getRowType() .getFieldList().get(inputRefIndx).getType()); exprNode = rexInputRef.accept(exprConv); return exprNode; }
ASTNode cond = where.getCondition().accept(new RexVisitor(schema, false, root.getCluster().getRexBuilder())); hiveAST.where = ASTBuilder.where(cond); RexInputRef iRef = new RexInputRef(groupBy.getGroupSet().nth(pos), groupBy.getCluster().getTypeFactory().createSqlType(SqlTypeName.ANY)); b.add(iRef.accept(new RexVisitor(schema, false, root.getCluster().getRexBuilder()))); RexInputRef iRef = new RexInputRef(groupBy.getGroupSet().nth(pos), groupBy.getCluster().getTypeFactory().createSqlType(SqlTypeName.ANY)); b.add(iRef.accept(new RexVisitor(schema, false, root.getCluster().getRexBuilder()))); HiveParser.TOK_GROUPING_SETS_EXPRESSION, "TOK_GROUPING_SETS_EXPRESSION"); for (int i : groupSet) { RexInputRef iRef = new RexInputRef(i, groupBy.getCluster().getTypeFactory() .createSqlType(SqlTypeName.ANY)); expression.add(iRef.accept(new RexVisitor(schema, false, root.getCluster().getRexBuilder()))); ASTNode cond = having.getCondition().accept(new RexVisitor(schema, false, root.getCluster().getRexBuilder())); hiveAST.having = ASTBuilder.having(cond); ASTNode function = buildUDTFAST(call.getOperator().getName(), children); sel.add(function); for (String alias : udtf.getRowType().getFieldNames()) { sel.add(HiveParser.Identifier, alias);
gbInfo.outputColNames.addAll(aggRel.getRowType().getFieldNames()); aggInputRel.getRowType(), new HashSet<Integer>(), aggRel.getCluster().getTypeFactory(), true); RexInputRef iRef = new RexInputRef(i, aggInputRel.getRowType().getFieldList() .get(i).getType()); tmpExprNodeDesc = iRef.accept(exprConv); gbInfo.gbKeys.add(tmpExprNodeDesc); gbInfo.gbKeyColNamesInInput.add(aggInputRel.getRowType().getFieldNames().get(i)); gbInfo.gbKeyTypes.add(tmpExprNodeDesc.getTypeInfo());
RexUtil.composeConjunction(rexBuilder, conditions, false); RelNode newRel = HiveSemiJoin.getSemiJoin(frame.r.getCluster(), frame.r.getTraitSet(), join.getLeft(), join.getRight(), condition, join.getLeftKeys(), join.getRightKeys()); rightFrame.r.getRowType().getFieldList(); conditions.add( rexBuilder.makeCall(callOp, RexInputRef.of(newLeftPos, newLeftOutput), new RexInputRef(newLeftFieldCount + newRightPos, newRightOutput.get(newRightPos).getType()))); } else { conditions.add( rexBuilder.makeCall(callOp, new RexInputRef(newLeftFieldCount + newRightPos, newRightOutput.get(newRightPos).getType()), RexInputRef.of(newLeftPos, newLeftOutput))); int oldLeftFieldCount = oldLeft.getRowType().getFieldCount(); int oldRightFieldCount = oldRight.getRowType().getFieldCount(); rel.getCluster().traitSetOf(HiveRelNode.CONVENTION), leftFrame.r, rightFrame.r, condition, ImmutableIntList.copyOf(leftKeys), ImmutableIntList.copyOf(rightKeys));
/** Infers the alias of an expression. * * <p>If the expression was created by {@link #alias}, replaces the expression * in the project list. */ private String inferAlias(List<RexNode> exprList, RexNode expr) { switch (expr.getKind()) { case INPUT_REF: final RexInputRef ref = (RexInputRef) expr; return peek(0).getRowType().getFieldNames().get(ref.getIndex()); case CAST: return inferAlias(exprList, ((RexCall) expr).getOperands().get(0)); case AS: final RexCall call = (RexCall) expr; for (;;) { final int i = exprList.indexOf(expr); if (i < 0) { break; } exprList.set(i, call.getOperands().get(0)); } return ((NlsString) ((RexLiteral) call.getOperands().get(1)).getValue()) .getValue(); default: return null; } }