-- Test: Edge Cases for Direct Numeric-Integer Comparison -- Purpose: Verify precision boundaries, overflow, and type alias handling -- Precision Boundary Tests for float4 (mantissa can exactly represent up to 2^24) -- float4 can exactly represent integers up to 16,777,216 SELECT '16777216::float4 = 16777216::int8'::text AS test, 16777216::float4 = 16777216::int8 AS result; test | result -----------------------------------+-------- 16777216::float4 = 16777216::int8 | t (1 row) SELECT '16777217::float4 = 16777217::int8'::text AS test, 16777217::float4 = 16777217::int8 AS result; -- Should be false (precision loss) test | result -----------------------------------+-------- 16777217::float4 = 16777217::int8 | f (1 row) -- Precision Boundary Tests for float8 (mantissa can exactly represent up to 2^53) -- float8 can exactly represent integers up to 9,007,199,254,740,992 SELECT '9007199254740992::float8 = 9007199254740992::int8'::text AS test, 9007199254740992::float8 = 9007199254740992::int8 AS result; test | result ---------------------------------------------------+-------- 9007199254740992::float8 = 9007199254740992::int8 | t (1 row) SELECT '9007199254740993::float8 = 9007199254740993::int8'::text AS test, 9007199254740993::float8 = 9007199254740993::int8 AS result; -- Should be false (precision loss) test | result ---------------------------------------------------+-------- 9007199254740993::float8 = 9007199254740993::int8 | f (1 row) -- Overflow Tests: numeric/float values outside integer type ranges SELECT '2147483648::numeric = 2147483647::int4'::text AS test, 2147483648::numeric = 2147483647::int4 AS result; -- numeric > int4_max test | result ----------------------------------------+-------- 2147483648::numeric = 2147483647::int4 | f (1 row) SELECT '2147483648::numeric > 2147483647::int4'::text AS test, 2147483648::numeric > 2147483647::int4 AS result; -- Should be true test | result ----------------------------------------+-------- 2147483648::numeric > 2147483647::int4 | t (1 row) SELECT '-2147483649::numeric = -2147483648::int4'::text AS test, (-2147483649.0)::numeric = (-2147483648)::int4 AS result; -- numeric < int4_min test | result ------------------------------------------+-------- -2147483649::numeric = -2147483648::int4 | f (1 row) SELECT '-2147483649::numeric < -2147483648::int4'::text AS test, (-2147483649.0)::numeric < (-2147483648)::int4 AS result; -- Should be true test | result ------------------------------------------+-------- -2147483649::numeric < -2147483648::int4 | t (1 row) -- Serial Type Alias Tests (smallserial = int2, serial = int4, bigserial = int8) CREATE TEMPORARY TABLE t_serial ( id_small SMALLSERIAL, id_normal SERIAL, id_big BIGSERIAL ); INSERT INTO t_serial DEFAULT VALUES; INSERT INTO t_serial DEFAULT VALUES; SELECT 'smallserial = numeric'::text AS test, id_small = 1::numeric AS result FROM t_serial WHERE id_small = 1; test | result -----------------------+-------- smallserial = numeric | t (1 row) SELECT 'serial = numeric'::text AS test, id_normal = 2::numeric AS result FROM t_serial WHERE id_normal = 2; test | result ------------------+-------- serial = numeric | t (1 row) SELECT 'bigserial = numeric'::text AS test, id_big = 1::numeric AS result FROM t_serial WHERE id_big = 1; test | result ---------------------+-------- bigserial = numeric | t (1 row) -- Decimal Type Alias Test (decimal = numeric) CREATE TEMPORARY TABLE t_decimal (val DECIMAL(10,2)); INSERT INTO t_decimal VALUES (100.00), (200.50); SELECT 'decimal = int4 (exact)'::text AS test, val = 100::int4 AS result FROM t_decimal WHERE val = 100::int4; test | result ------------------------+-------- decimal = int4 (exact) | t (1 row) SELECT 'decimal = int4 (fractional)'::text AS test, val = 200::int4 AS result FROM t_decimal WHERE val = 200::int4; -- Should be false test | result ------+-------- (0 rows) -- Edge Case: Zero in different representations SELECT '0::numeric = 0::int4'::text AS test, 0::numeric = 0::int4 AS result; test | result ----------------------+-------- 0::numeric = 0::int4 | t (1 row) SELECT '0.0::numeric = 0::int4'::text AS test, 0.0::numeric = 0::int4 AS result; test | result ------------------------+-------- 0.0::numeric = 0::int4 | t (1 row) SELECT '-0::numeric = 0::int4'::text AS test, (-0)::numeric = 0::int4 AS result; test | result -----------------------+-------- -0::numeric = 0::int4 | t (1 row) -- Edge Case: Numerics with implicit zero digits (weight > ndigits - 1) -- In base-10000, 100000000 has weight=2 but only one stored digit [1] -- The middle and trailing digit positions are implicitly zero SELECT '100000000::numeric = 100000000::int8'::text AS test, 100000000::numeric = 100000000::int8 AS result; -- Should be true test | result --------------------------------------+-------- 100000000::numeric = 100000000::int8 | t (1 row) SELECT '100000000.0::numeric = 100000000::int8'::text AS test, 100000000.0::numeric = 100000000::int8 AS result; -- Should be true (trailing .0) test | result ----------------------------------------+-------- 100000000.0::numeric = 100000000::int8 | t (1 row) SELECT '100000000.5::numeric = 100000000::int8'::text AS test, 100000000.5::numeric = 100000000::int8 AS result; -- Should be false (has fraction) test | result ----------------------------------------+-------- 100000000.5::numeric = 100000000::int8 | f (1 row) SELECT '100000000.5::numeric > 100000000::int8'::text AS test, 100000000.5::numeric > 100000000::int8 AS result; -- Should be true test | result ----------------------------------------+-------- 100000000.5::numeric > 100000000::int8 | t (1 row) SELECT '10000::numeric = 10000::int4'::text AS test, 10000::numeric = 10000::int4 AS result; -- Exactly one base-10000 digit test | result ------------------------------+-------- 10000::numeric = 10000::int4 | t (1 row) SELECT '10001::numeric = 10001::int4'::text AS test, 10001::numeric = 10001::int4 AS result; -- Two digits: [1, 1] test | result ------------------------------+-------- 10001::numeric = 10001::int4 | t (1 row) SELECT '-100000000::numeric = -100000000::int8'::text AS test, (-100000000)::numeric = (-100000000)::int8 AS result; -- Negative with implicit zeros test | result ----------------------------------------+-------- -100000000::numeric = -100000000::int8 | t (1 row) SELECT '-100000000.5::numeric < -100000000::int8'::text AS test, (-100000000.5)::numeric < (-100000000)::int8 AS result; -- Should be true test | result ------------------------------------------+-------- -100000000.5::numeric < -100000000::int8 | t (1 row) SELECT '0::float4 = 0::int4'::text AS test, 0::float4 = 0::int4 AS result; test | result ---------------------+-------- 0::float4 = 0::int4 | t (1 row) SELECT '0::float8 = 0::int4'::text AS test, 0::float8 = 0::int4 AS result; test | result ---------------------+-------- 0::float8 = 0::int4 | t (1 row) -- Edge Case: Negative numbers with fractional parts SELECT '-100.5::numeric = -100::int4'::text AS test, (-100.5)::numeric = (-100)::int4 AS result; -- Should be false test | result ------------------------------+-------- -100.5::numeric = -100::int4 | f (1 row) SELECT '-100.5::numeric <> -100::int4'::text AS test, (-100.5)::numeric <> (-100)::int4 AS result; -- Should be true test | result -------------------------------+-------- -100.5::numeric <> -100::int4 | t (1 row) SELECT '-100.5::numeric < -100::int4'::text AS test, (-100.5)::numeric < (-100)::int4 AS result; -- Should be true test | result ------------------------------+-------- -100.5::numeric < -100::int4 | t (1 row) SELECT '-100.5::numeric > -101::int4'::text AS test, (-100.5)::numeric > (-101)::int4 AS result; -- Should be true test | result ------------------------------+-------- -100.5::numeric > -101::int4 | t (1 row) -- Edge Case: Very small fractional parts SELECT '100.0000000001::numeric = 100::int4'::text AS test, 100.0000000001::numeric = 100::int4 AS result; -- Should be false test | result -------------------------------------+-------- 100.0000000001::numeric = 100::int4 | f (1 row) SELECT '100.0000000001::numeric > 100::int4'::text AS test, 100.0000000001::numeric > 100::int4 AS result; -- Should be true test | result -------------------------------------+-------- 100.0000000001::numeric > 100::int4 | t (1 row) SELECT '99.9999999999::numeric < 100::int4'::text AS test, 99.9999999999::numeric < 100::int4 AS result; -- Should be true test | result ------------------------------------+-------- 99.9999999999::numeric < 100::int4 | t (1 row) -- Edge Case: Maximum values for integer types SELECT '32767::numeric = 32767::int2'::text AS test, 32767::numeric = 32767::int2 AS result; -- int2 max test | result ------------------------------+-------- 32767::numeric = 32767::int2 | t (1 row) SELECT '2147483647::numeric = 2147483647::int4'::text AS test, 2147483647::numeric = 2147483647::int4 AS result; -- int4 max test | result ----------------------------------------+-------- 2147483647::numeric = 2147483647::int4 | t (1 row) SELECT '9223372036854775807::numeric = 9223372036854775807::int8'::text AS test, 9223372036854775807::numeric = 9223372036854775807::int8 AS result; -- int8 max test | result ----------------------------------------------------------+-------- 9223372036854775807::numeric = 9223372036854775807::int8 | t (1 row) -- Edge Case: Minimum values for integer types SELECT '-32768::numeric = -32768::int2'::text AS test, (-32768)::numeric = (-32768)::int2 AS result; -- int2 min test | result --------------------------------+-------- -32768::numeric = -32768::int2 | t (1 row) SELECT '-2147483648::numeric = -2147483648::int4'::text AS test, (-2147483648)::numeric = (-2147483648)::int4 AS result; -- int4 min test | result ------------------------------------------+-------- -2147483648::numeric = -2147483648::int4 | t (1 row) SELECT '-9223372036854775808::numeric = -9223372036854775808::int8'::text AS test, (-9223372036854775808)::numeric = (-9223372036854775808)::int8 AS result; -- int8 min test | result ------------------------------------------------------------+-------- -9223372036854775808::numeric = -9223372036854775808::int8 | t (1 row) -- Edge Case: Boundary values with fractional parts -- These test floor/comparison logic at integer type boundaries -- int2 boundary edge cases (MIN = -32768, MAX = 32767) SELECT 'int2 MIN - 0.1 < MIN'::text AS test, '-32768.1'::numeric < (-32768)::int2 AS result; -- Should be true (floor is -32769) test | result ----------------------+-------- int2 MIN - 0.1 < MIN | t (1 row) SELECT 'int2 MIN + 0.1 > MIN'::text AS test, '-32767.9'::numeric > (-32768)::int2 AS result; -- Should be true (floor is -32768, has fraction) test | result ----------------------+-------- int2 MIN + 0.1 > MIN | t (1 row) SELECT 'int2 MAX + 0.1 > MAX'::text AS test, '32767.1'::numeric > 32767::int2 AS result; -- Should be true (floor is 32767, has fraction) test | result ----------------------+-------- int2 MAX + 0.1 > MAX | t (1 row) SELECT 'int2 MAX + 0.1 = MAX'::text AS test, '32767.1'::numeric = 32767::int2 AS result; -- Should be false test | result ----------------------+-------- int2 MAX + 0.1 = MAX | f (1 row) -- int4 boundary edge cases (MIN = -2147483648, MAX = 2147483647) SELECT 'int4 MIN - 0.1 < MIN'::text AS test, '-2147483648.1'::numeric < (-2147483648)::int4 AS result; -- Should be true (floor is -2147483649) test | result ----------------------+-------- int4 MIN - 0.1 < MIN | t (1 row) SELECT 'int4 MIN + 0.1 > MIN'::text AS test, '-2147483647.9'::numeric > (-2147483648)::int4 AS result; -- Should be true (floor is -2147483648, has fraction) test | result ----------------------+-------- int4 MIN + 0.1 > MIN | t (1 row) SELECT 'int4 MAX + 0.1 > MAX'::text AS test, '2147483647.1'::numeric > 2147483647::int4 AS result; -- Should be true (floor is 2147483647, has fraction) test | result ----------------------+-------- int4 MAX + 0.1 > MAX | t (1 row) SELECT 'int4 MAX + 0.1 = MAX'::text AS test, '2147483647.1'::numeric = 2147483647::int4 AS result; -- Should be false test | result ----------------------+-------- int4 MAX + 0.1 = MAX | f (1 row) -- int8 boundary edge cases (MIN = -9223372036854775808, MAX = 9223372036854775807) SELECT 'int8 MIN - 0.1 < MIN'::text AS test, '-9223372036854775808.1'::numeric < ('-9223372036854775808'::numeric)::int8 AS result; -- Should be true (floor overflows) test | result ----------------------+-------- int8 MIN - 0.1 < MIN | t (1 row) SELECT 'int8 MIN + 0.1 > MIN'::text AS test, '-9223372036854775807.9'::numeric > ('-9223372036854775808'::numeric)::int8 AS result; -- Should be true (floor is MIN, has fraction) test | result ----------------------+-------- int8 MIN + 0.1 > MIN | t (1 row) SELECT 'int8 MAX + 0.1 > MAX'::text AS test, '9223372036854775807.1'::numeric > 9223372036854775807::int8 AS result; -- Should be true (floor is MAX, has fraction) test | result ----------------------+-------- int8 MAX + 0.1 > MAX | t (1 row) SELECT 'int8 MAX + 0.1 = MAX'::text AS test, '9223372036854775807.1'::numeric = 9223372036854775807::int8 AS result; -- Should be false test | result ----------------------+-------- int8 MAX + 0.1 = MAX | f (1 row) -- float4 boundary edge cases with int types -- Note: float4 has limited precision, but boundary comparisons should still work SELECT 'float4 at int2 MIN - 0.5'::text AS test, (-32768.5)::float4 < (-32768)::int2 AS result; -- Should be true test | result --------------------------+-------- float4 at int2 MIN - 0.5 | t (1 row) SELECT 'float4 at int2 MAX + 0.5'::text AS test, (32767.5)::float4 > 32767::int2 AS result; -- Should be true test | result --------------------------+-------- float4 at int2 MAX + 0.5 | t (1 row) SELECT 'float4 at int4 MIN - 0.5'::text AS test, (-2147483648.5)::float4 < (-2147483648)::int4 AS result; -- false: float4 rounds -2147483648.5 to exactly -2^31 = int4 MIN test | result --------------------------+-------- float4 at int4 MIN - 0.5 | f (1 row) -- float8 boundary edge cases with int types SELECT 'float8 at int2 MIN - 0.1'::text AS test, (-32768.1)::float8 < (-32768)::int2 AS result; -- Should be true test | result --------------------------+-------- float8 at int2 MIN - 0.1 | t (1 row) SELECT 'float8 at int2 MAX + 0.1'::text AS test, (32767.1)::float8 > 32767::int2 AS result; -- Should be true test | result --------------------------+-------- float8 at int2 MAX + 0.1 | t (1 row) SELECT 'float8 at int4 MIN - 0.1'::text AS test, (-2147483648.1)::float8 < (-2147483648)::int4 AS result; -- Should be true test | result --------------------------+-------- float8 at int4 MIN - 0.1 | t (1 row) SELECT 'float8 at int4 MAX + 0.1'::text AS test, (2147483647.1)::float8 > 2147483647::int4 AS result; -- Should be true test | result --------------------------+-------- float8 at int4 MAX + 0.1 | t (1 row) SELECT 'float8 at int8 MIN - 0.1'::text AS test, '-9223372036854775808.1'::float8 < ('-9223372036854775808'::numeric)::int8 AS result; -- false: float8 rounds to exactly -2^63 = int8 MIN test | result --------------------------+-------- float8 at int8 MIN - 0.1 | f (1 row) SELECT 'float8 at int8 MAX + 0.1'::text AS test, '9223372036854775807.1'::float8 > 9223372036854775807::int8 AS result; -- true: float8 rounds to 2^63, one above int8 MAX test | result --------------------------+-------- float8 at int8 MAX + 0.1 | t (1 row) -- Edge Case: SupportRequestSimplify (SRS) path (var op const) -- These require a table column to trigger the optimizer support function CREATE TEMPORARY TABLE t_boundary_int8 (val int8 NOT NULL); INSERT INTO t_boundary_int8 VALUES (('-9223372036854775808'::numeric)::int8), -- MIN (9223372036854775807); -- MAX -- Tests that go through SupportRequestSimplify (int8 column vs numeric constant) SELECT 'SRS: int8 col > numeric below MIN'::text AS test, val > '-9223372036854775808.1'::numeric AS result FROM t_boundary_int8 WHERE val = ('-9223372036854775808'::numeric)::int8; -- Should be true test | result -----------------------------------+-------- SRS: int8 col > numeric below MIN | t (1 row) SELECT 'SRS: int8 col < numeric above MAX'::text AS test, val < '9223372036854775807.1'::numeric AS result FROM t_boundary_int8 WHERE val = 9223372036854775807; -- Should be true test | result -----------------------------------+-------- SRS: int8 col < numeric above MAX | t (1 row) SELECT 'SRS: int8 col = numeric at MIN'::text AS test, val = '-9223372036854775808'::numeric AS result FROM t_boundary_int8 WHERE val = ('-9223372036854775808'::numeric)::int8; -- Should be true test | result --------------------------------+-------- SRS: int8 col = numeric at MIN | t (1 row) SELECT 'SRS: int8 col = numeric below MIN'::text AS test, val = '-9223372036854775808.1'::numeric AS result FROM t_boundary_int8 WHERE val = ('-9223372036854775808'::numeric)::int8; -- Should be false test | result -----------------------------------+-------- SRS: int8 col = numeric below MIN | f (1 row) -- Verify EXPLAIN shows the transformation (should be Result or use native int8 ops) EXPLAIN (COSTS OFF) SELECT * FROM t_boundary_int8 WHERE val > '-9223372036854775808.1'::numeric; QUERY PLAN --------------------------------------------------- Seq Scan on t_boundary_int8 Filter: (val >= '-9223372036854775808'::bigint) (2 rows) EXPLAIN (COSTS OFF) SELECT * FROM t_boundary_int8 WHERE val < '9223372036854775807.1'::numeric; QUERY PLAN ----------------------------- Seq Scan on t_boundary_int8 (1 row)