Skip to content

Commit 8f0eb7d

Browse files
yaooqinncloud-fan
authored andcommitted
[SPARK-29587][SQL] Support SQL Standard type real as float(4) numeric as decimal
### What changes were proposed in this pull request? The types decimal and numeric are equivalent. Both types are part of the SQL standard. the real type is 4 bytes, variable-precision, inexact, 6 decimal digits precision, same as our float, part of the SQL standard. ### Why are the changes needed? improve sql standard support other dbs https://www.postgresql.org/docs/9.3/datatype-numeric.html https://prestodb.io/docs/current/language/types.html#floating-point http://www.sqlservertutorial.net/sql-server-basics/sql-server-data-types/ MySQL treats REAL as a synonym for DOUBLE PRECISION (a nonstandard variation), unless the REAL_AS_FLOAT SQL mode is enabled. In MySQL, NUMERIC is implemented as DECIMAL, so the following remarks about DECIMAL apply equally to NUMERIC. ### Does this PR introduce any user-facing change? no ### How was this patch tested? add ut Closes #26537 from yaooqinn/SPARK-29587. Authored-by: Kent Yao <[email protected]> Signed-off-by: Wenchen Fan <[email protected]>
1 parent 24c4ce1 commit 8f0eb7d

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2179,17 +2179,18 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
21792179
case ("smallint" | "short", Nil) => ShortType
21802180
case ("int" | "integer", Nil) => IntegerType
21812181
case ("bigint" | "long", Nil) => LongType
2182-
case ("float", Nil) => FloatType
2182+
case ("float" | "real", Nil) => FloatType
21832183
case ("double", Nil) => DoubleType
21842184
case ("date", Nil) => DateType
21852185
case ("timestamp", Nil) => TimestampType
21862186
case ("string", Nil) => StringType
21872187
case ("character" | "char", length :: Nil) => CharType(length.getText.toInt)
21882188
case ("varchar", length :: Nil) => VarcharType(length.getText.toInt)
21892189
case ("binary", Nil) => BinaryType
2190-
case ("decimal" | "dec", Nil) => DecimalType.USER_DEFAULT
2191-
case ("decimal" | "dec", precision :: Nil) => DecimalType(precision.getText.toInt, 0)
2192-
case ("decimal" | "dec", precision :: scale :: Nil) =>
2190+
case ("decimal" | "dec" | "numeric", Nil) => DecimalType.USER_DEFAULT
2191+
case ("decimal" | "dec" | "numeric", precision :: Nil) =>
2192+
DecimalType(precision.getText.toInt, 0)
2193+
case ("decimal" | "dec" | "numeric", precision :: scale :: Nil) =>
21932194
DecimalType(precision.getText.toInt, scale.getText.toInt)
21942195
case ("interval", Nil) => CalendarIntervalType
21952196
case (dt, params) =>

sql/core/src/test/resources/sql-tests/inputs/show-create-table.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,8 @@ TBLPROPERTIES ('a' = '1');
5959

6060
SHOW CREATE TABLE tbl;
6161
DROP TABLE tbl;
62+
63+
-- float alias real and decimal alias numeric
64+
CREATE TABLE tbl (a REAL, b NUMERIC, c NUMERIC(10), d NUMERIC(10,1)) USING parquet;
65+
SHOW CREATE TABLE tbl;
66+
DROP TABLE tbl;

sql/core/src/test/resources/sql-tests/results/show-create-table.sql.out

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
-- Automatically generated by SQLQueryTestSuite
2-
-- Number of queries: 24
2+
-- Number of queries: 27
33

44

55
-- !query 0
@@ -220,3 +220,28 @@ DROP TABLE tbl
220220
struct<>
221221
-- !query 23 output
222222

223+
224+
225+
-- !query 24
226+
CREATE TABLE tbl (a REAL, b NUMERIC, c NUMERIC(10), d NUMERIC(10,1)) USING parquet
227+
-- !query 24 schema
228+
struct<>
229+
-- !query 24 output
230+
231+
232+
233+
-- !query 25
234+
SHOW CREATE TABLE tbl
235+
-- !query 25 schema
236+
struct<createtab_stmt:string>
237+
-- !query 25 output
238+
CREATE TABLE `tbl` (`a` FLOAT, `b` DECIMAL(10,0), `c` DECIMAL(10,0), `d` DECIMAL(10,1))
239+
USING parquet
240+
241+
242+
-- !query 26
243+
DROP TABLE tbl
244+
-- !query 26 schema
245+
struct<>
246+
-- !query 26 output
247+

0 commit comments

Comments
 (0)