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 76C2D6EC56; Mon, 4 Oct 2021 16:31:21 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 76C2D6EC56 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1633354281; bh=lJ7VsG8CLpz8Ty+FHWlXjeO2oVoAPUIATjwdBT+OrRU=; 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=wlflvtIrMCzi8taaq34vzgZ2ry1UZNk/bs802BFaWfd2019NrRibUh9zlbkNinDXs iKzJW3EuZ7DwVpnTm7TIzx/eBFznqEvDPzC0W7sQubgULUfUL+w6+ta+lh0Cejm8s6 mm1HwITQHOniqq2J3u6rqfK0a5RohVg3sGTpcML4= 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 252966DB07 for ; Mon, 4 Oct 2021 16:30:22 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 252966DB07 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1mXO2v-0004FS-HZ; Mon, 04 Oct 2021 16:30:21 +0300 To: v.shpilevoy@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Mon, 4 Oct 2021 16:30:21 +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: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD96A58C36AA2E99649BF631F26B0465AFD66E223DEB3A228DC182A05F5380850408EFD0AD4487DDD5BF2C97479543BEEB9999AD0C367C5B2192A068038AE5DE123 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE75644E22E05AA81AEB287FD4696A6DC2FA8DF7F3B2552694A4E2F5AFA99E116B42401471946AA11AF23F8577A6DFFEA7C5C69BD61D975C73F8F08D7030A58E5AD1A62830130A00468AEEEE3FBA3A834EE7353EFBB5533756680CE368667DD8FDE2CC25A3B054474457248D9AD48CB70FAA471835C12D1D9774AD6D5ED66289B5278DA827A17800CE70F3DDF2BBF19B93A9FA2833FD35BB23D2EF20D2F80756B5F868A13BD56FB6657A471835C12D1D977725E5C173C3A84C37EF884183F8E4D67117882F4460429728AD0CFFFB425014E868A13BD56FB6657E2021AF6380DFAD1A18204E546F3947CB11811A4A51E3B096D1867E19FE1407959CC434672EE6371089D37D7C0E48F6C8AA50765F79006377AA2284B41911753EFF80C71ABB335746BA297DBC24807EABDAD6C7F3747799A X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C80CE368667DD8FDE2CC25A3B054474457248D9AD48CB70FA9C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EFE323A604FE95ED09DC48ACC2A39D04F89CDFB48F4795C241BDAD6C7F3747799A X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34AF61ACC197BF517AED78002901E8F829B550A7EC3A11A438825AD5E08892FC2A65B8A0DB3D19D8981D7E09C32AA3244CEF7E029D91DEADB9139960192EED5A5663871F383B54D9B3729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj+1ww+tpaZepfwscrIqShYw== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D4B486B02B730ACB543D8264C9FBB6C6C83D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v1 2/3] sql: fix cast of small negative DECIMAL to INTEGER 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" This patch fixes an assertion when casting DECIMAL value less than 0 and greater than -1 to INTEGER. Part of #6485 --- src/box/sql/mem.c | 9 +++---- test/sql-tap/gh-6485-bugs-in-decimal.test.lua | 24 ++++++++++++++++++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index b0eba303e..bc6f9b35a 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -1106,9 +1106,9 @@ dec_to_int(struct Mem *mem) int64_t i; if (decimal_to_int64(&mem->u.d, &i) == NULL) return -1; - assert(i < 0); + assert(i <= 0); mem->u.i = i; - mem->type = MEM_TYPE_INT; + mem->type = i == 0 ? MEM_TYPE_UINT : MEM_TYPE_INT; mem->flags = 0; return 0; } @@ -1137,14 +1137,15 @@ dec_to_int_forced(struct Mem *mem) bool is_dec_int = decimal_is_int(&mem->u.d); if (decimal_is_neg(&mem->u.d)) { int64_t i; - mem->type = MEM_TYPE_INT; mem->flags = 0; if (decimal_to_int64(&mem->u.d, &i) == NULL) { mem->u.i = INT64_MIN; + mem->type = MEM_TYPE_INT; return -1; } - assert(i < 0); + assert(i <= 0); mem->u.i = i; + mem->type = i == 0 ? MEM_TYPE_UINT : MEM_TYPE_INT; /* * Decimal is floored when cast to int, which means that after * cast it becomes bigger if it was not integer. diff --git a/test/sql-tap/gh-6485-bugs-in-decimal.test.lua b/test/sql-tap/gh-6485-bugs-in-decimal.test.lua index 3f63f2b76..0b9b2ea0a 100755 --- a/test/sql-tap/gh-6485-bugs-in-decimal.test.lua +++ b/test/sql-tap/gh-6485-bugs-in-decimal.test.lua @@ -1,6 +1,6 @@ #!/usr/bin/env tarantool local test = require("sqltester") -test:plan(1) +test:plan(3) -- Make sure DECIMAL is not truncated when used in an index. test:do_execsql_test( @@ -13,4 +13,26 @@ test:do_execsql_test( ]], { }) +-- +-- Make sure that DECIMAL greater than -1 and less than 0 are correctly cast to +-- INTEGER. +-- +test:do_execsql_test( + "gh-6485-2", + [[ + SELECT CAST(CAST(-0.5 AS DECIMAL) AS INTEGER); + ]], { + 0 + }) + +test:do_execsql_test( + "gh-6485-3", + [[ + CREATE TABLE t(i INTEGER PRIMARY KEY); + INSERT INTO t VALUES(1); + SELECT i FROM t WHERE i IN (CAST(-0.1 AS DECIMAL), CAST(2 AS DECIMAL)); + DROP TABLE t; + ]], { + }) + test:finish_test() -- 2.25.1