By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
Help with an interesting Postgres question: Why isn't an Index Only Scan used on a partition accessed via the parent table?.
-- Type resolution has to kick in **before** the array constructor is applied
-- as there is not "array of unknown"
-- The default type of a quoted string literal ("string constant") is text
-- So the resulting type is text[]
SELECT pg_typeof(ARRAY['1']);
pg_typeof |
---|
text[] |
-- And there is no operator for integer[] && text[]:
SELECT '{1,2,3}'::int[] && ARRAY['1'];
ERROR: operator does not exist: integer[] && text[]
LINE 2: SELECT '{1,2,3}'::int[] && ARRAY['1'];
^
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
-- An explicit cast can fix it.
-- Ideally on the element type, *before* forming the array:
SELECT '{1,2,3}'::int[] && ARRAY[int '1']
, '{1,2,3}'::int[] && ARRAY['1'::integer]
-- But casting the array itself works, too:
, '{1,2,3}'::int[] && ARRAY['1']::integer[]
, '{1,2,3}'::int[] && ARRAY['1']::int[];
?column? | ?column? | ?column? | ?column? |
---|---|---|---|
t | t | t | t |
-- Without ARRAY constructor, type resolution can derive a fitting type from the implicit assignment in the expression:
SELECT 1 <> '1'; -- int is derived
?column? |
---|
f |
-- If *typed values* are passed in, you need to cast either way
SELECT '{1,2,3}'::int[] && ARRAY[text '1']; -- nothing to assume, incompatiple by definition
ERROR: operator does not exist: integer[] && text[]
LINE 2: SELECT '{1,2,3}'::int[] && ARRAY[text '1']; -- nothing to a...
^
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
-- If *typed values* are passed in, you need to cast either way
SELECT 1 <> text '1'; -- nothing to assume, incompatiple by definition
ERROR: operator does not exist: integer <> text
LINE 2: SELECT 1 <> text '1'; -- nothing to assume, incompatiple by ...
^
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
-- Works with explicit cast like demonstrated above:
SELECT '{1,2,3}'::int[] && ARRAY[(text '1')::int], 1 <> (text '1')::int;
?column? | ?column? |
---|---|
t | f |