[Tarantool-patches] [PATCH v1 6/8] sql: rework POSITION() function

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Sat Oct 9 00:58:32 MSK 2021


Thanks for the patch!

See 3 comments below.

> diff --git a/src/box/sql/func.c b/src/box/sql/func.c
> index 1d1a8b0cd..415a92738 100644
> --- a/src/box/sql/func.c
> +++ b/src/box/sql/func.c
> @@ -530,6 +530,68 @@ func_trim_str(struct sql_context *ctx, int argc, struct Mem *argv)
>  		ctx->is_aborted = true;
>  }
>  
> +/** Implementation of the POSITION() function. */
> +static void
> +func_position_octets(struct sql_context *ctx, int argc, struct Mem *argv)
> +{
> +	assert(argc == 2);
> +	(void)argc;
> +	if (mem_is_null(&argv[0]) || mem_is_null(&argv[1]))

1. There is mem_is_any_null(). The same in the next function.

> +		return;
> +	assert(mem_is_bytes(&argv[0]) && mem_is_bytes(&argv[1]));
> +
> +	const char *key = argv[0].z;
> +	const char *str = argv[1].z;
> +	int key_size = argv[0].n;
> +	int str_size = argv[1].n;
> +	if (key_size <= 0)
> +		return mem_set_uint(ctx->pOut, 1);
> +	/* Matching time O(n * m). */
> +	for (int i = 0; i <= str_size - key_size; ++i) {
> +		if (memcmp(&str[i], key, key_size) == 0)
> +			return mem_set_uint(ctx->pOut, i + 1);
> +	}

2. There is memmem().

> +	return mem_set_uint(ctx->pOut, 0);
> +}
> diff --git a/test/sql-tap/position.test.lua b/test/sql-tap/position.test.lua
> index 6a96ed9bc..5f62c7f54 100755
> --- a/test/sql-tap/position.test.lua
> +++ b/test/sql-tap/position.test.lua
> @@ -858,4 +858,14 @@ test:do_catchsql_test(
>      }
>  )
>  
> +-- gh-4145: Make sure that POSITION() can wirk with VARBINARY.

3. wirk -> work.

> +test:do_execsql_test(
> +    "position-2",
> +    [[
> +        SELECT POSITION(x'313233', x'30313231323334353132333435');
> +    ]], {
> +        4
> +    }
> +)
> +
>  test:finish_test()
> 


More information about the Tarantool-patches mailing list