Skip to content

Commit

Permalink
sql: fix typeof() for double values
Browse files Browse the repository at this point in the history
This patch corrects the result of typeof() for double values.
Previously, it gave the type "number" in the case of a
floating-point number. Now it gives "double".

Follow-up #3812
  • Loading branch information
ImeevMA authored and Korablev77 committed Jan 13, 2020
1 parent 99eb661 commit 2bc4fe6
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/box/sql/func.c
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ typeofFunc(sql_context * context, int NotUsed, sql_value ** argv)
z = "string";
break;
case MP_DOUBLE:
z = "number";
z = "double";
break;
case MP_BIN:
z = "varbinary";
Expand Down
12 changes: 6 additions & 6 deletions test/sql-tap/cast.test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,10 @@ test:do_execsql_test(
test:do_execsql_test(
"cast-1.26",
[[
SELECT typeof(CAST(123 AS NUMBER))
SELECT typeof(CAST(123 AS DOUBLE))
]], {
-- <cast-1.26>
"number"
"double"
-- </cast-1.26>
})

Expand Down Expand Up @@ -320,7 +320,7 @@ test:do_execsql_test(
SELECT typeof(123.456)
]], {
-- <cast-1.32>
"number"
"double"
-- </cast-1.32>
})

Expand Down Expand Up @@ -357,10 +357,10 @@ test:do_execsql_test(
test:do_execsql_test(
"cast-1.36",
[[
SELECT typeof(CAST(123.456 AS NUMBER))
SELECT typeof(CAST(123.456 AS DOUBLE))
]], {
-- <cast-1.36>
"number"
"double"
-- </cast-1.36>
})

Expand All @@ -380,7 +380,7 @@ test:do_execsql_test(
SELECT typeof(CAST(123.456 AS SCALAR))
]], {
-- <cast-1.38>
"number"
"double"
-- </cast-1.38>
})

Expand Down
4 changes: 2 additions & 2 deletions test/sql-tap/check.test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ test:do_execsql_test(
[[
CREATE TABLE t1(
x INTEGER CHECK( x<5 ),
y NUMBER CHECK( y>x ),
y DOUBLE CHECK( y>x ),
z INT primary key
);
]], {
Expand Down Expand Up @@ -207,7 +207,7 @@ test:do_execsql_test(
CREATE TABLE t2(
id INT primary key,
x SCALAR CONSTRAINT one CHECK( typeof(coalesce(x,0))=='integer'),
y NUMBER CONSTRAINT two CHECK( typeof(coalesce(y,0.1))=='number' ),
y DOUBLE CONSTRAINT two CHECK( typeof(coalesce(y,0.1))=='double' ),
z SCALAR CONSTRAINT three CHECK( typeof(coalesce(z,''))=='string' )
);
]], {
Expand Down
4 changes: 2 additions & 2 deletions test/sql-tap/func.test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ test:do_execsql_test(
SELECT typeof(round(5.1,1));
]], {
-- <func-4.14>
"number"
"double"
-- </func-4.14>
})

Expand All @@ -528,7 +528,7 @@ test:do_execsql_test(
SELECT typeof(round(5.1));
]], {
-- <func-4.15>
"number"
"double"
-- </func-4.15>
})

Expand Down
8 changes: 4 additions & 4 deletions test/sql-tap/select3.test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -376,25 +376,25 @@ test:do_execsql_test("select3-7.2", [[
test:do_execsql_test("select3-8.1", [[
DROP TABLE IF EXISTS A;
CREATE TABLE A (
A1 NUMBER,
A1 DOUBLE,
A2 TEXT,
A3 NUMBER,
A3 DOUBLE,
id int primary key
);
INSERT INTO A VALUES(39136,'ABC',1201900000, 1);
INSERT INTO A VALUES(39136,'ABC',1207000000, 2);
SELECT typeof(sum(a3)) FROM a;
]], {
-- <select3-8.1>
"number"
"double"
-- </select3-8.1>
})

test:do_execsql_test("select3-8.2", [[
SELECT typeof(sum(a3)) FROM a GROUP BY a1;
]], {
-- <select3-8.2>
"number"
"double"
-- </select3-8.2>
})

Expand Down
51 changes: 51 additions & 0 deletions test/sql/types.result
Original file line number Diff line number Diff line change
Expand Up @@ -2082,3 +2082,54 @@ box.execute("DROP TABLE t4;")
---
- row_count: 1
...
-- Make sure the typeof() function works correctly with DOUBLE.
box.execute("SELECT 1.0, typeof(1.0);")
---
- metadata:
- name: '1.0'
type: double
- name: typeof(1.0)
type: string
rows:
- [1, 'double']
...
box.execute("SELECT CAST(2 AS DOUBLE), typeof(CAST(2 AS DOUBLE));")
---
- metadata:
- name: CAST(2 AS DOUBLE)
type: double
- name: typeof(CAST(2 AS DOUBLE))
type: string
rows:
- [2, 'double']
...
box.execute("SELECT 3e3, typeof(3e3);")
---
- metadata:
- name: '3e3'
type: double
- name: typeof(3e3)
type: string
rows:
- [3000, 'double']
...
box.execute("CREATE TABLE t5 (d DOUBLE PRIMARY KEY);")
---
- row_count: 1
...
box.execute("INSERT INTO t5 VALUES (4), (5.5), (6e6);")
---
- row_count: 3
...
box.execute("SELECT d, TYPEOF(d) FROM t5;")
---
- metadata:
- name: D
type: double
- name: TYPEOF(d)
type: string
rows:
- [4, 'double']
- [5.5, 'double']
- [6000000, 'double']
...
9 changes: 9 additions & 0 deletions test/sql/types.test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -465,3 +465,12 @@ box.execute("CREATE TABLE t4 (i INT PRIMARY KEY, d DOUBLE DEFAULT 1.2345);")
box.execute("INSERT INTO t4(i) VALUES (1);")
box.execute("SELECT * FROM t4;")
box.execute("DROP TABLE t4;")

-- Make sure the typeof() function works correctly with DOUBLE.
box.execute("SELECT 1.0, typeof(1.0);")
box.execute("SELECT CAST(2 AS DOUBLE), typeof(CAST(2 AS DOUBLE));")
box.execute("SELECT 3e3, typeof(3e3);")

box.execute("CREATE TABLE t5 (d DOUBLE PRIMARY KEY);")
box.execute("INSERT INTO t5 VALUES (4), (5.5), (6e6);")
box.execute("SELECT d, TYPEOF(d) FROM t5;")

0 comments on commit 2bc4fe6

Please sign in to comment.