@>运算符不在int2 []类型上工作

时间:2013-02-19 19:39:25

标签: postgresql

我有一个int2 []数组类型的列。我想在这个专栏上进行IN查询。

我在谷歌上搜索它,发现了@>运算符在数组列中搜索,但它似乎不适用于int2类型的数组列。但它适用于int []

有谁知道为什么它不能用于int2 []?

ecabuk=# CREATE TABLE "Test"("Column1" int2[]);
CREATE TABLE

ecabuk=# INSERT INTO "Test" VALUES ('{10, 15, 20}');
INSERT 0 1

ecabuk=# INSERT INTO "Test" VALUES ('{10, 20, 30}');
INSERT 0 1

ecabuk=# EXPLAIN ANALYZE SELECT * FROM "Test" WHERE "Column1" @> ARRAY[20];
ERROR:  operator does not exist: smallint[] @> integer[]
LINE 1: ...LAIN ANALYZE SELECT * FROM "Test" WHERE "Column1" @> ARRAY[2...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

ecabuk=# ALTER TABLE "Test" ALTER COLUMN "Column1" type int[];
ALTER TABLE

ecabuk=# EXPLAIN ANALYZE SELECT * FROM "Test" WHERE "Column1" @> ARRAY[20];
                                            QUERY PLAN                                            
--------------------------------------------------------------------------------------------------
 Seq Scan on "Test"  (cost=0.00..26.38 rows=7 width=32) (actual time=0.200..0.204 rows=2 loops=1)
   Filter: ("Column1" @> '{20}'::integer[])
 Total runtime: 0.256 ms
(3 rows)

ecabuk=# 

2 个答案:

答案 0 :(得分:1)

当你指定ARRAY [20]时,它将其视为int []而不是int2 []。合适的演员阵容会让它表现得很好。

test=# EXPLAIN ANALYZE SELECT * FROM "Test" WHERE "Column1" @> ARRAY[2];
ERROR:  operator does not exist: smallint[] @> integer[]
LINE 1: ...LAIN ANALYZE SELECT * FROM "Test" WHERE "Column1" @> ARRAY[2...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
test=# EXPLAIN ANALYZE SELECT * FROM "Test" WHERE "Column1" @> ARRAY[20::int2];
                                            QUERY PLAN

--------------------------------------------------------------------------------------------------
 Seq Scan on "Test"  (cost=0.00..26.38 rows=7 width=32) (actual time=0.060..0.064 rows=2 loops=1)
   Filter: ("Column1" @> '{20}'::smallint[])
 Total runtime: 0.116 ms
(3 rows)


test=# EXPLAIN ANALYZE SELECT * FROM "Test" WHERE "Column1" @> ARRAY[20]::int2[];
                                            QUERY PLAN

--------------------------------------------------------------------------------------------------
 Seq Scan on "Test"  (cost=0.00..26.38 rows=7 width=32) (actual time=0.028..0.032 rows=2 loops=1)
   Filter: ("Column1" @> '{20}'::smallint[])
 Total runtime: 0.080 ms
(3 rows)

答案 1 :(得分:0)

在这里工作。你需要提供一个我害怕失败的例子:

=> SELECT ARRAY[1,2,3]::int2[] @> ARRAY[1,2]::int2[];
 ?column? 
----------
   t
  (1 row)

=> SELECT ARRAY[1,2,3]::int2[] @> ARRAY[3,4]::int2[];
 ?column? 
----------
  f
 (1 row)

如果它不起作用会很奇怪,因为它是在任何阵列上定义的:

=> \do '@>'
                               List of operators

   Schema   | Name | Left arg type | Right arg type | Result type | Description 
------------+------+---------------+----------------+-------------+-------------
 pg_catalog | @>   | aclitem[]     | aclitem        | boolean     | contains
 pg_catalog | @>   | anyarray      | anyarray       | boolean     | contains
 ...

在9.1和9.2中测试