private List<Expression> translate(List<Statement> list, List<RexLocalRef> rexList) { // First pass. Count how many times each sub-expression is used. this.list = null; for (RexNode rexExpr : rexList) { translate(rexExpr); } // Mark expressions as inline if they are not used more than once. for (Map.Entry<RexNode, Slot> entry : map.entrySet()) { if (entry.getValue().count < 2 || entry.getKey() instanceof RexLiteral) { inlineRexSet.add(entry.getKey()); } } // Second pass. When translating each expression, if it is used more // than once, the first time it is encountered, add a declaration to the // list and set its usage count to 0. this.list = list; this.map.clear(); List<Expression> translateds = new ArrayList<Expression>(); for (RexNode rexExpr : rexList) { translateds.add(translate(rexExpr)); } return translateds; }
private Expression translate(RexNode expr) { Slot slot = map.get(expr); if (slot == null) { Expression expression = translate0(expr); assert expression != null; final ParameterExpression parameter; if (!inlineRexSet.contains(expr) && !(expr instanceof RexLocalRef)) { parameter = Expressions.parameter(expression.getType(), "v" + map.size()); } else { parameter = null; } slot = new Slot(parameter, expression); if (parameter != null && list != null) { list.add(Expressions.declare(Modifier.FINAL, slot.parameterExpression, slot.expression)); } map.put(expr, slot); } slot.count++; return slot.parameterExpression != null ? slot.parameterExpression : slot.expression; }