Thread: Direct converting numeric types to bool
Attached patch allow direct convertion of numeric types to bool like integer::bool. Supported types: - smallint; - bigint; - real; - double precision; - decimal(numeric). This functionality is helped with migration from Oracle. -- Nikita Zhuchkov Postgres Professional: http://www.postgrespro.com The Russian Postgres Company
n.zhuchkov@postgrespro.ru писал 2018-02-28 18:04: > Attached patch allow direct convertion of numeric types to bool like > integer::bool. > Supported types: > - smallint; > - bigint; > - real; > - double precision; > - decimal(numeric). > > This functionality is helped with migration from Oracle.
Attachment
n.zhuchkov@postgrespro.ru writes: > Attached patch allow direct convertion of numeric types to bool like > integer::bool. > Supported types: > - smallint; > - bigint; > - real; > - double precision; > - decimal(numeric). > This functionality is helped with migration from Oracle. I think you forgot to attach the patch, but in any case: is this really a behavior we want? "Oracle has it" is not a good argument in my view, nor do I recall people complaining that they need such a behavior to migrate. regards, tom lane
Hi
2018-02-28 16:06 GMT+01:00 <n.zhuchkov@postgrespro.ru>:
n.zhuchkov@postgrespro.ru писал 2018-02-28 18:04:Attached patch allow direct convertion of numeric types to bool like
integer::bool.
Supported types:
- smallint;
- bigint;
- real;
- double precision;
- decimal(numeric).
This functionality is helped with migration from Oracle.
Looks little bit obscure to upstream code (can lives as extension outside)
all work can be done be function
CREATE OR REPLACE FUNCTION public.to_bool(anyelement)
RETURNS boolean
LANGUAGE sql
IMMUTABLE STRICT
AS $function$
select $1::int::boolean $function$
CREATE OR REPLACE FUNCTION public.to_bool(anyelement)
RETURNS boolean
LANGUAGE sql
IMMUTABLE STRICT
AS $function$
select $1::int::boolean $function$
I really doesn't see any sense to allow cast from double to boolean
-1 from me
Regards
Pavel
2018-02-28 16:13 GMT+01:00 Pavel Stehule <pavel.stehule@gmail.com>:
Hi2018-02-28 16:06 GMT+01:00 <n.zhuchkov@postgrespro.ru>:n.zhuchkov@postgrespro.ru писал 2018-02-28 18:04:Attached patch allow direct convertion of numeric types to bool like
integer::bool.
Supported types:
- smallint;
- bigint;
- real;
- double precision;
- decimal(numeric).
This functionality is helped with migration from Oracle.Looks little bit obscure to upstream code (can lives as extension outside)all work can be done be function
CREATE OR REPLACE FUNCTION public.to_bool(anyelement)
RETURNS boolean
LANGUAGE sql
IMMUTABLE STRICT
AS $function$
select $1::int::boolean $function$I really doesn't see any sense to allow cast from double to boolean
Long time Oracle had not boolean, so some ugly tricks was necessary there. There are not reason do same in Postgres.
-1 from meRegardsPavel
-----Original Message----- From: n.zhuchkov@postgrespro.ru [mailto:n.zhuchkov@postgrespro.ru] Sent: Wednesday, February 28, 2018 6:04 PM To: pgsql-hackers <pgsql-hackers@postgresql.org> Subject: Direct converting numeric types to bool Attached patch allow direct convertion of numeric types to bool like integer::bool. Supported types: - smallint; - bigint; - real; - double precision; - decimal(numeric). This functionality is helped with migration from Oracle. -- Nikita Zhuchkov Postgres Professional: http://www.postgrespro.com The Russian Postgres Company Hello! What prevent us from: postgres=# select 1::bigint::int::boolean; bool ------ t (1 row) It is just one additional casting and required no additional patching -- Alex Ignatov Postgres Professional: http://www.postgrespro.com The Russian Postgres Company
Thanks for your feedback! Really, support migration from Oracle isn't the best argumentation for this patch. Yes, in postgres already exists int::boolean casting and in case: select 10::bigint::int::boolean; it will perfectly work. But if you want to cast more significant number: select (2^32)::bigint::int::boolean; you receive the boundary value error. Also in operations with floating-point numbers, may arise a fault in the results of calculations, because of which the number will always be cast in true Thus it sounds like not a bad idea to support direct casting from numeric types to boolean. What do you think? Alex Ignatov писал 2018-02-28 18:23: > -----Original Message----- > From: n.zhuchkov@postgrespro.ru [mailto:n.zhuchkov@postgrespro.ru] > Sent: Wednesday, February 28, 2018 6:04 PM > To: pgsql-hackers <pgsql-hackers@postgresql.org> > Subject: Direct converting numeric types to bool > > Attached patch allow direct convertion of numeric types to bool like > integer::bool. > Supported types: > - smallint; > - bigint; > - real; > - double precision; > - decimal(numeric). > > This functionality is helped with migration from Oracle. > > -- > Nikita Zhuchkov > Postgres Professional: http://www.postgrespro.com The Russian Postgres > Company > > Hello! > > What prevent us from: > > postgres=# select 1::bigint::int::boolean; > bool > ------ > t > (1 row) > > It is just one additional casting and required no additional patching > -- > Alex Ignatov > Postgres Professional: http://www.postgrespro.com > The Russian Postgres Company
n.zhuchkov@postgrespro.ru writes: > Yes, in postgres already exists int::boolean casting and in case: > select 10::bigint::int::boolean; > it will perfectly work. But if you want to cast more significant number: > select (2^32)::bigint::int::boolean; > you receive the boundary value error. Right. I don't see a reason why we shouldn't create a bigint-to-bool cast to fix that, and smallint-to-bool maybe for completeness. However, I'm less excited about float or numeric to bool, because I don't think there is any very principled argument about what such a cast should do with infinities, NaNs, or denormalized numbers. Somebody who knows what should happen for their own application can create their own cast that handles those cases ... but I'm unsure that there's a one-size- fits-all answer that we could put into a default behavior. regards, tom lane