query on smallint array column

Started by Zhihong Yuabout 5 years ago3 messages
#1Zhihong Yu
zyu@yugabyte.com

Hi,
I was experimenting with the following query.

create table sint1(k int primary key, arr smallint[]);
create index s1 on sint1(arr);
insert into sint1 select s, array[s*s, s] FROM generate_series(1, 10) AS s;
select * from sint1 where arr @> array[4];
ERROR: operator does not exist: smallint[] @> integer[]
LINE 1: select * from sint1 where arr @> array[4];
^
HINT: No operator matches the given name and argument types. You might
need to add explicit type casts.
-------

I wonder if someone can enlighten me on the correct way to perform the type
cast.

Thanks

#2Pavel Stehule
pavel.stehule@gmail.com
In reply to: Zhihong Yu (#1)
Re: query on smallint array column

Hi

ne 13. 12. 2020 v 18:42 odesílatel Zhihong Yu <zyu@yugabyte.com> napsal:

Hi,
I was experimenting with the following query.

create table sint1(k int primary key, arr smallint[]);
create index s1 on sint1(arr);
insert into sint1 select s, array[s*s, s] FROM generate_series(1, 10) AS s;
select * from sint1 where arr @> array[4];
ERROR: operator does not exist: smallint[] @> integer[]
LINE 1: select * from sint1 where arr @> array[4];
^
HINT: No operator matches the given name and argument types. You might
need to add explicit type casts.
-------

I wonder if someone can enlighten me on the correct way to perform the
type cast.

postgres=# select * from sint1 where arr @> array[4::smallint];
┌───┬────────┐
│ k │ arr │
╞═══╪════════╡
│ 2 │ {4,2} │
│ 4 │ {16,4} │
└───┴────────┘
(2 rows)

postgres=# select * from sint1 where arr @> array[4]::smallint[];
┌───┬────────┐
│ k │ arr │
╞═══╪════════╡
│ 2 │ {4,2} │
│ 4 │ {16,4} │
└───┴────────┘
(2 rows)

postgres=#

Show quoted text

Thanks

#3Zhihong Yu
zyu@yugabyte.com
In reply to: Pavel Stehule (#2)
Re: query on smallint array column

Thanks Pavel for fast response.

On Sun, Dec 13, 2020 at 9:51 AM Pavel Stehule <pavel.stehule@gmail.com>
wrote:

Show quoted text

Hi

ne 13. 12. 2020 v 18:42 odesílatel Zhihong Yu <zyu@yugabyte.com> napsal:

Hi,
I was experimenting with the following query.

create table sint1(k int primary key, arr smallint[]);
create index s1 on sint1(arr);
insert into sint1 select s, array[s*s, s] FROM generate_series(1, 10) AS
s;
select * from sint1 where arr @> array[4];
ERROR: operator does not exist: smallint[] @> integer[]
LINE 1: select * from sint1 where arr @> array[4];
^
HINT: No operator matches the given name and argument types. You might
need to add explicit type casts.
-------

I wonder if someone can enlighten me on the correct way to perform the
type cast.

postgres=# select * from sint1 where arr @> array[4::smallint];
┌───┬────────┐
│ k │ arr │
╞═══╪════════╡
│ 2 │ {4,2} │
│ 4 │ {16,4} │
└───┴────────┘
(2 rows)

postgres=# select * from sint1 where arr @> array[4]::smallint[];
┌───┬────────┐
│ k │ arr │
╞═══╪════════╡
│ 2 │ {4,2} │
│ 4 │ {16,4} │
└───┴────────┘
(2 rows)

postgres=#

Thanks