Skip to content

Commit 246ee74

Browse files
author
Davies Liu
committed
address comments
1 parent 309d2e1 commit 246ee74

File tree

14 files changed

+69
-29
lines changed

14 files changed

+69
-29
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/CatalystTypeConverters.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,9 @@ object CatalystTypeConverters {
276276
}
277277

278278
private object TimestampConverter extends CatalystTypeConverter[Timestamp, Timestamp, Any] {
279-
override def toCatalystImpl(scalaValue: Timestamp): Long = DateUtils.fromTimestamp(scalaValue)
279+
override def toCatalystImpl(scalaValue: Timestamp): Long = DateUtils.fromJavaTimestamp(scalaValue)
280280
override def toScala(catalystValue: Any): Timestamp =
281-
if (catalystValue == null) null else DateUtils.toTimestamp(catalystValue.asInstanceOf[Long])
281+
if (catalystValue == null) null else DateUtils.toJavaTimestamp(catalystValue.asInstanceOf[Long])
282282
override def toScalaImpl(row: Row, column: Int): Timestamp = toScala(row.getLong(column))
283283
}
284284

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ case class Cast(child: Expression, dataType: DataType) extends UnaryExpression w
114114
case BinaryType => buildCast[Array[Byte]](_, UTF8String(_))
115115
case DateType => buildCast[Int](_, d => UTF8String(DateUtils.toString(d)))
116116
case TimestampType => buildCast[Long](_,
117-
t => UTF8String(timestampToString(DateUtils.toTimestamp(t))))
117+
t => UTF8String(timestampToString(DateUtils.toJavaTimestamp(t))))
118118
case _ => buildCast[Any](_, o => UTF8String(o.toString))
119119
}
120120

