[tarantool-patches] [PATCH] sql: remove implicit CAST for assignment of int field

Nikita Pettik korablev at tarantool.org
Tue May 21 17:10:30 MSK 2019


It also fixes misbehaviour during insertion of boolean values to
integer field: explicit CAST operator converts boolean to integer,
meanwhile implicit cast doesn't.
---
Branch: https://github.com/tarantool/tarantool/tree/np/bool-to-int-implicit-cast

 src/box/sql/insert.c    | 14 +-------------
 test/sql/types.result   | 14 ++++++++++++++
 test/sql/types.test.lua |  7 +++++++
 3 files changed, 22 insertions(+), 13 deletions(-)

diff --git a/src/box/sql/insert.c b/src/box/sql/insert.c
index 1261ab9c8..2813415be 100644
--- a/src/box/sql/insert.c
+++ b/src/box/sql/insert.c
@@ -66,20 +66,8 @@ sql_emit_table_types(struct Vdbe *v, struct space_def *def, int reg)
 		(enum field_type *) sqlDbMallocZero(db, sz);
 	if (colls_type == NULL)
 		return;
-	for (uint32_t i = 0; i < field_count; ++i) {
+	for (uint32_t i = 0; i < field_count; ++i)
 		colls_type[i] = def->fields[i].type;
-		/*
-		 * Force INTEGER type to handle queries like:
-		 * CREATE TABLE t1 (id INT PRIMARY KEY);
-		 * INSERT INTO t1 VALUES (1.123);
-		 *
-		 * In this case 1.123 should be truncated to 1.
-		 */
-		if (colls_type[i] == FIELD_TYPE_INTEGER) {
-			sqlVdbeAddOp2(v, OP_Cast, reg + i,
-					  FIELD_TYPE_INTEGER);
-		}
-	}
 	colls_type[field_count] = field_type_MAX;
 	sqlVdbeAddOp4(v, OP_ApplyType, reg, field_count, 0,
 			  (char *)colls_type, P4_DYNAMIC);
diff --git a/test/sql/types.result b/test/sql/types.result
index bc4518c01..ebdff3fe2 100644
--- a/test/sql/types.result
+++ b/test/sql/types.result
@@ -860,6 +860,20 @@ box.space.T:drop()
 box.space.T1:drop()
 ---
 ...
+-- Make sure that BOOLEAN is not implicitly converted to INTEGER
+-- while inserted to PRIMARY KEY field.
+--
+box.execute("CREATE TABLE t1 (id INT PRIMARY KEY);")
+---
+- row_count: 1
+...
+box.execute("INSERT INTO t1 VALUES (true);")
+---
+- error: 'Type mismatch: can not convert true to integer'
+...
+box.space.T1:drop()
+---
+...
 --
 -- gh-4103: If resulting value of arithmetic operations is
 -- integers, then make sure its type also integer (not number).
diff --git a/test/sql/types.test.lua b/test/sql/types.test.lua
index c51660cb9..75e3dfd98 100644
--- a/test/sql/types.test.lua
+++ b/test/sql/types.test.lua
@@ -206,6 +206,13 @@ box.execute("SELECT s FROM t1 WHERE s IN (true, 1, 'abcd')")
 box.space.T:drop()
 box.space.T1:drop()
 
+-- Make sure that BOOLEAN is not implicitly converted to INTEGER
+-- while inserted to PRIMARY KEY field.
+--
+box.execute("CREATE TABLE t1 (id INT PRIMARY KEY);")
+box.execute("INSERT INTO t1 VALUES (true);")
+box.space.T1:drop()
+
 --
 -- gh-4103: If resulting value of arithmetic operations is
 -- integers, then make sure its type also integer (not number).
-- 
2.15.1





More information about the Tarantool-patches mailing list