[tarantool-patches] [PATCH v3 4/9] sql: rework SQL_FUNC_COUNT flag semantics

Kirill Shcherbatov kshcherbatov at tarantool.org
Fri Aug 16 16:26:50 MSK 2019


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





More information about the Tarantool-patches mailing list