diff --git a/changelogs/unreleased/gh-7295-unit-arith-res-type.md b/changelogs/unreleased/gh-7295-unit-arith-res-type.md new file mode 100644 index 0000000000000000000000000000000000000000..90489f874e8c3904a317e25fc0b07f4a81718d96 --- /dev/null +++ b/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). diff --git a/src/box/sql/expr.c b/src/box/sql/expr.c index e7518c57b62e0a12cc1e16bcc9ee1b85d70de06a..c97cdd724ead1743ce7273be39464cb25585eea5 100644 --- a/src/box/sql/expr.c +++ b/src/box/sql/expr.c @@ -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)) diff --git a/test/sql-luatest/gh_7295_uint_and_int_def_err_test.lua b/test/sql-luatest/gh_7295_uint_and_int_def_err_test.lua new file mode 100644 index 0000000000000000000000000000000000000000..8998c26ce580280ef487a02d9fa44497b5802862 --- /dev/null +++ b/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