From 78e59b266508cb1c98c22521413d8e54cb752eff Mon Sep 17 00:00:00 2001
From: Nikita Pettik <korablev@tarantool.org>
Date: Tue, 23 Jul 2019 21:27:32 +0300
Subject: [PATCH] sql: use 'varbinary' as a name of type instead of 'blob'

We are going to introduce new column type 'VARBINARY', which allows to
store values with MP_BIN msgpack format. On the other hand, now it is also
possible to meet this type: all literals in form of x'...' are
supposed to be inserted into SCALAR column type exactly with MP_BIN
encoding. Prior to this moment type of such values (encoded as MP_BIN)
was called 'blob'. Thus, let's fix all visible to user messages using
'varbinary' name of type instead of 'blob'.
---
 src/box/lua/lua_sql.c          |  2 +-
 src/box/sql/func.c             | 13 +++++++------
 src/box/sql/vdbe.c             |  4 ++--
 src/box/sql/vdbeapi.c          | 16 ++++++++--------
 src/box/sql/vdbeaux.c          |  2 +-
 src/box/sql/vdbemem.c          |  8 ++++----
 test/sql-tap/cast.test.lua     |  4 ++--
 test/sql-tap/func.test.lua     |  2 +-
 test/sql-tap/lua_sql.test.lua  |  4 ++--
 test/sql-tap/position.test.lua | 16 ++++++++--------
 test/sql/types.result          | 30 +++++++++++++++---------------
 11 files changed, 51 insertions(+), 50 deletions(-)

diff --git a/src/box/lua/lua_sql.c b/src/box/lua/lua_sql.c
index f900c7ca52..67a51a82ce 100644
--- a/src/box/lua/lua_sql.c
+++ b/src/box/lua/lua_sql.c
@@ -166,7 +166,7 @@ lbox_sql_create_function(struct lua_State *L)
 		type = FIELD_TYPE_STRING;
 	else if (strcmp(type_arg, "NUMBER") == 0)
 		type = FIELD_TYPE_NUMBER;
-	else if (strcmp(type_arg, "BLOB") == 0)
+	else if (strcmp(type_arg, "VARBINARY") == 0)
 		type = FIELD_TYPE_SCALAR;
 	else if (strcmp(type_arg, "BOOL") == 0 ||
 		 strcmp(type_arg, "BOOLEAN") == 0)
diff --git a/src/box/sql/func.c b/src/box/sql/func.c
index fcf147c967..d9f671917a 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -119,7 +119,7 @@ typeofFunc(sql_context * context, int NotUsed, sql_value ** argv)
 		z = "number";
 		break;
 	case MP_BIN:
-		z = "scalar";
+		z = "varbinary";
 		break;
 	case MP_BOOL:
 		z = "boolean";
@@ -244,7 +244,7 @@ position_func(struct sql_context *context, int argc, struct Mem **argv)
 	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 BLOB",
+		diag_set(ClientError, ER_INCONSISTENT_TYPES, "TEXT or VARBINARY",
 			 mem_type_to_str(inconsistent_type_arg));
 		context->is_aborted = true;
 		return;
@@ -1172,7 +1172,8 @@ zeroblobFunc(sql_context * context, int argc, sql_value ** argv)
 	if (n < 0)
 		n = 0;
 	if (sql_result_zeroblob64(context, n) != 0) {
-		diag_set(ClientError, ER_SQL_EXECUTE, "string or blob too big");
+		diag_set(ClientError, ER_SQL_EXECUTE, "string or binary string"\
+			 "is too big");
 		context->is_aborted = true;
 	}
 }
@@ -1241,7 +1242,7 @@ replaceFunc(sql_context * context, int argc, sql_value ** argv)
 			testcase(nOut - 2 == db->aLimit[SQL_LIMIT_LENGTH]);
 			if (nOut - 1 > db->aLimit[SQL_LIMIT_LENGTH]) {
 				diag_set(ClientError, ER_SQL_EXECUTE, "string "\
-					 "or blob too big");
+					 "or binary string is too big");
 				context->is_aborted = true;
 				sql_free(zOut);
 				return;
