Combining array slicing and indexing causes incorrect/confusing results

Started by Jelte Fennema-Nioabout 1 year ago2 messagesbugs
Jump to latest
#1Jelte Fennema-Nio
postgres@jeltef.nl

Due to the way SubscriptingRef works there's no way for it to describe
a subscript sequence that both does a slice and a regular index in the
same SubscriptingRef. This results into weird behaviour like the
following:

select ('{1, 2, 3, 4}'::int[])[1:4][2];

int4
──────
{}
(1 row)

It's possible to get the expected result by adding some additional parenthesis

select (('{1, 2, 3, 4}'::int[])[1:4])[2];

int4
──────
2
(1 row)

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Jelte Fennema-Nio (#1)
Re: Combining array slicing and indexing causes incorrect/confusing results

Jelte Fennema-Nio <me@jeltef.nl> writes:

Due to the way SubscriptingRef works there's no way for it to describe
a subscript sequence that both does a slice and a regular index in the
same SubscriptingRef.

Well, yeah, because they're fundamentally different operations.
A slice produces a value of the array type, a regular index
operation produces a value of the element type. So if you
mix the notations we assume you really meant slicing in each
subscript.

regards, tom lane