From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id F186626C38 for ; Fri, 16 Aug 2019 09:26:58 -0400 (EDT) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id apI-QUqiwGzO for ; Fri, 16 Aug 2019 09:26:58 -0400 (EDT) Received: from smtp60.i.mail.ru (smtp60.i.mail.ru [217.69.128.40]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id 7EF2326F6D for ; Fri, 16 Aug 2019 09:26:58 -0400 (EDT) From: Kirill Shcherbatov Subject: [tarantool-patches] [PATCH v3 4/9] sql: rework SQL_FUNC_COUNT flag semantics Date: Fri, 16 Aug 2019 16:26:50 +0300 Message-Id: <956bd6c2edf79b70d116d4755e35a4975f1f8075.1565961887.git.kshcherbatov@tarantool.org> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-Help: List-Unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-Subscribe: List-Owner: List-post: List-Archive: To: tarantool-patches@freelists.org, korablev@tarantool.org Cc: Kirill Shcherbatov Tarantool's SQL engine generates a different VDBE bytecode for ..COUNT(*).. and ..COUNT(fieldname).. operations: the first one produces a lightweight OP_Count operation that uses native mechanism to report the count of record in index while the second one pessimistically opens a space read iterator and uses Count aggregate function. A helper routine is_simple_count decides whether such optimisation is correct. It used to use SQL_FUNC_COUNT flag to mark a dummy (non-functional) function entry with 0 arguments. This patch changes SQL_FUNC_COUNT semantics: now it is a marker of any COUNT function, while is_simple_count relies on count of arguments to distinguish aggregate and non-aggregate functions. Needed for #2200, #4113, #2233 --- src/box/sql/sqlInt.h | 3 ++- src/box/sql/func.c | 4 ++-- src/box/sql/select.c | 4 +++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h index 4adb30c5c..bc3c639e6 100644 --- a/src/box/sql/sqlInt.h +++ b/src/box/sql/sqlInt.h @@ -1312,7 +1312,8 @@ struct FuncDestructor { */ #define SQL_FUNC_LENGTH 0x0040 /* Built-in length() function */ #define SQL_FUNC_TYPEOF 0x0080 /* Built-in typeof() function */ -#define SQL_FUNC_COUNT 0x0100 /* Built-in count(*) aggregate */ +/** Built-in count() function. */ +#define SQL_FUNC_COUNT 0x0100 #define SQL_FUNC_COALESCE 0x0200 /* Built-in coalesce() or ifnull() */ #define SQL_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */ #define SQL_FUNC_CONSTANT 0x0800 /* Constant inputs give a constant output */ diff --git a/src/box/sql/func.c b/src/box/sql/func.c index 6bd2d0cd7..07c019db9 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -1903,9 +1903,9 @@ sqlRegisterBuiltinFunctions(void) AGGREGATE(avg, 1, 0, 0, sum_step, avgFinalize, FIELD_TYPE_NUMBER), AGGREGATE2(count, 0, 0, 0, countStep, countFinalize, + SQL_FUNC_COUNT, FIELD_TYPE_INTEGER), + AGGREGATE2(count, 1, 0, 0, countStep, countFinalize, SQL_FUNC_COUNT, FIELD_TYPE_INTEGER), - AGGREGATE(count, 1, 0, 0, countStep, countFinalize, - FIELD_TYPE_INTEGER), AGGREGATE(group_concat, 1, 0, 0, groupConcatStep, groupConcatFinalize, FIELD_TYPE_STRING), AGGREGATE(group_concat, 2, 0, 0, groupConcatStep, diff --git a/src/box/sql/select.c b/src/box/sql/select.c index c31076694..ddb5de87e 100644 --- a/src/box/sql/select.c +++ b/src/box/sql/select.c @@ -4381,7 +4381,9 @@ is_simple_count(struct Select *select, struct AggInfo *agg_info) return NULL; if (NEVER(agg_info->nFunc == 0)) return NULL; - if ((agg_info->aFunc[0].pFunc->funcFlags & SQL_FUNC_COUNT) == 0) + if ((agg_info->aFunc->pFunc->funcFlags & SQL_FUNC_COUNT) == 0 || + (agg_info->aFunc->pExpr->x.pList != NULL && + agg_info->aFunc->pExpr->x.pList->nExpr > 0)) return NULL; if (expr->flags & EP_Distinct) return NULL; -- 2.22.1