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 E68846E465; Fri, 8 Oct 2021 20:36:42 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org E68846E465 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1633714603; bh=7+40oszs2cGXxbiwBiOvcamtWpLId5Bi2DLdO0lWAj8=; 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=tugoKUCnjp8m5+8Bz9XEwVEqk9z/ufQmz5t2M91UZq795wL+JHUSJrYOFjntPzH7p 8P0IFGLzvhUlS7v4Ylm+fOXz9pDBs3dn7/OyEK0kqBM2z4GyMyazXsmMdJpjfT++ek Eq+pyvzf/caDUABGRV18i0uboRWopR2+HQRJjWPs= 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 4D3486E468 for ; Fri, 8 Oct 2021 20:31:51 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 4D3486E468 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1mYtio-000848-OU; Fri, 08 Oct 2021 20:31:51 +0300 To: v.shpilevoy@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Fri, 8 Oct 2021 20:31:50 +0300 Message-Id: <3be9607fa7ee20416b445f186c0c3eba11b6e780.1633713432.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: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD9A6D4E3B1981C4C7DC8C09104FADB757A98AD8127042C5321182A05F538085040476BF2BA2558A43443DA17815E43DEEA4C4DC013C7DFADF7A68C2C72A8F3B8A5 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE792C68BF9CD4C0E9EEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006379FED03E15F715FAA8638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D82728D4A7F2A378627E4637FE3684BC72117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC3733B5EC72352B9FA471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F4460429728776938767073520140C956E756FBB7A2CC0D3CB04F14752D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B62CFFCC7B69C47339089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C650BD2383FF25EF57FAE1AACCF995958B7BF54696E8223AD9C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF520746E264B47742699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34B3611847B8BC2D0BD6A9B61D9C5EB71F9BA10371B15265C8EDDEA83A53AB23ECB10A223BC189037A1D7E09C32AA3244C842C549A6CF6759BDAF47FD1CB4F510E408A6A02710B7304729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojMZ06aokA6brE/LF7TXzY5Q== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D0046B5D247FED31093227BA8FC6B99BD83D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B 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 b1c96ad81..65da47da1 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -943,6 +943,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) { @@ -990,51 +1016,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 @@ -1866,9 +1847,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