From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 1F7756EC55; Tue, 27 Jul 2021 14:32:04 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 1F7756EC55 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1627385524; bh=MPoqWMnprITpBQCd3Y+Fw50Y47V3B/ZUfkgDSgs7q0w=; h=To:Cc:Date:In-Reply-To:References:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=aLR3EpTx/Bt+IMX/nsovQVQ3g4u91AnnrHZR43/r88Qw7YUjHu5mpbIAZD2f5IXUP 4G88wPvVZ/ZdEAGrVn2Ou0DOYJ+81OryLgc5LT+ue64NJfEvzU6UYEKqUHcR+mplvz d3zrcVcQIClFKSTzAQPZ5ZJYNHv0zJopTRMLxlqQ= Received: from smtpng2.i.mail.ru (smtpng2.i.mail.ru [94.100.179.3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 11DFD6EC56 for ; Tue, 27 Jul 2021 14:31:33 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 11DFD6EC56 Received: by smtpng2.m.smailru.net with esmtpa (envelope-from ) id 1m8LJ6-0005Sz-8L; Tue, 27 Jul 2021 14:31:32 +0300 To: v.shpilevoy@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Tue, 27 Jul 2021 14:31:32 +0300 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-4EC0790: 10 X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD941C43E597735A9C351B198F4576AC7B2770D7874BA03B4AE182A05F5380850406D429B869868AD96D484ABD407A265A412E9577E61DA26727B405B4D12530B47 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE74162CA13069782AFEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F790063700B087299B9BE63E8638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8D6F35367583BB198A694F5C82C3DF56C117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCF1175FABE1C0F9B6A471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352033AC447995A7AD18618001F51B5FD3F9D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B62CFFCC7B69C47339089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C7BEA09003D200E084340DAF266767D659BDA37B1A2D41ADA9C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EFA5DF9383870C0FED699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34FF70CCB59A6ED870F15B5F4D93B9F984B07F1320AF454DF5840D55393CE08D69FF34AC4AB03BD5381D7E09C32AA3244C706C9BDBE0A14C526D6F7AEB8945789A24AF4FAF06DA24FD729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojMEANdStWW58sNH+sFfOHnA== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D5515D7CF66B9FD81425A9FE02CF16DC483D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v2 1/3] sql: disallow explicit cast of BOOLEAN to number X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Mergen Imeev via Tarantool-patches Reply-To: imeevma@tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Thank you for the review! My answers, diff and new patch below. On 26.07.2021 23:11, Vladislav Shpilevoy wrote: > Thanks for the patch! > > See 2 comments below. > >> diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c >> index 6b95e41d3..5c44bfdfc 100644 >> --- a/src/box/sql/mem.c >> +++ b/src/box/sql/mem.c >> @@ -683,24 +674,23 @@ str_to_bool(struct Mem *mem) >> { >> assert(mem->type == MEM_TYPE_STR); >> char *str = mem->z; >> + uint32_t len = mem->n; >> bool b; >> const char *str_true = "TRUE"; >> const char *str_false = "FALSE"; >> uint32_t len_true = strlen(str_true); >> uint32_t len_false = strlen(str_false); >> >> - for (; str[0] == ' '; str++); >> - if (strncasecmp(str, str_true, len_true) == 0) { >> + for (; isspace(str[0]); str++, len--); >> + for (; isspace(str[len - 1]); len--); >> + if (len != len_true && len != len_false) >> + return -1; >> + >> + if (len == len_true && strncasecmp(str, str_true, len) == 0) >> b = true; >> - str += len_true; >> - } else if (strncasecmp(str, str_false, len_false) == 0) { >> + else if (len == len_false && strncasecmp(str, str_false, len) == 0) >> b = false; >> - str += len_false; >> - } else { >> - return -1; >> - } >> - for (; str[0] == ' '; str++); >> - if (str[0] != '\0') >> + else >> return -1; > > 1. Why did you change str_to_bool() if the patch is only about > numbers <-> bool? > Fixed, moved to new patch. Also, added a test. >> mem_set_bool(mem, b); >> return 0; >> @@ -1074,19 +1036,11 @@ mem_cast_explicit(struct Mem *mem, enum field_type type) >> case FIELD_TYPE_INTEGER: >> return mem_to_int(mem); >> case FIELD_TYPE_BOOLEAN: >> - switch (mem->type) { >> - case MEM_TYPE_BOOL: >> + if (mem->type == MEM_TYPE_BOOL) >> return 0; >> - case MEM_TYPE_INT: >> - case MEM_TYPE_UINT: >> - return int_to_bool(mem); >> - case MEM_TYPE_STR: >> + if (mem->type == MEM_TYPE_STR) >> return str_to_bool(mem); >> - case MEM_TYPE_DOUBLE: >> - return double_to_bool(mem); >> - default: >> - return -1; > > 2. I would propose to keep the switch-case. Otherwise you are > going to jump back and forth between if and switch when these > places will be changed again. Fixed. Diff: diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index ccd368170..aeaa01d94 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -28,8 +28,6 @@ * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ -#include - #include "sqlInt.h" #include "mem.h" #include "vdbeInt.h" @@ -674,23 +672,24 @@ str_to_bool(struct Mem *mem) { assert(mem->type == MEM_TYPE_STR); char *str = mem->z; - uint32_t len = mem->n; bool b; const char *str_true = "TRUE"; const char *str_false = "FALSE"; uint32_t len_true = strlen(str_true); uint32_t len_false = strlen(str_false); - for (; isspace(str[0]); str++, len--); - for (; isspace(str[len - 1]); len--); - if (len != len_true && len != len_false) - return -1; - - if (len == len_true && strncasecmp(str, str_true, len) == 0) + for (; str[0] == ' '; str++); + if (strncasecmp(str, str_true, len_true) == 0) { b = true; - else if (len == len_false && strncasecmp(str, str_false, len) == 0) + str += len_true; + } else if (strncasecmp(str, str_false, len_false) == 0) { b = false; - else + str += len_false; + } else { + return -1; + } + for (; str[0] == ' '; str++); + if (str[0] != '\0') return -1; mem_set_bool(mem, b); return 0; @@ -1036,11 +1035,14 @@ mem_cast_explicit(struct Mem *mem, enum field_type type) case FIELD_TYPE_INTEGER: return mem_to_int(mem); case FIELD_TYPE_BOOLEAN: - if (mem->type == MEM_TYPE_BOOL) + switch (mem->type) { + case MEM_TYPE_BOOL: return 0; - if (mem->type == MEM_TYPE_STR) + case MEM_TYPE_STR: return str_to_bool(mem); - return -1; + default: + return -1; + } case FIELD_TYPE_VARBINARY: if (mem->type == MEM_TYPE_STR) return str_to_bin(mem); New patch: commit dda7ad8134ef47d21006fdbc7cc5378de8220bd5 Author: Mergen Imeev Date: Fri Jul 16 11:28:39 2021 +0300 sql: disallow explicit cast of BOOLEAN to number This patch removes explicit cast of BOOLEAN values to numeric types and explicit cast of numeric values to BOOLEAN. Part of #4470 diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index fa80f6d5a..aeaa01d94 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -634,17 +634,6 @@ int_to_str0(struct Mem *mem) return mem_copy_str0(mem, str); } -static inline int -int_to_bool(struct Mem *mem) -{ - assert((mem->type & (MEM_TYPE_INT | MEM_TYPE_UINT)) != 0); - mem->u.b = mem->u.i != 0; - mem->type = MEM_TYPE_BOOL; - assert(mem->flags == 0); - mem->field_type = FIELD_TYPE_BOOLEAN; - return 0; -} - static inline int str_to_str0(struct Mem *mem) { @@ -870,28 +859,6 @@ double_to_str0(struct Mem *mem) return 0; } -static inline int -double_to_bool(struct Mem *mem) -{ - assert(mem->type == MEM_TYPE_DOUBLE); - mem->u.b = mem->u.r != 0.; - mem->type = MEM_TYPE_BOOL; - assert(mem->flags == 0); - mem->field_type = FIELD_TYPE_BOOLEAN; - return 0; -} - -static inline int -bool_to_int(struct Mem *mem) -{ - assert(mem->type == MEM_TYPE_BOOL); - mem->u.u = (uint64_t)mem->u.b; - mem->type = MEM_TYPE_UINT; - assert(mem->flags == 0); - mem->field_type = FIELD_TYPE_UNSIGNED; - return 0; -} - static inline int bool_to_str0(struct Mem *mem) { @@ -942,8 +909,6 @@ mem_to_int(struct Mem *mem) return bytes_to_int(mem); if (mem->type == MEM_TYPE_DOUBLE) return double_to_int(mem); - if (mem->type == MEM_TYPE_BOOL) - return bool_to_int(mem); return -1; } @@ -979,8 +944,6 @@ mem_to_number(struct Mem *mem) assert(mem->type < MEM_TYPE_INVALID); if (mem_is_num(mem)) return 0; - if (mem->type == MEM_TYPE_BOOL) - return bool_to_int(mem); if ((mem->type & (MEM_TYPE_STR | MEM_TYPE_BIN)) != 0) { if (bytes_to_int(mem) == 0) return 0; @@ -1062,8 +1025,6 @@ mem_cast_explicit(struct Mem *mem, enum field_type type) return bytes_to_uint(mem); case MEM_TYPE_DOUBLE: return double_to_int(mem); - case MEM_TYPE_BOOL: - return bool_to_int(mem); default: return -1; } @@ -1077,13 +1038,8 @@ mem_cast_explicit(struct Mem *mem, enum field_type type) switch (mem->type) { case MEM_TYPE_BOOL: return 0; - case MEM_TYPE_INT: - case MEM_TYPE_UINT: - return int_to_bool(mem); case MEM_TYPE_STR: return str_to_bool(mem); - case MEM_TYPE_DOUBLE: - return double_to_bool(mem); default: return -1; } diff --git a/test/sql-tap/cast.test.lua b/test/sql-tap/cast.test.lua index 8e7b6640e..3dc49c38e 100755 --- a/test/sql-tap/cast.test.lua +++ b/test/sql-tap/cast.test.lua @@ -1,6 +1,6 @@ #!/usr/bin/env tarantool local test = require("sqltester") -test:plan(79) +test:plan(91) --!./tcltestrunner.lua -- 2005 June 25 @@ -877,4 +877,104 @@ test:do_execsql_test( -- }) +-- gh-4470: Make explicit casts work according to our rules. + +-- Make sure that explicit cast from BOOLEAN to numeric types throws an error. +test:do_catchsql_test( + "cast-6.1.1", + [[ + SELECT CAST(TRUE AS UNSIGNED); + ]], { + 1, "Type mismatch: can not convert boolean(TRUE) to unsigned" + }) + +test:do_catchsql_test( + "cast-6.1.2", + [[ + SELECT CAST(FALSE AS UNSIGNED); + ]], { + 1, "Type mismatch: can not convert boolean(FALSE) to unsigned" + }) + +test:do_catchsql_test( + "cast-6.1.3", + [[ + SELECT CAST(TRUE AS INTEGER); + ]], { + 1, "Type mismatch: can not convert boolean(TRUE) to integer" + }) + +test:do_catchsql_test( + "cast-6.1.4", + [[ + SELECT CAST(FALSE AS INTEGER); + ]], { + 1, "Type mismatch: can not convert boolean(FALSE) to integer" + }) + +test:do_catchsql_test( + "cast-6.1.5", + [[ + SELECT CAST(TRUE AS DOUBLE); + ]], { + 1, "Type mismatch: can not convert boolean(TRUE) to double" + }) + +test:do_catchsql_test( + "cast-6.1.6", + [[ + SELECT CAST(FALSE AS DOUBLE); + ]], { + 1, "Type mismatch: can not convert boolean(FALSE) to double" + }) + +test:do_catchsql_test( + "cast-6.1.7", + [[ + SELECT CAST(TRUE AS NUMBER); + ]], { + 1, "Type mismatch: can not convert boolean(TRUE) to number" + }) + +test:do_catchsql_test( + "cast-6.1.8", + [[ + SELECT CAST(FALSE AS NUMBER); + ]], { + 1, "Type mismatch: can not convert boolean(FALSE) to number" + }) + +-- Make sure that explicit cast numeric value to BOOLEAN throws an error. +test:do_catchsql_test( + "cast-6.2.1", + [[ + SELECT CAST(0 AS BOOLEAN); + ]], { + 1, "Type mismatch: can not convert integer(0) to boolean" + }) + +test:do_catchsql_test( + "cast-6.2.2", + [[ + SELECT CAST(-1 AS BOOLEAN); + ]], { + 1, "Type mismatch: can not convert integer(-1) to boolean" + }) + +test:do_catchsql_test( + "cast-6.2.3", + [[ + SELECT CAST(1.5 AS BOOLEAN); + ]], { + 1, "Type mismatch: can not convert double(1.5) to boolean" + }) + +test:do_catchsql_test( + "cast-6.2.4", + [[ + SELECT CAST(CAST(1 AS NUMBER) AS BOOLEAN); + ]], { + 1, "Type mismatch: can not convert integer(1) to boolean" + }) + test:finish_test() diff --git a/test/sql-tap/cse.test.lua b/test/sql-tap/cse.test.lua index e09f955a0..665633aed 100755 --- a/test/sql-tap/cse.test.lua +++ b/test/sql-tap/cse.test.lua @@ -31,11 +31,11 @@ test:do_test( INSERT INTO t1 VALUES(2,21,22,23,24,25); ]] return test:execsql [[ - SELECT b, -b, ~b, NOT CAST(b AS BOOLEAN), NOT NOT CAST(b AS BOOLEAN), b-b, b+b, b*b, b/b, b FROM t1 + SELECT b, -b, ~b, b - b, b + b, b * b, b / b, b FROM t1; ]] end, { -- - 11, -11, -12, false, true, 0, 22, 121, 1, 11, 21, -21, -22, false, true, 0, 42, 441, 1, 21 + 11, -11, -12, 0, 22, 121, 1, 11, 21, -21, -22, 0, 42, 441, 1, 21 -- }) @@ -102,7 +102,7 @@ test:do_execsql_test( test:do_execsql_test( "cse-1.6.3", [[ - SELECT CASE WHEN CAST(b AS BOOLEAN) THEN d WHEN CAST(e AS BOOLEAN) THEN f ELSE 999 END, b, c, d FROM t1 + SELECT CASE WHEN b != 0 THEN d WHEN e != 0 THEN f ELSE 999 END, b, c, d FROM t1; ]], { -- 13, 11, 12, 13, 23, 21, 22, 23 @@ -112,7 +112,7 @@ test:do_execsql_test( test:do_execsql_test( "cse-1.6.4", [[ - SELECT b, c, d, CASE WHEN CAST(b AS BOOLEAN) THEN d WHEN CAST(e AS BOOLEAN) THEN f ELSE 999 END FROM t1 + SELECT b, c, d, CASE WHEN b != 0 THEN d WHEN e != 0 THEN f ELSE 999 END FROM t1; ]], { -- 11, 12, 13, 13, 21, 22, 23, 23 @@ -122,7 +122,7 @@ test:do_execsql_test( test:do_execsql_test( "cse-1.6.5", [[ - SELECT b, c, d, CASE WHEN false THEN d WHEN CAST(e AS BOOLEAN) THEN f ELSE 999 END FROM t1 + SELECT b, c, d, CASE WHEN false THEN d WHEN e != 0 THEN f ELSE 999 END FROM t1; ]], { -- 11, 12, 13, 15, 21, 22, 23, 25 @@ -132,10 +132,10 @@ test:do_execsql_test( test:do_execsql_test( "cse-1.7", [[ - SELECT a, -a, ~a, NOT CAST(a AS BOOLEAN), NOT NOT CAST(a AS BOOLEAN), a-a, a+a, a*a, a/a, a FROM t1 + SELECT a, -a, ~a, a - a, a + a, a * a, a / a, a FROM t1 ]], { -- - 1, -1 ,-2, false, true, 0, 2, 1, 1, 1, 2, -2, -3, false, true, 0, 4, 4, 1, 2 + 1, -1 ,-2, 0, 2, 1, 1, 1, 2, -2, -3, 0, 4, 4, 1, 2 -- }) @@ -152,10 +152,10 @@ test:do_execsql_test( test:do_execsql_test( "cse-1.9", [[ - SELECT NOT CAST(b AS BOOLEAN), ~b, NOT NOT CAST(b AS BOOLEAN), b FROM t1 + SELECT ~b, b FROM t1 ]], { -- - false, -12, true, 11, false, -22, true, 21 + -12, 11, -22, 21 -- }) diff --git a/test/sql-tap/e_select1.test.lua b/test/sql-tap/e_select1.test.lua index ab0faa376..8293965b8 100755 --- a/test/sql-tap/e_select1.test.lua +++ b/test/sql-tap/e_select1.test.lua @@ -910,7 +910,8 @@ test:do_select_tests( {"3", "SELECT sum(b+1) FROM z1 NATURAL LEFT JOIN z3", {-43.06}}, {"4", "SELECT sum(b+2) FROM z1 NATURAL LEFT JOIN z3", {-38.06}}, - {"5", "SELECT sum(CAST(b IS NOT NULL AS INTEGER)) FROM z1 NATURAL LEFT JOIN z3", {5}}, + {"5", "SELECT sum(CASE WHEN b IS NOT NULL THEN 1 ELSE 0 END) ".. + "FROM z1 NATURAL LEFT JOIN z3", {5}}, }) -- EVIDENCE-OF: R-26684-40576 Each non-aggregate expression in the diff --git a/test/sql-tap/in1.test.lua b/test/sql-tap/in1.test.lua index 4b51da6e8..bdf1f207a 100755 --- a/test/sql-tap/in1.test.lua +++ b/test/sql-tap/in1.test.lua @@ -100,10 +100,11 @@ test:do_execsql_test( test:do_execsql_test( "in-1.7", [[ - SELECT a+ 100*CAST((a BETWEEN 1 and 3) AS INTEGER) FROM t1 ORDER BY b + SELECT a, a BETWEEN 1 AND 3 FROM t1 ORDER BY b; ]], { -- - 101, 102, 103, 4, 5, 6, 7, 8, 9, 10 + 1, true, 2, true, 3, true, 4, false, 5, false, + 6, false, 7, false, 8, false, 9, false, 10, false -- }) @@ -157,10 +158,11 @@ test:do_execsql_test( test:do_execsql_test( "in-2.5", [[ - SELECT a+100*(CAST(b IN (8,16,24) AS INTEGER)) FROM t1 ORDER BY b + SELECT b, b IN (8,16,24) FROM t1 ORDER BY b; ]], { -- - 1, 2, 103, 104, 5, 6, 7, 8, 9, 10 + 2, false, 4, false, 8, true, 16, true, 32, false, 64, false, + 128, false, 256, false, 512, false, 1024, false -- }) @@ -207,7 +209,7 @@ test:do_execsql_test( test:do_execsql_test( "in-2.10", [[ - SELECT a FROM t1 WHERE LEAST(0, CAST(b IN (a,30) AS INT)) <> 0 + SELECT a, b FROM t1 WHERE LEAST(TRUE, b IN (a, 30)); ]], { -- @@ -253,10 +255,11 @@ test:do_execsql_test( test:do_execsql_test( "in-3.3", [[ - SELECT a + 100*(CAST(b IN (SELECT b FROM t1 WHERE a<5) AS INTEGER)) FROM t1 ORDER BY b + SELECT a, b, b IN (SELECT b FROM t1 WHERE a < 5) FROM t1 ORDER BY b; ]], { -- - 101, 102, 103, 104, 5, 6, 7, 8, 9, 10 + 1, 2, true, 2, 4, true, 3, 8, true, 4, 16, true, 5, 32, false, 6, 64, + false, 7, 128, false, 8, 256, false, 9, 512, false, 10, 1024, false -- }) diff --git a/test/sql-tap/misc3.test.lua b/test/sql-tap/misc3.test.lua index 313484b5d..9add3dd9e 100755 --- a/test/sql-tap/misc3.test.lua +++ b/test/sql-tap/misc3.test.lua @@ -510,7 +510,7 @@ test:do_execsql_test( test:do_execsql_test( "misc-8.2", [[ - SELECT count(*) FROM t3 WHERE 1+CAST((b IN ('abc','xyz')) AS INTEGER)==2 + SELECT count(*) FROM t3 WHERE b IN ('abc','xyz'); ]], { -- 2 diff --git a/test/sql/boolean.result b/test/sql/boolean.result index 5594d92cb..d54de8fe7 100644 --- a/test/sql/boolean.result +++ b/test/sql/boolean.result @@ -500,25 +500,25 @@ INSERT INTO t3 VALUES (4, false) | ... -- Check CAST from BOOLEAN to the other types. -SELECT cast(true AS INTEGER), cast(false AS INTEGER); +SELECT cast(true AS INTEGER); | --- - | - metadata: - | - name: COLUMN_1 - | type: integer - | - name: COLUMN_2 - | type: integer - | rows: - | - [1, 0] + | - null + | - 'Type mismatch: can not convert boolean(TRUE) to integer' | ... -SELECT cast(true AS NUMBER), cast(false AS NUMBER); +SELECT cast(false AS INTEGER); | --- - | - metadata: - | - name: COLUMN_1 - | type: number - | - name: COLUMN_2 - | type: number - | rows: - | - [1, 0] + | - null + | - 'Type mismatch: can not convert boolean(FALSE) to integer' + | ... +SELECT cast(true AS NUMBER); + | --- + | - null + | - 'Type mismatch: can not convert boolean(TRUE) to number' + | ... +SELECT cast(false AS NUMBER); + | --- + | - null + | - 'Type mismatch: can not convert boolean(FALSE) to number' | ... -- gh-4462: ensure that text representation is uppercase. SELECT cast(true AS TEXT), cast(false AS TEXT); @@ -543,27 +543,30 @@ SELECT cast(true AS BOOLEAN), cast(false AS BOOLEAN); | ... -- Check CAST to BOOLEAN from the other types. -SELECT cast(100 AS BOOLEAN), cast(1 AS BOOLEAN), cast(0 AS BOOLEAN); +SELECT cast(100 AS BOOLEAN); | --- - | - metadata: - | - name: COLUMN_1 - | type: boolean - | - name: COLUMN_2 - | type: boolean - | - name: COLUMN_3 - | type: boolean - | rows: - | - [true, true, false] + | - null + | - 'Type mismatch: can not convert integer(100) to boolean' | ... -SELECT cast(0.123 AS BOOLEAN), cast(0.0 AS BOOLEAN); +SELECT cast(1 AS BOOLEAN); | --- - | - metadata: - | - name: COLUMN_1 - | type: boolean - | - name: COLUMN_2 - | type: boolean - | rows: - | - [true, false] + | - null + | - 'Type mismatch: can not convert integer(1) to boolean' + | ... +SELECT cast(0 AS BOOLEAN); + | --- + | - null + | - 'Type mismatch: can not convert integer(0) to boolean' + | ... +SELECT cast(0.123 AS BOOLEAN); + | --- + | - null + | - 'Type mismatch: can not convert double(0.123) to boolean' + | ... +SELECT cast(0.0 AS BOOLEAN); + | --- + | - null + | - 'Type mismatch: can not convert double(0.0) to boolean' | ... SELECT cast('true' AS BOOLEAN), cast('false' AS BOOLEAN); | --- diff --git a/test/sql/boolean.test.sql b/test/sql/boolean.test.sql index 65a830349..13991ca0c 100644 --- a/test/sql/boolean.test.sql +++ b/test/sql/boolean.test.sql @@ -127,15 +127,20 @@ INSERT INTO t3 VALUES (3, true) INSERT INTO t3 VALUES (4, false) -- Check CAST from BOOLEAN to the other types. -SELECT cast(true AS INTEGER), cast(false AS INTEGER); -SELECT cast(true AS NUMBER), cast(false AS NUMBER); +SELECT cast(true AS INTEGER); +SELECT cast(false AS INTEGER); +SELECT cast(true AS NUMBER); +SELECT cast(false AS NUMBER); -- gh-4462: ensure that text representation is uppercase. SELECT cast(true AS TEXT), cast(false AS TEXT); SELECT cast(true AS BOOLEAN), cast(false AS BOOLEAN); -- Check CAST to BOOLEAN from the other types. -SELECT cast(100 AS BOOLEAN), cast(1 AS BOOLEAN), cast(0 AS BOOLEAN); -SELECT cast(0.123 AS BOOLEAN), cast(0.0 AS BOOLEAN); +SELECT cast(100 AS BOOLEAN); +SELECT cast(1 AS BOOLEAN); +SELECT cast(0 AS BOOLEAN); +SELECT cast(0.123 AS BOOLEAN); +SELECT cast(0.0 AS BOOLEAN); SELECT cast('true' AS BOOLEAN), cast('false' AS BOOLEAN); SELECT cast('TRUE' AS BOOLEAN), cast('FALSE' AS BOOLEAN); diff --git a/test/sql/types.result b/test/sql/types.result index a0b8668be..f8e9781f2 100644 --- a/test/sql/types.result +++ b/test/sql/types.result @@ -1035,11 +1035,8 @@ box.execute("SELECT CAST(18446744073709551615 AS SCALAR);") ... box.execute("SELECT CAST(18446744073709551615 AS BOOLEAN);") --- -- metadata: - - name: COLUMN_1 - type: boolean - rows: - - [true] +- null +- 'Type mismatch: can not convert integer(18446744073709551615) to boolean' ... box.execute("SELECT CAST('18446744073709551615' AS INTEGER);") --- @@ -1105,11 +1102,8 @@ box.execute("SELECT CAST(-1.5 AS UNSIGNED);") ... box.execute("SELECT CAST(true AS UNSIGNED);") --- -- metadata: - - name: COLUMN_1 - type: unsigned - rows: - - [1] +- null +- 'Type mismatch: can not convert boolean(TRUE) to unsigned' ... box.execute("SELECT CAST('123' AS UNSIGNED);") ---