You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by do...@apache.org on 2019/07/24 02:41:19 UTC

[spark] branch master updated: [SPARK-28435][SQL] Support accepting the interval keyword in the schema string

This is an automated email from the ASF dual-hosted git repository.

dongjoon pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/master by this push:
     new d67b98e  [SPARK-28435][SQL] Support accepting the interval keyword in the schema string
d67b98e is described below

commit d67b98ea016e9b714bef68feaac108edd08159c9
Author: Yuming Wang <yu...@ebay.com>
AuthorDate: Tue Jul 23 19:40:57 2019 -0700

    [SPARK-28435][SQL] Support accepting the interval keyword in the schema string
    
    ## What changes were proposed in this pull request?
    
    https://github.com/apache/spark/pull/7355 add support casting between IntervalType and StringType for scala interface:
    ```scala
    import org.apache.spark.sql.types._
    import org.apache.spark.sql.catalyst.expressions._
    
    Cast(Literal("interval 3 month 1 hours"), CalendarIntervalType).eval()
    res0: Any = interval 3 months 1 hours
    ```
    But SQL interface does not support it:
    ```sql
    scala> spark.sql("SELECT CAST('interval 3 month 1 hour' AS interval)").show
    org.apache.spark.sql.catalyst.parser.ParseException:
    DataType interval is not supported.(line 1, pos 41)
    
    == SQL ==
    SELECT CAST('interval 3 month 1 hour' AS interval)
    -----------------------------------------^^^
    
      at org.apache.spark.sql.catalyst.parser.AstBuilder.$anonfun$visitPrimitiveDataType$1(AstBuilder.scala:1931)
      at org.apache.spark.sql.catalyst.parser.ParserUtils$.withOrigin(ParserUtils.scala:108)
      at org.apache.spark.sql.catalyst.parser.AstBuilder.visitPrimitiveDataType(AstBuilder.scala:1909)
      at org.apache.spark.sql.catalyst.parser.AstBuilder.visitPrimitiveDataType(AstBuilder.scala:52)
    ...
    ```
    
    This PR add supports accepting the `interval` keyword in the schema string. So that SQL interface can support this feature.
    
    ## How was this patch tested?
    
    unit tests
    
    Closes #25189 from wangyum/SPARK-28435.
    
    Authored-by: Yuming Wang <yu...@ebay.com>
    Signed-off-by: Dongjoon Hyun <dh...@apple.com>
---
 .../apache/spark/sql/catalyst/parser/AstBuilder.scala  |  1 +
 .../sql/catalyst/parser/DataTypeParserSuite.scala      |  1 +
 .../org/apache/spark/sql/types/StructTypeSuite.scala   |  5 +++++
 sql/core/src/test/resources/sql-tests/inputs/cast.sql  |  4 ++++
 .../src/test/resources/sql-tests/results/cast.sql.out  | 18 +++++++++++++++++-
 5 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala
index e599128..a7a3b96 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala
@@ -1926,6 +1926,7 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
       case ("decimal", precision :: Nil) => DecimalType(precision.getText.toInt, 0)
       case ("decimal", precision :: scale :: Nil) =>
         DecimalType(precision.getText.toInt, scale.getText.toInt)
+      case ("interval", Nil) => CalendarIntervalType
       case (dt, params) =>
         val dtStr = if (params.nonEmpty) s"$dt(${params.mkString(",")})" else dt
         throw new ParseException(s"DataType $dtStr is not supported.", ctx)
diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/DataTypeParserSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/DataTypeParserSuite.scala
index 2c491cd..1a62860 100644
--- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/DataTypeParserSuite.scala
+++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/DataTypeParserSuite.scala
@@ -58,6 +58,7 @@ class DataTypeParserSuite extends SparkFunSuite {
   checkDataType("varchAr(20)", StringType)
   checkDataType("cHaR(27)", StringType)
   checkDataType("BINARY", BinaryType)
+  checkDataType("interval", CalendarIntervalType)
 
   checkDataType("array<doublE>", ArrayType(DoubleType, true))
   checkDataType("Array<map<int, tinYint>>", ArrayType(MapType(IntegerType, ByteType, true), true))
diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/types/StructTypeSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/types/StructTypeSuite.scala
index c493088..e1fe983 100644
--- a/sql/catalyst/src/test/scala/org/apache/spark/sql/types/StructTypeSuite.scala
+++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/types/StructTypeSuite.scala
@@ -82,4 +82,9 @@ class StructTypeSuite extends SparkFunSuite {
     assert(7 == schema.treeString(0).split("\n").length)
     assert(7 == schema.treeString(-1).split("\n").length)
   }
+
+  test("interval keyword in schema string") {
+    val interval = "`a` INTERVAL"
+    assert(fromDDL(interval).toDDL === interval)
+  }
 }
diff --git a/sql/core/src/test/resources/sql-tests/inputs/cast.sql b/sql/core/src/test/resources/sql-tests/inputs/cast.sql
index 7244cd3..8a035f5 100644
--- a/sql/core/src/test/resources/sql-tests/inputs/cast.sql
+++ b/sql/core/src/test/resources/sql-tests/inputs/cast.sql
@@ -56,3 +56,7 @@ SELECT HEX(CAST(-123L AS binary));
 DESC FUNCTION boolean;
 DESC FUNCTION EXTENDED boolean;
 -- TODO: migrate all cast tests here.
+
+-- cast string to interval and interval to string
+SELECT CAST('interval 3 month 1 hour' AS interval);
+SELECT CAST(interval 3 month 1 hour AS string);
diff --git a/sql/core/src/test/resources/sql-tests/results/cast.sql.out b/sql/core/src/test/resources/sql-tests/results/cast.sql.out
index 21b18e9..adad21f 100644
--- a/sql/core/src/test/resources/sql-tests/results/cast.sql.out
+++ b/sql/core/src/test/resources/sql-tests/results/cast.sql.out
@@ -1,5 +1,5 @@
 -- Automatically generated by SQLQueryTestSuite
--- Number of queries: 33
+-- Number of queries: 35
 
 
 -- !query 0
@@ -271,3 +271,19 @@ Extended Usage:
 
 Function: boolean
 Usage: boolean(expr) - Casts the value `expr` to the target data type `boolean`.
+
+
+-- !query 33
+SELECT CAST('interval 3 month 1 hour' AS interval)
+-- !query 33 schema
+struct<CAST(interval 3 month 1 hour AS INTERVAL):interval>
+-- !query 33 output
+interval 3 months 1 hours
+
+
+-- !query 34
+SELECT CAST(interval 3 month 1 hour AS string)
+-- !query 34 schema
+struct<CAST(interval 3 months 1 hours AS STRING):string>
+-- !query 34 output
+interval 3 months 1 hours


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org