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 324F16C7D7; Tue, 27 Apr 2021 19:55:53 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 324F16C7D7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1619542553; bh=E72BWzHAJdRK2dl8BfeZ2wHeIQRGUAXIspVLwhYuZLU=; 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=eWGtb9012yuDxLMAJsgZ/nsI3gAGjCssQ2+oEcHeWw96RepDK9HCdOCrIvOkt7CyP LCkx0rs4dp3NMDdeYbopVJuk697Rlmgzx95XkyYwCRdL1mrOcnBMQi0tFOV/CVERvz lzpOrNDDkTbm+lwDDpI3w7jf44eYkVK+WMRRdP2A= Received: from smtpng2.m.smailru.net (smtpng2.m.smailru.net [94.100.179.3]) (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 7775D6FC8F for ; Tue, 27 Apr 2021 19:55:23 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 7775D6FC8F Received: by smtpng2.m.smailru.net with esmtpa (envelope-from ) id 1lbQza-0007XD-V3; Tue, 27 Apr 2021 19:55:23 +0300 To: v.shpilevoy@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Tue, 27 Apr 2021 19:55:22 +0300 Message-Id: <05d176765118a7357b5b15d7fcfc08104463522c.1619542456.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-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD9ECFD8CE5F059401062EF72DCC8B8CDABD8D4F98D4AE0C03D182A05F53808504031C6BBB6335400990ADEE153E347301AC64ACE5A27E05ABB90AEE20634ED11A3 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE73C871DD2182510D5EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637CB87E27B59CE093D8638F802B75D45FF914D58D5BE9E6BC1A93B80C6DEB9DEE97C6FB206A91F05B2D7DC62EAA4988D7C95404EA4DDC5EB5D2CC0D3CB04F14752D2E47CDBA5A96583C09775C1D3CA48CFCA5A41EBD8A3A0199FA2833FD35BB23D2EF20D2F80756B5F868A13BD56FB6657A471835C12D1D977725E5C173C3A84C3CF36E64A7E3F8E58117882F4460429728AD0CFFFB425014E868A13BD56FB6657E2021AF6380DFAD1A18204E546F3947CB11811A4A51E3B096D1867E19FE1407959CC434672EE6371089D37D7C0E48F6C8AA50765F79006377AA2284B41911753EFF80C71ABB335746BA297DBC24807EABDAD6C7F3747799A X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C8E0B2E50BE5001ACE4AD33F6D71181DE66CA91A835FC5E329C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF0417BEADF48D1460699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D3430FC2BC1B917F018326EEA0AEA6A341F84DD619E81662C615108F4E708FCD949EDFD7F96F34046B81D7E09C32AA3244C0FD255B5B2109818E0571015AF9FDD996C24832127668422729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojmGF4M/OAxzfzPsheTetAkQ== X-Mailru-Sender: 689FA8AB762F73936BC43F508A063822E40BD5BA86213CF7C6A9B3820AE5FD6883D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v2 1/3] sql: initialize MEM used in aggregate functions 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 adds proper initialization for the MEM, which is used in the aggregate functions min() and max(). Part of #4906 --- src/box/sql/func.c | 21 ++++++++++----------- src/box/sql/sqlInt.h | 7 +++++++ src/box/sql/vdbeapi.c | 20 ++++++++++++++++++++ 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/box/sql/func.c b/src/box/sql/func.c index 9c28d5122..d282b2cea 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -1766,14 +1766,14 @@ minmaxStep(sql_context * context, int NotUsed, sql_value ** argv) struct func_sql_builtin *func = (struct func_sql_builtin *)context->func; - pBest = (Mem *) sql_aggregate_context(context, sizeof(*pBest)); + pBest = sql_context_agg_mem(context); if (!pBest) return; if (mem_is_null(argv[0])) { - if (pBest->flags) + if (!mem_is_null(pBest)) sqlSkipAccumulatorLoad(context); - } else if (pBest->flags) { + } else if (!mem_is_null(pBest)) { int cmp; struct coll *pColl = sqlGetFuncCollSeq(context); /* @@ -1798,14 +1798,13 @@ minmaxStep(sql_context * context, int NotUsed, sql_value ** argv) static void minMaxFinalize(sql_context * context) { - sql_value *pRes; - pRes = (sql_value *) sql_aggregate_context(context, 0); - if (pRes) { - if (pRes->flags) { - sql_result_value(context, pRes); - } - mem_destroy(pRes); - } + struct Mem *mem = context->pMem; + struct Mem *res; + if (!mem_is_agg(mem) || mem_get_agg(mem, (void **)&res) != 0) + return; + if (!mem_is_null(res)) + sql_result_value(context, res); + mem_destroy(res); } /* diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h index b548ddad4..ef8dcd693 100644 --- a/src/box/sql/sqlInt.h +++ b/src/box/sql/sqlInt.h @@ -484,6 +484,13 @@ void * sql_aggregate_context(sql_context *, int nBytes); +/** + * Allocate or return the aggregate context containing struct MEM for a user + * function. A new context is allocated on the first call. Subsequent calls + * return the same context that was returned on prior calls. + */ +struct Mem * +sql_context_agg_mem(struct sql_context *context); int sql_column_count(sql_stmt * pStmt); diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c index 655743fb1..aaae12e41 100644 --- a/src/box/sql/vdbeapi.c +++ b/src/box/sql/vdbeapi.c @@ -394,6 +394,26 @@ sql_aggregate_context(sql_context * p, int nByte) return accum; } +struct Mem * +sql_context_agg_mem(struct sql_context *ctx) +{ + assert(ctx != NULL && ctx->func != NULL); + assert(ctx->func->def->language == FUNC_LANGUAGE_SQL_BUILTIN); + assert(ctx->func->def->aggregate == FUNC_AGGREGATE_GROUP); + struct Mem *mem; + if (!mem_is_agg(ctx->pMem)) { + if (mem_set_agg(ctx->pMem, ctx->func, sizeof(*mem)) != 0) + return NULL; + if (mem_get_agg(ctx->pMem, (void **)&mem) != 0) + return NULL; + mem_create(mem); + return mem; + } + if (mem_get_agg(ctx->pMem, (void **)&mem) != 0) + return NULL; + return mem; +} + /* * Return the number of columns in the result set for the statement pStmt. */ -- 2.25.1