@@ -159,7 +159,7 @@ case class Cast(child: Expression, dataType: DataType) extends UnaryExpression w
159159
if (periodIdx != -1 && n.length() - periodIdx > 9) {
160160
n = n.substring(0, periodIdx + 10)
161161
}
162-
try DateUtils.fromTimestamp(Timestamp.valueOf(n))
162+
try DateUtils.fromJavaTimestamp(Timestamp.valueOf(n))
163163
catch { case _: java.lang.IllegalArgumentException => null }
164164
})
165165
case BooleanType =>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ object Literal {
3737
case d: BigDecimal => Literal(Decimal(d), DecimalType.Unlimited)
3838
case d: java.math.BigDecimal => Literal(Decimal(d), DecimalType.Unlimited)
3939
case d: Decimal => Literal(d, DecimalType.Unlimited)
40-
case t: Timestamp => Literal(DateUtils.fromTimestamp(t), TimestampType)
40+
case t: Timestamp => Literal(DateUtils.fromJavaTimestamp(t), TimestampType)
4141
case d: Date => Literal(DateUtils.fromJavaDate(d), DateType)
4242
case a: Array[Byte] => Literal(a, BinaryType)
4343
case null => Literal(null, NullType)

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateUtils.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ object DateUtils {
9292
/**
9393
* Return a java.sql.Timestamp from number of 100ns since epoch
9494
*/
95-
def toTimestamp(ns: Long): Timestamp = {
95+
def toJavaTimestamp(num100ns: Long): Timestamp = {
9696
// setNanos() will overwrite the millisecond part, so the milliseconds should be
9797
// cut off at seconds
98-
var seconds = ns / HUNDRED_NANOS_PER_SECOND
99-
var nanos = ns % HUNDRED_NANOS_PER_SECOND
98+
var seconds = num100ns / HUNDRED_NANOS_PER_SECOND
99+
var nanos = num100ns % HUNDRED_NANOS_PER_SECOND
100100
// setNanos() can not accept negative value
101101
if (nanos < 0) {
102102
nanos += HUNDRED_NANOS_PER_SECOND
@@ -110,7 +110,7 @@ object DateUtils {
110110
/**
111111
* Return the number of 100ns since epoch from java.sql.Timestamp.
112112
*/
113-
def fromTimestamp(t: Timestamp): Long = {
113+
def fromJavaTimestamp(t: Timestamp): Long = {
114114
if (t != null) {
115115
t.getTime() * 10000L + (t.getNanos().toLong / 100) % 10000L
116116
} else {

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastSuite.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class CastSuite extends SparkFunSuite with ExpressionEvalHelper {
138138
checkEvaluation(cast(cast(sd, DateType), StringType), sd)
139139
checkEvaluation(cast(cast(d, StringType), DateType), 0)
140140
checkEvaluation(cast(cast(nts, TimestampType), StringType), nts)
141-
checkEvaluation(cast(cast(ts, StringType), TimestampType), DateUtils.fromTimestamp(ts))
141+
checkEvaluation(cast(cast(ts, StringType), TimestampType), DateUtils.fromJavaTimestamp(ts))
142142

143143
// all convert to string type to check
144144
checkEvaluation(cast(cast(cast(nts, TimestampType), DateType), StringType), sd)
@@ -270,9 +270,9 @@ class CastSuite extends SparkFunSuite with ExpressionEvalHelper {
270270
checkEvaluation(cast(ts, LongType), 15.toLong)
271271
checkEvaluation(cast(ts, FloatType), 15.002f)
272272
checkEvaluation(cast(ts, DoubleType), 15.002)
273-
checkEvaluation(cast(cast(tss, ShortType), TimestampType), DateUtils.fromTimestamp(ts))
274-
checkEvaluation(cast(cast(tss, IntegerType), TimestampType), DateUtils.fromTimestamp(ts))
275-
checkEvaluation(cast(cast(tss, LongType), TimestampType), DateUtils.fromTimestamp(ts))
273+
checkEvaluation(cast(cast(tss, ShortType), TimestampType), DateUtils.fromJavaTimestamp(ts))
274+
checkEvaluation(cast(cast(tss, IntegerType), TimestampType), DateUtils.fromJavaTimestamp(ts))
275+
checkEvaluation(cast(cast(tss, LongType), TimestampType), DateUtils.fromJavaTimestamp(ts))
276276
checkEvaluation(
277277
cast(cast(millis.toFloat / 1000, TimestampType), FloatType),
278278
millis.toFloat / 1000)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.sql.catalyst.util
19+
20+
import java.sql.Timestamp
21+
22+
import org.apache.spark.SparkFunSuite
23+
24+
25+
class DateUtilsSuite extends SparkFunSuite {
26+
27+
test("timestamp") {
28+
val now = new Timestamp(System.currentTimeMillis())
29+
now.setNanos(100)
30+
val ns = DateUtils.fromJavaTimestamp(now)
31+
assert(ns % 10000000L == 1)
32+
assert(DateUtils.toJavaTimestamp(ns) == now)
33+
34+
List(-111111111111L, -1L, 0, 1L, 111111111111L).foreach { t =>
35+
val ts = DateUtils.toJavaTimestamp(t)
36+
assert(DateUtils.fromJavaTimestamp(ts) == t)
37+
assert(DateUtils.toJavaTimestamp(DateUtils.fromJavaTimestamp(ts)) == ts)
38+
}
39+
}
40+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ object EvaluatePython {
142142
case (ud, udt: UserDefinedType[_]) => toJava(udt.serialize(ud), udt.sqlType)
143143

144144
case (date: Int, DateType) => DateUtils.toJavaDate(date)
145-
case (t: Long, TimestampType) => DateUtils.toTimestamp(t)
145+
case (t: Long, TimestampType) => DateUtils.toJavaTimestamp(t)
146146
case (s: UTF8String, StringType) => s.toString
147147

148148
// Pyrolite can handle Timestamp and Decimal
@@ -186,7 +186,7 @@ object EvaluatePython {
186186
case (c: java.util.Calendar, TimestampType) =>
187187
c.getTimeInMillis * 10000L
188188
case (t: java.sql.Timestamp, TimestampType) =>
189-
DateUtils.fromTimestamp(t)
189+
DateUtils.fromJavaTimestamp(t)
190190

191191
case (_, udt: UserDefinedType[_]) =>
192192
fromJava(obj, udt.sqlType)

sql/core/src/main/scala/org/apache/spark/sql/jdbc/JDBCRDD.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ private[sql] class JDBCRDD(
420420
case TimestampConversion =>
421421
val t = rs.getTimestamp(pos)
422422
if (t != null) {
423-
mutableRow.setLong(i, DateUtils.fromTimestamp(t))
423+
mutableRow.setLong(i, DateUtils.fromJavaTimestamp(t))
424424
} else {
425425
mutableRow.update(i, null)
426426
}

sql/core/src/main/scala/org/apache/spark/sql/parquet/ParquetConverter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ private[parquet] abstract class CatalystConverter extends GroupConverter {
268268
* Read a Timestamp value from a Parquet Int96Value
269269
*/
270270
protected[parquet] def readTimestamp(value: Binary): Long = {
271-
DateUtils.fromTimestamp(CatalystTimestampConverter.convertToTimestamp(value))
271+
DateUtils.fromJavaTimestamp(CatalystTimestampConverter.convertToTimestamp(value))
272272
}
273273
}
274274

sql/core/src/main/scala/org/apache/spark/sql/parquet/ParquetTableSupport.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ private[parquet] class RowWriteSupport extends WriteSupport[Row] with Logging {
313313
}
314314

315315
private[parquet] def writeTimestamp(ts: Long): Unit = {
316-
val binaryNanoTime = CatalystTimestampConverter.convertFromTimestamp(DateUtils.toTimestamp(ts))
316+
val binaryNanoTime = CatalystTimestampConverter.convertFromTimestamp(DateUtils.toJavaTimestamp(ts))
317317
writer.addBinary(binaryNanoTime)
318318
}
319319
}

0 commit comments

Comments
 (0)