[Tarantool-patches] [PATCH v5 10/17] sql: check args of position()

imeevma at tarantool.org imeevma at tarantool.org
Tue Jul 14 18:48:32 MSK 2020


After this patch, the argument types of the position() function
will be checked properly.

Part of #4159
---
 src/box/sql/func.c             | 24 ++++------------
 test/sql-tap/func5.test.lua    | 51 +++++++++++++++++++++++++++++++++-
 test/sql-tap/position.test.lua |  6 ++--
 3 files changed, 58 insertions(+), 23 deletions(-)

diff --git a/src/box/sql/func.c b/src/box/sql/func.c
index 2910f308f..a1b284762 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -532,22 +532,8 @@ position_func(struct sql_context *context, int argc, struct Mem **argv)
 
 	if (haystack_type == MP_NIL || needle_type == MP_NIL)
 		return;
-	/*
-	 * Position function can be called only with string
-	 * or blob params.
-	 */
-	struct Mem *inconsistent_type_arg = NULL;
-	if (needle_type != MP_STR && needle_type != MP_BIN)
-		inconsistent_type_arg = needle;
-	if (haystack_type != MP_STR && haystack_type != MP_BIN)
-		inconsistent_type_arg = haystack;
-	if (inconsistent_type_arg != NULL) {
-		diag_set(ClientError, ER_INCONSISTENT_TYPES,
-			 "text or varbinary",
-			 mem_type_to_str(inconsistent_type_arg));
-		context->is_aborted = true;
-		return;
-	}
+	assert(needle_type == MP_STR || needle_type == MP_BIN);
+	assert(haystack_type == MP_STR || haystack_type == MP_BIN);
 	/*
 	 * Both params of Position function must be of the same
 	 * type.
@@ -2683,9 +2669,9 @@ static struct {
 	}, {
 	 .name = "POSITION",
 	 .param_count = 2,
-	 .first_arg = FIELD_TYPE_ANY,
-	 .args = FIELD_TYPE_ANY,
-	 .is_blob_like_str = false,
+	 .first_arg = FIELD_TYPE_STRING,
+	 .args = FIELD_TYPE_STRING,
+	 .is_blob_like_str = true,
 	 .returns = FIELD_TYPE_INTEGER,
 	 .aggregate = FUNC_AGGREGATE_NONE,
 	 .is_deterministic = true,
diff --git a/test/sql-tap/func5.test.lua b/test/sql-tap/func5.test.lua
index b704ed7d5..170dee9f9 100755
--- a/test/sql-tap/func5.test.lua
+++ b/test/sql-tap/func5.test.lua
@@ -1,6 +1,6 @@
 #!/usr/bin/env tarantool
 test = require("sqltester")
-test:plan(99)
+test:plan(106)
 
 --!./tcltestrunner.lua
 -- 2010 August 27
@@ -836,4 +836,53 @@ test:do_catchsql_test(
         1, "Type mismatch: can not convert varbinary to string"
     })
 
+test:do_execsql_test(
+    "func-5-6.12.1", [[
+        SELECT position(NULL, NULL);
+    ]],{
+        ""
+    })
+
+test:do_catchsql_test(
+    "func-5-6.12.2", [[
+        SELECT position(23, 123);
+    ]], {
+        1, "Type mismatch: can not convert 23 to string"
+    })
+
+test:do_catchsql_test(
+    "func-5-6.12.3", [[
+        SELECT position(-12, -123);
+    ]], {
+        1, "Type mismatch: can not convert -12 to string"
+    })
+
+test:do_catchsql_test(
+    "func-5-6.12.4", [[
+        SELECT position(-5.5, -5.5);
+    ]], {
+        1, "Type mismatch: can not convert -5.5 to string"
+    })
+
+test:do_execsql_test(
+    "func-5-6.12.5", [[
+        SELECT position('23', '-123');
+    ]], {
+        3
+    })
+
+test:do_catchsql_test(
+    "func-5-6.12.6", [[
+        SELECT position(false, true);
+    ]], {
+        1, "Type mismatch: can not convert FALSE to string"
+    })
+
+test:do_execsql_test(
+    "func-5-6.12.7", [[
+        SELECT position(X'34', X'3334');
+    ]], {
+        2
+    })
+
 test:finish_test()
diff --git a/test/sql-tap/position.test.lua b/test/sql-tap/position.test.lua
index e0455abc9..0d4f6f371 100755
--- a/test/sql-tap/position.test.lua
+++ b/test/sql-tap/position.test.lua
@@ -228,7 +228,7 @@ test:do_test(
         return test:catchsql "SELECT position(34, 12345);"
     end, {
         -- <position-1.23>
-        1, "Inconsistent types: expected text or varbinary got unsigned"
+        1, "Type mismatch: can not convert 34 to string"
         -- </position-1.23>
     })
 
@@ -238,7 +238,7 @@ test:do_test(
         return test:catchsql "SELECT position(34, 123456.78);"
     end, {
         -- <position-1.24>
-        1, "Inconsistent types: expected text or varbinary got real"
+        1, "Type mismatch: can not convert 34 to string"
         -- </position-1.24>
     })
 
@@ -248,7 +248,7 @@ test:do_test(
         return test:catchsql "SELECT position(x'3334', 123456.78);"
     end, {
         -- <position-1.25>
-        1, "Inconsistent types: expected text or varbinary got real"
+        1, "Type mismatch: can not convert 123456.78 to string"
         -- </position-1.25>
     })
 
-- 
2.25.1



More information about the Tarantool-patches mailing list