-
Notifications
You must be signed in to change notification settings - Fork 28.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-4508] [SQL] build native date type to conform behavior to Hive
The previous #3732 is reverted due to some test failure. Have fixed that. Author: Daoyuan Wang <[email protected]> Closes #4325 from adrian-wang/datenative and squashes the following commits: 096e20d [Daoyuan Wang] fix for mixed timezone 0ed0fdc [Daoyuan Wang] fix test data a2fdd4e [Daoyuan Wang] getDate c37832b [Daoyuan Wang] row to catalyst f0005b1 [Daoyuan Wang] add date in sql parser and java type conversion 024c9a6 [Daoyuan Wang] clean some import order d6715fc [Daoyuan Wang] refactoring Date as Primitive Int internally 374abd5 [Daoyuan Wang] spark native date type support
- Loading branch information
1 parent
5adbb39
commit db821ed
Showing
49 changed files
with
204 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
sql/catalyst/src/main/scala/org/apache/spark/sql/types/DateUtils.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.sql.types | ||
|
||
import java.sql.Date | ||
import java.util.{Calendar, TimeZone} | ||
|
||
import org.apache.spark.sql.catalyst.expressions.Cast | ||
|
||
/** | ||
* helper function to convert between Int value of days since 1970-01-01 and java.sql.Date | ||
*/ | ||
object DateUtils { | ||
private val MILLIS_PER_DAY = 86400000 | ||
|
||
// Java TimeZone has no mention of thread safety. Use thread local instance to be safe. | ||
private val LOCAL_TIMEZONE = new ThreadLocal[TimeZone] { | ||
override protected def initialValue: TimeZone = { | ||
Calendar.getInstance.getTimeZone | ||
} | ||
} | ||
|
||
private def javaDateToDays(d: Date): Int = { | ||
millisToDays(d.getTime) | ||
} | ||
|
||
def millisToDays(millisLocal: Long): Int = { | ||
((millisLocal + LOCAL_TIMEZONE.get().getOffset(millisLocal)) / MILLIS_PER_DAY).toInt | ||
} | ||
|
||
private def toMillisSinceEpoch(days: Int): Long = { | ||
val millisUtc = days.toLong * MILLIS_PER_DAY | ||
millisUtc - LOCAL_TIMEZONE.get().getOffset(millisUtc) | ||
} | ||
|
||
def fromJavaDate(date: java.sql.Date): Int = { | ||
javaDateToDays(date) | ||
} | ||
|
||
def toJavaDate(daysSinceEpoch: Int): java.sql.Date = { | ||
new java.sql.Date(toMillisSinceEpoch(daysSinceEpoch)) | ||
} | ||
|
||
def toString(days: Int): String = Cast.threadLocalDateFormat.get.format(toJavaDate(days)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.