|
| 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.hive |
| 19 | + |
| 20 | +import java.io.{DataInput, DataOutput, IOException} |
| 21 | +import java.time.LocalDate |
| 22 | +import java.util.Calendar |
| 23 | + |
| 24 | +import org.apache.hadoop.hive.serde2.io.DateWritable |
| 25 | +import org.apache.hadoop.io.WritableUtils |
| 26 | + |
| 27 | +import org.apache.spark.sql.catalyst.util.{DateTimeConstants, DateTimeUtils} |
| 28 | + |
| 29 | +/** |
| 30 | + * The class accepts/returns days in Gregorian calendar and rebase them |
| 31 | + * via conversion to local date in Julian calendar for dates before 1582-10-15 |
| 32 | + * in read/write for backward compatibility with Spark 2.4 and earlier versions. |
| 33 | + * |
| 34 | + * @param gregorianDays The number of days since the epoch 1970-01-01 in |
| 35 | + * Gregorian calendar. |
| 36 | + */ |
| 37 | +class DaysWritable(var gregorianDays: Int = 0) extends DateWritable { |
| 38 | + |
| 39 | + private final val JULIAN_CUTOVER_DAY = |
| 40 | + rebaseGregorianToJulianDays(DateTimeUtils.GREGORIAN_CUTOVER_DAY.toInt) |
| 41 | + |
| 42 | + override def getDays: Int = gregorianDays |
| 43 | + |
| 44 | + @throws[IOException] |
| 45 | + override def readFields(in: DataInput): Unit = { |
| 46 | + val days = WritableUtils.readVInt(in) |
| 47 | + gregorianDays = if (days < JULIAN_CUTOVER_DAY) { |
| 48 | + rebaseJulianToGregorianDays(days) |
| 49 | + } else { |
| 50 | + days |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @throws[IOException] |
| 55 | + override def write(out: DataOutput): Unit = { |
| 56 | + val rebasedDays = if (gregorianDays < DateTimeUtils.GREGORIAN_CUTOVER_DAY) { |
| 57 | + rebaseGregorianToJulianDays(gregorianDays) |
| 58 | + } else { |
| 59 | + gregorianDays |
| 60 | + } |
| 61 | + WritableUtils.writeVInt(out, rebasedDays) |
| 62 | + } |
| 63 | + |
| 64 | + // Rebasing days since the epoch to store the same number of days |
| 65 | + // as by Spark 2.4 and earlier versions. Spark 3.0 switched to |
| 66 | + // Proleptic Gregorian calendar (see SPARK-26651), and as a consequence of that, |
| 67 | + // this affects dates before 1582-10-15. Spark 2.4 and earlier versions use |
| 68 | + // Julian calendar for dates before 1582-10-15. So, the same local date may |
| 69 | + // be mapped to different number of days since the epoch in different calendars. |
| 70 | + // For example: |
| 71 | + // Proleptic Gregorian calendar: 1582-01-01 -> -141714 |
| 72 | + // Julian calendar: 1582-01-01 -> -141704 |
| 73 | + // The code below converts -141714 to -141704. |
| 74 | + private def rebaseGregorianToJulianDays(daysSinceEpoch: Int): Int = { |
| 75 | + val millis = Math.multiplyExact(daysSinceEpoch, DateTimeConstants.MILLIS_PER_DAY) |
| 76 | + val utcCal = new Calendar.Builder() |
| 77 | + .setCalendarType("gregory") |
| 78 | + .setTimeZone(DateTimeUtils.TimeZoneUTC) |
| 79 | + .setInstant(millis) |
| 80 | + .build() |
| 81 | + val localDate = LocalDate.of( |
| 82 | + utcCal.get(Calendar.YEAR), |
| 83 | + utcCal.get(Calendar.MONTH) + 1, |
| 84 | + utcCal.get(Calendar.DAY_OF_MONTH)) |
| 85 | + Math.toIntExact(localDate.toEpochDay) |
| 86 | + } |
| 87 | + |
| 88 | + private def rebaseJulianToGregorianDays(daysSinceEpoch: Int): Int = { |
| 89 | + val localDate = LocalDate.ofEpochDay(daysSinceEpoch) |
| 90 | + val utcCal = new Calendar.Builder() |
| 91 | + .setCalendarType("gregory") |
| 92 | + .setTimeZone(DateTimeUtils.TimeZoneUTC) |
| 93 | + .setDate(localDate.getYear, localDate.getMonthValue - 1, localDate.getDayOfMonth) |
| 94 | + .build() |
| 95 | + Math.toIntExact(Math.floorDiv(utcCal.getTimeInMillis, DateTimeConstants.MILLIS_PER_DAY)) |
| 96 | + } |
| 97 | +} |
0 commit comments