From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id 31FB12330C for ; Tue, 16 Jul 2019 20:53:34 -0400 (EDT) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id vsyd9JVBIdn6 for ; Tue, 16 Jul 2019 20:53:34 -0400 (EDT) Received: from smtp49.i.mail.ru (smtp49.i.mail.ru [94.100.177.109]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id E63612301F for ; Tue, 16 Jul 2019 20:53:33 -0400 (EDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) Subject: [tarantool-patches] Re: [PATCH 6/6] sql: allow to specify UNSIGNED column type From: "n.pettik" In-Reply-To: Date: Wed, 17 Jul 2019 03:53:32 +0300 Content-Transfer-Encoding: quoted-printable Message-Id: <552F96C1-DAC5-4F18-9F5A-BF50C6BBC205@tarantool.org> References: <734EC309-6DCF-42C2-8041-135A8B68E935@tarantool.org> <9a397d31-1cae-0dd0-cdd6-733388cb01af@tarantool.org> Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-Help: List-Unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-Subscribe: List-Owner: List-post: List-Archive: To: tarantool-patches@freelists.org Cc: Vladislav Shpilevoy > ------------------------- > vdbemem.c:661 >=20 >> if ((pMem->flags & MEM_Blob) !=3D 0 && type =3D=3D = FIELD_TYPE_NUMBER) { >> bool is_neg; >> if (sql_atoi64(pMem->z, (int64_t *) &pMem->u.i, = &is_neg, >> pMem->n) =3D=3D 0) { >> MemSetTypeFlag(pMem, MEM_Real); >> pMem->u.r =3D pMem->u.i; >> return 0; >> } >> return ! sqlAtoF(pMem->z, &pMem->u.r, pMem->n); >> } >=20 > Here you use assign 'pMem.u.i' to 'pMem.u.r' without checking > its sign. In theory something like that should work wrong now: >=20 > box.execute("SELECT CAST('9223372036854775837' AS REAL)") >=20 > But you need to somehow write that number as blob. Thanks, you are absolutely right: tarantool> SELECT CAST(x'3138343436373434303733373039353531363135' AS = real) --- - metadata: - name: CAST(x'3138343436373434303733373039353531363135' AS real) type: number rows: - [-1] ... Fix: diff --git a/src/box/sql/vdbemem.c b/src/box/sql/vdbemem.c index 64acb5d41..087cd20f1 100644 --- a/src/box/sql/vdbemem.c +++ b/src/box/sql/vdbemem.c @@ -658,7 +658,10 @@ sqlVdbeMemCast(Mem * pMem, enum field_type type) if (sql_atoi64(pMem->z, (int64_t *) &pMem->u.i, = &is_neg, pMem->n) =3D=3D 0) { MemSetTypeFlag(pMem, MEM_Real); - pMem->u.r =3D pMem->u.i; + if (is_neg) + pMem->u.r =3D pMem->u.i; + else + pMem->u.r =3D pMem->u.u; return 0; } return ! sqlAtoF(pMem->z, &pMem->u.r, pMem->n); diff --git a/test/sql-tap/cast.test.lua b/test/sql-tap/cast.test.lua index 315f49d08..3027657a5 100755 --- a/test/sql-tap/cast.test.lua +++ b/test/sql-tap/cast.test.lua @@ -1,6 +1,6 @@ #!/usr/bin/env tarantool test =3D require("sqltester") -test:plan(82) +test:plan(84) =20 --!./tcltestrunner.lua -- 2005 June 25 @@ -793,6 +793,18 @@ if true then --test:execsql("PRAGMA = encoding")[1][1]=3D=3D"UTF-8" then }) end =20 +test:do_execsql_test( + "case-3.25", + [[ + SELECT CAST(x'3138343436373434303733373039353531363135' AS = REAL); + ]], { 1.844674407371e+19 } ) + +test:do_execsql_test( + "case-3.26", + [[ + SELECT CAST(x'3138343436373434303733373039353531363135' AS = INT); + ]], { 18446744073709551615LL } ) + test:do_execsql_test( "case-3.31", [[ > ------------------------- > vdbetrace.c:90 >=20 > Function sqlVdbeExpandSql stringifies integers, but > misses a version for big unsigned. diff --git a/src/box/sql/vdbetrace.c b/src/box/sql/vdbetrace.c index f78d5b093..2ee9f668c 100644 --- a/src/box/sql/vdbetrace.c +++ b/src/box/sql/vdbetrace.c @@ -151,6 +151,8 @@ sqlVdbeExpandSql(Vdbe * p, /* The prepared = statement being evaluated */ sqlStrAccumAppend(&out, "NULL", 4); } else if (pVar->flags & MEM_Int) { sqlXPrintf(&out, "%lld", pVar->u.i); + } else if (pVar->flags & MEM_UInt) { + sqlXPrintf(&out, "%llu", pVar->u.u); } else if (pVar->flags & MEM_Real) { sqlXPrintf(&out, "%!.15g", pVar->u.r); } else if (pVar->flags & MEM_Str) { > ------------------------- > vdbe.c:307 >=20 >> case FIELD_TYPE_INTEGER: >> case FIELD_TYPE_UNSIGNED: >> if ((record->flags & MEM_Int) =3D=3D MEM_Int) >> return 0; >> if ((record->flags & MEM_UInt) =3D=3D MEM_UInt) >> return 0; >> if ((record->flags & MEM_Real) =3D=3D MEM_Real) { >> int64_t i =3D (int64_t) record->u.r; >> if (i =3D=3D record->u.r) >> mem_set_int(record, record->u.r, >> record->u.r <=3D -1); >> return 0; >> } >=20 > It is a part of function mem_apply_type. When target type is > UNSIGNED, and a value is MEM_Int, you do nothing. Why? Looks like > it is possible to pass here a negative value, and CAST UNSIGNED > would do nothing. Basically, this function implements sort of implicit cast which takes place before comparison/assignment. For comparisons it makes no sense - we can compare integer with unsigned value - the latter is always greater. For assignment it is also meaningless: if we attempt at inserting negative values to unsigned field appropriate error will be raised anyway. If you can come up with specific example, let=E2=80=99s discuss it.