Skip to content

Commit

Permalink
sql: change rules used to determine NULLIF() type
Browse files Browse the repository at this point in the history
This patch introduces new rules to determine type of NULLIF() built-in
function.

Closes tarantool#6990

@TarantoolBot document
Title: New rules to determine type of result of NULLIF

The type of the result of NULLIF() function now matches the type of the
first argument.
  • Loading branch information
ImeevMA authored and locker committed Oct 11, 2022
1 parent 90f6446 commit 805cbaa
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 5 deletions.
4 changes: 4 additions & 0 deletions changelogs/unreleased/gh-6989-type-of-NULLIF.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## feature/sql

* Now the type of result of NULLIF() is the same as the type of the first
argument (gh-6989).
27 changes: 26 additions & 1 deletion src/box/sql/func.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,12 @@ func_nullif(struct sql_context *ctx, int argc, const struct Mem *argv)
{
assert(argc == 2);
(void)argc;
if (!mem_is_comparable(&argv[1])) {
ctx->is_aborted = true;
diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(&argv[1]),
"scalar");
return;
}
if (mem_cmp_scalar(&argv[0], &argv[1], ctx->coll) == 0)
return;
if (mem_copy(ctx->pOut, &argv[0]) != 0)
Expand Down Expand Up @@ -1954,8 +1960,27 @@ static struct sql_func_definition definitions[] = {
{"MIN", 1, {FIELD_TYPE_SCALAR}, FIELD_TYPE_SCALAR, step_minmax, NULL},
{"NOW", 0, {}, FIELD_TYPE_DATETIME, func_now, NULL},

{"NULLIF", 2, {FIELD_TYPE_SCALAR, FIELD_TYPE_SCALAR}, FIELD_TYPE_SCALAR,
{"NULLIF", 2, {FIELD_TYPE_SCALAR, field_type_MAX}, FIELD_TYPE_SCALAR,
func_nullif, NULL},
{"NULLIF", 2, {FIELD_TYPE_UNSIGNED, field_type_MAX},
FIELD_TYPE_UNSIGNED, func_nullif, NULL},
{"NULLIF", 2, {FIELD_TYPE_STRING, field_type_MAX}, FIELD_TYPE_STRING,
func_nullif, NULL},
{"NULLIF", 2, {FIELD_TYPE_DOUBLE, field_type_MAX}, FIELD_TYPE_DOUBLE,
func_nullif, NULL},
{"NULLIF", 2, {FIELD_TYPE_INTEGER, field_type_MAX},
FIELD_TYPE_INTEGER, func_nullif, NULL},
{"NULLIF", 2, {FIELD_TYPE_BOOLEAN, field_type_MAX},
FIELD_TYPE_BOOLEAN, func_nullif, NULL},
{"NULLIF", 2, {FIELD_TYPE_VARBINARY, field_type_MAX},
FIELD_TYPE_VARBINARY, func_nullif, NULL},
{"NULLIF", 2, {FIELD_TYPE_DECIMAL, field_type_MAX},
FIELD_TYPE_DECIMAL, func_nullif, NULL},
{"NULLIF", 2, {FIELD_TYPE_UUID, field_type_MAX}, FIELD_TYPE_UUID,
func_nullif, NULL},
{"NULLIF", 2, {FIELD_TYPE_DATETIME, field_type_MAX},
FIELD_TYPE_DATETIME, func_nullif, NULL},

{"POSITION", 2, {FIELD_TYPE_STRING, FIELD_TYPE_STRING},
FIELD_TYPE_INTEGER, func_position_characters, NULL},
{"POSITION", 2, {FIELD_TYPE_VARBINARY, FIELD_TYPE_VARBINARY},
Expand Down
36 changes: 36 additions & 0 deletions test/sql-luatest/gh_6989_nullif_result_type_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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_assertion_in_modulo'})
g.server:start()
end)

g.after_all(function()
g.server:stop()
end)

g.test_assertion_in_modulo = function()
g.server:exec(function()
local t = require('luatest')
local sql = "SELECT nullif(1, '2');"
local res = "integer"
t.assert_equals(box.execute(sql).metadata[1].type, res)

sql = "SELECT nullif('1', '1');"
res = "string"
t.assert_equals(box.execute(sql).metadata[1].type, res)

sql = "SELECT nullif([1], '2');"
res = "Failed to execute SQL statement: wrong arguments for function "..
"NULLIF()"
local _, err = box.execute(sql)
t.assert_equals(err.message, res)

sql = "SELECT nullif(1, [2]);"
res = "Type mismatch: can not convert array([2]) to scalar"
_, err = box.execute(sql)
t.assert_equals(err.message, res)
end)
end
3 changes: 1 addition & 2 deletions test/sql-tap/array.test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -827,8 +827,7 @@ test:do_catchsql_test(
[[
SELECT NULLIF(1, a) FROM t;
]], {
1,
"Failed to execute SQL statement: wrong arguments for function NULLIF()"
1, "Type mismatch: can not convert array([123]) to scalar"
})

test:do_catchsql_test(
Expand Down
3 changes: 1 addition & 2 deletions test/sql-tap/map.test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -830,8 +830,7 @@ test:do_catchsql_test(
[[
SELECT NULLIF(1, m) FROM t;
]], {
1,
"Failed to execute SQL statement: wrong arguments for function NULLIF()"
1, [[Type mismatch: can not convert map({"abc": 123}) to scalar]]
})

test:do_catchsql_test(
Expand Down

0 comments on commit 805cbaa

Please sign in to comment.