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 B663B6EC57; Tue, 13 Jul 2021 10:04:21 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org B663B6EC57 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1626159861; bh=NBVjSG9cSYfo23x7ceQaZ7vdQ7tsHhGrSjQPtacXg3w=; 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=fPCI806dyiC0uFArsmIBpjir1ERkrfx+y9E/qaHcVtJ7Hi5ZBZRpQtbbj/zKZ/OqL 913yexLAt40dHgUwPVC8rGjS9z+rglxH5N1lzIQ3wTBYUrEQq8zaXnJfG319I8drFJ CdVuZ98bBzoJPv31eR5hFJrfOlmg6Xb9Z9SPUreM= 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 10BF66EC57 for ; Tue, 13 Jul 2021 10:03:55 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 10BF66EC57 Received: by smtpng2.m.smailru.net with esmtpa (envelope-from ) id 1m3CSQ-00052d-Ap; Tue, 13 Jul 2021 10:03:54 +0300 To: tsafin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Tue, 13 Jul 2021 10:03:54 +0300 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD97BB0EF39AD2B33D5391930EFB6A35F20EC172CEE85ECBD22182A05F53808504031E4910FED595928A409B62CBFACFF4E749B970F6E706217DEC47557439C6953 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7364F8074C6DFACE2C2099A533E45F2D0395957E7521B51C2CFCAF695D4D8E9FCEA1F7E6F0F101C6778DA827A17800CE7A609AE8E79ADB41CEA1F7E6F0F101C6723150C8DA25C47586E58E00D9D99D84E1BDDB23E98D2D38BBCA57AF85F7723F2D8E6C640D8C78B8F669C89F20E25AFC2CC7F00164DA146DAFE8445B8C89999728AA50765F7900637D0FEED2715E18529389733CBF5DBD5E9C8A9BA7A39EFB766F5D81C698A659EA7CC7F00164DA146DA9985D098DBDEAEC821E93C0F2A571C7BF6B57BC7E6449061A352F6E88A58FB86F5D81C698A659EA7E827F84554CEF5019E625A9149C048EE9ECD01F8117BC8BEE2021AF6380DFAD18AA50765F790063735872C767BF85DA227C277FBC8AE2E8B953A8A48A05D51F175ECD9A6C639B01B4E70A05D1297E1BBCB5012B2E24CD356 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975CE68746B1F2AB10C6185CD8995E7470C3BF9F736B6EC327579C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF309DFB797F6729CB699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34FA5A4D623E7343BBBA35B27B9B7637C75D827469E3F12C17EE3E304AEDD59431D1C3809A13B6F7681D7E09C32AA3244C891C3B334A79FC5E695289B5734026028894E9C85370243E729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojAZDAgpmGsvb0cggvKGqGYg== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5DEEC57A25FBDDC7FC8B1254141BD983F283D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v2 1/4] sql: truncate values in type mismatch error 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" STRING, MAP, and ARRAY values that are too long can make the type mismatch error description less descriptive than necessary. This patch truncates values that are too long and adds "..." to indicate that the value has been truncated. Part of #6176 --- src/box/sql/mem.c | 18 ++++--- src/box/sql/mem.h | 3 +- ...-4766-wrong-cast-from-blob-to-int.test.lua | 7 +-- test/sql-tap/sql-errors.test.lua | 47 ++++++++++++++++++- test/sql/types.result | 6 +-- 5 files changed, 62 insertions(+), 19 deletions(-) diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index 2595e2fd4..05f053c55 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -57,6 +57,7 @@ sqlVdbeMemGrow(struct Mem *pMem, int n, int preserve); enum { BUF_SIZE = 32, + STR_VALUE_MAX_LEN = 128, }; bool @@ -72,26 +73,31 @@ mem_is_field_compatible(const struct Mem *mem, enum field_type type) const char * mem_str(const struct Mem *mem) { - char buf[BUF_SIZE]; + char buf[STR_VALUE_MAX_LEN]; switch (mem->type) { case MEM_TYPE_NULL: return "NULL"; case MEM_TYPE_STR: - if ((mem->flags & MEM_Term) != 0) - return mem->z; + if (mem->n > STR_VALUE_MAX_LEN) + return tt_sprintf("%.*s...", STR_VALUE_MAX_LEN, mem->z); return tt_cstr(mem->z, mem->n); case MEM_TYPE_INT: return tt_sprintf("%lld", mem->u.i); case MEM_TYPE_UINT: return tt_sprintf("%llu", mem->u.u); case MEM_TYPE_DOUBLE: - sql_snprintf(BUF_SIZE, &buf[0], "%!.15g", mem->u.r); + sql_snprintf(STR_VALUE_MAX_LEN, buf, "%!.15g", mem->u.r); return tt_sprintf("%s", buf); case MEM_TYPE_BIN: return "varbinary"; case MEM_TYPE_MAP: - case MEM_TYPE_ARRAY: - return mp_str(mem->z); + case MEM_TYPE_ARRAY: { + const char *str = mp_str(mem->z); + if (strlen(str) <= STR_VALUE_MAX_LEN) + return str; + memcpy(buf, str, STR_VALUE_MAX_LEN); + return tt_sprintf("%.*s...", STR_VALUE_MAX_LEN, buf); + } case MEM_TYPE_UUID: return tt_uuid_str(&mem->u.uuid); case MEM_TYPE_BOOL: diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h index b3cd5c545..0bbc63e68 100644 --- a/src/box/sql/mem.h +++ b/src/box/sql/mem.h @@ -264,7 +264,8 @@ mem_is_field_compatible(const struct Mem *mem, enum field_type type); /** * Return a string that represent content of MEM. String is either allocated - * using static_alloc() of just a static variable. + * using static_alloc() of just a static variable. This function should only be + * used for debugging or displaying MEM values in errors. */ const char * mem_str(const struct Mem *mem); diff --git a/test/sql-tap/gh-4766-wrong-cast-from-blob-to-int.test.lua b/test/sql-tap/gh-4766-wrong-cast-from-blob-to-int.test.lua index da8c2dcd6..28cf50184 100755 --- a/test/sql-tap/gh-4766-wrong-cast-from-blob-to-int.test.lua +++ b/test/sql-tap/gh-4766-wrong-cast-from-blob-to-int.test.lua @@ -34,12 +34,7 @@ test:do_catchsql_test( "SELECT CAST('" .. long_str .. "1234'" .. " AS INTEGER);", { 1, "Type mismatch: can not convert 000000000000000000000000000000000" .. "0000000000000000000000000000000000000000000000000000000000000000000" .. - "0000000000000000000000000000000000000000000000000000000000000000000" .. - "0000000000000000000000000000000000000000000000000000000000000000000" .. - "0000000000000000000000000000000000000000000000000000000000000000000" .. - "0000000000000000000000000000000000000000000000000000000000000000000" .. - "0000000000000000000000000000000000000000000000000000000000000000000" .. - "000000000000000000000000000000000000000000000" + "0000000000000000000000000000... to integer" }) test:finish_test() diff --git a/test/sql-tap/sql-errors.test.lua b/test/sql-tap/sql-errors.test.lua index 469193a2b..96ee683eb 100755 --- a/test/sql-tap/sql-errors.test.lua +++ b/test/sql-tap/sql-errors.test.lua @@ -1,6 +1,6 @@ #!/usr/bin/env tarantool local test = require("sqltester") -test:plan(72) +test:plan(76) test:execsql([[ CREATE TABLE t0 (i INT PRIMARY KEY, a INT); @@ -780,4 +780,49 @@ test:do_catchsql_test( -- }) +-- +-- gh-6176: Make sure that type mismatch error description with too long STRING, +-- MAP or ARRAY is printed correctly. +-- +local str1 = string.rep('a', 200) +local str2 = string.rep('ы', 200) + +test:do_catchsql_test( + "sql-errors-3.1", + "SELECT CAST('"..str1.."'AS UNSIGNED);", { + 1, "Type mismatch: can not convert aaaaaaaaaaaaaaaaaaaaaaaaaa".. + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".. + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa... to unsigned" + }) + +test:do_catchsql_test( + "sql-errors-3.2", + "SELECT CAST('"..str2.."'AS UNSIGNED);", { + 1, "Type mismatch: can not convert ыыыыыыыыыыыыыыыыыыыыыыыыыы".. + "ыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыыы... to unsigned" + }) + +local format = {{'I', 'integer'}, {'A', 'array'}, {'M', 'map'}} +local s = box.schema.space.create('TEST', {format=format}) +s:create_index('I') +s:insert({1, {str1}, {a = 1, b = str1}}) + +test:do_catchsql_test( + "sql-errors-3.3", + "SELECT CAST(a AS UNSIGNED) from test;", { + 1, 'Type mismatch: can not convert ["aaaaaaaaaaaaaaaaaaaaaaaa'.. + 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'.. + 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa... to unsigned' + }) + +test:do_catchsql_test( + "sql-errors-3.4", + "SELECT CAST(m AS UNSIGNED) from test;", { + 1, 'Type mismatch: can not convert {"a": 1, "b": "aaaaaaaaaaa'.. + 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'.. + 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa... to unsigned' + }) + +test:execsql('DROP TABLE test;') + test:finish_test() diff --git a/test/sql/types.result b/test/sql/types.result index 687ca3b15..a46967158 100644 --- a/test/sql/types.result +++ b/test/sql/types.result @@ -1651,11 +1651,7 @@ box.execute('INSERT INTO t1(a) SELECT a FROM t2;') - null - 'Type mismatch: can not convert [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, - 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, - 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, - 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, - 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, - 112, 113, 114, 115, 116, 117, 11' + ... to scalar' ... s:drop() --- -- 2.25.1