Skip to content

Commit

Permalink
sql: do not force FP representation for NUMBER field
Browse files Browse the repository at this point in the history
During value decoding fetched from space's field FP representation was
forced in case type of field was NUMBER. It was so since NUMBER used to
substitute DOUBLE field type (in fact NUMBER mimicked DOUBLE type). Since
now DOUBLE is a separate field type, there's no such necessity. Hence from
now integers from NUMBER field are treated as integers.

Implemented by Mergen Imeev <imeevma@gmail.com>

Closes #4233

@TarantoolBot document
Title: NUMBER column type changes

From now NUMBER behaves in the same way as in NoSQL Tarantool.
Previously, NUMBER was rather synonym to what now DOUBLE means: it used
to force floating point representation of values, even if they were
integers. A few examples:

1) CAST operation:

Obsolete behaviour:
SELECT CAST(922337206854774800 AS NUMBER), CAST(5 AS NUMBER) / 10;
---
 rows:
- [922337206854774784, 0.5]

New behaviour:
SELECT CAST(922337206854774800 AS NUMBER), CAST(5 AS NUMBER) / 10;
---
 rows:
- [922337206854774800, 0]

Obsolete behaviour:
SELECT CAST(true AS NUMBER);
---
- null
- 'Type mismatch: can not convert TRUE to number'
...

New behaviour:
SELECT CAST(true AS NUMBER);
---
 rows:
- [1]
...

CAST boolean to NUMBER is allowed since it is allowed to convert
booleans to integers; in turn NUMBER comprises integer type.

2) Preserving integer representation:

Obsolete behaviour:
CREATE TABLE t (n NUMBER PRIMARY KEY);
INSERT INTO t VALUES (3), (-4), (5.0);
SELECT n, n/10 FROM t;
---
 rows:
- [-4, -0.4]
- [3, 0.3]
- [5, 0.5]

New behaviour:
SELECT n, n/10 FROM t;
---
 rows:
- [-4, 0]
- [3, 0]
- [5, 0.5]
  • Loading branch information
Korablev77 committed Feb 12, 2020
1 parent 7a1e01f commit 0dd8be7
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 22 deletions.
8 changes: 0 additions & 8 deletions src/box/sql/vdbe.c
Expand Up @@ -2721,14 +2721,6 @@ case OP_Column: {
default_val_mem != NULL) {
sqlVdbeMemShallowCopy(pDest, default_val_mem, MEM_Static);
}
if ((pDest->flags & (MEM_Int | MEM_UInt)) != 0) {
if (field_type == FIELD_TYPE_NUMBER) {
if ((pDest->flags & MEM_Int) != 0)
sqlVdbeMemSetDouble(pDest, pDest->u.i);
else
sqlVdbeMemSetDouble(pDest, pDest->u.u);
}
}
pDest->field_type = field_type;
op_column_out:
REGISTER_TRACE(p, pOp->p3, pDest);
Expand Down
62 changes: 61 additions & 1 deletion test/sql-tap/numcast.test.lua
@@ -1,6 +1,6 @@
#!/usr/bin/env tarantool
test = require("sqltester")
test:plan(25)
test:plan(31)

--!./tcltestrunner.lua
-- 2013 March 20
Expand Down Expand Up @@ -191,4 +191,64 @@ test:do_execsql_test(
10, 10
})

test:do_execsql_test(
"numcast-3.6",
[[
CREATE TABLE t1 (id INT PRIMARY KEY, n NUMBER);
INSERT INTO t1 VALUES (1, 9223372036854775807);
INSERT INTO t1 VALUES (2, -9223372036854775807);
INSERT INTO t1 VALUES (3, 9007199254740992.0);
SELECT n, n/100 FROM t1;
]], {
9223372036854775807ULL, 92233720368547758ULL,
-9223372036854775807LL, -92233720368547758LL,
9007199254740992, 90071992547409.92
})

test:do_execsql_test(
"numcast-3.7",
[[
CREATE TABLE t2(a NUMBER primary key);
INSERT INTO t2 VALUES(-56);
INSERT INTO t2 VALUES(44.0);
INSERT INTO t2 VALUES(46);
INSERT INTO t2 VALUES(56.0);
SELECT (a + 25) / 50 FROM t2;
]], {
0,1.38,1,1.62
})


test:do_execsql_test(
"numcast-3.8",
[[
SELECT (1 + 0) / 3, (1 + 0.) / 3, (1 + 0) / 3.;
]], {
0, 0.33333333333333, 0.33333333333333
})

test:do_execsql_test(
"numcast-3.9",
[[
SELECT (1 - 0) / 3, (1 - 0.) / 3, (1 - 0) / 3.;
]], {
0, 0.33333333333333, 0.33333333333333
})

test:do_execsql_test(
"numcast-3.10",
[[
SELECT (1 * 1) / 3, (1 * 1.) / 3, (1 * 1) / 3.;
]], {
0, 0.33333333333333, 0.33333333333333
})

test:do_execsql_test(
"numcast-3.11",
[[
SELECT (1 / 1) / 3, (1 / 1.) / 3, (1 / 1) / 3.;
]], {
0 , 0.33333333333333, 0.33333333333333
})

