From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id AE5836EC40; Thu, 19 Aug 2021 15:04:28 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org AE5836EC40 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1629374668; bh=6P/JOvHQkKcvPYElnezVRFUimOzR0lZ3tpFMm5jWG6U=; h=To:Cc:Date:In-Reply-To:References:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=kcrhVOAwpP85Dp1qKdj6z2BL2LkvKlwQtVuacIVgtbJEVg6v2n64vgsDiLEEX+Hme dH4lQo8+dfBTZybcPeJNr0+5adC013B9FfN3u2DWaRwfpEoIU8ZInj1jLoFTIBncwd LVXh3Qpu43x+fE+hmDKdyzQQl2xy7J5QNGg4cC5w= Received: from smtpng2.i.mail.ru (smtpng2.i.mail.ru [94.100.179.3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id D45786EC40 for ; Thu, 19 Aug 2021 15:03:03 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org D45786EC40 Received: by smtpng2.m.smailru.net with esmtpa (envelope-from ) id 1mGglC-00010n-MF; Thu, 19 Aug 2021 15:03:03 +0300 To: kyukhin@tarantool.org, v.ioffe@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Thu, 19 Aug 2021 15:03:02 +0300 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD92087353F0EC44DD91BCCB18F2C129F87F36E61E9E4584E9D182A05F5380850400B77D73D64EFB180F696B03A28C8F2387CFAB5234668C06D02E51170C3FE1C81 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7B8498AC83B273C12EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637E80306D42697F3998638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8D5881D40978CCC8D9C67680F98EE813D117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCAA867293B0326636D2E47CDBA5A96583BD4B6F7A4D31EC0BC014FD901B82EE079FA2833FD35BB23D27C277FBC8AE2E8BAA867293B0326636D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B6300D3B61E77C8D3B089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975CF160826E4E1956AEEE0A8B6C5CE2B94E7D9BB9E5D57DF97F9C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF836F5ADB0B4F9314699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D3429538671E6527D32AB01492833595C9C11F6BD53BA415C6E8FD87E7CF0E556927E2D5ED8FBECA8431D7E09C32AA3244C98E4AD6C68BC7CB89E8D9D04E172377935DA7DC5AF9B58C0FACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojGSxK+6r6oBHTTtOVc4woPQ== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D24441D9E0C6D206B16BB28797852F0CB83D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v1 3/9] sql: check number of arguments during parsing X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Mergen Imeev via Tarantool-patches Reply-To: imeevma@tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Prior to this patch, the number of arguments for functions with a variable number of arguments was checked at runtime. After this patch, it will be checked during parsing. For functions with a constant number of arguments, it is always checked during parsing. Part of #6105 --- src/box/sql/func.c | 23 +++-- test/sql-tap/built-in-functions.test.lua | 119 +++++++++++++++++++++++ test/sql-tap/engine.cfg | 3 + test/sql-tap/func.test.lua | 8 +- test/sql-tap/func2.test.lua | 18 ++-- test/sql-tap/func5.test.lua | 6 +- test/sql-tap/select1.test.lua | 2 +- test/sql-tap/uuid.test.lua | 2 +- test/sql/collation.result | 2 +- 9 files changed, 158 insertions(+), 25 deletions(-) create mode 100755 test/sql-tap/built-in-functions.test.lua diff --git a/src/box/sql/func.c b/src/box/sql/func.c index 76c8831f4..4d893affc 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -2095,14 +2095,25 @@ find_built_in_func(struct Expr *expr, struct sql_func_dictionary *dict) { const char *name = expr->u.zToken; int n = expr->x.pList != NULL ? expr->x.pList->nExpr : 0; - struct func *func = &dict->functions[0]->base; - assert(func->def->exports.sql); - int param_count = func->def->param_count; - if (param_count != -1 && param_count != n) { - diag_set(ClientError, ER_FUNC_WRONG_ARG_COUNT, name, - tt_sprintf("%d", func->def->param_count), n); + int argc_min = dict->argc_min; + int argc_max = dict->argc_max; + if (n < argc_min || n > argc_max) { + const char *str; + if (argc_min == argc_max) + str = tt_sprintf("%d", argc_min); + else if (argc_max == SQL_MAX_FUNCTION_ARG && n < argc_min) + str = tt_sprintf("at least %d", argc_min); + else + str = tt_sprintf("from %d to %d", argc_min, argc_max); + diag_set(ClientError, ER_FUNC_WRONG_ARG_COUNT, name, str, n); return NULL; } + struct func *func = NULL; + for (uint32_t i = 0; i < dict->count; ++i) { + func = &dict->functions[i]->base; + if (func->def->param_count == n) + break; + } return func; } diff --git a/test/sql-tap/built-in-functions.test.lua b/test/sql-tap/built-in-functions.test.lua new file mode 100755 index 000000000..c704e71a6 --- /dev/null +++ b/test/sql-tap/built-in-functions.test.lua @@ -0,0 +1,119 @@ +#!/usr/bin/env tarantool +local test = require("sqltester") +test:plan(10) + +-- +-- Make sure that number of arguments check is checked properly for SQL built-in +-- functions with variable number of arguments. +-- +test:do_catchsql_test( + "builtins-1.1", + [[ + SELECT COUNT(1, 2); + ]], + { + 1, [[Wrong number of arguments is passed to COUNT(): ]].. + [[expected from 0 to 1, got 2]] + } +) + +test:do_catchsql_test( + "builtins-1.2", + [[ + SELECT GREATEST(); + ]], + { + 1, [[Wrong number of arguments is passed to GREATEST(): ]].. + [[expected at least 2, got 0]] + } +) + +test:do_catchsql_test( + "builtins-1.3", + [[ + SELECT GROUP_CONCAT(); + ]], + { + 1, [[Wrong number of arguments is passed to GROUP_CONCAT(): ]].. + [[expected from 1 to 2, got 0]] + } +) + +test:do_catchsql_test( + "builtins-1.4", + [[ + SELECT GROUP_CONCAT(1, 2, 3); + ]], + { + 1, [[Wrong number of arguments is passed to GROUP_CONCAT(): ]].. + [[expected from 1 to 2, got 3]] + } +) + +test:do_catchsql_test( + "builtins-1.5", + [[ + SELECT LEAST(); + ]], + { + 1, [[Wrong number of arguments is passed to LEAST(): ]].. + [[expected at least 2, got 0]] + } +) + +test:do_catchsql_test( + "builtins-1.6", + [[ + SELECT ROUND(); + ]], + { + 1, [[Wrong number of arguments is passed to ROUND(): ]].. + [[expected from 1 to 2, got 0]] + } +) + +test:do_catchsql_test( + "builtins-1.7", + [[ + SELECT ROUND(1, 2, 3); + ]], + { + 1, [[Wrong number of arguments is passed to ROUND(): ]].. + [[expected from 1 to 2, got 3]] + } +) + +test:do_catchsql_test( + "builtins-1.8", + [[ + SELECT SUBSTR('1'); + ]], + { + 1, [[Wrong number of arguments is passed to SUBSTR(): ]].. + [[expected from 2 to 3, got 1]] + } +) + +test:do_catchsql_test( + "builtins-1.9", + [[ + SELECT SUBSTR('1', '2', '3', '4'); + ]], + { + 1, [[Wrong number of arguments is passed to SUBSTR(): ]].. + [[expected from 2 to 3, got 4]] + } +) + +test:do_catchsql_test( + "builtins-1.10", + [[ + SELECT UUID(1, 2); + ]], + { + 1, [[Wrong number of arguments is passed to UUID(): ]].. + [[expected from 0 to 1, got 2]] + } +) + +test:finish_test() diff --git a/test/sql-tap/engine.cfg b/test/sql-tap/engine.cfg index 2060828bc..35754f769 100644 --- a/test/sql-tap/engine.cfg +++ b/test/sql-tap/engine.cfg @@ -32,6 +32,9 @@ "gh-6239-quote-with-double-arg.test.lua": { "memtx": {"engine": "memtx"} }, + "built-in-functions.test.lua": { + "memtx": {"engine": "memtx"} + }, "gh-4077-iproto-execute-no-bind.test.lua": {}, "*": { "memtx": {"engine": "memtx"}, diff --git a/test/sql-tap/func.test.lua b/test/sql-tap/func.test.lua index 4cc722d1e..637d67a30 100755 --- a/test/sql-tap/func.test.lua +++ b/test/sql-tap/func.test.lua @@ -427,7 +427,7 @@ test:do_catchsql_test( SELECT round(a,b,c) FROM t1 ]], { -- - 1, "Wrong number of arguments is passed to ROUND(): expected 1 or 2, got 3" + 1, "Wrong number of arguments is passed to ROUND(): expected from 1 to 2, got 3" -- }) @@ -487,7 +487,7 @@ test:do_catchsql_test( SELECT round() FROM t1 ORDER BY a ]], { -- - 1, "Wrong number of arguments is passed to ROUND(): expected 1 or 2, got 0" + 1, "Wrong number of arguments is passed to ROUND(): expected from 1 to 2, got 0" -- }) @@ -2553,7 +2553,7 @@ test:do_catchsql_test( SELECT coalesce() ]], { -- - 1, "Wrong number of arguments is passed to COALESCE(): expected at least two, got 0" + 1, "Wrong number of arguments is passed to COALESCE(): expected at least 2, got 0" -- }) @@ -2563,7 +2563,7 @@ test:do_catchsql_test( SELECT coalesce(1) ]], { -- - 1, "Wrong number of arguments is passed to COALESCE(): expected at least two, got 1" + 1, "Wrong number of arguments is passed to COALESCE(): expected at least 2, got 1" -- }) diff --git a/test/sql-tap/func2.test.lua b/test/sql-tap/func2.test.lua index 95b8a1f5f..792f020f1 100755 --- a/test/sql-tap/func2.test.lua +++ b/test/sql-tap/func2.test.lua @@ -50,7 +50,7 @@ test:do_catchsql_test( SELECT SUBSTR() ]], { -- - 1, "Wrong number of arguments is passed to SUBSTR(): expected 1 or 2, got 0" + 1, "Wrong number of arguments is passed to SUBSTR(): expected from 2 to 3, got 0" -- }) @@ -60,7 +60,7 @@ test:do_catchsql_test( SELECT SUBSTR('Supercalifragilisticexpialidocious') ]], { -- - 1, "Wrong number of arguments is passed to SUBSTR(): expected 1 or 2, got 1" + 1, "Wrong number of arguments is passed to SUBSTR(): expected from 2 to 3, got 1" -- }) @@ -70,7 +70,7 @@ test:do_catchsql_test( SELECT SUBSTR('Supercalifragilisticexpialidocious', 1,1,1) ]], { -- - 1, "Wrong number of arguments is passed to SUBSTR(): expected 1 or 2, got 4" + 1, "Wrong number of arguments is passed to SUBSTR(): expected from 2 to 3, got 4" -- }) @@ -673,7 +673,7 @@ if ("ሴ" ~= "u1234") SELECT SUBSTR() ]], { -- - 1, "Wrong number of arguments is passed to SUBSTR(): expected 1 or 2, got 0" + 1, "Wrong number of arguments is passed to SUBSTR(): expected from 2 to 3, got 0" -- }) @@ -683,7 +683,7 @@ if ("ሴ" ~= "u1234") SELECT SUBSTR('hiሴho') ]], { -- - 1, "Wrong number of arguments is passed to SUBSTR(): expected 1 or 2, got 1" + 1, "Wrong number of arguments is passed to SUBSTR(): expected from 2 to 3, got 1" -- }) @@ -693,7 +693,7 @@ if ("ሴ" ~= "u1234") SELECT SUBSTR('hiሴho', 1,1,1) ]], { -- - 1, "Wrong number of arguments is passed to SUBSTR(): expected 1 or 2, got 4" + 1, "Wrong number of arguments is passed to SUBSTR(): expected from 2 to 3, got 4" -- }) @@ -1038,7 +1038,7 @@ test:do_catchsql_test( SELECT SUBSTR() ]], { -- - 1, "Wrong number of arguments is passed to SUBSTR(): expected 1 or 2, got 0" + 1, "Wrong number of arguments is passed to SUBSTR(): expected from 2 to 3, got 0" -- }) @@ -1048,7 +1048,7 @@ test:do_catchsql_test( SELECT SUBSTR(x'1234') ]], { -- - 1, "Wrong number of arguments is passed to SUBSTR(): expected 1 or 2, got 1" + 1, "Wrong number of arguments is passed to SUBSTR(): expected from 2 to 3, got 1" -- }) @@ -1058,7 +1058,7 @@ test:do_catchsql_test( SELECT SUBSTR(x'1234', 1,1,1) ]], { -- - 1, "Wrong number of arguments is passed to SUBSTR(): expected 1 or 2, got 4" + 1, "Wrong number of arguments is passed to SUBSTR(): expected from 2 to 3, got 4" -- }) diff --git a/test/sql-tap/func5.test.lua b/test/sql-tap/func5.test.lua index 44755b1f7..253c59967 100755 --- a/test/sql-tap/func5.test.lua +++ b/test/sql-tap/func5.test.lua @@ -276,19 +276,19 @@ test:do_catchsql_test( "func-5-5.1", [[ SELECT LEAST(false); - ]], { 1, "Wrong number of arguments is passed to LEAST(): expected at least two, got 1" } ) + ]], { 1, "Wrong number of arguments is passed to LEAST(): expected at least 2, got 1" } ) test:do_catchsql_test( "func-5-5.2", [[ SELECT GREATEST('abc'); - ]], { 1, "Wrong number of arguments is passed to GREATEST(): expected at least two, got 1" } ) + ]], { 1, "Wrong number of arguments is passed to GREATEST(): expected at least 2, got 1" } ) test:do_catchsql_test( "func-5-5.3", [[ SELECT LEAST(); - ]], { 1, "Wrong number of arguments is passed to LEAST(): expected at least two, got 0" } ) + ]], { 1, "Wrong number of arguments is passed to LEAST(): expected at least 2, got 0" } ) -- Make sure that ifnull() returns type of corresponding (i.e. first -- non-null) argument. diff --git a/test/sql-tap/select1.test.lua b/test/sql-tap/select1.test.lua index dbc6e193d..ba12470f3 100755 --- a/test/sql-tap/select1.test.lua +++ b/test/sql-tap/select1.test.lua @@ -250,7 +250,7 @@ test:do_catchsql_test( SELECT count(f1,f2) FROM test1 ]], { -- - 1, "Wrong number of arguments is passed to COUNT(): expected 0 or 1, got 2" + 1, "Wrong number of arguments is passed to COUNT(): expected from 0 to 1, got 2" -- }) diff --git a/test/sql-tap/uuid.test.lua b/test/sql-tap/uuid.test.lua index 177798cfa..c22af8e50 100755 --- a/test/sql-tap/uuid.test.lua +++ b/test/sql-tap/uuid.test.lua @@ -1290,7 +1290,7 @@ test:do_catchsql_test( [[ SELECT uuid(4, 5); ]], { - 1, "Wrong number of arguments is passed to UUID(): expected one or zero, got 2" + 1, "Wrong number of arguments is passed to UUID(): expected from 0 to 1, got 2" }) -- Make sure the uuid() function generates a new UUID each time when called. diff --git a/test/sql/collation.result b/test/sql/collation.result index 1fcc212d8..8444cc7ab 100644 --- a/test/sql/collation.result +++ b/test/sql/collation.result @@ -298,7 +298,7 @@ box.execute("SELECT * FROM t WHERE a COLLATE \"binary\" = c COLLATE \"unicode\"; box.execute("SELECT * FROM t WHERE a COLLATE \"binary\" = substr();") --- - null -- 'Wrong number of arguments is passed to SUBSTR(): expected 1 or 2, got 0' +- 'Wrong number of arguments is passed to SUBSTR(): expected from 2 to 3, got 0' ... -- Compound queries perform implicit comparisons between values. -- Hence, rules for collations compatibilities are the same. -- 2.25.1