-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-29783][SQL] Support SQL Standard/ISO_8601 output style for interval type #26418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
88418e0
02be22b
429ee49
da119c5
fff17d5
2af8593
b3c9e08
005a28d
5ea0ff8
fa9c41e
f89e7c1
981eae5
be65a4d
748fbad
3aa723c
0f54bac
3cf71e9
8d18fac
0c530cc
08b7359
7641e5e
0f54af8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ import org.apache.spark.sql.catalyst.expressions.codegen.Block._ | |
| import org.apache.spark.sql.catalyst.util._ | ||
| import org.apache.spark.sql.catalyst.util.DateTimeConstants._ | ||
| import org.apache.spark.sql.catalyst.util.DateTimeUtils._ | ||
| import org.apache.spark.sql.catalyst.util.IntervalUtils._ | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.internal.SQLConf.IntervalStyle._ | ||
| import org.apache.spark.sql.types._ | ||
|
|
@@ -283,12 +284,12 @@ abstract class CastBase extends UnaryExpression with TimeZoneAwareExpression wit | |
| // UDFToString | ||
| private[this] def castToString(from: DataType): Any => Any = from match { | ||
| case CalendarIntervalType => SQLConf.get.intervalOutputStyle match { | ||
| case SQL_STANDARD => buildCast[CalendarInterval](_, i => | ||
| UTF8String.fromString(IntervalUtils.toSqlStandardString(i))) | ||
| case ISO_8601 => buildCast[CalendarInterval](_, i => | ||
| UTF8String.fromString(IntervalUtils.toIso8601String(i))) | ||
| case _ => buildCast[CalendarInterval](_, i => | ||
| UTF8String.fromString(i.toString)) | ||
| case SQL_STANDARD => | ||
| buildCast[CalendarInterval](_, i => UTF8String.fromString(toSqlStandardString(i))) | ||
| case ISO_8601 => | ||
| buildCast[CalendarInterval](_, i => UTF8String.fromString(toIso8601String(i))) | ||
| case _ => | ||
| buildCast[CalendarInterval](_, i => UTF8String.fromString(toMultiUnitsString(i))) | ||
| } | ||
| case BinaryType => buildCast[Array[Byte]](_, UTF8String.fromBytes) | ||
| case DateType => buildCast[Int](_, d => UTF8String.fromString(dateFormatter.format(d))) | ||
|
|
@@ -994,6 +995,16 @@ abstract class CastBase extends UnaryExpression with TimeZoneAwareExpression wit | |
| timestampFormatter.getClass) | ||
| (c, evPrim, evNull) => code"""$evPrim = UTF8String.fromString( | ||
| org.apache.spark.sql.catalyst.util.DateTimeUtils.timestampToString($tf, $c));""" | ||
| case CalendarIntervalType => | ||
| val iu = IntervalUtils.getClass.getCanonicalName.stripSuffix("$") | ||
| SQLConf.get.intervalOutputStyle match { | ||
|
||
| case SQL_STANDARD => | ||
| (c, evPrim, _) => code"""$evPrim = UTF8String.fromString($iu.toSqlStandardString($c));""" | ||
| case ISO_8601 => | ||
| (c, evPrim, _) => code"""$evPrim = UTF8String.fromString($iu.toIso8601String($c));""" | ||
| case _ => | ||
|
||
| (c, evPrim, _) => code"""$evPrim = UTF8String.fromString($iu.toMultiUnitsString($c));""" | ||
| } | ||
| case ArrayType(et, _) => | ||
| (c, evPrim, evNull) => { | ||
| val buffer = ctx.freshVariable("buffer", classOf[UTF8StringBuilder]) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -409,6 +409,7 @@ case class Literal (value: Any, dataType: DataType) extends LeafExpression { | |
| DateTimeUtils.getZoneId(SQLConf.get.sessionLocalTimeZone)) | ||
| s"TIMESTAMP('${formatter.format(v)}')" | ||
| case (v: Array[Byte], BinaryType) => s"X'${DatatypeConverter.printHexBinary(v)}'" | ||
| case (v: CalendarInterval, CalendarIntervalType) => IntervalUtils.toMultiUnitsString(v) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry if this is already asked above but why we didn't change this?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have not supported to parse interval from iso/SQL standard format yet
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did we not support iso/SQL standard format here together?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| case _ => value.toString | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,8 @@ import java.nio.charset.StandardCharsets | |
| import java.sql.{Date, Timestamp} | ||
|
|
||
| import org.apache.spark.sql.Row | ||
| import org.apache.spark.sql.catalyst.util.{DateFormatter, DateTimeUtils, IntervalUtils, TimestampFormatter} | ||
| import org.apache.spark.sql.catalyst.util.{DateFormatter, DateTimeUtils, TimestampFormatter} | ||
| import org.apache.spark.sql.catalyst.util.IntervalUtils._ | ||
| import org.apache.spark.sql.execution.command.{DescribeCommandBase, ExecutedCommandExec, ShowTablesCommand} | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.internal.SQLConf.IntervalStyle._ | ||
|
|
@@ -101,9 +102,9 @@ object HiveResult { | |
| case (decimal, DecimalType()) => decimal.toString | ||
| case (interval: CalendarInterval, CalendarIntervalType) => | ||
| SQLConf.get.intervalOutputStyle match { | ||
| case SQL_STANDARD => IntervalUtils.toSqlStandardString(interval) | ||
| case ISO_8601 => IntervalUtils.toIso8601String(interval) | ||
| case _ => interval.toString | ||
| case SQL_STANDARD => toSqlStandardString(interval) | ||
| case ISO_8601 => toIso8601String(interval) | ||
| case _ => toMultiUnitsString(interval) | ||
|
||
| } | ||
| case (other, tpe) if primitiveTypes contains tpe => other.toString | ||
| } | ||
|
|
@@ -129,9 +130,9 @@ object HiveResult { | |
| case (decimal: java.math.BigDecimal, DecimalType()) => formatDecimal(decimal) | ||
| case (interval: CalendarInterval, CalendarIntervalType) => | ||
| SQLConf.get.intervalOutputStyle match { | ||
| case SQL_STANDARD => IntervalUtils.toSqlStandardString(interval) | ||
| case ISO_8601 => IntervalUtils.toIso8601String(interval) | ||
| case _ => interval.toString | ||
| case SQL_STANDARD => toSqlStandardString(interval) | ||
| case ISO_8601 => toIso8601String(interval) | ||
| case _ => toMultiUnitsString(interval) | ||
| } | ||
| case (interval, CalendarIntervalType) => interval.toString | ||
| case (other, _ : UserDefinedType[_]) => other.toString | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: it's recommended to specify the last enum instead of using a wildcard.