[tarantool-patches] [PATCH v3 9/9] sql: better error messages on invalid arguments

Kirill Shcherbatov kshcherbatov at tarantool.org
Fri Aug 16 16:26:55 MSK 2019


Previous error message was not informative enough:
"wrong number of arguments to function".

An updated code provide more information in such case, when
possible:
"invalid number of arguments is passed to FUNCTION_NAME: expected
 n, got m".
---
 src/box/sql/resolve.c           | 14 +++++++++++---
 test/box/function1.result       | 10 +++++-----
 test/sql-tap/func.test.lua      | 22 +++++++++++-----------
 test/sql-tap/func2.test.lua     | 18 +++++++++---------
 test/sql-tap/limit.test.lua     |  4 ++--
 test/sql-tap/select1.test.lua   | 12 ++++++------
 test/sql/icu-upper-lower.result |  4 ++--
 7 files changed, 46 insertions(+), 38 deletions(-)

diff --git a/src/box/sql/resolve.c b/src/box/sql/resolve.c
index 682bb9c8e..2c037a5a4 100644
--- a/src/box/sql/resolve.c
+++ b/src/box/sql/resolve.c
@@ -678,10 +678,18 @@ resolveExprStep(Walker * pWalker, Expr * pExpr)
 				pParse->is_aborted = true;
 				pNC->nErr++;
 			} else if (wrong_num_args) {
-				const char *err = "wrong number of arguments "\
-						  "to function %.*s()";
+				const char *err;
+				if (func->def->param_count >= 0) {
+					err = "invalid number of arguments is "
+					      "passed to %.*s(): expected %d, "
+					      "got %d";
+				} else {
+					err = "invalid number of arguments is "
+					      "passed to %.*s()";
+				}
 				diag_set(ClientError, ER_SQL_PARSER_GENERIC,
-					 tt_sprintf(err, nId, zId));
+					 tt_sprintf(err, nId, zId,
+						    func->def->param_count, n));
 				pParse->is_aborted = true;
 				pNC->nErr++;
 			}
diff --git a/test/box/function1.result b/test/box/function1.result
index ebd9be700..25c470d0e 100644
--- a/test/box/function1.result
+++ b/test/box/function1.result
@@ -405,12 +405,12 @@ box.execute('SELECT "function1.divide"()')
 box.execute('SELECT "function1.divide"(6)')
 ---
 - null
-- wrong number of arguments to function function1.divide()
+- 'invalid number of arguments is passed to function1.divide(): expected 0, got 1'
 ...
 box.execute('SELECT "function1.divide"(6, 3)')
 ---
 - null
-- wrong number of arguments to function function1.divide()
+- 'invalid number of arguments is passed to function1.divide(): expected 0, got 2'
 ...
 box.func["function1.divide"]:drop()
 ---
@@ -429,17 +429,17 @@ test_run:cmd("setopt delimiter ''");
 box.execute('SELECT "function1.divide"()')
 ---
 - null
-- wrong number of arguments to function function1.divide()
+- 'invalid number of arguments is passed to function1.divide(): expected 2, got 0'
 ...
 box.execute('SELECT "function1.divide"(6)')
 ---
 - null
-- wrong number of arguments to function function1.divide()
+- 'invalid number of arguments is passed to function1.divide(): expected 2, got 1'
 ...
 box.execute('SELECT "function1.divide"(6, 3, 3)')
 ---
 - null
-- wrong number of arguments to function function1.divide()
+- 'invalid number of arguments is passed to function1.divide(): expected 2, got 3'
 ...
 box.execute('SELECT "function1.divide"(6, 3)')
 ---
diff --git a/test/sql-tap/func.test.lua b/test/sql-tap/func.test.lua
index ec06c903d..ae87bf61b 100755
--- a/test/sql-tap/func.test.lua
+++ b/test/sql-tap/func.test.lua
@@ -69,7 +69,7 @@ test:do_catchsql_test(
         SELECT length(*) FROM tbl1 ORDER BY t1
     ]], {
         -- <func-1.1>
-        1, "wrong number of arguments to function LENGTH()"
+        1, "invalid number of arguments is passed to LENGTH(): expected 1, got 0"
         -- </func-1.1>
     })
 
@@ -79,7 +79,7 @@ test:do_catchsql_test(
         SELECT length(t1,5) FROM tbl1 ORDER BY t1
     ]], {
         -- <func-1.2>
-        1, "wrong number of arguments to function LENGTH()"
+        1, "invalid number of arguments is passed to LENGTH(): expected 1, got 2"
         -- </func-1.2>
     })
 
