Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ package org.apache.spark.sql.execution

import java.nio.charset.StandardCharsets
import java.sql.{Date, Timestamp}
import java.time.{Instant, LocalDate, ZoneOffset}
import java.time.{Duration, Instant, LocalDate, Period, ZoneOffset}

import org.apache.spark.sql.Row
import org.apache.spark.sql.catalyst.util.{DateFormatter, DateTimeUtils, TimestampFormatter}
import org.apache.spark.sql.catalyst.util.IntervalUtils.{durationToMicros, periodToMonths, toDayTimeIntervalString, toYearMonthIntervalString}
import org.apache.spark.sql.execution.command.{DescribeCommandBase, ExecutedCommandExec, ShowTablesCommand, ShowViewsCommand}
import org.apache.spark.sql.execution.datasources.v2.{DescribeTableExec, ShowTablesExec}
import org.apache.spark.sql.internal.SQLConf
Expand Down Expand Up @@ -117,6 +118,10 @@ object HiveResult {
struct.toSeq.zip(fields).map { case (v, t) =>
s""""${t.name}":${toHiveString((v, t.dataType), true, formatters)}"""
}.mkString("{", ",", "}")
case (period: Period, YearMonthIntervalType) =>
toYearMonthIntervalString(periodToMonths(period))
case (duration: Duration, DayTimeIntervalType) =>
toDayTimeIntervalString(durationToMicros(duration))
case (other, _: UserDefinedType[_]) => other.toString
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.apache.spark.sql.execution

import java.time.{Duration, Period}

import org.apache.spark.sql.catalyst.util.DateTimeTestUtils
import org.apache.spark.sql.connector.InMemoryTableCatalog
import org.apache.spark.sql.execution.HiveResult._
Expand Down Expand Up @@ -107,4 +109,20 @@ class HiveResultSuite extends SharedSparkSession {
}
}
}

test("SPARK-34984: year-month interval formatting in hive result") {
val df = Seq(Period.ofYears(-10).minusMonths(1)).toDF("i")
val plan1 = df.queryExecution.executedPlan
assert(hiveResultString(plan1) === Seq("INTERVAL '-10-1' YEAR TO MONTH"))
val plan2 = df.selectExpr("array(i)").queryExecution.executedPlan
assert(hiveResultString(plan2) === Seq("[INTERVAL '-10-1' YEAR TO MONTH]"))
}

test("SPARK-34984: day-time interval formatting in hive result") {
val df = Seq(Duration.ofDays(5).plusMillis(10)).toDF("i")
val plan1 = df.queryExecution.executedPlan
assert(hiveResultString(plan1) === Seq("INTERVAL '5 00:00:00.01' DAY TO SECOND"))
val plan2 = df.selectExpr("array(i)").queryExecution.executedPlan
assert(hiveResultString(plan2) === Seq("[INTERVAL '5 00:00:00.01' DAY TO SECOND]"))
}
}