[Tarantool-patches] [PATCH v1 02/21] sql: refactor GREATEST() and LEAST() functions

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Fri Oct 15 01:42:50 MSK 2021


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?

> +
> +	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;
> +}


More information about the Tarantool-patches mailing list