@@ -365,7 +365,7 @@ test:do_test(
         return test:catchsql("SELECT abs(a,b) FROM t1")
     end, {
         -- <func-4.1>
-        1, "wrong number of arguments to function ABS()"
+        1, "invalid number of arguments is passed to ABS(): expected 1, got 2"
         -- </func-4.1>
     })
 
@@ -377,7 +377,7 @@ test:do_catchsql_test(
         SELECT abs() FROM t1
     ]], {
         -- <func-4.2>
-        1, "wrong number of arguments to function ABS()"
+        1, "invalid number of arguments is passed to ABS(): expected 1, got 0"
         -- </func-4.2>
     })
 
@@ -428,7 +428,7 @@ test:do_catchsql_test(
         SELECT round(a,b,c) FROM t1
     ]], {
         -- <func-4.5>
-        1, "wrong number of arguments to function ROUND()"
+        1, "invalid number of arguments is passed to ROUND()"
         -- </func-4.5>
     })
 
@@ -488,7 +488,7 @@ test:do_catchsql_test(
         SELECT round() FROM t1 ORDER BY a
     ]], {
         -- <func-4.11>
-        1, "wrong number of arguments to function ROUND()"
+        1, "invalid number of arguments is passed to ROUND()"
         -- </func-4.11>
     })
 
@@ -778,7 +778,7 @@ test:do_catchsql_test(
         SELECT upper(*) FROM t2
     ]], {
         -- <func-5.5>
-        1, "wrong number of arguments to function UPPER()"
+        1, "invalid number of arguments is passed to UPPER(): expected 1, got 0"
         -- </func-5.5>
     })
 
@@ -1782,7 +1782,7 @@ test:do_catchsql_test(
         SELECT replace(1,2);
     ]], {
         -- <func-21.1>
-        1, "wrong number of arguments to function REPLACE()"
+        1, "invalid number of arguments is passed to REPLACE(): expected 3, got 2"
         -- </func-21.1>
     })
 
@@ -1792,7 +1792,7 @@ test:do_catchsql_test(
         SELECT replace(1,2,3,4);
     ]], {
         -- <func-21.2>
-        1, "wrong number of arguments to function REPLACE()"
+        1, "invalid number of arguments is passed to REPLACE(): expected 3, got 4"
         -- </func-21.2>
     })
 
@@ -2540,7 +2540,7 @@ test:do_catchsql_test(
         SELECT coalesce()
     ]], {
         -- <func-27.1>
-        1, "wrong number of arguments to function COALESCE()"
+        1, "invalid number of arguments is passed to COALESCE()"
         -- </func-27.1>
     })
 
@@ -2550,7 +2550,7 @@ test:do_catchsql_test(
         SELECT coalesce(1)
     ]], {
         -- <func-27.2>
-        1, "wrong number of arguments to function COALESCE()"
+        1, "invalid number of arguments is passed to COALESCE()"
         -- </func-27.2>
     })
 
diff --git a/test/sql-tap/func2.test.lua b/test/sql-tap/func2.test.lua
index b70197d09..8ae565040 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 to function SUBSTR()"
+        1, "invalid number of arguments is passed to SUBSTR()"
         -- </func2-1.2.1>
     })
 
@@ -60,7 +60,7 @@ test:do_catchsql_test(
         SELECT SUBSTR('Supercalifragilisticexpialidocious')
     ]], {
         -- <func2-1.2.2>
-        1, "wrong number of arguments to function SUBSTR()"
+        1, "invalid number of arguments is passed to SUBSTR()"
         -- </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 to function SUBSTR()"
+        1, "invalid number of arguments is passed to SUBSTR()"
         -- </func2-1.2.3>
     })
 
@@ -673,7 +673,7 @@ if ("ሴ" ~= "u1234")
             SELECT SUBSTR()
         ]], {
             -- <func2-2.1.2>
-            1, "wrong number of arguments to function SUBSTR()"
+            1, "invalid number of arguments is passed to SUBSTR()"
             -- </func2-2.1.2>
         })
 
@@ -683,7 +683,7 @@ if ("ሴ" ~= "u1234")
             SELECT SUBSTR('hiሴho')
         ]], {
             -- <func2-2.1.3>
-            1, "wrong number of arguments to function SUBSTR()"
+            1, "invalid number of arguments is passed to SUBSTR()"
             -- </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 to function SUBSTR()"
+            1, "invalid number of arguments is passed to SUBSTR()"
             -- </func2-2.1.4>
         })
 
