Skip to content

Commit

Permalink
rename to addImmutableStateIfNotExists
Browse files Browse the repository at this point in the history
  • Loading branch information
mgaido91 committed Dec 13, 2017
1 parent c3e30a0 commit 90f517c
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ case class MonotonicallyIncreasingID() extends LeafExpression with Nondeterminis
val countTerm = ctx.freshName("count")
val partitionMaskTerm = "partitionMask"
ctx.addMutableState(ctx.JAVA_LONG, countTerm)
ctx.addSingleMutableState(ctx.JAVA_LONG, partitionMaskTerm)
ctx.addImmutableStateIfNotExists(ctx.JAVA_LONG, partitionMaskTerm)
ctx.addPartitionInitializationStatement(s"$countTerm = 0L;")
ctx.addPartitionInitializationStatement(s"$partitionMaskTerm = ((long) partitionIndex) << 33;")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ case class SparkPartitionID() extends LeafExpression with Nondeterministic {

override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
val idTerm = "partitionId"
ctx.addSingleMutableState(ctx.JAVA_INT, idTerm)
ctx.addImmutableStateIfNotExists(ctx.JAVA_INT, idTerm)
ctx.addPartitionInitializationStatement(s"$idTerm = partitionIndex;")
ev.copy(code = s"final ${ctx.javaType(dataType)} ${ev.value} = $idTerm;", isNull = "false")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,17 +195,20 @@ class CodegenContext {
}

/**
* Add a mutable state as a field to the generated class only if it does not exist yet a field
* Add an immutable state as a field to the generated class only if it does not exist yet a field
* with that name. This helps reducing the number of the generated class' fields, since the same
* variable can be reused by many functions.
*
* Even though the added variables are not declared as final, they should never be reassigned in
* the generated code to prevent errors and unexpected behaviors.
*
* Internally, this method calls `addMutableState`.
*
* @param javaType Java type of the field.
* @param variableName Name of the field.
* @param initCode The statement(s) to put into the init() method to initialize this field.
*/
def addSingleMutableState(
def addImmutableStateIfNotExists(
javaType: String,
variableName: String,
initCode: String = ""): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,8 @@ case class DayOfWeek(child: Expression) extends UnaryExpression with ImplicitCas
val cal = classOf[Calendar].getName
val c = "calDayOfWeek"
val dtu = DateTimeUtils.getClass.getName.stripSuffix("$")
ctx.addSingleMutableState(cal, c, s"""$c = $cal.getInstance($dtu.getTimeZone("UTC"));""")
ctx.addImmutableStateIfNotExists(cal, c,
s"""$c = $cal.getInstance($dtu.getTimeZone("UTC"));""")
s"""
$c.setTimeInMillis($time * 1000L * 3600L * 24L);
${ev.value} = $c.get($cal.DAY_OF_WEEK);
Expand Down Expand Up @@ -486,7 +487,7 @@ case class WeekOfYear(child: Expression) extends UnaryExpression with ImplicitCa
val cal = classOf[Calendar].getName
val c = "calWeekOfYear"
val dtu = DateTimeUtils.getClass.getName.stripSuffix("$")
ctx.addSingleMutableState(cal, c,
ctx.addImmutableStateIfNotExists(cal, c,
s"""
|$c = $cal.getInstance($dtu.getTimeZone("UTC"));
|$c.setFirstDayOfWeek($cal.MONDAY);
Expand Down Expand Up @@ -1019,7 +1020,8 @@ case class FromUTCTimestamp(left: Expression, right: Expression)
val tzClass = classOf[TimeZone].getName
val dtu = DateTimeUtils.getClass.getName.stripSuffix("$")
ctx.addMutableState(tzClass, tzTerm, s"""$tzTerm = $dtu.getTimeZone("$tz");""")
ctx.addSingleMutableState(tzClass, utcTerm, s"""$utcTerm = $dtu.getTimeZone("UTC");""")
ctx.addImmutableStateIfNotExists(tzClass, utcTerm,
s"""$utcTerm = $dtu.getTimeZone("UTC");""")
val eval = left.genCode(ctx)
ev.copy(code = s"""
|${eval.code}
Expand Down Expand Up @@ -1195,7 +1197,8 @@ case class ToUTCTimestamp(left: Expression, right: Expression)
val tzClass = classOf[TimeZone].getName
val dtu = DateTimeUtils.getClass.getName.stripSuffix("$")
ctx.addMutableState(tzClass, tzTerm, s"""$tzTerm = $dtu.getTimeZone("$tz");""")
ctx.addSingleMutableState(tzClass, utcTerm, s"""$utcTerm = $dtu.getTimeZone("UTC");""")
ctx.addImmutableStateIfNotExists(tzClass, utcTerm,
s"""$utcTerm = $dtu.getTimeZone("UTC");""")
val eval = left.genCode(ctx)
ev.copy(code = s"""
|${eval.code}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,7 @@ case class EncodeUsingSerializer(child: Expression, kryo: Boolean)
$serializer = ($serializerInstanceClass) new $serializerClass($env.conf()).newInstance();
}
"""
ctx.addSingleMutableState(serializerInstanceClass, serializer, serializerInit)
ctx.addImmutableStateIfNotExists(serializerInstanceClass, serializer, serializerInit)

// Code to serialize.
val input = child.genCode(ctx)
Expand Down Expand Up @@ -1218,7 +1218,7 @@ case class DecodeUsingSerializer[T](child: Expression, tag: ClassTag[T], kryo: B
$serializer = ($serializerInstanceClass) new $serializerClass($env.conf()).newInstance();
}
"""
ctx.addSingleMutableState(serializerInstanceClass, serializer, serializerInit)
ctx.addImmutableStateIfNotExists(serializerInstanceClass, serializer, serializerInit)

// Code to deserialize.
val input = child.genCode(ctx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,11 @@ class CodeGenerationSuite extends SparkFunSuite with ExpressionEvalHelper {
val ctx = new CodegenContext
val mutableState1 = "field1"
val mutableState2 = "field2"
ctx.addSingleMutableState("int", mutableState1)
ctx.addSingleMutableState("int", mutableState1)
ctx.addSingleMutableState("String", mutableState2)
ctx.addSingleMutableState("int", mutableState1)
ctx.addSingleMutableState("String", mutableState2)
ctx.addImmutableStateIfNotExists("int", mutableState1)
ctx.addImmutableStateIfNotExists("int", mutableState1)
ctx.addImmutableStateIfNotExists("String", mutableState2)
ctx.addImmutableStateIfNotExists("int", mutableState1)
ctx.addImmutableStateIfNotExists("String", mutableState2)
assert(ctx.mutableStates.length == 2)
}
}

0 comments on commit 90f517c

Please sign in to comment.