Thread: Union with array types

Union with array types

From
Juhan-Peep Ernits
Date:
Hello!

The example is the following:

create table arraytest ( id int,  mystuff varchar(40)[][]);
insert into arraytest values (1,'{{"a","b"}}');
insert into arraytest values (2,'{{"c","d"}}');


 (select id, mystuff from arraytest where id=1) union (select id, mystuf
f from arraytest where id=2);

ERROR:  Unable to identify an ordering operator '<' for type '_varchar'
        Use an explicit ordering operator or modify the query

PG version is CVS from Feb 26, 2001.

What kind of explicit ordering operator would one need? Since the array
type seems to remember only the fact that it is an array of a particular
type, not the dimension (ie if one creates a field of type
varchar(40)[][] the result is the same as creating a field of type
varchar(40)[] and it is possible to store stuff in the form
varchar(40)[][][] also in that field), it is up to the person who
creates the union to ensure that the types actually match.
Is it necessary to require the explicit ordering operator in this
case? What am I missing?

Best regards,

Juhan Ernits




Re: Union with array types

From
Tom Lane
Date:
Juhan-Peep Ernits <juhan@cc.ioc.ee> writes:
>  (select id, mystuff from arraytest where id=1) union (select id, mystuf
> f from arraytest where id=2);

> ERROR:  Unable to identify an ordering operator '<' for type '_varchar'
>         Use an explicit ordering operator or modify the query

> Is it necessary to require the explicit ordering operator in this
> case?

Yes, because UNION implies duplicate removal, which requires sorting.
If you don't actually need duplicate removal, use UNION ALL.

            regards, tom lane