@@ -1038,7 +1038,7 @@ test:do_catchsql_test(
         SELECT SUBSTR()
     ]], {
         -- <func2-3.1.2>
-        1, "wrong number of arguments to function SUBSTR()"
+        1, "invalid number of arguments is passed to SUBSTR()"
         -- </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 to function SUBSTR()"
+        1, "invalid number of arguments is passed to SUBSTR()"
         -- </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 to function SUBSTR()"
+        1, "invalid number of arguments is passed to SUBSTR()"
         -- </func2-3.1.4>
     })
 
diff --git a/test/sql-tap/limit.test.lua b/test/sql-tap/limit.test.lua
index 8445ab18e..76c5cba4d 100755
--- a/test/sql-tap/limit.test.lua
+++ b/test/sql-tap/limit.test.lua
@@ -771,7 +771,7 @@ test:do_catchsql_test(
         SELECT * FROM t1 LIMIT replace(1)
     ]], {
         -- <limit-12.1>
-        1, "wrong number of arguments to function REPLACE()"
+        1, "invalid number of arguments is passed to REPLACE(): expected 3, got 1"
         -- </limit-12.1>
     })
 
@@ -781,7 +781,7 @@ test:do_catchsql_test(
         SELECT * FROM t1 LIMIT 5 OFFSET replace(1)
     ]], {
         -- <limit-12.2>
-        1, 'wrong number of arguments to function REPLACE()'
+        1, 'invalid number of arguments is passed to REPLACE(): expected 3, got 1'
         -- </limit-12.2>
     })
 
diff --git a/test/sql-tap/select1.test.lua b/test/sql-tap/select1.test.lua
index 924c0ccb1..096bd23c1 100755
--- a/test/sql-tap/select1.test.lua
+++ b/test/sql-tap/select1.test.lua
@@ -244,7 +244,7 @@ test:do_catchsql_test(
         SELECT count(f1,f2) FROM test1
     ]], {
         -- <select1-2.1>
-        1, "wrong number of arguments to function COUNT()"
+        1, "invalid number of arguments is passed to COUNT()"
         -- </select1-2.1>
     })
 
@@ -324,7 +324,7 @@ test:do_catchsql_test(
         SELECT min(*) FROM test1
     ]], {
         -- <select1-2.6>
-        1, "wrong number of arguments to function MIN()"
+        1, "invalid number of arguments is passed to MIN(): expected 1, got 0"
         -- </select1-2.6>
     })
 
@@ -389,7 +389,7 @@ test:do_catchsql_test(
         SELECT MAX(*) FROM test1
     ]], {
         -- <select1-2.9>
-        1, "wrong number of arguments to function MAX()"
+        1, "invalid number of arguments is passed to MAX(): expected 1, got 0"
         -- </select1-2.9>
     })
 
@@ -469,7 +469,7 @@ test:do_catchsql_test(
         SELECT SUM(*) FROM test1
     ]], {
         -- <select1-2.14>
-        1, "wrong number of arguments to function SUM()"
+        1, "invalid number of arguments is passed to SUM(): expected 1, got 0"
         -- </select1-2.14>
     })
 
@@ -489,7 +489,7 @@ test:do_catchsql_test(
         SELECT sum(f1,f2) FROM test1
     ]], {
         -- <select1-2.16>
-        1, "wrong number of arguments to function SUM()"
+        1, "invalid number of arguments is passed to SUM(): expected 1, got 2"
         -- </select1-2.16>
     })
 
@@ -691,7 +691,7 @@ test:do_catchsql_test(
         SELECT f1 FROM test1 WHERE count(f1,f2)!=11
     ]], {
         -- <select1-3.9>
-        1, "wrong number of arguments to function COUNT()"
+        1, "invalid number of arguments is passed to COUNT()"
         -- </select1-3.9>
     })
 
diff --git a/test/sql/icu-upper-lower.result b/test/sql/icu-upper-lower.result
index 88266c8c5..e853c25e4 100644
--- a/test/sql/icu-upper-lower.result
+++ b/test/sql/icu-upper-lower.result
@@ -277,7 +277,7 @@ test_run:cmd("setopt delimiter ''");
 box.execute("select upper('1', 2)")
 ---
 - null
-- wrong number of arguments to function UPPER()
+- 'invalid number of arguments is passed to UPPER(): expected 1, got 2'
 ...
 box.execute("select upper(\"1\")")
 ---
@@ -287,5 +287,5 @@ box.execute("select upper(\"1\")")
 box.execute("select upper()")
 ---
 - null
-- wrong number of arguments to function UPPER()
+- 'invalid number of arguments is passed to UPPER(): expected 1, got 0'
 ...
-- 
2.22.1





More information about the Tarantool-patches mailing list