[Tarantool-patches] [PATCH v6 07/22] sql: check operands of LIKE

imeevma at tarantool.org imeevma at tarantool.org
Thu Jul 16 17:46:12 MSK 2020


After this patch, the operand types of LIKE will be checked
properly.

Part of #4159
---
 src/box/sql/func.c          | 17 +++----------
 test/sql-tap/func5.test.lua | 51 ++++++++++++++++++++++++++++++++++++-
 test/sql/types.result       | 24 +++++++----------
 3 files changed, 63 insertions(+), 29 deletions(-)

diff --git a/src/box/sql/func.c b/src/box/sql/func.c
index a22ba38e2..e03d7d2e2 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -1203,18 +1203,9 @@ likeFunc(sql_context *context, int argc, sql_value **argv)
 	sql *db = sql_context_db_handle(context);
 	int rhs_type = sql_value_type(argv[0]);
 	int lhs_type = sql_value_type(argv[1]);
-
-	if (lhs_type != MP_STR || rhs_type != MP_STR) {
-		if (lhs_type == MP_NIL || rhs_type == MP_NIL)
-			return;
-		char *inconsistent_type = rhs_type != MP_STR ?
-					  mem_type_to_str(argv[0]) :
-					  mem_type_to_str(argv[1]);
-		diag_set(ClientError, ER_INCONSISTENT_TYPES, "text",
-			 inconsistent_type);
-		context->is_aborted = true;
+	if (lhs_type == MP_NIL || rhs_type == MP_NIL)
 		return;
-	}
+	assert(lhs_type == MP_STR && rhs_type == MP_STR);
 	const char *zB = (const char *) sql_value_text(argv[0]);
 	const char *zA = (const char *) sql_value_text(argv[1]);
 	const char *zB_end = zB + sql_value_bytes(argv[0]);
@@ -2565,8 +2556,8 @@ static struct {
 	}, {
 	 .name = "LIKE",
 	 .param_count = -1,
-	 .first_arg = FIELD_TYPE_ANY,
-	 .args = FIELD_TYPE_ANY,
+	 .first_arg = FIELD_TYPE_STRING,
+	 .args = FIELD_TYPE_STRING,
 	 .is_blob_like_str = false,
 	 .returns = FIELD_TYPE_INTEGER,
 	 .aggregate = FUNC_AGGREGATE_NONE,
diff --git a/test/sql-tap/func5.test.lua b/test/sql-tap/func5.test.lua
index 0398d8a9d..84877a919 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(78)
+test:plan(85)
 
 --!./tcltestrunner.lua
 -- 2010 August 27
@@ -689,4 +689,53 @@ test:do_execsql_test(
         2
     })
 
+test:do_execsql_test(
+    "func-5-6.9.1", [[
+        SELECT NULL like NULL;
+    ]],{
+        ""
+    })
+
+test:do_catchsql_test(
+    "func-5-6.9.2", [[
+        SELECT 123 like 123;
+    ]], {
+        1, "Type mismatch: can not convert 123 to string"
+    })
+
+test:do_catchsql_test(
+    "func-5-6.9.3", [[
+        SELECT -123 like -123;
+    ]], {
+        1, "Type mismatch: can not convert -123 to string"
+    })
+
+test:do_catchsql_test(
+    "func-5-6.9.4", [[
+        SELECT -5.5 like -5.5;
+    ]], {
+        1, "Type mismatch: can not convert -5.5 to string"
+    })
+
+test:do_execsql_test(
+    "func-5-6.9.5", [[
+        SELECT '-123' like '-123';
+    ]], {
+        true
+    })
+
+test:do_catchsql_test(
+    "func-5-6.9.6", [[
+        SELECT false like false;
+    ]], {
+        1, "Type mismatch: can not convert FALSE to string"
+    })
+
+test:do_catchsql_test(
+    "func-5-6.9.7", [[
+        SELECT X'3334' like X'3334';
+    ]], {
+        1, "Type mismatch: can not convert varbinary to string"
+    })
+
 test:finish_test()
diff --git a/test/sql/types.result b/test/sql/types.result
index 9cb0818e6..e0cf2d7d9 100644
--- a/test/sql/types.result
+++ b/test/sql/types.result
@@ -215,25 +215,22 @@ box.execute("INSERT INTO t1 VALUES (randomblob(5));")
 box.execute("SELECT * FROM t1 WHERE s LIKE 'blob';")
 ---
 - null
-- 'Inconsistent types: expected text got varbinary'
+- 'Type mismatch: can not convert varbinary to string'
 ...
 box.execute("SELECT * FROM t1 WHERE 'blob' LIKE s;")
 ---
 - null
-- 'Inconsistent types: expected text got varbinary'
+- 'Type mismatch: can not convert varbinary to string'
 ...
 box.execute("SELECT * FROM t1 WHERE 'blob' LIKE x'0000';")
 ---
 - null
-- 'Inconsistent types: expected text got varbinary'
+- 'Type mismatch: can not convert varbinary to string'
 ...
 box.execute("SELECT s LIKE NULL FROM t1;")
 ---
-- metadata:
-  - name: s LIKE NULL
-    type: integer
-  rows:
-  - [null]
+- null
+- 'Type mismatch: can not convert varbinary to string'
 ...
 box.execute("DELETE FROM t1;")
 ---
@@ -246,20 +243,17 @@ box.execute("INSERT INTO t1 VALUES (1);")
 box.execute("SELECT * FROM t1 WHERE s LIKE 'int';")
 ---
 - null
-- 'Inconsistent types: expected text got unsigned'
+- 'Type mismatch: can not convert 1 to string'
 ...
 box.execute("SELECT * FROM t1 WHERE 'int' LIKE 4;")
 ---
 - null
-- 'Inconsistent types: expected text got unsigned'
+- 'Type mismatch: can not convert 4 to string'
 ...
 box.execute("SELECT NULL LIKE s FROM t1;")
 ---
-- metadata:
-  - name: NULL LIKE s
-    type: integer
-  rows:
-  - [null]
+- null
+- 'Type mismatch: can not convert 1 to string'
 ...
 box.space.T1:drop()
 ---
-- 
2.25.1



More information about the Tarantool-patches mailing list