From 2885cf8455ef6ff064bb6462abb4557728df6fad Mon Sep 17 00:00:00 2001 From: Mergen Imeev <imeevma@gmail.com> Date: Sat, 15 Jun 2019 13:51:58 +0300 Subject: [PATCH] sql: ANSI aliases for LENGTH() This patch creates aliases CHARACTER_LENGTH() and CHAR_LENGTH() for LENGTH(). These functions are added because they are described in ANSI. Closes #3929 @TarantoolBot document Title: SQL functions CHAR_LENGTH() and CHARACTER_LENGTH() The SQL functions CHAR_LENGTH() and CHARACTER_LENGTH() work the same as the LENGTH() function. They take exactly one argument. If an argument of type TEXT or can be cast to a TEXT value using internal casting rules, these functions return the length of the TEXT value that represents the argument. They throw an error if the argument cannot be cast to a TEXT value. --- src/box/sql/func.c | 3 +++ test/sql-tap/func3.test.lua | 23 ++++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/box/sql/func.c b/src/box/sql/func.c index 761a3abae5..98cad51fd2 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -1893,6 +1893,9 @@ sqlRegisterBuiltinFunctions(void) FIELD_TYPE_STRING), FUNCTION2(length, 1, 0, 0, lengthFunc, SQL_FUNC_LENGTH, FIELD_TYPE_INTEGER), + FUNCTION(char_length, 1, 0, 0, lengthFunc, FIELD_TYPE_INTEGER), + FUNCTION(character_length, 1, 0, 0, lengthFunc, + FIELD_TYPE_INTEGER), FUNCTION(position, 2, 0, 1, position_func, FIELD_TYPE_INTEGER), FUNCTION(printf, -1, 0, 0, printfFunc, FIELD_TYPE_STRING), FUNCTION(unicode, 1, 0, 0, unicodeFunc, FIELD_TYPE_STRING), diff --git a/test/sql-tap/func3.test.lua b/test/sql-tap/func3.test.lua index 6d6411ca6e..2d0579d931 100755 --- a/test/sql-tap/func3.test.lua +++ b/test/sql-tap/func3.test.lua @@ -1,6 +1,6 @@ #!/usr/bin/env tarantool test = require("sqltester") -test:plan(25) +test:plan(35) --!./tcltestrunner.lua -- 2010 August 27 @@ -285,5 +285,26 @@ test:do_test( end, test:execsql "EXPLAIN SELECT min(1.0+'2.0',4*11)") +-- +-- gh-3929: sql: ANSI aliases for LENGTH(). +-- +suits = {} +suits[1] = {str = '123456789', len = 9} +suits[2] = {str = '\x80', len = 1} +suits[3] = {str = '\x61\x62\x63', len = 3} +suits[4] = {str = '\x7f\x80\x81', len = 3} +suits[5] = {str = '\x61\xc0', len = 2} +suits[6] = {str = '\x61\xc0\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80', len = 12} +suits[7] = {str = '\xc0\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80', len = 11} +suits[8] = {str = '\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80', len = 10} +suits[9] = {str = '\x80\x80\x80\x80\x80\xf0\x80\x80\x80\x80', len = 7} +suits[10] = {str = '\x80\x80\x80\x80\x80\xf0\x80\x80\x80\xff', len = 7} + +for k,v in pairs(suits) do + test:do_execsql_test( + "func3-6."..k, + "SELECT CHAR_LENGTH('"..v.str.."'), CHARACTER_LENGTH('"..v.str.."');", + {v.len, v.len}) +end test:finish_test() -- GitLab