diff options
author | Tarek Auel <tarek.auel@googlemail.com> | 2015-07-20 09:41:25 -0700 |
---|---|---|
committer | Reynold Xin <rxin@databricks.com> | 2015-07-20 09:41:25 -0700 |
commit | a15ecd057a6226e5cf83ca05c46748624a1cfc8c (patch) | |
tree | 8750a49aace72b2285edd0b1d46a5cd8a9f0fee5 /sql | |
parent | 5112b7f58b9b8031ff79b9184dafe12b71ba1f79 (diff) | |
download | spark-a15ecd057a6226e5cf83ca05c46748624a1cfc8c.tar.gz spark-a15ecd057a6226e5cf83ca05c46748624a1cfc8c.tar.bz2 spark-a15ecd057a6226e5cf83ca05c46748624a1cfc8c.zip |
[SPARK-9177][SQL] Reuse of calendar object in WeekOfYear
https://issues.apache.org/jira/browse/SPARK-9177
rxin Are we sure that this is thread safe? chenghao-intel explained in another PR that every partition (if I remember correctly) uses one expression instance. This instance isn't used by multiple threads, is it? If not, we are fine.
Author: Tarek Auel <tarek.auel@googlemail.com>
Closes #7516 from tarekauel/SPARK-9177 and squashes the following commits:
0c1313a [Tarek Auel] [SPARK-9177] utilize more powerful addMutableState
6e2f03f [Tarek Auel] Merge branch 'master' into SPARK-9177
a69ec92 [Tarek Auel] [SPARK-9177] address comment
6cfb180 [Tarek Auel] [SPARK-9177] calendar as lazy transient val
ff97b09 [Tarek Auel] [SPARK-9177] Reuse calendar object in interpreted code and codegen
Diffstat (limited to 'sql')
-rw-r--r-- | sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/datetimeFunctions.scala | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/datetimeFunctions.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/datetimeFunctions.scala index 8024455092..9e55f0546e 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/datetimeFunctions.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/datetimeFunctions.scala @@ -213,10 +213,14 @@ case class WeekOfYear(child: Expression) extends UnaryExpression with ImplicitCa override def dataType: DataType = IntegerType - override protected def nullSafeEval(date: Any): Any = { + @transient private lazy val c = { val c = Calendar.getInstance(TimeZone.getTimeZone("UTC")) c.setFirstDayOfWeek(Calendar.MONDAY) c.setMinimalDaysInFirstWeek(4) + c + } + + override protected def nullSafeEval(date: Any): Any = { c.setTimeInMillis(date.asInstanceOf[Int] * 1000L * 3600L * 24L) c.get(Calendar.WEEK_OF_YEAR) } @@ -225,10 +229,13 @@ case class WeekOfYear(child: Expression) extends UnaryExpression with ImplicitCa nullSafeCodeGen(ctx, ev, (time) => { val cal = classOf[Calendar].getName val c = ctx.freshName("cal") + ctx.addMutableState(cal, c, + s""" + $c = $cal.getInstance(java.util.TimeZone.getTimeZone("UTC")); + $c.setFirstDayOfWeek($cal.MONDAY); + $c.setMinimalDaysInFirstWeek(4); + """) s""" - $cal $c = $cal.getInstance(java.util.TimeZone.getTimeZone("UTC")); - $c.setFirstDayOfWeek($cal.MONDAY); - $c.setMinimalDaysInFirstWeek(4); $c.setTimeInMillis($time * 1000L * 3600L * 24L); ${ev.primitive} = $c.get($cal.WEEK_OF_YEAR); """ |