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 6128C6DB02; Tue, 5 Oct 2021 12:48:09 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 6128C6DB02 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1633427289; bh=uz+gfC3IjYVWa7Jr+7SeRnqHe6GmKD3+IQlNqN5HfKY=; h=Date:To:Cc:References:In-Reply-To:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=hcuV+YTbzaizuL3+GmNc7JbpA66x902eyqIMQI97T4WfAEeX8R4gPn9xwTuv2RIAy qi0MNJZH/J2Ej41VmI43xV5DuagoRUImyFMVb3Fxv60cRW52DgsBuYuaP3w9WsJAzt Z3tSLYErRKwwBXFHaNQxJFOkv/5WdFJefnzId+9M= 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 6F0756DB02 for ; Tue, 5 Oct 2021 12:48:08 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 6F0756DB02 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1mXh3P-0004sC-L3; Tue, 05 Oct 2021 12:48:08 +0300 Date: Tue, 5 Oct 2021 12:48:06 +0300 To: Vladislav Shpilevoy Cc: tarantool-patches@dev.tarantool.org Message-ID: <20211005094806.GE55311@tarantool.org> References: <3543f4417e240c74d1dea9a2b6e086aeca950167.1633092363.git.imeevma@gmail.com> <6dfd69ff-a807-b0d5-4896-4b5118ee2679@tarantool.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <6dfd69ff-a807-b0d5-4896-4b5118ee2679@tarantool.org> X-4EC0790: 10 X-7564579A: EEAE043A70213CC8 X-77F55803: 4F1203BC0FB41BD9064ADF4728AA0EE98ADA9845FA0A29B03C9FFB0F24678598182A05F5380850400C16D54E0F4E749CE84690CF01D994A53370C4D2C1B67952F0BE42374426952D X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE72777B9F191503624EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006375E7A1B5661595F038638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8B0C05955BACA69EECACF6F1C965FD843117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCC908CD1B87A134A2A471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F4460429728776938767073520140C956E756FBB7AF04B652EEC242312D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B66F6A3E018CF4DC80089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: 0D63561A33F958A5C6553BEEA697BC0A2EB50CC28CBBCEDD519F7F117D3924A7D59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA75C4D20244F7083972410CA545F18667F91A7EA1CDA0B5A7A0 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34411AA3F52B5778B6893E2557F82FEC01FCE658DD60F7888D828CB57868171FEC49D30CE19E08026D1D7E09C32AA3244CEC84F625C29C7CA542FD004A01558C6A6C24832127668422729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojhAh8SZXECpCV6E8q9NWiyw== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D193026C94EC8DAED254DB1CABBAFEAAF83D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH v4 10/16] sql: refactor AVG() 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: Mergen Imeev Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Thank you for the review! My answer below. On Mon, Oct 04, 2021 at 11:53:19PM +0200, Vladislav Shpilevoy wrote: > Thanks for the fixes! > > > diff --git a/src/box/sql/func.c b/src/box/sql/func.c > > index 8a9de1254..94ec811ef 100644 > > --- a/src/box/sql/func.c > > +++ b/src/box/sql/func.c > > @@ -102,6 +102,58 @@ fin_total(struct sql_context *ctx) > > <...> > > > + > > +/** Finalizer for the AVG() function. */ > > +static void > > +fin_avg(struct sql_context *ctx) > > +{ > > + assert(mem_is_null(ctx->pMem) || mem_is_bin(ctx->pMem)); > > + if (mem_is_null(ctx->pMem)) > > + return mem_set_null(ctx->pOut); > > + struct Mem *tmp = (struct Mem *)ctx->pMem->z; > > + uint32_t *count_val = (uint32_t *)(tmp + 1); > > + struct Mem sum; > > + mem_create(&sum); > > + mem_copy_as_ephemeral(&sum, tmp); > > + mem_destroy(tmp); > > + struct Mem count; > > + mem_create(&count); > > + mem_set_uint(&count, *count_val); > > + if (mem_div(&sum, &count, ctx->pOut) != 0) > > + ctx->is_aborted = true; > > Consider this: > > ==================== > @@ -141,17 +141,14 @@ fin_avg(struct sql_context *ctx) > assert(mem_is_null(ctx->pMem) || mem_is_bin(ctx->pMem)); > if (mem_is_null(ctx->pMem)) > return mem_set_null(ctx->pOut); > - struct Mem *tmp = (struct Mem *)ctx->pMem->z; > - uint32_t *count_val = (uint32_t *)(tmp + 1); > - struct Mem sum; > - mem_create(&sum); > - mem_copy_as_ephemeral(&sum, tmp); > - mem_destroy(tmp); > + struct Mem *sum = (struct Mem *)ctx->pMem->z; > + uint32_t *count_val = (uint32_t *)(sum + 1); > struct Mem count; > mem_create(&count); > mem_set_uint(&count, *count_val); > if (mem_div(&sum, &count, ctx->pOut) != 0) > ctx->is_aborted = true; > + mem_destroy(sum); > } This will work, however, I think it will create some unnecessary restrictions due to changes with pMem and pOut in a few patches. I suggest to apply part of you diff with exception of mem_destroy(), which I sugget to replace by assert(). We have full control over this tmp/sum mem and we know, that there will be no memory to free, so assert should be enough. What do you think of this diff? diff --git a/src/box/sql/func.c b/src/box/sql/func.c index a811e55f9..8213704e9 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -141,16 +141,13 @@ fin_avg(struct sql_context *ctx) assert(mem_is_null(ctx->pMem) || mem_is_bin(ctx->pMem)); if (mem_is_null(ctx->pMem)) return mem_set_null(ctx->pOut); - struct Mem *tmp = (struct Mem *)ctx->pMem->z; - uint32_t *count_val = (uint32_t *)(tmp + 1); - struct Mem sum; - mem_create(&sum); - mem_copy_as_ephemeral(&sum, tmp); - mem_destroy(tmp); + struct Mem *sum = (struct Mem *)ctx->pMem->z; + uint32_t *count_val = (uint32_t *)(sum + 1); + assert(!VdbeMemDynamic(sum) && sum->szMalloc == 0); struct Mem count; mem_create(&count); mem_set_uint(&count, *count_val); - if (mem_div(&sum, &count, ctx->pOut) != 0) + if (mem_div(sum, &count, ctx->pOut) != 0) ctx->is_aborted = true; }