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 F094E24F0D for ; Fri, 5 Jul 2019 12:36:11 -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 6KGvI0q6hnLt for ; Fri, 5 Jul 2019 12:36:11 -0400 (EDT) Received: from smtp32.i.mail.ru (smtp32.i.mail.ru [94.100.177.92]) (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 AF3DF24EED for ; Fri, 5 Jul 2019 12:36:11 -0400 (EDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.8\)) Subject: [tarantool-patches] Re: [PATCH 4/6] sql: make built-in functions operate on unsigned values From: "n.pettik" In-Reply-To: Date: Fri, 5 Jul 2019 19:36:09 +0300 Content-Transfer-Encoding: quoted-printable Message-Id: <6ECEFD7B-62E4-45AF-A6AE-030B714DD0C0@tarantool.org> References: <4acf9fab112e7ca119f1b2bf5efea53b3831ea4f.1559919361.git.korablev@tarantool.org> <4cbc3612-b655-8b2b-d879-6b1445c9535b@tarantool.org> <947FF2ED-8FBF-4796-8CFB-C12BE610A90A@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 > On 2 Jul 2019, at 00:53, Vladislav Shpilevoy = wrote: > Thanks for the fixes! > On 01/07/2019 16:21, n.pettik wrote: >>=20 >>>> diff --git a/src/box/sql/func.c b/src/box/sql/func.c >>>> index 457c9b92b..365f8f9d2 100644 >>>> --- a/src/box/sql/func.c >>>> +++ b/src/box/sql/func.c >>>> @@ -645,21 +638,13 @@ ICU_CASE_CONVERT(Upper); >>>> static void >>>> randomFunc(sql_context * context, int NotUsed, sql_value ** = NotUsed2) >>>> { >>>> - sql_int64 r; >>>> + int64_t r; >>>> UNUSED_PARAMETER2(NotUsed, NotUsed2); >>>> sql_randomness(sizeof(r), &r); >>>> - if (r < 0) { >>>> - /* We need to prevent a random number of = 0x8000000000000000 >>>> - * (or -9223372036854775808) since when you do abs() of = that >>>> - * number of you get the same value back again. To do = this >>>> - * in a way that is testable, mask the sign bit off of = negative >>>> - * values, resulting in a positive value. Then take the >>>> - * 2s complement of that positive value. The end result = can >>>> - * therefore be no less than -9223372036854775807. >>>> - */ >>>> - r =3D -(r & LARGEST_INT64); >>>> - } >>>> - sql_result_int64(context, r); >>>> + if (r < 0) >>>> + sql_result_int(context, r); >>>> + else >>>> + sql_result_uint(context, r); >>>=20 >>> 1. To avoid such mess I propose you the same as for mem_set_int - = make >>> several functions: >>=20 >> What kind of mess do you mean? Here we have two functions: >> one sets unsigned result, another one - signed. Pretty straight >> approach, isn=E2=80=99t it? What is more, this is the only place = where >> branching is required. >=20 > If it is so straight, then why do you need branching at all? > I see, that sql_result_int() calls mem_set_i64, which is able > to take positive values. >=20 > I've done this and the tests passed. Why? You had already answered your question: > I see, that sql_result_int() calls mem_set_i64, which is able > to take positive values. Anyway sql_randomness() doesn=E2=80=99t return sign of random number. So actually, your diff is completely correct and I=E2=80=99ve applied = it. > @@ -621,10 +621,7 @@ randomFunc(sql_context * context, int NotUsed, = sql_value ** NotUsed2) > int64_t r; > UNUSED_PARAMETER2(NotUsed, NotUsed2); > sql_randomness(sizeof(r), &r); > - if (r < 0) > - sql_result_int(context, r); > - else > - sql_result_uint(context, r); > + sql_result_int(context, r); > } >=20 >>=20 >>> /* Return an int64 value, sign is detected inside. */ >>> sql_result_i64(int64_t value); >>>=20 >>> /* Return a uint64 value. */ >>> sql_result_u64(uint64_t value); >>>=20 >>> /* Return an integer value with explicit sign. */ >>> sql_result_int(int64_t value, bool is_negative); >>>=20 >>>> diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h >>>> index c0e2ab699..4697e3003 100644 >>>> --- a/src/box/sql/sqlInt.h >>>> +++ b/src/box/sql/sqlInt.h >>>> @@ -455,6 +455,9 @@ sql_column_subtype(struct sql_stmt *stmt, int = i); >>>> sql_int64 >>>> sql_value_int64(sql_value *); >>>>=20 >>>> +uint64_t >>>> +sql_value_uint64(sql_value *val); >>>> + >>>> const unsigned char * >>>> sql_value_text(sql_value *); >>>>=20 >>>> @@ -496,13 +499,13 @@ void >>>> sql_result_error_code(sql_context *, int); >>>>=20 >>>> void >>>> -sql_result_int(sql_context *, int); >>>> +sql_result_int(sql_context *, int64_t); >>>>=20 >>>> void >>>> -sql_result_bool(struct sql_context *ctx, bool value); >>>> +sql_result_uint(sql_context *ctx, int64_t u_val); >>>=20 >>> 2. Why result_uint takes int instead of uint? >>=20 >> Because mem_set_int() accepts int64 and to avoid >> extra casts to uint (since almost al used values are singed). >=20 > This function has nothing to do with mem_set_int(). > This function calls mem_set_u64(), and its argument is > unsigned. So what is a problem, again? Sorry, answered before I introduced mem_set_u64(). Now there=E2=80=99s no problem: diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h index 088cfbc51..976b4486a 100644 --- a/src/box/sql/sqlInt.h +++ b/src/box/sql/sqlInt.h @@ -398,7 +398,7 @@ void sql_result_double(sql_context *, double); =20 void -sql_result_uint(sql_context *ctx, int64_t u_val); +sql_result_uint(sql_context *ctx, uint64_t u_val); =20 void sql_result_int(sql_context *, int64_t); diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c index 2b1115074..ff7ce658b 100644 --- a/src/box/sql/vdbeapi.c +++ b/src/box/sql/vdbeapi.c @@ -330,7 +330,7 @@ sql_result_double(sql_context * pCtx, double rVal) } =20 void -sql_result_uint(sql_context *ctx, int64_t u_val) +sql_result_uint(sql_context *ctx, uint64_t u_val) { mem_set_u64(ctx->pOut, u_val); }