Skip to content

Commit 2c2b007

Browse files
committed
[SPARK-13135][SQL] Don't print expressions recursively in generated code
1 parent 0df3cfb commit 2c2b007

File tree

8 files changed

+24
-20
lines changed

8 files changed

+24
-20
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Expression.scala

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818
package org.apache.spark.sql.catalyst.expressions
1919

2020
import org.apache.spark.sql.catalyst.InternalRow
21-
import org.apache.spark.sql.catalyst.analysis.{Analyzer, TypeCheckResult, UnresolvedAttribute}
21+
import org.apache.spark.sql.catalyst.analysis.{TypeCheckResult, UnresolvedAttribute}
2222
import org.apache.spark.sql.catalyst.expressions.codegen._
2323
import org.apache.spark.sql.catalyst.trees.TreeNode
24-
import org.apache.spark.sql.catalyst.util.sequenceOption
2524
import org.apache.spark.sql.types._
2625

2726
////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -96,15 +95,13 @@ abstract class Expression extends TreeNode[Expression] {
9695
ctx.subExprEliminationExprs.get(this).map { subExprState =>
9796
// This expression is repeated meaning the code to evaluated has already been added
9897
// as a function and called in advance. Just use it.
99-
val code = s"/* ${this.toCommentSafeString} */"
100-
ExprCode(code, subExprState.isNull, subExprState.value)
98+
ExprCode("", subExprState.isNull, subExprState.value)
10199
}.getOrElse {
102100
val isNull = ctx.freshName("isNull")
103101
val value = ctx.freshName("value")
104102
val ve = ExprCode("", isNull, value)
105103
ve.code = genCode(ctx, ve)
106-
// Add `this` in the comment.
107-
ve.copy(s"/* ${this.toCommentSafeString} */\n" + ve.code.trim)
104+
ve
108105
}
109106
}
110107

