Обсуждение: Direct converting numeric types to bool

Поиск
Список
Период
Сортировка

Direct converting numeric types to bool

От
n.zhuchkov@postgrespro.ru
Дата:
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


Re: Direct converting numeric types to bool

От
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.

Вложения

Re: Direct converting numeric types to bool

От
Tom Lane
Дата:
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


Re: Direct converting numeric types to bool

От
Pavel Stehule
Дата:
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$

I really doesn't see any sense to allow cast from double to boolean

-1 from me

Regards

Pavel

Re: Direct converting numeric types to bool

От
Pavel Stehule
Дата:


2018-02-28 16:13 GMT+01:00 Pavel Stehule <pavel.stehule@gmail.com>:
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$

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 me

Regards

Pavel


RE: Direct converting numeric types to bool

От
"Alex Ignatov"
Дата:
-----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



Re: Direct converting numeric types to bool

От
n.zhuchkov@postgrespro.ru
Дата:
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


Re: Direct converting numeric types to bool

От
Tom Lane
Дата:
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