Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
9546a0f
[SPARK-35111][SQL] Support Cast string to year-month interval
AngersZhuuuu Apr 21, 2021
691c1f4
Update CastSuite.scala
AngersZhuuuu Apr 21, 2021
f5b02ee
Update CastSuite.scala
AngersZhuuuu Apr 22, 2021
15424a7
Update IntervalUtils.scala
AngersZhuuuu Apr 22, 2021
879817b
Update CastSuite.scala
AngersZhuuuu Apr 22, 2021
62d175b
Update CastSuite.scala
AngersZhuuuu Apr 22, 2021
2c75bba
save
AngersZhuuuu Apr 22, 2021
5b134fa
Merge branch 'master' into SPARK-SPARK-35111
AngersZhuuuu Apr 22, 2021
6d14414
Update CastSuite.scala
AngersZhuuuu Apr 22, 2021
d19bbc8
Update Cast.scala
AngersZhuuuu Apr 23, 2021
ff904a1
save
AngersZhuuuu Apr 25, 2021
d0e30e4
Merge branch 'master' into SPARK-SPARK-35111
AngersZhuuuu Apr 25, 2021
3b84baa
follow comment
AngersZhuuuu Apr 25, 2021
b05f7e6
update
AngersZhuuuu Apr 28, 2021
25c08e0
Update IntervalUtils.scala
AngersZhuuuu Apr 28, 2021
f636d41
update
AngersZhuuuu Apr 28, 2021
ce69004
Update CastSuite.scala
AngersZhuuuu Apr 28, 2021
092d01a
Update IntervalUtils.scala
AngersZhuuuu Apr 28, 2021
f088f64
Update IntervalUtils.scala
AngersZhuuuu Apr 28, 2021
80499b8
Update IntervalUtils.scala
AngersZhuuuu Apr 29, 2021
ca19c09
Update IntervalUtils.scala
AngersZhuuuu Apr 29, 2021
3df92b6
Update Cast.scala
AngersZhuuuu Apr 29, 2021
3adde87
Update IntervalUtils.scala
AngersZhuuuu Apr 29, 2021
0f82987
follow comment
AngersZhuuuu Apr 29, 2021
2c8785b
Update CastSuite.scala
AngersZhuuuu Apr 29, 2021
253c70e
follow comment
AngersZhuuuu Apr 29, 2021
9c70b88
Update IntervalUtils.scala
AngersZhuuuu Apr 29, 2021
5ca83ab
update
AngersZhuuuu Apr 30, 2021
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
Prev Previous commit
Next Next commit
save
  • Loading branch information
AngersZhuuuu committed Apr 25, 2021
commit ff904a1c2ce30910b046317c653497f968bf15fe
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ import java.util.concurrent.TimeUnit

import scala.util.control.NonFatal

import org.apache.spark.sql.catalyst.expressions.Literal
import org.apache.spark.sql.catalyst.parser.CatalystSqlParser
import org.apache.spark.sql.catalyst.util.DateTimeConstants._
import org.apache.spark.sql.catalyst.util.DateTimeUtils.millisToMicros
import org.apache.spark.sql.catalyst.util.IntervalStringStyles.{ANSI_STYLE, HIVE_STYLE, IntervalStyle}
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types.Decimal
import org.apache.spark.sql.types.{CalendarIntervalType, Decimal, YearMonthIntervalType}
import org.apache.spark.unsafe.types.{CalendarInterval, UTF8String}

// The style of textual representation of intervals
Expand Down Expand Up @@ -93,20 +95,11 @@ object IntervalUtils {

private val yearMonthPattern = "^([+|-])?(\\d+)-(\\d+)$".r

private val yearMonthFuzzyPattern = "[^-|+]*?([+|-]?[\\d]+-[\\d]+).*".r

def fromYearMonthString(input: UTF8String): CalendarInterval = {
if (input == null || input.toString == null) {
throw new IllegalArgumentException("Interval year-month string must be not null")
} else {
val intervalString = input.trimAll().toString
intervalString match {
case yearMonthFuzzyPattern(payLoad) =>
fromYearMonthString(payLoad)
case _ =>
throw new IllegalArgumentException(
s"Interval string does not match year-month format of 'y-m': $input")
}
fromYearMonthString(input.trimAll().toString)
}
}

Expand All @@ -128,14 +121,24 @@ object IntervalUtils {
s"Error parsing interval year-month string: ${e.getMessage}", e)
}
}

input.trim match {
case yearMonthPattern("-", yearStr, monthStr) =>
toInterval(yearStr, monthStr, -1)
case yearMonthPattern(_, yearStr, monthStr) =>
toInterval(yearStr, monthStr, 1)
case _ =>
throw new IllegalArgumentException(
s"Interval string does not match year-month format of 'y-m': $input")
try {
CatalystSqlParser.parseExpression(input) match {
case Literal(value: Int, _: YearMonthIntervalType) => new CalendarInterval(value, 0, 0)
case Literal(value: CalendarInterval, _: CalendarIntervalType) => value
case _ => throw new IllegalArgumentException(
s"Interval string does not match year-month format of 'y-m': $input")
}
} catch {
case NonFatal(e) => throw new IllegalArgumentException(
s"Interval string does not match year-month format of 'y-m': $input")
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1785,8 +1785,6 @@ class CastSuite extends CastSuiteBase {
checkEvaluation(cast(Literal.create("INTERVAL '10-1' YEAR TO MONTH"),
YearMonthIntervalType), 121)
checkEvaluation(cast(Literal.create("10-1"), YearMonthIntervalType), 121)
checkEvaluation(cast(Literal.create("abc10-1"), YearMonthIntervalType), 121)
checkEvaluation(cast(Literal.create("abca10-1-da-"), YearMonthIntervalType), 121)
checkEvaluation(cast(Literal.create(null, StringType), YearMonthIntervalType), null)

Seq("INTERVAL '0-0' YEAR TO MONTH" -> "INTERVAL '0-0' YEAR TO MONTH",
Expand Down