@@ -221,7 +218,7 @@ abstract class Expression extends TreeNode[Expression] {
221218
* Returns the string representation of this expression that is safe to be put in
222219
* code comments of generated code.
223220
*/
224-
protected def toCommentSafeString: String = this.toString
221+
def toCommentSafeString: String = this.toString
225222
.replace("*/", "\\*\\/")
226223
.replace("\\u", "\\\\u")
227224

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/codegen/CodeGenerator.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ import org.apache.spark.util.Utils
3636
/**
3737
* Java source for evaluating an [[Expression]] given a [[InternalRow]] of input.
3838
*
39-
* @param code The sequence of statements required to evaluate the expression.
40-
* @param isNull A term that holds a boolean value representing whether the expression evaluated
41-
* to null.
42-
* @param value A term for a (possibly primitive) value of the result of the evaluation. Not
43-
* valid if `isNull` is set to `true`.
39+
* @param code The Java source code required to evaluate the expression.
40+
* @param isNull Name of the variable that holds the boolean value representing whether the
41+
* expression evaluated to null.
42+
* @param value Name of the variable that holds the (possibly primitive) value of the result
43+
* of the evaluation. Not valid if `isNull` is `true`.
4444
*/
4545
case class ExprCode(var code: String, var isNull: String, var value: String)
4646

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/codegen/GenerateMutableProjection.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ object GenerateMutableProjection extends CodeGenerator[Seq[Expression], () => Mu
125125

126126
public java.lang.Object apply(java.lang.Object _i) {
127127
InternalRow ${ctx.INPUT_ROW} = (InternalRow) _i;
128+
// project list: ${expressions.map(_.toCommentSafeString).mkString(", ")}
128129
$evalSubexpr
129130
$allProjections
130131
// copy all the results into MutableRow

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/codegen/GeneratePredicate.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ object GeneratePredicate extends CodeGenerator[Expression, (InternalRow) => Bool
5656
}
5757

5858
public boolean eval(InternalRow ${ctx.INPUT_ROW}) {
59+
// predicate: ${predicate.toCommentSafeString}
5960
${eval.code}
6061
return !${eval.isNull} && ${eval.value};
6162
}

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/codegen/GenerateSafeProjection.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ object GenerateSafeProjection extends CodeGenerator[Seq[Expression], Projection]
171171

172172
public java.lang.Object apply(java.lang.Object _i) {
173173
InternalRow ${ctx.INPUT_ROW} = (InternalRow) _i;
174+
// project list: ${expressions.map(_.toCommentSafeString).mkString(", ")}
174175
$allExpressions
175176
return mutableRow;
176177
}

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/codegen/GenerateUnsafeProjection.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ object GenerateUnsafeProjection extends CodeGenerator[Seq[Expression], UnsafePro
332332

333333
val code =
334334
s"""
335+
// project list: ${expressions.map(_.toCommentSafeString).mkString("[", ", ", "]")}
335336
$resetBufferHolder
336337
$evalSubexpr
337338
$writeExpressions

sql/core/src/main/scala/org/apache/spark/sql/execution/aggregate/TungstenAggregate.scala

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ case class TungstenAggregate(
198198
ctx.addNewFunction(doAgg,
199199
s"""
200200
| private void $doAgg() throws java.io.IOException {
201-
| // initialize aggregation buffer
201+
| // initialize agg buffer:
202+
| // ${aggregateExpressions.map(_.toCommentSafeString).mkString("[", ", ", "]")}
202203
| ${bufVars.map(_.code).mkString("\n")}
203204
|
204205
| ${child.asInstanceOf[CodegenSupport].produce(ctx, this)}
@@ -222,7 +223,7 @@ case class TungstenAggregate(
222223
// only have DeclarativeAggregate
223224
val functions = aggregateExpressions.map(_.aggregateFunction.asInstanceOf[DeclarativeAggregate])
224225
val inputAttrs = functions.flatMap(_.aggBufferAttributes) ++ child.output
225-
val updateExpr = aggregateExpressions.flatMap { e =>
226+
val updateExpr: Seq[Expression] = aggregateExpressions.flatMap { e =>
226227
e.mode match {
227228
case Partial | Complete =>
228229
e.aggregateFunction.asInstanceOf[DeclarativeAggregate].updateExpressions
@@ -242,9 +243,9 @@ case class TungstenAggregate(
242243
}
243244

244245
s"""
245-
| // do aggregate
246+
| // aggregate: ${updateExpr.map(_.toCommentSafeString).mkString("[", ", ", "]")}
246247
| ${aggVals.map(_.code).mkString("\n")}
247-
| // update aggregation buffer
248+
| // update agg buffer
248249
| ${updates.mkString("")}
249250
""".stripMargin
250251
}
@@ -427,17 +428,17 @@ case class TungstenAggregate(
427428
}
428429

429430
s"""
430-
// generate grouping key
431+
// grouping key: ${groupingExpressions.map(_.toCommentSafeString).mkString("[", ", ", "]")}
431432
${keyCode.code}
432433
UnsafeRow $buffer = $hashMapTerm.getAggregationBufferFromUnsafeRow($key);
433434
if ($buffer == null) {
434435
// failed to allocate the first page
435436
throw new OutOfMemoryError("No enough memory for aggregation");
436437
}
437438

438-
// evaluate aggregate function
439+
// aggregate: ${updateExpr.map(_.toCommentSafeString).mkString("[", ", ", "]")}
439440
${evals.map(_.code).mkString("\n")}
440-
// update aggregate buffer
441+
// update agg buffer
441442
${updates.mkString("\n")}
442443
"""
443444
}

sql/core/src/main/scala/org/apache/spark/sql/execution/basicOperators.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ case class Project(projectList: Seq[NamedExpression], child: SparkPlan)
5151
ctx.currentVars = input
5252
val output = exprs.map(_.gen(ctx))
5353
s"""
54+
| // project list: ${exprs.map(_.toCommentSafeString).mkString("[", ", ", "]")}
5455
| ${output.map(_.code).mkString("\n")}
5556
|
5657
| ${consume(ctx, output)}
@@ -89,11 +90,12 @@ case class Filter(condition: Expression, child: SparkPlan) extends UnaryNode wit
8990
}
9091

9192
override def doConsume(ctx: CodegenContext, input: Seq[ExprCode]): String = {
92-
val expr = ExpressionCanonicalizer.execute(
93+
val expr: Expression = ExpressionCanonicalizer.execute(
9394
BindReferences.bindReference(condition, child.output))
9495
ctx.currentVars = input
9596
val eval = expr.gen(ctx)
9697
s"""
98+
| // predicate: ${expr.toCommentSafeString}
9799
| ${eval.code}
98100
| if (!${eval.isNull} && ${eval.value}) {
99101
| ${consume(ctx, ctx.currentVars)}

0 commit comments

Comments
 (0)