Thread: datatype of constant is not propagated into aggregate query

datatype of constant is not propagated into aggregate query

From
Pavel Stehule
Date:
Hello

create table t1(d date, n integer);

postgres=# insert into t1 select '2001-01-01', 1 from generate_series(1,3);
INSERT 0 3

but

postgres=# insert into t1 select distinct '2001-01-01', 1 from
generate_series(1,3);
ERROR:  column "d" is of type date but expression is of type text
LINE 1: insert into t1 select distinct '2001-01-01', 1 from generate...                                      ^
HINT:  You will need to rewrite or cast the expression.

Regards

Pavel Stehule


Re: datatype of constant is not propagated into aggregate query

From
Tom Lane
Date:
Pavel Stehule <pavel.stehule@gmail.com> writes:
> postgres=# insert into t1 select distinct '2001-01-01', 1 from
> generate_series(1,3);
> ERROR:  column "d" is of type date but expression is of type text
> LINE 1: insert into t1 select distinct '2001-01-01', 1 from generate...
>                                        ^
> HINT:  You will need to rewrite or cast the expression.

The DISTINCT forces the sub-SELECT to decide on a type for the constant
(so that it can pick a semantics for DISTINCT-ing).  And it chooses text
by default.  This is maybe not terribly convenient, but I'm not sure it
would be a good idea at all for a surrounding INSERT to change the
semantics of a SELECT.  Even discounting the implementation
difficulties, I don't think that'd satisfy the POLA.

(We do have a hack for adjusting the type if what the sub-select returns
is still of type UNKNOWN; that's safe because it implies that the
sub-select does not care about the type of the result column.)
        regards, tom lane


Re: datatype of constant is not propagated into aggregate query

From
Pavel Stehule
Date:
2012/3/11 Tom Lane <tgl@sss.pgh.pa.us>:
> Pavel Stehule <pavel.stehule@gmail.com> writes:
>> postgres=# insert into t1 select distinct '2001-01-01', 1 from
>> generate_series(1,3);
>> ERROR:  column "d" is of type date but expression is of type text
>> LINE 1: insert into t1 select distinct '2001-01-01', 1 from generate...
>>                                        ^
>> HINT:  You will need to rewrite or cast the expression.
>
> The DISTINCT forces the sub-SELECT to decide on a type for the constant
> (so that it can pick a semantics for DISTINCT-ing).  And it chooses text
> by default.  This is maybe not terribly convenient, but I'm not sure it
> would be a good idea at all for a surrounding INSERT to change the
> semantics of a SELECT.  Even discounting the implementation
> difficulties, I don't think that'd satisfy the POLA.
>
> (We do have a hack for adjusting the type if what the sub-select returns
> is still of type UNKNOWN; that's safe because it implies that the
> sub-select does not care about the type of the result column.)

in this case the constant is forwarded to result without any change,
so it can be UNKNOWN.

Pavel

>
>                        regards, tom lane