public static String explain(final RelNode rel, SqlExplainLevel detailLevel) { String explain = ""; try { explain = RelOptUtil.toString(rel); } catch (StackOverflowError e) { LOG.error("StackOverflowError occurred while extracting plan. Please report it to the dev@ mailing list."); LOG.error("RelNode " + rel + " ExplainLevel " + detailLevel, e); LOG.error("Forcing plan to empty string and continue... SQL Runner may not working properly after."); } return explain; } }
private OpAttr translateJoin(RelNode joinRel) throws SemanticException { String[] baseSrc = new String[joinRel.getInputs().size()]; String tabAlias = getHiveDerivedTableAlias(); OpAttr[] inputs = new OpAttr[joinRel.getInputs().size()]; List<Operator<?>> children = new ArrayList<Operator<?>>(joinRel.getInputs().size()); for (int i = 0; i < inputs.length; i++) { inputs[i] = dispatch(joinRel.getInput(i)); children.add(inputs[i].inputs.get(0)); baseSrc[i] = inputs[i].tabAlias; if (joinRel instanceof HiveMultiJoin || !(joinRel instanceof SemiJoin)) { int shift = inputs[0].inputs.get(0).getSchema().getSignature().size(); for (int i = 1; i < inputs.length; i++) { newVcolsInCalcite.addAll(HiveCalciteUtil.shiftVColsSet(inputs[i].vcolsInCalcite, shift)); shift += inputs[i].inputs.get(0).getSchema().getSignature().size(); throw new SemanticException ("Can't handle join type: " + joinRel.getClass().getName()); List<List<ExprNodeDesc>> filterExpressions = Lists.newArrayList(); for (int i = 0; i< joinFilters.size(); i++) { List<ExprNodeDesc> filterExpressionsForInput = new ArrayList<ExprNodeDesc>(); if (joinFilters.get(i) != null) { for (RexNode conj : RelOptUtil.conjunctions(joinFilters.get(i))) { ExprNodeDesc expr = convertToExprNode(conj, joinRel, null, newVcolsInCalcite); filterExpressionsForInput.add(expr);
private PlannerResult planExplanation( final RelNode rel, final SqlExplain explain, final Set<String> datasourceNames ) { final String explanation = RelOptUtil.dumpPlan("", rel, explain.getFormat(), explain.getDetailLevel()); final Supplier<Sequence<Object[]>> resultsSupplier = Suppliers.ofInstance( Sequences.simple(ImmutableList.of(new Object[]{explanation}))); final RelDataTypeFactory typeFactory = rel.getCluster().getTypeFactory(); return new PlannerResult( resultsSupplier, typeFactory.createStructType( ImmutableList.of(Calcites.createSqlType(typeFactory, SqlTypeName.VARCHAR)), ImmutableList.of("PLAN") ), datasourceNames ); } }
private boolean addBaseConditions(RexNode condition, StatisticsPayload payload, boolean redundant, Map<String, Double> baseConditionMap, RelDataType rowType) { boolean res = redundant; if (condition.getKind() == SqlKind.AND) { for(RexNode pred : RelOptUtil.conjunctions(condition)) { res = addBaseConditions(pred, payload, res, baseConditionMap, rowType); } } else if (condition.getKind() == SqlKind.OR) { for(RexNode pred : RelOptUtil.disjunctions(condition)) { res = addBaseConditions(pred, payload, res, baseConditionMap, rowType); } } else { // base condition String conditionAsStr = convertRexToString(condition, rowType); if (!redundant) { baseConditionMap.put(conditionAsStr, payload.getRowCount()); return true; } else { baseConditionMap.put(conditionAsStr, -1.0); return false; } } return res; } /*
return null; if (condition.getKind() == SqlKind.AND) { boolean first = true; for(RexNode pred : RelOptUtil.conjunctions(condition)) { if (first) { sb.append(convertRexToString(pred, rowType)); } else if (condition.getKind() == SqlKind.OR) { boolean first = true; for(RexNode pred : RelOptUtil.disjunctions(condition)) { if (first) { sb.append(convertRexToString(pred, rowType)); if (inputRefMapping.keySet().size() > 0) { String replCondition = condition.toString(); for (String inputRef : inputRefMapping.keySet()) { replCondition = replCondition.replace(inputRef, inputRefMapping.get(inputRef));
RelOptUtil.createProject( newInput, mapNewInputToOutputs.get(newInput)); RelNode distinct = RelOptUtil.createDistinctRel(project); RelOptCluster cluster = distinct.getCluster(); offset += distinct.getRowType().getFieldCount(); r = LogicalJoin.create(r, distinct, cluster.getRexBuilder().makeLiteral(true), ImmutableSet.<CorrelationId>of(), JoinRelType.INNER);
protected void perform(RelOptRuleCall call, Filter filter, Join join) { final List<RexNode> joinFilters = RelOptUtil.conjunctions(join.getCondition()); final List<RexNode> origJoinFilters = ImmutableList.copyOf(joinFilters); final List<RexNode> aboveFilters = filter != null ? RelOptUtil.conjunctions(filter.getCondition()) : Lists.<RexNode> newArrayList(); final ImmutableList<RexNode> origAboveFilters = ImmutableList.copyOf(aboveFilters); if (smart && !origAboveFilters.isEmpty() && join.getJoinType() != JoinRelType.INNER) { joinType = RelOptUtil.simplifyJoin(join, origAboveFilters, joinType); if (RelOptUtil.classifyFilters(join, aboveFilters, joinType, !(join instanceof EquiJoin), !joinType.generatesNullsOnLeft(), !joinType.generatesNullsOnRight(), joinFilters, leftFilters, rightFilters)) { if (RelOptUtil.classifyFilters(join, joinFilters, joinType, false, !joinType.generatesNullsOnRight(), !joinType.generatesNullsOnLeft(), joinFilters, leftFilters, rightFilters)) { filterPushed = true; final ImmutableList<RelDataType> fieldTypes = ImmutableList.<RelDataType> builder() .addAll(RelOptUtil.getFieldTypeList(leftRel.getRowType())) .addAll(RelOptUtil.getFieldTypeList(rightRel.getRowType())).build(); final RexNode joinFilter = RexUtil.composeConjunction(rexBuilder, RexUtil.fixUp(rexBuilder, joinFilters, fieldTypes), false); RexUtil.fixUp(rexBuilder, aboveFilters, RelOptUtil.getFieldTypeList(relBuilder.peek().getRowType())));
/** * Projects all {@code input} output fields plus the additional expressions. * * @param input Input relational expression * @param additionalExprs Additional expressions and names * @return the new Project */ private RelNode createProjectWithAdditionalExprs( RelNode input, List<Pair<RexNode, String>> additionalExprs) { final List<RelDataTypeField> fieldList = input.getRowType().getFieldList(); List<Pair<RexNode, String>> projects = Lists.newArrayList(); for (Ord<RelDataTypeField> field : Ord.zip(fieldList)) { projects.add( Pair.of( (RexNode) rexBuilder.makeInputRef( field.e.getType(), field.i), field.e.getName())); } projects.addAll(additionalExprs); return RelOptUtil.createProject(input, Pair.left(projects), Pair.right(projects), false, relBuilder); }
if (RelOptUtil.getVariablesUsed(right).size() > 0) { return; List<RexFieldAccess> correlatedJoinKeys = Lists.newArrayList(); } else if (cm.mapRefRelToCorRef.containsKey(aggInputProject)) { if (RelOptUtil.getVariablesUsed(right).size() > 0) { return; int nFields = left.getRowType().getFieldCount(); ImmutableBitSet allCols = ImmutableBitSet.range(nFields); ImmutableList.of( Pair.<RexNode, String>of(rexBuilder.makeLiteral(true), "nullIndicator"))); int nullIndicatorPos = join.getRowType().getFieldCount() - 1; cluster.getTypeFactory().createTypeWithNullability( RelNode joinOutputProject = RelOptUtil.createProject( join, joinOutputProjects, null, false, relBuilder); RelNode newAggOutputProject = RelOptUtil.createProject( newAggregate, newAggOutputProjectList, null, false, relBuilder);
call.transformTo(topOperator.copy(topOperator.getTraitSet(), ImmutableList.of(left))); return; final List<Integer> newRightKeyBuilder = Lists.newArrayList(); final List<Integer> aggregateKeys = aggregate.getGroupSet().asList(); for (int key : joinInfo.rightKeys) { final RelNode newRight = aggregate.getInput(); final RexNode newCondition = RelOptUtil.createEquiJoinCondition(left, joinInfo.leftKeys, newRight, newRightKeys, rexBuilder); Join rightJoin = (Join)(((HepRelVertex)aggregate.getInput()).getCurrentRel()); List<RexNode> projects = new ArrayList<>(); for(int i=0; i<rightJoin.getRowType().getFieldCount(); i++){ projects.add(rexBuilder.makeInputRef(rightJoin, i)); RelNode topProject = call.builder().push(rightJoin).project(projects, rightJoin.getRowType().getFieldNames(), semi = call.builder().push(left).push(aggregate.getInput()).semiJoin(newCondition).build(); call.transformTo(topOperator.copy(topOperator.getTraitSet(), ImmutableList.of(semi)));
return ImmutableList.of(); String rexNodeString = r.toString(); if (predicatesToExclude.add(rexNodeString)) { stringToRexNode.put(rexNodeString, r); return ImmutableList.of(); final RelMetadataQuery mq = inp.getCluster().getMetadataQuery(); for (RexNode pred : mq.getPulledUpPredicates(inp).pulledUpPredicates) { predicatesInSubtree.add(pred.toString()); predicatesInSubtree.addAll(Lists.transform(RelOptUtil.conjunctions(pred), REX_STR_FN)); final ImmutableList.Builder<RexNode> newConjuncts = ImmutableList.builder(); for (Entry<String,RexNode> e : stringToRexNode.entrySet()) { if (predicatesInSubtree.add(e.getKey())) {
@SuppressWarnings("incomplete-switch") private static List<RexNode> rewriteProjects(List<RexNode> projects, RexNode newPushedCondition, RelBuilder relBuilder) { final List<RexNode> conjunctions = RelOptUtil.conjunctions(newPushedCondition); final Map<String, RexNode> conditions = new HashMap<String, RexNode>(); for (RexNode conjunction: conjunctions) { switch (conjCall.getOperator().getKind()) { case EQUALS: if (!(RexUtil.isConstant(conjCall.operands.get(0))) && RexUtil.isConstant(conjCall.operands.get(1))) { conditions.put(conjCall.operands.get(0).toString(), conjCall.operands.get(1)); } else if (!(RexUtil.isConstant(conjCall.operands.get(1))) && RexUtil.isConstant(conjCall.operands.get(0))) { conditions.put(conjCall.operands.get(1).toString(), conjCall.operands.get(0)); conditions.put(conjCall.operands.get(0).toString(), relBuilder.getRexBuilder().makeNullLiteral( conjCall.operands.get(0).getType().getSqlTypeName())); List<RexNode> newProjects = Lists.newArrayList(projects); replacer.mutate(newProjects); if (replacer.replaced) {
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); }
final Aggregate aggregate = call.rel(0); final Join join = call.rel(1); final RexBuilder rexBuilder = aggregate.getCluster().getRexBuilder(); final RelBuilder relBuilder = call.builder(); final List<Integer> leftKeys = Lists.newArrayList(); final List<Integer> rightKeys = Lists.newArrayList(); final List<Boolean> filterNulls = Lists.newArrayList(); RexNode nonEquiConj = RelOptUtil.splitJoinCondition(join.getLeft(), join.getRight(), join.getCondition(), leftKeys, rightKeys, filterNulls); if (!nonEquiConj.isAlwaysTrue()) { return; call1 = splitter.split(aggCall.e, mapping); } else { call1 = splitter.other(rexBuilder.getTypeFactory(), aggCall.e); belowOffset += side.newInput.getRowType().getFieldCount(); sides.add(side); join.getRowType().getFieldCount(), belowOffset); final RexNode newCondition =
Mappings.TargetMapping leftMapping = Mappings.createShiftMapping( nSysFields + nFieldsLeft, nSysFields, 0, nFieldsLeft); leftChildPredicates = lPreds.accept( new RexPermuteInputsShuttle(leftMapping, joinRel.getInput(0))); for (RexNode r : RelOptUtil.conjunctions(leftChildPredicates)) { exprFields.put(r.toString(), RelOptUtil.InputFinder.bits(r)); allExprsDigests.add(r.toString()); new RexPermuteInputsShuttle(rightMapping, joinRel.getInput(1))); for (RexNode r : RelOptUtil.conjunctions(rightChildPredicates)) { exprFields.put(r.toString(), RelOptUtil.InputFinder.bits(r)); allExprsDigests.add(r.toString()); RexBuilder rexBuilder = joinRel.getCluster().getRexBuilder(); List<RexNode> exprs = RelOptUtil.conjunctions( compose(rexBuilder, ImmutableList.of(joinRel.getCondition()))); Lists.transform(exprs, new Function<RexNode, Void>() { public Void apply(RexNode input) {
final int origLeftInputCount = root.getRowType().getFieldCount(); if (leftKeys != null) { List<RexNode> newLeftInputExpr = Lists.newArrayList(); for (int i = 0; i < origLeftInputCount; i++) { newLeftInputExpr.add(rexBuilder.makeInputRef(root, i)); final List<Integer> leftJoinKeys = Lists.newArrayList(); for (RexNode leftKey : leftKeys) { int index = newLeftInputExpr.indexOf(leftKey); RelOptUtil.createProject( root, newLeftInputExpr, final int rightOffset = root.getRowType().getFieldCount() - newLeftInput.getRowType().getFieldCount(); final List<Integer> rightKeys = Util.range(rightOffset, rightOffset + leftKeys.size()); RelOptUtil.createEquiJoinCondition(newLeftInput, leftJoinKeys, rel, rightKeys, rexBuilder); } else { joinCond = rexBuilder.makeLiteral(true);
relToHiveRR, relToHiveColNameCalcitePosMap, false); } else { calciteJoinCond = cluster.getRexBuilder().makeLiteral(true); List<RexNode> rightJoinKeys = new ArrayList<RexNode>(); RexNode nonEquiConds = RelOptUtil.splitJoinCondition(sysFieldList, leftRel, rightRel, calciteJoinCond, leftJoinKeys, rightJoinKeys, null, null); nonEquiConds = RexUtil.shift(nonEquiConds, leftRel.getRowType().getFieldCount(), inputRels[0].getRowType().getFieldCount() - leftRel.getRowType().getFieldCount()); RexUtil.composeConjunction(cluster.getRexBuilder(), ImmutableList.of(remainingEquiCond, nonEquiConds), false) : nonEquiConds; topRel = HiveSemiJoin.getSemiJoin(cluster, cluster.traitSetOf(HiveRelNode.CONVENTION), inputRels[0], inputRels[1], calciteJoinCond, ImmutableIntList.copyOf(leftKeys), ImmutableIntList.copyOf(rightKeys)); for (int i = 0; i < leftRel.getRowType().getFieldCount(); i++) { final RelDataTypeField field = leftRel.getRowType().getFieldList().get(i); topFields.add(leftRel.getCluster().getRexBuilder().makeInputRef(field.getType(), i)); topFieldNames.add(field.getName());
RelOptUtil.conjunctions(filter.getCondition()); RelOptUtil.classifyFilters( corr, aboveFilters, final RexBuilder rexBuilder = corr.getCluster().getRexBuilder(); final RelBuilder relBuilder = call.builder(); final RelNode leftRel = corr.copy(corr.getTraitSet(), ImmutableList.of(leftRel, rightRel)); relBuilder.filter( RexUtil.fixUp(rexBuilder, aboveFilters, RelOptUtil.getFieldTypeList(relBuilder.peek().getRowType())));
RelNode newProject = RelOptUtil.createProject(aggregate, ImmutableList.of(rexBuilder.makeCast( cluster.getTypeFactory().createTypeWithNullability(projExprs.get(0).getType(), true), projExprs.get(0))), null, false, relBuilder);
conjuctiveElements = RelOptUtil.conjunctions(predicate); mapOfProjIndxInJoinSchemaToLeafPInfo.put(e.getKey(), ImmutableList.copyOf(e.getValue()));