cloud-fan commented on a change in pull request #26418: [SPARK-29783][SQL]
Support SQL Standard/ISO_8601 output style for interval type
URL: https://github.com/apache/spark/pull/26418#discussion_r345323865
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/IntervalUtils.scala
##########
@@ -424,6 +425,111 @@ object IntervalUtils {
fromDoubles(interval.months / num, interval.days / num,
interval.microseconds / num)
}
+ def toMultiUnitsString(interval: CalendarInterval): String = {
+ if (interval.months == 0 && interval.days == 0 && interval.microseconds ==
0) {
+ return "0 seconds"
+ }
+ val sb = new StringBuilder
+ if (interval.months != 0) {
+ appendUnit(sb, interval.months / 12, "years")
+ appendUnit(sb, interval.months % 12, "months")
+ }
+ appendUnit(sb, interval.days, "days")
+ if (interval.microseconds != 0) {
+ var rest = interval.microseconds
+ appendUnit(sb, rest / MICROS_PER_HOUR, "hours")
+ rest %= MICROS_PER_HOUR
+ appendUnit(sb, rest / MICROS_PER_MINUTE, "minutes")
+ rest %= MICROS_PER_MINUTE
+ if (rest != 0) {
+ val s = BigDecimal.valueOf(rest, 6).stripTrailingZeros.toPlainString
+ sb.append(s).append(" seconds ")
+ }
+ }
+ sb.setLength(sb.length - 1)
+ sb.toString
+ }
+
+ private def appendUnit(sb: StringBuilder, value: Long, unit: String): Unit =
{
+ if (value != 0) sb.append(value).append(' ').append(unit).append(' ')
+ }
+
+ def toSqlStandardString(interval: CalendarInterval): String = {
+ val yearMonthPart = if (interval.months < 0) {
+ val ma = math.abs(interval.months)
+ "-" + ma / 12 + "-" + ma % 12
+ } else if (interval.months > 0) {
+ "+" + interval.months / 12 + "-" + interval.months % 12
+ } else {
+ ""
+ }
+
+ val dayPart = if (interval.days < 0) {
+ interval.days.toString
+ } else if (interval.days > 0) {
+ "+" + interval.days
+ } else {
+ ""
+ }
+
+ val timePart = if (interval.microseconds != 0) {
+ val sign = if (interval.microseconds > 0) "+" else "-"
+ val sb = new StringBuilder(sign)
+ var rest = math.abs(interval.microseconds)
+ sb.append(rest / MICROS_PER_HOUR)
+ sb.append(':')
+ rest = rest % MICROS_PER_HOUR
Review comment:
nit: `rest %= ...`
----------------------------------------------------------------
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]