@@ -1742,8 +1743,8 @@ groupConcatFinalize(sql_context * context)
 	pAccum = sql_aggregate_context(context, 0);
 	if (pAccum) {
 		if (pAccum->accError == STRACCUM_TOOBIG) {
-			diag_set(ClientError, ER_SQL_EXECUTE, "string or blob "\
-				 "too big");
+			diag_set(ClientError, ER_SQL_EXECUTE, "string or binary"\
+				 "string is too big");
 			context->is_aborted = true;
 		} else if (pAccum->accError == STRACCUM_NOMEM) {
 			context->is_aborted = true;
diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c
index 3a8dc04c11..a5462266e4 100644
--- a/src/box/sql/vdbe.c
+++ b/src/box/sql/vdbe.c
@@ -598,7 +598,7 @@ mem_type_to_str(const struct Mem *p)
 	case MEM_Real:
 		return "REAL";
 	case MEM_Blob:
-		return "BLOB";
+		return "VARBINARY";
 	case MEM_Bool:
 		return "BOOLEAN";
 	default:
@@ -1476,7 +1476,7 @@ case OP_Concat: {           /* same as TK_CONCAT, in1, in2, out3 */
 		char *inconsistent_type = str_type_p1 == 0 ?
 					  mem_type_to_str(pIn1) :
 					  mem_type_to_str(pIn2);
-		diag_set(ClientError, ER_INCONSISTENT_TYPES, "TEXT or BLOB",
+		diag_set(ClientError, ER_INCONSISTENT_TYPES, "TEXT or VARBINARY",
 			 inconsistent_type);
 		goto abort_due_to_error;
 	}
diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c
index ecf1b36018..63b46fdbc8 100644
--- a/src/box/sql/vdbeapi.c
+++ b/src/box/sql/vdbeapi.c
@@ -293,8 +293,8 @@ invokeValueDestructor(const void *p,	/* Value to destroy */
 		xDel((void *)p);
 	}
 	if (pCtx) {
-		diag_set(ClientError, ER_SQL_EXECUTE, "string or blob is too "\
-			 "big");
+		diag_set(ClientError, ER_SQL_EXECUTE, "string or binary string"\
+			 "is too big");
 		pCtx->is_aborted = true;
 	}
 	return -1;
@@ -392,8 +392,8 @@ sql_result_zeroblob64(sql_context * pCtx, u64 n)
 {
 	Mem *pOut = pCtx->pOut;
 	if (n > (u64) pOut->db->aLimit[SQL_LIMIT_LENGTH]) {
-		diag_set(ClientError, ER_SQL_EXECUTE, "string or blob is too "\
-			 "big");
+		diag_set(ClientError, ER_SQL_EXECUTE, "string or binary string"\
+			 "is too big");
 		return -1;
 	}
 	sqlVdbeMemSetZeroBlob(pCtx->pOut, (int)n);
@@ -941,7 +941,7 @@ sql_bind_blob(sql_stmt * pStmt,
 	struct Mem *var = &p->aVar[i - 1];
 	if (sqlVdbeMemSetStr(var, zData, nData, 0, xDel) != 0)
 		return -1;
-	return sql_bind_type(p, i, "BLOB");
+	return sql_bind_type(p, i, "VARBINARY");
 }
 
 int
@@ -1025,7 +1025,7 @@ sql_bind_ptr(struct sql_stmt *stmt, int i, void *ptr)
 	struct Vdbe *p = (struct Vdbe *) stmt;
 	int rc = vdbeUnbind(p, i);
 	if (rc == 0) {
-		rc = sql_bind_type(p, i, "BLOB");
+		rc = sql_bind_type(p, i, "VARBINARY");
 		mem_set_ptr(&p->aVar[i - 1], ptr);
 	}
 	return rc;
@@ -1069,8 +1069,8 @@ sql_bind_zeroblob64(sql_stmt * pStmt, int i, sql_uint64 n)
 {
 	Vdbe *p = (Vdbe *) pStmt;
 	if (n > (u64) p->db->aLimit[SQL_LIMIT_LENGTH]) {
-		diag_set(ClientError, ER_SQL_EXECUTE, "string or blob is too "\
-			 "big");
+		diag_set(ClientError, ER_SQL_EXECUTE, "string or binary string"\
+			 "is too big");
 		return -1;
 	}
 	assert((n & 0x7FFFFFFF) == n);
diff --git a/src/box/sql/vdbeaux.c b/src/box/sql/vdbeaux.c
index 6c699bdc61..e2a8c291ce 100644
--- a/src/box/sql/vdbeaux.c
+++ b/src/box/sql/vdbeaux.c
@@ -1194,7 +1194,7 @@ displayP4(Op * pOp, char *zTemp, int nTemp)
 				zP4 = "NULL";
 			} else {
 				assert(pMem->flags & MEM_Blob);
-				zP4 = "(blob)";
+				zP4 = "(binary string)";
 			}
 			break;
 		}
diff --git a/src/box/sql/vdbemem.c b/src/box/sql/vdbemem.c
index 268b5e9798..51564b2ed8 100644
--- a/src/box/sql/vdbemem.c
+++ b/src/box/sql/vdbemem.c
@@ -1028,8 +1028,8 @@ sqlVdbeMemSetStr(Mem * pMem,	/* Memory cell to set to string value */
 			nAlloc += 1; //SQL_UTF8
 		}
 		if (nByte > iLimit) {
-			diag_set(ClientError, ER_SQL_EXECUTE, "string or blob "\
-				 "is too big");
+			diag_set(ClientError, ER_SQL_EXECUTE, "string or binary"\
+				 "string is too big");
 			return -1;
 		}
 		testcase(nAlloc == 0);
@@ -1054,8 +1054,8 @@ sqlVdbeMemSetStr(Mem * pMem,	/* Memory cell to set to string value */
 	pMem->flags = flags;
 
 	if (nByte > iLimit) {
-		diag_set(ClientError, ER_SQL_EXECUTE, "string or blob is too "\
-			 "big");
+		diag_set(ClientError, ER_SQL_EXECUTE, "string or binary string"\
+			 "is too big");
 		return -1;
 	}
 
diff --git a/test/sql-tap/cast.test.lua b/test/sql-tap/cast.test.lua
index 667fc0d34f..4f9c527bd6 100755
--- a/test/sql-tap/cast.test.lua
+++ b/test/sql-tap/cast.test.lua
@@ -40,7 +40,7 @@ test:do_execsql_test(
         SELECT typeof(x'616263')
     ]], {
         -- <cast-1.2>
-        "scalar"
+        "varbinary"
         -- </cast-1.2>
     })
 
@@ -90,7 +90,7 @@ test:do_execsql_test(
         SELECT typeof(CAST(x'616263' AS SCALAR))
     ]], {
         -- <cast-1.8>
-        "scalar"
+        "varbinary"
         -- </cast-1.8>
     })
 
