From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 2CF2A6EC40; Wed, 18 Aug 2021 19:52:58 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 2CF2A6EC40 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1629305578; bh=DwArT+Iyy9wpQ4UF1sS/Oo49mApqUJaQOkDoLbTl1sA=; h=To:References:Date:In-Reply-To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=m5z7X2L7/aSQEomgEC35DLMA5SDkNvd1LrZMAaaJUxgaRELjpolflXUBH3nvuAUJz c4A7092RYBER0yAl8rSIxYQ75EG/ayThuefXtK60JmXglkT51vt/SJmgIeC0TWXij1 H/ae2Txzy9NLkhbwD9zFi9v3d+f6d+RxB2+D2Dpg= Received: from smtp16.mail.ru (smtp16.mail.ru [94.100.176.153]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id CBCB96EC40 for ; Wed, 18 Aug 2021 19:52:56 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org CBCB96EC40 Received: by smtp16.mail.ru with esmtpa (envelope-from ) id 1mGOoB-0005AX-8z; Wed, 18 Aug 2021 19:52:55 +0300 To: Mergen Imeev References: <20210818130108.GA392133@tarantool.org> Message-ID: <68fc21c3-38b8-a85e-faf5-356d6c0e40ca@tarantool.org> Date: Wed, 18 Aug 2021 19:52:42 +0300 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 MIME-Version: 1.0 In-Reply-To: <20210818130108.GA392133@tarantool.org> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD92087353F0EC44DD972FF4A7D76DB5E242D14FEF1BD8BF4AC182A05F538085040C9912E64EDFB03742EFAE0AF193D97F0048F148D1BD00A92D699719806B108D5 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7A72B1EA4C8D5AD81EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637A005C90FB6EB35FF8638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D88D18103EA71A9DB125CB14076F733051117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCF1175FABE1C0F9B6A471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352033AC447995A7AD18CB629EEF1311BF91D2E47CDBA5A96583BA9C0B312567BB2376E601842F6C81A19E625A9149C048EE902A1BE408319B2932F3B5D5B27460C0D8FC6C240DEA7642DBF02ECDB25306B2B78CF848AE20165D0A6AB1C7CE11FEE34AB4081B6A6C2E076E0066C2D8992A16C4224003CC836476EA7A3FFF5B025636E2021AF6380DFAD1A18204E546F3947CB11811A4A51E3B096D1867E19FE1407959CC434672EE6371089D37D7C0E48F6C8AA50765F7900637FB177F6A8366F17BEFF80C71ABB335746BA297DBC24807EABDAD6C7F3747799A X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A213B5FB47DCBC3458834459D11680B5058B313CED562851DA67782A66302D32AD X-C1DE0DAB: 0D63561A33F958A59C4B35EB57B10250A18CA070625F6E664299FB1EB5D93A40D59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA75C69C5C0DDE134364410CA545F18667F91A7EA1CDA0B5A7A0 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D346AD04FDA812EEFF1D293D709EFD4F362FB4C04633A3C4D0730F52D06D3700E99A5CC713EBDED23591D7E09C32AA3244C54CC37C78359028FF24A43F25B8498318580396430872480FACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojGSxK+6r6oBFHYN43UEjyPw== X-Mailru-Sender: 6CA451E36783D721CBEA96CEA26D325D606E80DCFA3B0C9C96B2E606A8EC0DA5B7CBEF92542CD7C82F97C478340294DCC77752E0C033A69E0F0C7111264B8915FF1320A92A5534336C18EFA0BB12DBB0 X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH v1 2/4] sql: introduce field type decimal X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Safin Timur via Tarantool-patches Reply-To: Safin Timur Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" LGTM. (Though this apparent interdependency between patches is a good indication that they better to live in a single patch, but I do not insist) Timur On 18.08.2021 16:01, Mergen Imeev wrote: > Hi! Thank ypu fpr the review! My answers and diff below. > > On Mon, Aug 16, 2021 at 10:22:42PM +0300, Safin Timur wrote: >> Please see below several notes... >> >> On 16.08.2021 18:57, Mergen Imeev via Tarantool-patches wrote: >>> This patch introduces a decimal field type. However, implicit and >>> explicit casts and arithmetic operations for this type will be presented >>> in next few patches. Literals also will be introduced later. >>> >>> Part of #4415 >>> --- >>> extra/mkkeywordhash.c | 2 +- >>> src/box/sql/expr.c | 3 + >>> src/box/sql/func.c | 4 + >>> src/box/sql/mem.c | 173 +++++-- >>> src/box/sql/mem.h | 18 +- >>> src/box/sql/parse.y | 1 + >>> src/box/sql/sqlInt.h | 1 + >>> test/sql-tap/CMakeLists.txt | 1 + >>> test/sql-tap/decimal.c | 48 ++ >>> test/sql-tap/decimal.test.lua | 441 ++++++++++++++++++ >>> test/sql-tap/engine.cfg | 3 + >>> .../gh-5913-segfault-on-select-uuid.test.lua | 83 ---- >>> .../sql-tap/gh-6024-funcs-return-bin.test.lua | 8 +- >>> 13 files changed, 661 insertions(+), 125 deletions(-) >>> create mode 100644 test/sql-tap/decimal.c >>> create mode 100755 test/sql-tap/decimal.test.lua >>> delete mode 100755 test/sql-tap/gh-5913-segfault-on-select-uuid.test.lua >>> >>> diff --git a/extra/mkkeywordhash.c b/extra/mkkeywordhash.c >>> index 0d998506c..1c9d12295 100644 >>> --- a/extra/mkkeywordhash.c >>> +++ b/extra/mkkeywordhash.c >>> @@ -196,7 +196,7 @@ static Keyword aKeywordTable[] = { >>> { "CURRENT_TIMESTAMP", "TK_STANDARD", true }, >>> { "DATE", "TK_STANDARD", true }, >>> { "DATETIME", "TK_STANDARD", true }, >>> - { "DECIMAL", "TK_STANDARD", true }, >>> + { "DECIMAL", "TK_DECIMAL", true }, >> >> DEC is standard alias to DECIMAL. We should support that (similarly to >> INTEGER vs INT). >> > Added. > >>> { "DECLARE", "TK_STANDARD", true }, >>> { "DENSE_RANK", "TK_STANDARD", true }, >>> { "DESCRIBE", "TK_STANDARD", true }, >>> diff --git a/src/box/sql/expr.c b/src/box/sql/expr.c >>> index c67a7091c..275dbc5ba 100644 >>> --- a/src/box/sql/expr.c >>> +++ b/src/box/sql/expr.c >> >> ... >> >>> diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c >>> index 066940fac..016f0e80b 100644 >>> --- a/src/box/sql/mem.c >>> +++ b/src/box/sql/mem.c >>> @@ -1191,6 +1208,10 @@ mem_cast_explicit(struct Mem *mem, enum field_type >>> type) >>> return -1; >>> case FIELD_TYPE_NUMBER: >>> return mem_to_number(mem); >>> + case FIELD_TYPE_DECIMAL: >>> + if (mem->type == MEM_TYPE_DEC) >>> + return 0; >>> + return -1; >> >> So CAST(expr as DECIMAL) will only work for DECIMALs? It should be rather >> behaving similar to other numeric types. >> > Added in another patch. > >>> case FIELD_TYPE_UUID: >>> if (mem->type == MEM_TYPE_UUID) { >>> mem->flags = 0; >>> @@ -1274,6 +1295,10 @@ mem_cast_implicit(struct Mem *mem, enum field_type >>> type) >>> return -1; >>> mem->flags = MEM_Number; >>> return 0; >>> + case FIELD_TYPE_DECIMAL: >>> + if (mem->type == MEM_TYPE_DEC) >>> + return 0; >>> + return -1; >> >> Same question as above - implicit conversions to decimal should be >> numeric-like. >> >> > Added in another patch. > >>> case FIELD_TYPE_MAP: >>> if (mem->type == MEM_TYPE_MAP) >>> return 0; >>> @@ -1595,12 +1620,12 @@ mem_concat(struct Mem *a, struct Mem *b, struct >>> Mem *result) >>> static inline int >>> check_types_numeric_arithmetic(const struct Mem *a, const struct Mem *b) >>> { >>> - if (!mem_is_num(a) || mem_is_metatype(a)) { >>> + if (!mem_is_num(a) || mem_is_metatype(a) || a->type == >>> MEM_TYPE_DEC) { >>> diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(a), >>> "integer, unsigned or double"); >>> return -1; >> >> I don't understant - we would raise if not a numeric (and decimal is part of >> numeric) or decimal specifically? So you do not want arithmetic types with >> decimals? >> >> > Added in another patch. > >>> } >>> - if (!mem_is_num(b) || mem_is_metatype(b)) { >>> + if (!mem_is_num(b) || mem_is_metatype(b) || b->type == >>> MEM_TYPE_DEC) { >> >> The same confusion as above.. >> > Added in another patch. > >>> diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(b), >>> "integer, unsigned or double"); >>> return -1; >>> @@ -2926,26 +3040,10 @@ port_lua_get_vdbemem(struct port *base, uint32_t >>> *size) >>> case MP_EXT: { >>> assert(field.ext_type == MP_UUID || >>> field.ext_type == MP_DECIMAL); >>> - char *buf; >>> - uint32_t size; >>> - uint32_t svp = region_used(&fiber()->gc); >>> - if (field.ext_type == MP_UUID) { >>> + if (field.ext_type == MP_UUID) >>> mem_set_uuid(&val[i], field.uuidval); >>> - break; >>> - } else { >>> - size = mp_sizeof_decimal(field.decval); >>> - buf = region_alloc(&fiber()->gc, size); >>> - if (buf == NULL) { >>> - diag_set(OutOfMemory, size, >>> - "region_alloc", "buf"); >>> - goto error; >>> - } >>> - mp_encode_decimal(buf, field.decval); >>> - } >>> - int rc = mem_copy_bin(&val[i], buf, size); >>> - region_truncate(&fiber()->gc, svp); >>> - if (rc != 0) >>> - goto error; >>> + else >>> + mem_set_dec(&val[i], field.decval); >> >> Nice! Now it's much compacter and is more readable than before! >> >>> diff --git a/src/box/sql/parse.y b/src/box/sql/parse.y >>> index bd041e862..436c98cd9 100644 >>> --- a/src/box/sql/parse.y >>> +++ b/src/box/sql/parse.y >>> @@ -1863,6 +1863,7 @@ number_typedef(A) ::= NUMBER . { A.type = >>> FIELD_TYPE_NUMBER; } >>> number_typedef(A) ::= DOUBLE . { A.type = FIELD_TYPE_DOUBLE; } >>> number_typedef(A) ::= INT|INTEGER_KW . { A.type = FIELD_TYPE_INTEGER; } >>> number_typedef(A) ::= UNSIGNED . { A.type = FIELD_TYPE_UNSIGNED; } >>> +number_typedef(A) ::= DECIMAL . { A.type = FIELD_TYPE_DECIMAL; } >> >> Here please add alias to DEC, as it was done with INT. >> > Added, test fixed. > >>> diff --git a/test/sql-tap/CMakeLists.txt b/test/sql-tap/CMakeLists.txt >>> index bd2b9f33f..87f23b2f7 100644 >>> --- a/test/sql-tap/CMakeLists.txt >>> +++ b/test/sql-tap/CMakeLists.txt >>> @@ -2,3 +2,4 @@ include_directories(${MSGPUCK_INCLUDE_DIRS}) >>> build_module(gh-5938-wrong-string-length gh-5938-wrong-string-length.c) >>> build_module(gh-6024-funcs-return-bin gh-6024-funcs-return-bin.c) >>> build_module(sql_uuid sql_uuid.c) >>> +build_module(decimal decimal.c) >>> diff --git a/test/sql-tap/decimal.c b/test/sql-tap/decimal.c >>> new file mode 100644 >>> index 000000000..4d9d1ce19 >>> --- /dev/null >>> +++ b/test/sql-tap/decimal.c >>> @@ -0,0 +1,48 @@ >>> +#include "msgpuck.h" >>> +#include "module.h" >>> +#include "mp_decimal.h" >>> +#include "mp_extension_types.h" >>> + >>> +enum { >>> + BUF_SIZE = 512, >>> +}; >>> + >>> +int >>> +is_dec(box_function_ctx_t *ctx, const char *args, const char *args_end) >>> +{ >>> + (void)args_end; >>> + uint32_t arg_count = mp_decode_array(&args); >>> + if (arg_count != 1) { >>> + return box_error_set(__FILE__, __LINE__, ER_PROC_C, >>> + "invalid argument count"); >>> + } >>> + bool is_uuid; >>> + if (mp_typeof(*args) == MP_EXT) { >>> + const char *str = args; >>> + int8_t type; >>> + mp_decode_extl(&str, &type); >>> + is_uuid = type == MP_DECIMAL; >>> + } else { >>> + is_uuid = false; >> >> Here we see remnants from copy-paste from uuid related code, I assume you >> meant that variable should be named `is_decimal`. >> > True, fixed. > >>> + } >>> + >>> + char res[BUF_SIZE]; >>> + memset(res, '\0', BUF_SIZE); >>> + char *end = mp_encode_bool(res, is_uuid); >>> + box_return_mp(ctx, res, end); >>> + return 0; >>> +} >>> + >>> +int >>> diff --git a/test/sql-tap/engine.cfg b/test/sql-tap/engine.cfg >>> index 820c72b00..511d0a716 100644 >>> --- a/test/sql-tap/engine.cfg >>> +++ b/test/sql-tap/engine.cfg >>> @@ -26,6 +26,9 @@ >>> "metatypes.test.lua": { >>> "memtx": {"engine": "memtx"} >>> }, >>> + "decimal.test.lua": { >>> + "memtx": {"engine": "memtx"} >>> + }, >> >> BTW, why this exception for the test? >> > I see no reason to test both vinyl and memtx. > >>> "gh-4077-iproto-execute-no-bind.test.lua": {}, >>> "*": { >>> "memtx": {"engine": "memtx"}, >> >> Thanks, >> Timur > > > Diff: > > > diff --git a/extra/mkkeywordhash.c b/extra/mkkeywordhash.c > index 1c9d12295..3e4200417 100644 > --- a/extra/mkkeywordhash.c > +++ b/extra/mkkeywordhash.c > @@ -196,6 +196,7 @@ static Keyword aKeywordTable[] = { > { "CURRENT_TIMESTAMP", "TK_STANDARD", true }, > { "DATE", "TK_STANDARD", true }, > { "DATETIME", "TK_STANDARD", true }, > + { "DEC", "TK_DECIMAL", true }, > { "DECIMAL", "TK_DECIMAL", true }, > { "DECLARE", "TK_STANDARD", true }, > { "DENSE_RANK", "TK_STANDARD", true }, > diff --git a/test/sql-tap/decimal.c b/test/sql-tap/decimal.c > index 4d9d1ce19..fd7c3e0c9 100644 > --- a/test/sql-tap/decimal.c > +++ b/test/sql-tap/decimal.c > @@ -16,19 +16,19 @@ is_dec(box_function_ctx_t *ctx, const char *args, const char *args_end) > return box_error_set(__FILE__, __LINE__, ER_PROC_C, > "invalid argument count"); > } > - bool is_uuid; > + bool is_dec; > if (mp_typeof(*args) == MP_EXT) { > const char *str = args; > int8_t type; > mp_decode_extl(&str, &type); > - is_uuid = type == MP_DECIMAL; > + is_dec = type == MP_DECIMAL; > } else { > - is_uuid = false; > + is_dec = false; > } > > char res[BUF_SIZE]; > memset(res, '\0', BUF_SIZE); > - char *end = mp_encode_bool(res, is_uuid); > + char *end = mp_encode_bool(res, is_dec); > box_return_mp(ctx, res, end); > return 0; > } > diff --git a/test/sql-tap/decimal.test.lua b/test/sql-tap/decimal.test.lua > index dd69ca370..10217a806 100755 > --- a/test/sql-tap/decimal.test.lua > +++ b/test/sql-tap/decimal.test.lua > @@ -9,16 +9,26 @@ local dec = require("decimal") > local dec1 = dec.new("111") > local dec2 = dec.new("55555") > local dec3 = dec.new("3333") > +local dec4 = dec.new("-13") > +local dec5 = dec.new("0") > +local dec6 = dec.new("-0") > > -- Check that it is possible to create spaces with DECIMAL field. > test:do_execsql_test( > "dec-1", > [[ > - CREATE TABLE t1 (i INT PRIMARY KEY, u DECIMAL); > + CREATE TABLE t0 (i INT PRIMARY KEY, u DEC); > + CREATE TABLE t1 (i INT PRIMARY KEY, u DEC); > CREATE TABLE t2 (u DECIMAL PRIMARY KEY); > ]], { > }) > > +box.space.T0:insert({1, dec1}) > +box.space.T0:insert({2, dec2}) > +box.space.T0:insert({3, dec3}) > +box.space.T0:insert({4, dec4}) > +box.space.T0:insert({5, dec5}) > +box.space.T0:insert({6, dec6}) > box.space.T1:insert({1, dec1}) > box.space.T1:insert({2, dec2}) > box.space.T1:insert({3, dec3}) > @@ -33,9 +43,9 @@ box.space.T2:insert({dec3}) > test:do_execsql_test( > "dec-2.1.1", > [[ > - SELECT * FROM t1; > + SELECT * FROM t0; > ]], { > - 1, dec1, 2, dec2, 3, dec3, 4, dec1, 5, dec1, 6, dec2 > + 1, dec1, 2, dec2, 3, dec3, 4, dec4, 5, dec5, 6, dec6 > }) > > test:do_execsql_test( > @@ -50,17 +60,17 @@ test:do_execsql_test( > test:do_execsql_test( > "dec-2.2.1", > [[ > - SELECT * FROM t1 ORDER BY u; > + SELECT * FROM t0 ORDER BY u; > ]], { > - 1, dec1, 4, dec1, 5, dec1, 3, dec3, 2, dec2, 6, dec2 > + 4, dec4, 5, dec5, 6, dec6, 1, dec1, 3, dec3, 2, dec2 > }) > > test:do_execsql_test( > "dec-2.2.2", > [[ > - SELECT * FROM t1 ORDER BY u DESC; > + SELECT * FROM t0 ORDER BY u DESC; > ]], { > - 2, dec2, 6, dec2, 3, dec3, 1, dec1, 4, dec1, 5, dec1 > + 2, dec2, 3, dec3, 1, dec1, 5, dec5, 6, dec6, 4, dec4 > }) > > test:do_execsql_test( >