如何选择不为空的数组?

Eva*_*oll 5 postgresql null condition array postgresql-9.5

为什么这如此棘手,令牌设置为什么,它不等于 null 也不等于空字符串?

SELECT lexemes
FROM ts_debug('This is a title')
WHERE alias = 'asciiword';

 lexemes 
---------
 {}
 {}
 {}
 {titl}
(4 rows)
Run Code Online (Sandbox Code Playgroud)

好吧..所以我想摆脱{}

SELECT lexemes
FROM ts_debug('This is a title')
WHERE alias = 'asciiword'
  AND lexemes <> '{}'
  AND lexemes <> ARRAY[]::text[]
  AND lexemes IS NOT NULL
  AND lexemes <> ARRAY[' ']
  AND lexemes <> ARRAY[null]::text[];
Run Code Online (Sandbox Code Playgroud)

我知道其中大多数都行不通。,但我完全困惑为什么<> '{}'不起作用<> ARRAY[]::text;。我该如何过滤掉这个?

Erw*_*ter 5

原因似乎是该列中的空字符串具有数组维度[1:0]。通常应该是NULL. 看:

SELECT lexemes, array_dims(lexemes) FROM ts_debug('a title');

 lexemes | array_dims
---------+------------
 {}      | [1:0]  -- !!
         |
 {titl}  | [1:1]
Run Code Online (Sandbox Code Playgroud)

空数组通常将 NULL 作为数组维度。

SELECT '{}'::text[] AS test, array_dims('{}'::text[]);

 test    | array_dims
---------+------------
 {}      | <NULL>
Run Code Online (Sandbox Code Playgroud)

因此,比较lexemes = '{}'::text[]返回FALSE。对我来说看起来像一个错误。我测试了 8.4 - 10 Beta 版本。所有版本都有。

作为解决方法,排除所有空数组,包括奇数情况(和 NULL):

SELECT *
FROM   ts_debug('This is a title')
WHERE  cardinality(lexemes) > 0;
Run Code Online (Sandbox Code Playgroud)

或者比较文本表示:

...
AND    lexemes::text <> '{}';
Run Code Online (Sandbox Code Playgroud)

我提交了错误报告#14826。