diff --git a/test/sql-tap/func.test.lua b/test/sql-tap/func.test.lua
index e44b23dc25..5922622ec1 100755
--- a/test/sql-tap/func.test.lua
+++ b/test/sql-tap/func.test.lua
@@ -978,7 +978,7 @@ test:do_execsql_test(
         SELECT typeof(randomblob(32));
     ]], {
         -- <func-9.4>
-        "scalar"
+        "varbinary"
         -- </func-9.4>
     })
 
diff --git a/test/sql-tap/lua_sql.test.lua b/test/sql-tap/lua_sql.test.lua
index 921fc39227..b0ecccd527 100755
--- a/test/sql-tap/lua_sql.test.lua
+++ b/test/sql-tap/lua_sql.test.lua
@@ -108,7 +108,7 @@ local from_lua_to_sql = {
 local function check_from_lua_to_sql(i)
     return from_lua_to_sql[i][2]
 end
-box.internal.sql_create_function("check_from_lua_to_sql", "BLOB", check_from_lua_to_sql)
+box.internal.sql_create_function("check_from_lua_to_sql", "VARBINARY", check_from_lua_to_sql)
 
 -- check for different types
 for i = 1, #from_lua_to_sql, 1 do
@@ -125,7 +125,7 @@ local from_lua_to_sql_bad = {
 local function check_from_lua_to_sql_bad(i)
     return from_lua_to_sql_bad[i]
 end
-box.internal.sql_create_function("check_from_lua_to_sql_bad", "BLOB", check_from_lua_to_sql_bad)
+box.internal.sql_create_function("check_from_lua_to_sql_bad", "VARBINARY", check_from_lua_to_sql_bad)
 
 for i = 1, #from_lua_to_sql_bad, 1 do
     test:do_catchsql_test(
diff --git a/test/sql-tap/position.test.lua b/test/sql-tap/position.test.lua
index 40b8a943bf..9539f7bf75 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 BLOB got UNSIGNED"
+        1, "Inconsistent types: expected TEXT or VARBINARY got UNSIGNED"
         -- </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 BLOB got REAL"
+        1, "Inconsistent types: expected TEXT or VARBINARY got REAL"
         -- </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 BLOB got REAL"
+        1, "Inconsistent types: expected TEXT or VARBINARY got REAL"
         -- </position-1.25>
     })
 
@@ -554,7 +554,7 @@ test:do_test(
         return test:catchsql("SELECT position('x', x'78c3a4e282ac79');")
     end, {
         -- <position-1.54>
-        1, "Inconsistent types: expected TEXT got BLOB"
+        1, "Inconsistent types: expected TEXT got VARBINARY"
         -- </position-1.54>
     })
 
@@ -564,7 +564,7 @@ test:do_test(
         return test:catchsql "SELECT position('y', x'78c3a4e282ac79');"
     end, {
         -- <position-1.55>
-        1, "Inconsistent types: expected TEXT got BLOB"
+        1, "Inconsistent types: expected TEXT got VARBINARY"
         -- </position-1.55>
     })
 
@@ -614,7 +614,7 @@ test:do_test(
         return test:catchsql "SELECT position(x'79', 'xä€y');"
     end, {
         -- <position-1.57.1>
-        1, "Inconsistent types: expected BLOB got TEXT"
+        1, "Inconsistent types: expected VARBINARY got TEXT"
         -- </position-1.57.1>
     })
 
@@ -624,7 +624,7 @@ test:do_test(
         return test:catchsql "SELECT position(x'a4', 'xä€y');"
     end, {
         -- <position-1.57.2>
-        1, "Inconsistent types: expected BLOB got TEXT"
+        1, "Inconsistent types: expected VARBINARY got TEXT"
         -- </position-1.57.2>
     })
 
@@ -634,7 +634,7 @@ test:do_test(
         return test:catchsql "SELECT position('y', x'78c3a4e282ac79');"
     end, {
         -- <position-1.57.3>
-        1, "Inconsistent types: expected TEXT got BLOB"
+        1, "Inconsistent types: expected TEXT got VARBINARY"
         -- </position-1.57.3>
     })
 
diff --git a/test/sql/types.result b/test/sql/types.result
index 7044e68346..c89da47487 100644
--- a/test/sql/types.result
+++ b/test/sql/types.result
@@ -152,33 +152,33 @@ sp:drop()
 --
 box.execute("SELECT 'abc' || 1;")
 ---
-- error: 'Inconsistent types: expected TEXT or BLOB got UNSIGNED'
+- error: 'Inconsistent types: expected TEXT or VARBINARY got UNSIGNED'
 ...
 box.execute("SELECT 'abc' || 1.123;")
 ---
-- error: 'Inconsistent types: expected TEXT or BLOB got REAL'
+- error: 'Inconsistent types: expected TEXT or VARBINARY got REAL'
 ...
 box.execute("SELECT 1 || 'abc';")
 ---
-- error: 'Inconsistent types: expected TEXT or BLOB got UNSIGNED'
+- error: 'Inconsistent types: expected TEXT or VARBINARY got UNSIGNED'
 ...
 box.execute("SELECT 1.123 || 'abc';")
 ---
-- error: 'Inconsistent types: expected TEXT or BLOB got REAL'
+- error: 'Inconsistent types: expected TEXT or VARBINARY got REAL'
 ...
 box.execute("SELECt 'a' || 'b' || 1;")
 ---
-- error: 'Inconsistent types: expected TEXT or BLOB got UNSIGNED'
+- error: 'Inconsistent types: expected TEXT or VARBINARY got UNSIGNED'
 ...
 -- What is more, they must be of the same type.
 --
 box.execute("SELECT 'abc' || randomblob(5);")
 ---
-- error: 'Inconsistent types: expected TEXT got BLOB'
+- error: 'Inconsistent types: expected TEXT got VARBINARY'
 ...
 box.execute("SELECT randomblob(5) || 'x';")
 ---
-- error: 'Inconsistent types: expected BLOB got TEXT'
+- error: 'Inconsistent types: expected VARBINARY got TEXT'
 ...
 -- Result of BLOBs concatenation must be BLOB.
 --
@@ -188,7 +188,7 @@ box.execute("VALUES (TYPEOF(randomblob(5) || zeroblob(5)));")
   - name: column1
     type: string
   rows:
-  - ['scalar']
+  - ['varbinary']
 ...
 -- gh-3954: LIKE accepts only arguments of type TEXT and NULLs.
 --
@@ -202,15 +202,15 @@ box.execute("INSERT INTO t1 VALUES (randomblob(5));")
 ...
 box.execute("SELECT * FROM t1 WHERE s LIKE 'blob';")
 ---
-- error: 'Inconsistent types: expected TEXT got BLOB'
+- error: 'Inconsistent types: expected TEXT got VARBINARY'
 ...
 box.execute("SELECT * FROM t1 WHERE 'blob' LIKE s;")
 ---
-- error: 'Inconsistent types: expected TEXT got BLOB'
+- error: 'Inconsistent types: expected TEXT got VARBINARY'
 ...
 box.execute("SELECT * FROM t1 WHERE 'blob' LIKE x'0000';")
 ---
-- error: 'Inconsistent types: expected TEXT got BLOB'
+- error: 'Inconsistent types: expected TEXT got VARBINARY'
 ...
 box.execute("SELECT s LIKE NULL FROM t1;")
 ---
@@ -393,7 +393,7 @@ box.execute("SELECT 1 LIMIT 1 OFFSET true;")
 ...
 box.execute("SELECT 'abc' || true;")
 ---
-- error: 'Inconsistent types: expected TEXT or BLOB got BOOLEAN'
+- error: 'Inconsistent types: expected TEXT or VARBINARY got BOOLEAN'
 ...
 -- Boolean can take part in arithmetic operations.
 --
@@ -1795,7 +1795,7 @@ box.execute("SELECT CASE 1 WHEN 1 THEN 666 WHEN 2 THEN 123 ELSE 'asd' END")
 ---
 - metadata:
   - name: CASE 1 WHEN 1 THEN 666 WHEN 2 THEN 123 ELSE 'asd' END
-    type: integer
+    type: scalar
   rows:
   - [666]
 ...
@@ -1813,7 +1813,7 @@ box.execute("SELECT CASE 'a' WHEN 'a' THEN 1 WHEN 'b' THEN 2 WHEN 'c' THEN 3 WHE
 - metadata:
   - name: CASE 'a' WHEN 'a' THEN 1 WHEN 'b' THEN 2 WHEN 'c' THEN 3 WHEN 'd' THEN 4
       WHEN 'e' THEN 5 WHEN 'f' THEN 'asd' END
-    type: integer
+    type: scalar
   rows:
   - [1]
 ...
@@ -1822,7 +1822,7 @@ box.execute("SELECT CASE 'a' WHEN 'a' THEN 1 WHEN 'b' THEN 2 WHEN 'c' THEN 3 WHE
 - metadata:
   - name: CASE 'a' WHEN 'a' THEN 1 WHEN 'b' THEN 2 WHEN 'c' THEN 3 WHEN 'd' THEN 4
       WHEN 'e' THEN 5 WHEN 'f' THEN 6 ELSE 'asd' END
-    type: integer
+    type: scalar
   rows:
   - [1]
 ...
-- 
GitLab