test:finish_test()
12 changes: 6 additions & 6 deletions test/sql-tap/sort.test.lua
Expand Up @@ -243,7 +243,7 @@ test:do_execsql_test(
SELECT v FROM t1 ORDER BY v;
]], {
-- <sort-2.1.1>
"x-123.0", "x-2.15", "x-2b", "x-3.141592653", "x-4221.0", "x0.0013442", "x1.6", "x11.0"
"x-123.0", "x-2.15", "x-2b", "x-3.141592653", "x-4221.0", "x0.0013442", "x1.6", "x11"
-- </sort-2.1.1>
})

Expand All @@ -253,7 +253,7 @@ test:do_execsql_test(
SELECT v FROM t1 ORDER BY substr(v,2,999);
]], {
-- <sort-2.1.2>
"x-123.0", "x-2.15", "x-2b", "x-3.141592653", "x-4221.0", "x0.0013442", "x1.6", "x11.0"
"x-123.0", "x-2.15", "x-2b", "x-3.141592653", "x-4221.0", "x0.0013442", "x1.6", "x11"
-- </sort-2.1.2>
})

Expand All @@ -263,7 +263,7 @@ test:do_execsql_test(
SELECT v FROM t1 ORDER BY substr(v,2,999) DESC;
]], {
-- <sort-2.1.4>
"x11.0", "x1.6", "x0.0013442", "x-4221.0", "x-3.141592653", "x-2b", "x-2.15", "x-123.0"
"x11", "x1.6", "x0.0013442", "x-4221.0", "x-3.141592653", "x-2b", "x-2.15", "x-123.0"
-- </sort-2.1.4>
})

Expand Down Expand Up @@ -381,7 +381,7 @@ test:do_execsql_test(
SELECT v FROM t1 ORDER BY 1;
]], {
-- <sort-4.6>
"x-123.0", "x-2.15", "x-2b", "x-3.141592653", "x-4.0e9", "x-4221.0", "x0.0013442", "x01234567890123456789", "x1.6", "x11.0", "x2.7", "x5.0e10"
"x-123.0", "x-2.15", "x-2b", "x-3.141592653", "x-4.0e9", "x-4221.0", "x0.0013442", "x01234567890123456789", "x1.6", "x11", "x2.7", "x5.0e10"
-- </sort-4.6>
})

Expand All @@ -391,7 +391,7 @@ test:do_execsql_test(
SELECT v FROM t1 ORDER BY 1 DESC;
]], {
-- <sort-4.7>
"x5.0e10", "x2.7", "x11.0", "x1.6", "x01234567890123456789", "x0.0013442", "x-4221.0", "x-4.0e9", "x-3.141592653", "x-2b", "x-2.15", "x-123.0"
"x5.0e10", "x2.7", "x11", "x1.6", "x01234567890123456789", "x0.0013442", "x-4221.0", "x-4.0e9", "x-3.141592653", "x-2b", "x-2.15", "x-123.0"
-- </sort-4.7>
})

Expand All @@ -401,7 +401,7 @@ test:do_execsql_test(
SELECT substr(v,2,99) FROM t1 ORDER BY 1;
]], {
-- <sort-4.8>
"-123.0","-2.15","-2b","-3.141592653","-4.0e9","-4221.0","0.0013442","01234567890123456789","1.6","11.0","2.7","5.0e10"
"-123.0","-2.15","-2b","-3.141592653","-4.0e9","-4221.0","0.0013442","01234567890123456789","1.6","11","2.7","5.0e10"
-- </sort-4.8>
})

Expand Down
12 changes: 6 additions & 6 deletions test/sql-tap/tkt-91e2e8ba6f.test.lua
Expand Up @@ -35,7 +35,7 @@ test:do_execsql_test(
SELECT x/10, y/10 FROM t1;
]], {
-- <1.2>
1, 1.1
1, 1
-- </1.2>
})

Expand All @@ -45,7 +45,7 @@ test:do_execsql_test(
SELECT x/10, y/10 FROM (SELECT * FROM t1);
]], {
-- <1.3>
1, 1.1
1, 1
-- </1.3>
})

Expand All @@ -55,7 +55,7 @@ test:do_execsql_test(
SELECT x/10, y/10 FROM (SELECT * FROM t1 LIMIT 5 OFFSET 0);
]], {
-- <1.4>
1, 1.1
1, 1
-- </1.4>
})

Expand All @@ -65,7 +65,7 @@ test:do_execsql_test(
SELECT x/10, y/10 FROM (SELECT * FROM t1 LIMIT 5 OFFSET 0) LIMIT 5 OFFSET 0;
]], {
-- <1.5>
1, 1.1
1, 1
-- </1.5>
})

Expand All @@ -77,7 +77,7 @@ test:do_execsql_test(
LIMIT 5 OFFSET 0;
]], {
-- <1.6>
1, 1.1
1, 1
-- </1.6>
})

Expand All @@ -92,7 +92,7 @@ test:do_execsql_test(
SELECT a.x/10, a.y/10 FROM v1 AS a, t1 AS b WHERE a.x = b.x LIMIT 5 OFFSET 0;
]], {
-- <1.7>
1, 1.1
1, 1
-- </1.7>
})

Expand Down
2 changes: 1 addition & 1 deletion test/sql/integer-overflow.result
Expand Up @@ -170,7 +170,7 @@ box.execute("SELECT * FROM t;")
- name: A
type: number
rows:
- [1, 1.844674407371e+19]
- [1, 18446744073709551615]
- [2, -1]
...
box.space.T:drop()
Expand Down

0 comments on commit 0dd8be7

Please sign in to comment.