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 A77346EC42; Mon, 5 Jul 2021 18:28:19 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org A77346EC42 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1625498899; bh=NpSawIsxY4NGLCxYyiz8QiK9JFS2KczbmVB6O4pDDv4=; 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=O+G0txbiA39/KN4EI4V3m7osVfCgSDE3s0nyqC1kaJxA/g1kWJhb1r5TS4y1NOO2E m+jhIm5ZTs6aclxgFjk8euzNDyw07yz2ALYzyKAkp98S4xV4vROevGCNK0dqYGtVNA q9toztm6mo6TeQO70TNZaK7WhSOkeITN/PHtf1LQ= Received: from smtpng1.i.mail.ru (smtpng1.i.mail.ru [94.100.181.251]) (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 6BDA56EC42 for ; Mon, 5 Jul 2021 18:27:51 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 6BDA56EC42 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1m0QVi-0007FQ-Oq; Mon, 05 Jul 2021 18:27:51 +0300 To: v.shpilevoy@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Mon, 5 Jul 2021 18:27:50 +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: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD954DFF1DC42D673FB4F75AC5594ACDC16869A51A860A12816182A05F538085040F83C8A6937B77BE4CC24ED985C845808682D1CF6B19C3BEBD9164159F3E87CD3 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE74D0D2DEF2EB846B0EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637911538129B0A8D078638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D88EC19906759667387D4A796614BA41A6117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC8C7ADC89C2F0B2A5A471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F44604297287769387670735201E561CDFBCA1751FF6B57BC7E6449061A352F6E88A58FB86F5D81C698A659EA7E827F84554CEF5019E625A9149C048EE9ECD01F8117BC8BEE2021AF6380DFAD18AA50765F790063735872C767BF85DA227C277FBC8AE2E8BDC0F6C5B2EEF3D0C75ECD9A6C639B01B4E70A05D1297E1BBCB5012B2E24CD356 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C62E2A2CC9B321AEE2D078AAE35FEDA590B4038D06C3AE5E59C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EFB1D0AC14175A0720699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D341E2D05735FCBECD156C87A54982526BBEC0F127DD95A3FFF80403A76BD6257DEBCCF72D4B385C4CF1D7E09C32AA3244CC77AEA3D45DC8F0DC3B7C8127934545C435BF7150578642F729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj5fH2RN9TpJmbrVVh/PqWFw== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D6541D7041B875D89CC3D109AE4306B7983D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B 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 | 22 ++++++--- 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, 66 insertions(+), 19 deletions(-) diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index 6f3bf52e5..630f1a135 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -56,6 +56,7 @@ sqlVdbeMemGrow(struct Mem *pMem, int n, int preserve); enum { BUF_SIZE = 32, + STR_VALUE_MAX_LEN = 128, }; bool @@ -71,26 +72,35 @@ 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 + 1]; 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) { + memcpy(buf, mem->z, STR_VALUE_MAX_LEN); + buf[STR_VALUE_MAX_LEN] = '\0'; + return tt_sprintf("%s...", buf); + } 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 + 1, 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); + buf[STR_VALUE_MAX_LEN] = '\0'; + return tt_sprintf("%s...", 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