From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <tarantool-patches-bounces@dev.tarantool.org>
Received: from [87.239.111.99] (localhost [127.0.0.1])
	by dev.tarantool.org (Postfix) with ESMTP id 7D2846EC41;
	Fri, 13 Aug 2021 06:18:35 +0300 (MSK)
DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 7D2846EC41
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev;
	t=1628824715; bh=h5o5cPoIG5oEWkanXr4z8D7qKSIDclOqemkODvDVXLM=;
	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=QW6ojux33fclfNzXO3rqLyOlL8yJZFhxpVahK1SuT8ilULdnzhO60lhOws/FDovis
	 8byyhlmci3f/QBEuqLES+3xb4Yd+b+lA/8IT+pRhkIpUTiiA5quy4VyNC845f0Yc4K
	 4HtvLkEaRGYThK+OP0CnU1bnOVNHUJKqb2qHeG0M=
Received: from smtpng1.i.mail.ru (smtpng1.i.mail.ru [94.100.181.251])
 (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 D5C1C6EC42
 for <tarantool-patches@dev.tarantool.org>;
 Fri, 13 Aug 2021 06:17:11 +0300 (MSK)
DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org D5C1C6EC42
Received: by smtpng1.m.smailru.net with esmtpa (envelope-from
 <imeevma@tarantool.org>)
 id 1mENh0-0003CT-RC; Fri, 13 Aug 2021 06:17:11 +0300
To: vdavydov@tarantool.org
Cc: tarantool-patches@dev.tarantool.org
Date: Fri, 13 Aug 2021 06:17:10 +0300
Message-Id: <9d8a797cc8016430c47d70500774ee65fd483f0a.1628824421.git.imeevma@gmail.com>
X-Mailer: git-send-email 2.25.1
In-Reply-To: <cover.1628824421.git.imeevma@gmail.com>
References: <cover.1628824421.git.imeevma@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-7564579A: B8F34718100C35BD
X-77F55803: 4F1203BC0FB41BD92087353F0EC44DD9ECFD080E047A606F6525B29142351271182A05F5380850401B26A64245D952C4B4B64E8E48F30EA47E71073B3392CEE6D9C4FD2A82EAEEAC
X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7466896EF24E80F12EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637ABBE65DF69007335EA1F7E6F0F101C6723150C8DA25C47586E58E00D9D99D84E1BDDB23E98D2D38BBCA57AF85F7723F2CA55644EF3B3EF32E67BE1A6755C9442CC7F00164DA146DAFE8445B8C89999728AA50765F7900637F6B57BC7E64490618DEB871D839B7333395957E7521B51C2DFABB839C843B9C08941B15DA834481F8AA50765F7900637F6B57BC7E6449061A352F6E88A58FB86F5D81C698A659EA7E827F84554CEF5019E625A9149C048EE9ECD01F8117BC8BEE2021AF6380DFAD18AA50765F790063735872C767BF85DA227C277FBC8AE2E8BDC0F6C5B2EEF3D0C75ECD9A6C639B01B4E70A05D1297E1BBCB5012B2E24CD356
X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A213B5FB47DCBC3458834459D11680B5059A91936EF7B6732DE21B4ADDFAA3A125
X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C69415AB31670C86C9F91C87600DD28B9BA7C081E5C2F688C9C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EFE37876E7723AB534DC48ACC2A39D04F89CDFB48F4795C241BDAD6C7F3747799A
X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34F1257DC9690AEBA14103086D44EAD6668A4D4C032EDFF17A8A489A337F2CAEAAF1B38BFF7F37F4E11D7E09C32AA3244C4DAA629DC9DE82DB5374079C6DE282A195A9E0DC41E9A4CF729B2BEF169E0186
X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj0dLV0c3jbkzgJTgIFILq2w==
X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5DAF4476F875B33A473A324F45A4298F5583D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B
X-Mras: Ok
Subject: [Tarantool-patches] [PATCH v1 03/10] 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 <tarantool-patches.dev.tarantool.org>
List-Unsubscribe: <https://lists.tarantool.org/mailman/options/tarantool-patches>, 
 <mailto:tarantool-patches-request@dev.tarantool.org?subject=unsubscribe>
List-Archive: <https://lists.tarantool.org/pipermail/tarantool-patches/>
List-Post: <mailto:tarantool-patches@dev.tarantool.org>
List-Help: <mailto:tarantool-patches-request@dev.tarantool.org?subject=help>
List-Subscribe: <https://lists.tarantool.org/mailman/listinfo/tarantool-patches>, 
 <mailto:tarantool-patches-request@dev.tarantool.org?subject=subscribe>
From: Mergen Imeev via Tarantool-patches <tarantool-patches@dev.tarantool.org>
Reply-To: imeevma@tarantool.org
Errors-To: tarantool-patches-bounces@dev.tarantool.org
Sender: "Tarantool-patches" <tarantool-patches-bounces@dev.tarantool.org>

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 7d53b1646..27e38ec16 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -2104,14 +2104,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 820c72b00..7d7d281dd 100644
--- a/test/sql-tap/engine.cfg
+++ b/test/sql-tap/engine.cfg
@@ -26,6 +26,9 @@
     "metatypes.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
     ]], {
         -- <func-4.5>
-        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"
         -- </func-4.5>
     })
 
