cloud-fan commented on a change in pull request #27890: [SPARK-31076][SQL][FOLLOWUP] Incapsulate date rebasing to `DaysWritable` URL: https://github.com/apache/spark/pull/27890#discussion_r392162971
########## File path: sql/hive/src/main/scala/org/apache/spark/sql/hive/DaysWritable.scala ########## @@ -0,0 +1,113 @@ +/* + * 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.hive + +import java.io.{DataInput, DataOutput, IOException} +import java.sql.Date +import java.time.LocalDate +import java.util.Calendar + +import org.apache.hadoop.hive.serde2.io.DateWritable +import org.apache.hadoop.io.WritableUtils + +import org.apache.spark.sql.catalyst.util.{DateTimeConstants, DateTimeUtils} + +/** + * The class accepts/returns days in Gregorian calendar and rebase them + * via conversion to local date in Julian calendar for dates before 1582-10-15 + * in read/write for backward compatibility with Spark 2.4 and earlier versions. + * + * @param gregorianDays The number of days since the epoch 1970-01-01 in + * Gregorian calendar. + */ +private[hive] class DaysWritable(var gregorianDays: Int = 0) extends DateWritable { + + def this(dateWritable: DateWritable) = { + this(dateWritable match { + case daysWritable: DaysWritable => daysWritable.gregorianDays + case dateWritable: DateWritable => + DaysWritable.rebaseJulianToGregorianDays(dateWritable.getDays) + }) + } + + override def getDays: Int = gregorianDays + override def get(): Date = { + val days = DaysWritable.rebaseGregorianToJulianDays(gregorianDays) + new Date(DateWritable.daysToMillis(days)) + } + + @throws[IOException] + override def write(out: DataOutput): Unit = { + // Rebasing days since the epoch to store the same number of days + // as by Spark 2.4 and earlier versions. Spark 3.0 switched to + // Proleptic Gregorian calendar (see SPARK-26651), and as a consequence of that, + // this affects dates before 1582-10-15. Spark 2.4 and earlier versions use + // Julian calendar for dates before 1582-10-15. So, the same local date may + // be mapped to different number of days since the epoch in different calendars. + // For example: + // Proleptic Gregorian calendar: 1582-01-01 -> -141714 + // Julian calendar: 1582-01-01 -> -141704 + // The code below converts -141714 to -141704. + val writeDays = DaysWritable.rebaseGregorianToJulianDays(gregorianDays) Review comment: shall we cache the `julianDays`? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
