Skip to content

Commit ae6b711

Browse files
yaooqinncloud-fan
authored andcommitted
[SPARK-29941][SQL] Add ansi type aliases for char and decimal
### What changes were proposed in this pull request? Checked with SQL Standard and PostgreSQL > CHAR is equivalent to CHARACTER. DEC is equivalent to DECIMAL. INT is equivalent to INTEGER. VARCHAR is equivalent to CHARACTER VARYING. ... ```sql postgres=# select dec '1.0'; numeric --------- 1.0 (1 row) postgres=# select CHARACTER '. second'; bpchar ---------- . second (1 row) postgres=# select CHAR '. second'; bpchar ---------- . second (1 row) ``` ### Why are the changes needed? For better ansi support ### Does this PR introduce any user-facing change? yes, we add character as char and dec as decimal ### How was this patch tested? add ut Closes #26574 from yaooqinn/SPARK-29941. Authored-by: Kent Yao <[email protected]> Signed-off-by: Wenchen Fan <[email protected]>
1 parent c32e228 commit ae6b711

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2153,12 +2153,12 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
21532153
case ("date", Nil) => DateType
21542154
case ("timestamp", Nil) => TimestampType
21552155
case ("string", Nil) => StringType
2156-
case ("char", length :: Nil) => CharType(length.getText.toInt)
2156+
case ("character" | "char", length :: Nil) => CharType(length.getText.toInt)
21572157
case ("varchar", length :: Nil) => VarcharType(length.getText.toInt)
21582158
case ("binary", Nil) => BinaryType
2159-
case ("decimal", Nil) => DecimalType.USER_DEFAULT
2160-
case ("decimal", precision :: Nil) => DecimalType(precision.getText.toInt, 0)
2161-
case ("decimal", precision :: scale :: Nil) =>
2159+
case ("decimal" | "dec", Nil) => DecimalType.USER_DEFAULT
2160+
case ("decimal" | "dec", precision :: Nil) => DecimalType(precision.getText.toInt, 0)
2161+
case ("decimal" | "dec", precision :: scale :: Nil) =>
21622162
DecimalType(precision.getText.toInt, scale.getText.toInt)
21632163
case ("interval", Nil) => CalendarIntervalType
21642164
case (dt, params) =>

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/DataTypeParserSuite.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,13 @@ class DataTypeParserSuite extends SparkFunSuite {
5151
checkDataType("dOUBle", DoubleType)
5252
checkDataType("decimal(10, 5)", DecimalType(10, 5))
5353
checkDataType("decimal", DecimalType.USER_DEFAULT)
54+
checkDataType("Dec(10, 5)", DecimalType(10, 5))
55+
checkDataType("deC", DecimalType.USER_DEFAULT)
5456
checkDataType("DATE", DateType)
5557
checkDataType("timestamp", TimestampType)
5658
checkDataType("string", StringType)
5759
checkDataType("ChaR(5)", StringType)
60+
checkDataType("ChaRacter(5)", StringType)
5861
checkDataType("varchAr(20)", StringType)
5962
checkDataType("cHaR(27)", StringType)
6063
checkDataType("BINARY", BinaryType)

0 commit comments

Comments
 (0)