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 2E01A6CE3F; Thu, 11 Nov 2021 13:55:06 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 2E01A6CE3F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1636628106; bh=1vJ2fBGwHfKA+okLqAKCkx9M7A98kTF+bN22mRAUWDM=; 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=ViMHT2WkCyDdVHdbwhkAN/tOsXJKzhZVzwFArABaNEn3OYvQNLDuVaOpbhXGi3JON Bl8LDrYlbkl/lwIXUHIZ2cZaRW/7V9QypH6Lqhhd2+UoHMckU3QfFn7SLkLacheJkk 7NVOHWIps/Y6jR0ooRgTxwrsEaq0PO2YSjkhYqg4= 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 D1C6871069 for ; Thu, 11 Nov 2021 13:49:08 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org D1C6871069 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1ml7dk-000523-1S; Thu, 11 Nov 2021 13:49:08 +0300 To: kyukhin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Thu, 11 Nov 2021 13:49:07 +0300 Message-Id: <917e5e387b2f833ab32dc966f6a7f92889951a2c.1636627579.git.imeevma@gmail.com> 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: 4F1203BC0FB41BD9731B3922EC063979EC8F5BB1F79852547911E65F5173D5CB00894C459B0CD1B96FCED265D6669E32E03DAE67086077EFBD7B9CED87D255673079A20884B034D2 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE70CB15FA6C489297DEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637BC9F889FE9D346E88638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8CDA7965F323825A8836A9F33579F66B0117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC55D5BE2F85BDEC5FA471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F44604297287769387670735209647ADFADE5905B1618001F51B5FD3F9D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B66F6A3E018CF4DC80089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C7E9FEBB9C11794A13912ED3B3BC570DD08DAD22B4FF497F49C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EFCDE59DDCCE53AF2B699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34D2AC226BFD4557246DB7388210B1FE564C3829FF947A8FEA748AB3895395E4F8365C92625827B0B21D7E09C32AA3244CED1FC18BF9F98F547E598B02967B7EEDFE8DA44ABE2443F7729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj4t8MBgWr8bJLGwH4JSl4VA== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5DB9A565CD6B7CF19370F416B190D8030C83D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v1 10/21] sql: refactor ROUND() function 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" Part of #4145 --- src/box/sql/func.c | 75 +++++++++++++++++----------------------------- 1 file changed, 28 insertions(+), 47 deletions(-) diff --git a/src/box/sql/func.c b/src/box/sql/func.c index 3a788b450..c5428d87b 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -962,6 +962,32 @@ func_typeof(struct sql_context *ctx, int argc, struct Mem *argv) return mem_set_str0_static(ctx->pOut, mem_type_to_str(&argv[0])); } +/** Implementation of the ROUND() function. */ +static void +func_round(struct sql_context *ctx, int argc, struct Mem *argv) +{ + assert(argc == 1 || argc == 2); + if (mem_is_null(&argv[0]) || (argc == 2 && mem_is_null(&argv[1]))) + return; + assert(mem_is_double(&argv[0])); + assert(argc == 1 || mem_is_int(&argv[1])); + uint64_t n = (argc == 2 && mem_is_uint(&argv[1])) ? argv[1].u.u : 0; + + double d = argv[0].u.r; + struct Mem *res = ctx->pOut; + if (n != 0) + return mem_set_double(res, atof(tt_sprintf("%.*f", n, d))); + /* + * DOUBLE values greater than 2^53 or less than -2^53 have no digits + * after the decimal point. + */ + assert(9007199254740992 == (int64_t)1 << 53); + if (d <= -9007199254740992.0 || d >= 9007199254740992.0) + return mem_set_double(res, d); + double delta = d < 0 ? -0.5 : 0.5; + return mem_set_double(res, (double)(int64_t)(d + delta)); +} + static const unsigned char * mem_as_ustr(struct Mem *mem) { @@ -1009,51 +1035,6 @@ sql_func_uuid(struct sql_context *ctx, int argc, struct Mem *argv) mem_set_uuid(ctx->pOut, &uuid); } -/* - * Implementation of the round() function - */ -static void -roundFunc(struct sql_context *context, int argc, struct Mem *argv) -{ - int64_t n = 0; - double r; - if (argc != 1 && argc != 2) { - diag_set(ClientError, ER_FUNC_WRONG_ARG_COUNT, "ROUND", - "1 or 2", argc); - context->is_aborted = true; - return; - } - if (argc == 2) { - if (mem_is_null(&argv[1])) - return; - n = mem_get_int_unsafe(&argv[1]); - if (n < 0) - n = 0; - } - if (mem_is_null(&argv[0])) - return; - if (!mem_is_num(&argv[0]) && !mem_is_str(&argv[0])) { - diag_set(ClientError, ER_SQL_TYPE_MISMATCH, - mem_str(&argv[0]), "number"); - context->is_aborted = true; - return; - } - r = mem_get_double_unsafe(&argv[0]); - /* If Y==0 and X will fit in a 64-bit int, - * handle the rounding directly, - * otherwise use printf. - */ - if (n == 0 && r >= 0 && r < (double)(LARGEST_INT64 - 1)) { - r = (double)((sql_int64) (r + 0.5)); - } else if (n == 0 && r < 0 && (-r) < (double)(LARGEST_INT64 - 1)) { - r = -(double)((sql_int64) ((-r) + 0.5)); - } else { - const char *rounded_value = tt_sprintf("%.*f", n, r); - sqlAtoF(rounded_value, &r, sqlStrlen30(rounded_value)); - } - sql_result_double(context, r); -} - /* * Allocate nByte bytes of space using sqlMalloc(). If the * allocation fails, return NULL. If nByte is larger than the @@ -1885,9 +1866,9 @@ static struct sql_func_definition definitions[] = { {"REPLACE", 3, {FIELD_TYPE_VARBINARY, FIELD_TYPE_VARBINARY, FIELD_TYPE_VARBINARY}, FIELD_TYPE_VARBINARY, replaceFunc, NULL}, - {"ROUND", 1, {FIELD_TYPE_DOUBLE}, FIELD_TYPE_DOUBLE, roundFunc, NULL}, + {"ROUND", 1, {FIELD_TYPE_DOUBLE}, FIELD_TYPE_DOUBLE, func_round, NULL}, {"ROUND", 2, {FIELD_TYPE_DOUBLE, FIELD_TYPE_INTEGER}, FIELD_TYPE_DOUBLE, - roundFunc, NULL}, + func_round, NULL}, {"ROW_COUNT", 0, {}, FIELD_TYPE_INTEGER, sql_row_count, NULL}, {"SOUNDEX", 1, {FIELD_TYPE_STRING}, FIELD_TYPE_STRING, soundexFunc, NULL}, -- 2.25.1