Last active
December 20, 2025 18:32
-
-
Save JasonKleban/d8ae92481c94c512cbe7f690aae1d82a to your computer and use it in GitHub Desktop.
Postgresql - Logical casts Int4 to/from Int8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| int4_bits_from_int8 := (WITH ValueAndShiftRight(int8_bits, shiftRight) AS ( | |
| SELECT -8187830383155378919::bigint, 32 | |
| ) | |
| SELECT | |
| (CASE (int8_bits::bit(64) >> shiftRight)::bigint & (1::bigint << 31) | |
| WHEN 0 THEN ((int8_bits::bit(64) >> shiftRight) & x'FFFFFFFF'::bigint::bit(64))::bigint | |
| ELSE ~((~((int8_bits::bit(64) >> shiftRight))::bigint & x'FFFFFFFF'::bigint)::int) END)::int | |
| FROM ValueAndShiftRight); | |
| int8_bits_from_int4 := (WITH ValueAndShiftLeft(int4_bits, shiftLeft) AS ( | |
| SELECT -11::int, 31 | |
| ) | |
| SELECT | |
| CASE | |
| WHEN 0 <= int4_bits THEN int4_bits::bigint << shiftLeft | |
| ELSE (~((~int4_bits)::bigint) & x'FFFFFFFF'::bigint) << shiftLeft END | |
| FROM ValueAndShiftLeft); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment