[Tarantool-patches] [PATCH v1 02/21] sql: refactor GREATEST() and LEAST() functions
Mergen Imeev
imeevma at tarantool.org
Mon Oct 25 11:17:10 MSK 2021
Thank you for the review! My answer and diff below.
On Fri, Oct 15, 2021 at 12:42:50AM +0200, Vladislav Shpilevoy wrote:
> Thanks for working on this!
>
> > diff --git a/src/box/sql/func.c b/src/box/sql/func.c
> > index dd5e7d785..7886f5f40 100644
> > --- a/src/box/sql/func.c
> > +++ b/src/box/sql/func.c
> > @@ -777,6 +777,29 @@ func_char(struct sql_context *ctx, int argc, struct Mem *argv)
> > mem_set_str_allocated(ctx->pOut, str, (char *)ptr - str);
> > }
> >
> > +/** Implementation of the GREATEST() and LEAST() functions. */
> > +static void
> > +func_greatest_least(struct sql_context *ctx, int argc, struct Mem *argv)
> > +{
> > + assert(argc > 1);
> > + int mask = ctx->func->def->name[0] == 'G' ? -1 : 0;
> > + assert(ctx->func->def->name[0] == 'G' ||
> > + ctx->func->def->name[0] == 'L');
>
> Why don't you use the flag SQL_FUNC_MAX like it was done before?
>
This flag, together with SQL_FUNC_MIN, has only two uses. The first is to show
that the aggregate function is a MIN() or MAX() function, and use the feature
described in "Define rule for GROUP BY" discussion:
https://github.com/tarantool/tarantool/discussions/6416
Another use is to determine is the function MIN() or MAX().
Since I am planning to drop that feature, I think it is best to remove these
flags from LEAST()/GREATEST() now.
I removed these flags from the definitions of the LEAST()/GREATEST() functions.
In general, I find it awkward to use flags here, and I think we should drop them
in the future. The only useful flag is SQL_FUNC_AGG.
> > +
> > + if (mem_is_null(&argv[0]))
> > + return;
> > + int best = 0;
> > + for (int i = 1; i < argc; ++i) {
> > + if (mem_is_null(&argv[i]))
> > + return;
> > + int cmp = mem_cmp_scalar(&argv[best], &argv[i], ctx->coll);
> > + if ((cmp ^ mask) >= 0)
> > + best = i;
> > + }
> > + if (mem_copy(ctx->pOut, &argv[best]) != 0)
> > + ctx->is_aborted = true;
> > +}
Diff:
diff --git a/src/box/sql/func.c b/src/box/sql/func.c
index 6bde3759d..7d2f03542 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -768,7 +768,12 @@ func_char(struct sql_context *ctx, int argc, struct Mem *argv)
mem_set_str_allocated(ctx->pOut, str, len);
}
-/** Implementation of the GREATEST() and LEAST() functions. */
+/**
+ * Implementation of the GREATEST() and LEAST() functions.
+ *
+ * The GREATEST() function returns the largest of the given arguments.
+ * The LEAST() function returns the smallest of the given arguments.
+ */
static void
func_greatest_least(struct sql_context *ctx, int argc, struct Mem *argv)
{
@@ -1766,13 +1771,11 @@ static struct sql_func_dictionary dictionaries[] = {
{"CHAR_LENGTH", 1, 1, 0, true, 0, NULL},
{"COALESCE", 2, SQL_MAX_FUNCTION_ARG, SQL_FUNC_COALESCE, true, 0, NULL},
{"COUNT", 0, 1, SQL_FUNC_AGG, false, 0, NULL},
- {"GREATEST", 2, SQL_MAX_FUNCTION_ARG, SQL_FUNC_MAX | SQL_FUNC_NEEDCOLL,
- true, 0, NULL},
+ {"GREATEST", 2, SQL_MAX_FUNCTION_ARG, SQL_FUNC_NEEDCOLL, true, 0, NULL},
{"GROUP_CONCAT", 1, 2, SQL_FUNC_AGG, false, 0, NULL},
{"HEX", 1, 1, 0, true, 0, NULL},
{"IFNULL", 2, 2, SQL_FUNC_COALESCE, true, 0, NULL},
- {"LEAST", 2, SQL_MAX_FUNCTION_ARG, SQL_FUNC_MIN | SQL_FUNC_NEEDCOLL,
- true, 0, NULL},
+ {"LEAST", 2, SQL_MAX_FUNCTION_ARG, SQL_FUNC_NEEDCOLL, true, 0, NULL},
{"LENGTH", 1, 1, SQL_FUNC_LENGTH, true, 0, NULL},
{"LIKE", 2, 3, SQL_FUNC_LIKE | SQL_FUNC_NEEDCOLL, true, 0, NULL},
{"LIKELIHOOD", 2, 2, SQL_FUNC_UNLIKELY, true, 0, NULL},
More information about the Tarantool-patches
mailing list