@@ -487,7 +487,7 @@ test:do_catchsql_test(
         SELECT round() FROM t1 ORDER BY a
     ]], {
         -- <func-4.11>
-        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"
         -- </func-4.11>
     })
 
@@ -2553,7 +2553,7 @@ test:do_catchsql_test(
         SELECT coalesce()
     ]], {
         -- <func-27.1>
-        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"
         -- </func-27.1>
     })
 
@@ -2563,7 +2563,7 @@ test:do_catchsql_test(
         SELECT coalesce(1)
     ]], {
         -- <func-27.2>
-        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"
         -- </func-27.2>
     })
 
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()
     ]], {
         -- <func2-1.2.1>
-        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"
         -- </func2-1.2.1>
     })
 
@@ -60,7 +60,7 @@ test:do_catchsql_test(
         SELECT SUBSTR('Supercalifragilisticexpialidocious')
     ]], {
         -- <func2-1.2.2>
-        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"
         -- </func2-1.2.2>
     })
 
@@ -70,7 +70,7 @@ test:do_catchsql_test(
         SELECT SUBSTR('Supercalifragilisticexpialidocious', 1,1,1)
     ]], {
         -- <func2-1.2.3>
-        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"
         -- </func2-1.2.3>
     })
 
@@ -673,7 +673,7 @@ if ("ሴ" ~= "u1234")
             SELECT SUBSTR()
         ]], {
             -- <func2-2.1.2>
-            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"
             -- </func2-2.1.2>
         })
 
@@ -683,7 +683,7 @@ if ("ሴ" ~= "u1234")
             SELECT SUBSTR('hiሴho')
         ]], {
             -- <func2-2.1.3>
-            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"
             -- </func2-2.1.3>
         })
 
@@ -693,7 +693,7 @@ if ("ሴ" ~= "u1234")
             SELECT SUBSTR('hiሴho', 1,1,1)
         ]], {
             -- <func2-2.1.4>
-            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"
             -- </func2-2.1.4>
         })
 
@@ -1038,7 +1038,7 @@ test:do_catchsql_test(
         SELECT SUBSTR()
     ]], {
         -- <func2-3.1.2>
-        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"
         -- </func2-3.1.2>
     })
 
@@ -1048,7 +1048,7 @@ test:do_catchsql_test(
         SELECT SUBSTR(x'1234')
     ]], {
         -- <func2-3.1.3>
-        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"
         -- </func2-3.1.3>
     })
 
@@ -1058,7 +1058,7 @@ test:do_catchsql_test(
         SELECT SUBSTR(x'1234', 1,1,1)
     ]], {
         -- <func2-3.1.4>
-        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"
         -- </func2-3.1.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
     ]], {
         -- <select1-2.1>
-        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"
         -- </select1-2.1>
     })
 
diff --git a/test/sql-tap/uuid.test.lua b/test/sql-tap/uuid.test.lua
index 57e638046..92691773d 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