Why is this SELECT evaluated?

From: Miles Elam <miles(dot)elam(at)productops(dot)com>
To: PG-General Mailing List <pgsql-general(at)postgresql(dot)org>
Subject: Why is this SELECT evaluated?
Date: 2023-01-28 19:29:13
Message-ID: CAALojA-nHoxDr7B2k0e1-EtGMPsGPZiCVeS_ds0aHG0SEOrPxg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Postgres v15

Given this example of a conversion from a byte array to an int8
masquerading as an "unsigned" int4

SELECT (get_byte(bytes, byte_offset)::int8 << 24)
| (get_byte(bytes, byte_offset + 1) << 16)
| (get_byte(bytes, byte_offset + 2) << 8)
| (get_byte(bytes, byte_offset + 3))
FROM ( VALUES ('\x010000'::bytea, 0) ) b(bytes, byte_offset)
WHERE length(bytes) >= (4 + byte_offset)
;

Why does this error result?

ERROR: index 3 out of valid range, 0..2
SQL state: 2202E

I was under the impression that if the WHERE clause evaluated to
false, the SELECT clause would not be evaluated. Why is get_byte(...)
ever run in the first place even though length(bytes) is 3?

- Miles Elam

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Erik Wienhold 2023-01-28 20:34:01 Re: Why is this SELECT evaluated?
Previous Message Ron 2023-01-28 02:26:31 Re: Indexes mysteriously change to ON ONLY