Skip to content

Commit

Permalink
sql: ariphmetic of unsigned values
Browse files Browse the repository at this point in the history
After this patch, the result type of arithmetic between two unsigned
values will be INTEGER.

Closes tarantool#7295

NO_DOC=bugfix
  • Loading branch information
ImeevMA committed Jul 4, 2022
1 parent 28426f6 commit 0c21525
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
4 changes: 4 additions & 0 deletions changelogs/unreleased/gh-7295-unit-arith-res-type.md
@@ -0,0 +1,4 @@
## bugfix/sql

* The result type of arithmetic between two unsigned values is now
INTEGER (gh-7295).
7 changes: 3 additions & 4 deletions src/box/sql/expr.c
Expand Up @@ -413,11 +413,10 @@ sql_type_result(enum field_type lhs, enum field_type rhs)
return FIELD_TYPE_DOUBLE;
if (lhs == FIELD_TYPE_DECIMAL || rhs == FIELD_TYPE_DECIMAL)
return FIELD_TYPE_DECIMAL;
if (lhs == FIELD_TYPE_INTEGER || rhs == FIELD_TYPE_INTEGER)
return FIELD_TYPE_INTEGER;
assert(lhs == FIELD_TYPE_UNSIGNED ||
rhs == FIELD_TYPE_UNSIGNED);
return FIELD_TYPE_UNSIGNED;
lhs == FIELD_TYPE_INTEGER ||
rhs == FIELD_TYPE_UNSIGNED || rhs == FIELD_TYPE_INTEGER);
return FIELD_TYPE_INTEGER;
}
if ((lhs == FIELD_TYPE_DATETIME && rhs == FIELD_TYPE_DATETIME) ||
(lhs == FIELD_TYPE_INTERVAL && rhs == FIELD_TYPE_INTERVAL))
Expand Down
43 changes: 43 additions & 0 deletions test/sql-luatest/gh_7295_uint_and_int_def_err_test.lua
@@ -0,0 +1,43 @@
local server = require('test.luatest_helpers.server')
local t = require('luatest')
local g = t.group()

g.before_all(function()
g.server = server:new({alias = 'test_uint_int'})
g.server:start()
g.server:exec(function()
box.execute([[CREATE TABLE t(i UNSIGNED PRIMARY KEY, a UNSIGNED);]])
box.execute([[INSERT INTO t VALUES (1, 2);]])
end)
end)

g.after_all(function()
g.server:exec(function()
box.execute([[DROP TABLE t;]])
end)
g.server:stop()
end)

g.test_uint_int_1 = function()
g.server:exec(function()
local t = require('luatest')
local res = {{name = "COLUMN_1", type = "integer"}}
t.assert_equals(box.execute([[SELECT i + a FROM t;]]).metadata, res)
t.assert_equals(box.execute([[SELECT i - a FROM t;]]).metadata, res)
t.assert_equals(box.execute([[SELECT i * a FROM t;]]).metadata, res)
t.assert_equals(box.execute([[SELECT i / a FROM t;]]).metadata, res)
t.assert_equals(box.execute([[SELECT i % a FROM t;]]).metadata, res)
t.assert_equals(box.execute([[SELECT i & a FROM t;]]).metadata, res)
t.assert_equals(box.execute([[SELECT i | a FROM t;]]).metadata, res)
t.assert_equals(box.execute([[SELECT i >> a FROM t;]]).metadata, res)
t.assert_equals(box.execute([[SELECT i << a FROM t;]]).metadata, res)
end)
end

g.test_uint_int_2 = function()
g.server:exec(function()
local t = require('luatest')
local sql = [[SELECT i - a FROM t ORDER BY 1 LIMIT 1;]]
t.assert_equals(box.execute(sql).rows, {{-1}})
end)
end

0 comments on commit 0c21525

Please sign in to comment.