[Tarantool-patches] [PATCH v2 10/15] sql: rework COUNT()
Mergen Imeev
imeevma at tarantool.org
Sat Sep 25 14:34:44 MSK 2021
Thank you for the review! I replaced manual checks of MEM type by mem_is_*()
functions. Diff and new patch below.
On Tue, Sep 21, 2021 at 01:59:23PM +0300, Mergen Imeev via Tarantool-patches wrote:
> This patch does some refactoring for the SQL built-in aggregate function
> COUNT().
>
> Part of #4145
> ---
> src/box/sql/func.c | 64 +++++++++++++++++++---------------------------
> 1 file changed, 26 insertions(+), 38 deletions(-)
>
> diff --git a/src/box/sql/func.c b/src/box/sql/func.c
> index b5f154fb1..113df423a 100644
> --- a/src/box/sql/func.c
> +++ b/src/box/sql/func.c
> @@ -150,6 +150,29 @@ fin_avg(struct sql_context *ctx)
> ctx->is_aborted = true;
> }
>
> +/** Implementation of the COUNT() function. */
> +static void
> +step_count(struct sql_context *ctx, int argc, struct Mem **argv)
> +{
> + assert(argc == 0 || argc == 1);
> + if (ctx->pMem->type == MEM_TYPE_NULL)
> + mem_set_uint(ctx->pMem, 0);
> + if (argc == 1 && argv[0]->type == MEM_TYPE_NULL)
> + return;
> + assert(ctx->pMem->type == MEM_TYPE_UINT);
> + ++ctx->pMem->u.u;
> +}
> +
> +/** Finalizer for the COUNT() function. */
> +static void
> +fin_count(struct sql_context *ctx)
> +{
> + assert(ctx->pMem->type == MEM_TYPE_NULL || mem_is_uint(ctx->pMem));
> + if (ctx->pMem->type == MEM_TYPE_NULL)
> + return mem_set_uint(ctx->pOut, 0);
> + mem_copy_as_ephemeral(ctx->pOut, ctx->pMem);
> +}
> +
> static const unsigned char *
> mem_as_ustr(struct Mem *mem)
> {
> @@ -1704,41 +1727,6 @@ soundexFunc(sql_context * context, int argc, sql_value ** argv)
> }
> }
>
> -/*
> - * The following structure keeps track of state information for the
> - * count() aggregate function.
> - */
> -typedef struct CountCtx CountCtx;
> -struct CountCtx {
> - i64 n;
> -};
> -
> -/*
> - * Routines to implement the count() aggregate function.
> - */
> -static void
> -countStep(sql_context * context, int argc, sql_value ** argv)
> -{
> - CountCtx *p;
> - if (argc != 0 && argc != 1) {
> - diag_set(ClientError, ER_FUNC_WRONG_ARG_COUNT,
> - "COUNT", "0 or 1", argc);
> - context->is_aborted = true;
> - return;
> - }
> - p = sql_aggregate_context(context, sizeof(*p));
> - if ((argc == 0 || !mem_is_null(argv[0])) && p != NULL)
> - p->n++;
> -}
> -
> -static void
> -countFinalize(sql_context * context)
> -{
> - CountCtx *p;
> - p = sql_aggregate_context(context, 0);
> - sql_result_uint(context, p ? p->n : 0);
> -}
> -
> /*
> * Routines to implement min() and max() aggregate functions.
> */
> @@ -2008,9 +1996,9 @@ static struct sql_func_definition definitions[] = {
> NULL},
> {"COALESCE", -1, {FIELD_TYPE_ANY}, FIELD_TYPE_SCALAR, sql_builtin_stub,
> NULL},
> - {"COUNT", 0, {}, FIELD_TYPE_INTEGER, countStep, countFinalize},
> - {"COUNT", 1, {FIELD_TYPE_ANY}, FIELD_TYPE_INTEGER, countStep,
> - countFinalize},
> + {"COUNT", 0, {}, FIELD_TYPE_INTEGER, step_count, fin_count},
> + {"COUNT", 1, {FIELD_TYPE_ANY}, FIELD_TYPE_INTEGER, step_count,
> + fin_count},
>
> {"GREATEST", -1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_INTEGER, minmaxFunc,
> NULL},
> --
> 2.25.1
>
Diff:
diff --git a/src/box/sql/func.c b/src/box/sql/func.c
index b4c25c935..00db6512a 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -159,11 +159,11 @@ static void
step_count(struct sql_context *ctx, int argc, struct Mem **argv)
{
assert(argc == 0 || argc == 1);
- if (ctx->pMem->type == MEM_TYPE_NULL)
+ if (mem_is_null(ctx->pMem))
mem_set_uint(ctx->pMem, 0);
- if (argc == 1 && argv[0]->type == MEM_TYPE_NULL)
+ if (argc == 1 && mem_is_null(argv[0]))
return;
- assert(ctx->pMem->type == MEM_TYPE_UINT);
+ assert(mem_is_uint(ctx->pMem));
++ctx->pMem->u.u;
}
@@ -171,8 +171,8 @@ step_count(struct sql_context *ctx, int argc, struct Mem **argv)
static void
fin_count(struct sql_context *ctx)
{
- assert(ctx->pMem->type == MEM_TYPE_NULL || mem_is_uint(ctx->pMem));
- if (ctx->pMem->type == MEM_TYPE_NULL)
+ assert(mem_is_null(ctx->pMem) || mem_is_uint(ctx->pMem));
+ if (mem_is_null(ctx->pMem))
return mem_set_uint(ctx->pOut, 0);
mem_copy_as_ephemeral(ctx->pOut, ctx->pMem);
}
New patch:
commit 000ac8430821d33aaa939d426c30c1de2d5a7cab
Author: Mergen Imeev <imeevma at gmail.com>
Date: Thu Sep 9 18:26:04 2021 +0300
sql: rework COUNT()
This patch does some refactoring for the SQL built-in aggregate function
COUNT().
Part of #4145
diff --git a/src/box/sql/func.c b/src/box/sql/func.c
index e436ffbe1..00db6512a 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -154,6 +154,29 @@ fin_avg(struct sql_context *ctx)
ctx->is_aborted = true;
}
+/** Implementation of the COUNT() function. */
+static void
+step_count(struct sql_context *ctx, int argc, struct Mem **argv)
+{
+ assert(argc == 0 || argc == 1);
+ if (mem_is_null(ctx->pMem))
+ mem_set_uint(ctx->pMem, 0);
+ if (argc == 1 && mem_is_null(argv[0]))
+ return;
+ assert(mem_is_uint(ctx->pMem));
+ ++ctx->pMem->u.u;
+}
+
+/** Finalizer for the COUNT() function. */
+static void
+fin_count(struct sql_context *ctx)
+{
+ assert(mem_is_null(ctx->pMem) || mem_is_uint(ctx->pMem));
+ if (mem_is_null(ctx->pMem))
+ return mem_set_uint(ctx->pOut, 0);
+ mem_copy_as_ephemeral(ctx->pOut, ctx->pMem);
+}
+
static const unsigned char *
mem_as_ustr(struct Mem *mem)
{
@@ -1708,41 +1731,6 @@ soundexFunc(sql_context * context, int argc, sql_value ** argv)
}
}
-/*
- * The following structure keeps track of state information for the
- * count() aggregate function.
- */
-typedef struct CountCtx CountCtx;
-struct CountCtx {
- i64 n;
-};
-
-/*
- * Routines to implement the count() aggregate function.
- */
-static void
-countStep(sql_context * context, int argc, sql_value ** argv)
-{
- CountCtx *p;
- if (argc != 0 && argc != 1) {
- diag_set(ClientError, ER_FUNC_WRONG_ARG_COUNT,
- "COUNT", "0 or 1", argc);
- context->is_aborted = true;
- return;
- }
- p = sql_aggregate_context(context, sizeof(*p));
- if ((argc == 0 || !mem_is_null(argv[0])) && p != NULL)
- p->n++;
-}
-
-static void
-countFinalize(sql_context * context)
-{
- CountCtx *p;
- p = sql_aggregate_context(context, 0);
- sql_result_uint(context, p ? p->n : 0);
-}
-
/*
* Routines to implement min() and max() aggregate functions.
*/
@@ -2012,9 +2000,9 @@ static struct sql_func_definition definitions[] = {
NULL},
{"COALESCE", -1, {FIELD_TYPE_ANY}, FIELD_TYPE_SCALAR, sql_builtin_stub,
NULL},
- {"COUNT", 0, {}, FIELD_TYPE_INTEGER, countStep, countFinalize},
- {"COUNT", 1, {FIELD_TYPE_ANY}, FIELD_TYPE_INTEGER, countStep,
- countFinalize},
+ {"COUNT", 0, {}, FIELD_TYPE_INTEGER, step_count, fin_count},
+ {"COUNT", 1, {FIELD_TYPE_ANY}, FIELD_TYPE_INTEGER, step_count,
+ fin_count},
{"GREATEST", -1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_INTEGER, minmaxFunc,
NULL},
More information about the Tarantool-patches
mailing list