-- Test exact comparison operators for float × integer combinations -- Includes precision boundary tests for float4 and float8 -- Load extension CREATE EXTENSION IF NOT EXISTS pg_num2int_direct_comp; -- Test 1: Basic equality - float with no fractional part SELECT 10::float4 = 10::int2 AS "float4=int2_exact"; SELECT 10::float4 = 10::int4 AS "float4=int4_exact"; SELECT 10::float4 = 10::int8 AS "float4=int8_exact"; SELECT 10::float8 = 10::int2 AS "float8=int2_exact"; SELECT 10::float8 = 10::int4 AS "float8=int4_exact"; SELECT 10::float8 = 10::int8 AS "float8=int8_exact"; -- Test 2: Fractional values never equal integers SELECT 10.5::float4 = 10::int2 AS "float4_frac=int2_false"; SELECT 10.5::float4 = 10::int4 AS "float4_frac=int4_false"; SELECT 10.5::float8 = 10::int8 AS "float8_frac=int8_false"; -- Test 3: CRITICAL - float4 precision boundary at 2^24 (16,777,216) -- float4 can exactly represent integers up to 2^24 SELECT 16777216::float4 = 16777216::int4 AS "float4=int4_at_limit_true"; -- Beyond 2^24, float4 loses precision for ODD numbers (this is the key test case) SELECT 16777217::float4 = 16777217::int4 AS "float4=int4_beyond_limit_FALSE"; SELECT 16777219::float4 = 16777219::int4 AS "float4=int4_beyond_limit_FALSE2"; -- Test 4: float8 precision boundary at 2^53 (within int8 but testable with int4) SELECT 9007199254740992::float8 = 9007199254740992::int8 AS "float8=int8_at_limit_true"; SELECT 9007199254740993::float8 = 9007199254740993::int8 AS "float8=int8_beyond_limit_FALSE"; -- Test 5: Inequality operators with floats SELECT 10::float4 <> 5::int2 AS "float4<>int2_diff"; SELECT 10::float4 <> 5::int4 AS "float4<>int4_diff"; SELECT 10::float8 <> 5::int8 AS "float8<>int8_diff"; -- Test 6: Inequality with fractional part SELECT 10.1::float4 <> 10::int4 AS "float4_frac<>int4"; SELECT 10.1::float8 <> 10::int8 AS "float8_frac<>int8"; -- Test 7: Negative values SELECT (-5)::float4 = (-5)::int2 AS "float4_neg=int2"; SELECT (-5)::float4 = (-5)::int4 AS "float4_neg=int4"; SELECT (-5)::float8 = (-5)::int8 AS "float8_neg=int8"; -- Test 8: Zero SELECT 0::float4 = 0::int2 AS "float4_zero=int2"; SELECT 0::float4 = 0::int4 AS "float4_zero=int4"; SELECT 0::float8 = 0::int8 AS "float8_zero=int8"; -- Test 9: Commutative property (will be fixed in Phase 4 - operator shells issue) -- SELECT 10::int2 = 10::float4 AS "int2=float4_commute"; -- SELECT 10::int4 = 10::float8 AS "int4=float8_commute"; -- Workaround: Use float on left for now SELECT 10::float4 = 10::int2 AS "float4=int2_commute"; SELECT 10::float8 = 10::int4 AS "float8=int4_commute"; -- Test 10: Real type (alias for float4) SELECT 10::real = 10::int4 AS "real=int4"; SELECT 10.5::real <> 10::int4 AS "real_frac<>int4"; -- Test 11: Double precision type (alias for float8) SELECT 10::float8 = 10::int8 AS "double=int8"; SELECT 10.5::float8 <> 10::int8 AS "double_frac<>int8"; -- Test 12: NaN handling (NaN never equals anything) SELECT 'NaN'::float4 = 10::int4 AS "NaN_float4=int4_false"; SELECT 'NaN'::float8 = 10::int8 AS "NaN_float8=int8_false"; SELECT 'NaN'::float4 <> 10::int4 AS "NaN_float4<>int4_true"; -- Test 13: Infinity handling SELECT 'Infinity'::float4 = 10::int4 AS "Inf_float4=int4_false"; SELECT '-Infinity'::float8 = 10::int8 AS "NegInf_float8=int8_false"; SELECT 'Infinity'::float4 <> 10::int4 AS "Inf_float4<>int4_true"; -- ============================================================================ -- Range Comparison Tests (<, >, <=, >=) -- ============================================================================ -- Test 14: Less than operator (<) with float4 SELECT 5::float4 < 10::int2 AS "float4_lt_int2_true"; SELECT 10::float4 < 10::int2 AS "float4_lt_int2_equal_false"; SELECT 15::float4 < 10::int2 AS "float4_lt_int2_false"; SELECT 5::float4 < 10::int4 AS "float4_lt_int4_true"; SELECT 10::float4 < 10::int4 AS "float4_lt_int4_equal_false"; SELECT 15::float4 < 10::int4 AS "float4_lt_int4_false"; SELECT 5::float4 < 10::int8 AS "float4_lt_int8_true"; SELECT 10::float4 < 10::int8 AS "float4_lt_int8_equal_false"; SELECT 15::float4 < 10::int8 AS "float4_lt_int8_false"; -- Test 15: Less than operator (<) with float8 SELECT 5::float8 < 10::int2 AS "float8_lt_int2_true"; SELECT 10::float8 < 10::int2 AS "float8_lt_int2_equal_false"; SELECT 15::float8 < 10::int2 AS "float8_lt_int2_false"; SELECT 5::float8 < 10::int4 AS "float8_lt_int4_true"; SELECT 10::float8 < 10::int4 AS "float8_lt_int4_equal_false"; SELECT 15::float8 < 10::int4 AS "float8_lt_int4_false"; SELECT 5::float8 < 10::int8 AS "float8_lt_int8_true"; SELECT 10::float8 < 10::int8 AS "float8_lt_int8_equal_false"; SELECT 15::float8 < 10::int8 AS "float8_lt_int8_false"; -- Test 16: Greater than operator (>) with float4 SELECT 15::float4 > 10::int2 AS "float4_gt_int2_true"; SELECT 10::float4 > 10::int2 AS "float4_gt_int2_equal_false"; SELECT 5::float4 > 10::int2 AS "float4_gt_int2_false"; SELECT 15::float4 > 10::int4 AS "float4_gt_int4_true"; SELECT 10::float4 > 10::int4 AS "float4_gt_int4_equal_false"; SELECT 5::float4 > 10::int4 AS "float4_gt_int4_false"; SELECT 15::float4 > 10::int8 AS "float4_gt_int8_true"; SELECT 10::float4 > 10::int8 AS "float4_gt_int8_equal_false"; SELECT 5::float4 > 10::int8 AS "float4_gt_int8_false"; -- Test 17: Greater than operator (>) with float8 SELECT 15::float8 > 10::int2 AS "float8_gt_int2_true"; SELECT 10::float8 > 10::int2 AS "float8_gt_int2_equal_false"; SELECT 5::float8 > 10::int2 AS "float8_gt_int2_false"; SELECT 15::float8 > 10::int4 AS "float8_gt_int4_true"; SELECT 10::float8 > 10::int4 AS "float8_gt_int4_equal_false"; SELECT 5::float8 > 10::int4 AS "float8_gt_int4_false"; SELECT 15::float8 > 10::int8 AS "float8_gt_int8_true"; SELECT 10::float8 > 10::int8 AS "float8_gt_int8_equal_false"; SELECT 5::float8 > 10::int8 AS "float8_gt_int8_false"; -- Test 18: Less than or equal operator (<=) with float4 and float8 SELECT 5::float4 <= 10::int4 AS "float4_le_int4_less"; SELECT 10::float4 <= 10::int4 AS "float4_le_int4_equal"; SELECT 15::float4 <= 10::int4 AS "float4_le_int4_false"; SELECT 5::float8 <= 10::int8 AS "float8_le_int8_less"; SELECT 10::float8 <= 10::int8 AS "float8_le_int8_equal"; SELECT 15::float8 <= 10::int8 AS "float8_le_int8_false"; -- Test 19: Greater than or equal operator (>=) with float4 and float8 SELECT 15::float4 >= 10::int4 AS "float4_ge_int4_greater"; SELECT 10::float4 >= 10::int4 AS "float4_ge_int4_equal"; SELECT 5::float4 >= 10::int4 AS "float4_ge_int4_false"; SELECT 15::float8 >= 10::int8 AS "float8_ge_int8_greater"; SELECT 10::float8 >= 10::int8 AS "float8_ge_int8_equal"; SELECT 5::float8 >= 10::int8 AS "float8_ge_int8_false"; -- Test 20: Range operators with fractional values SELECT 10.5::float4 < 11::int4 AS "float4_frac_lt_ceil_true"; SELECT 10.5::float4 < 10::int4 AS "float4_frac_lt_floor_false"; SELECT 10.5::float4 > 10::int4 AS "float4_frac_gt_floor_true"; SELECT 10.5::float4 > 11::int4 AS "float4_frac_gt_ceil_false"; SELECT 10.5::float8 <= 11::int8 AS "float8_frac_le_ceil_true"; SELECT 10.5::float8 >= 10::int8 AS "float8_frac_ge_floor_true"; -- Test 21: Range operators with negative values SELECT (-10)::float4 < (-5)::int4 AS "float4_neg_lt_int4"; SELECT (-5)::float8 > (-10)::int8 AS "float8_neg_gt_int8"; -- Test 22: NaN and Infinity with range operators (NaN is treated as greater for ordering) SELECT 'NaN'::float4 < 10::int4 AS "NaN_float4_lt_int4_false"; SELECT 'NaN'::float4 > 10::int4 AS "NaN_float4_gt_int4_true"; SELECT 'Infinity'::float4 > 10::int4 AS "Inf_float4_gt_int4_true"; SELECT '-Infinity'::float8 < 10::int8 AS "NegInf_float8_lt_int8_true"; -- -- Phase 3: User Story 1 - Catalog Verification Tests (T012-T014a) -- -- T012/T014 [US1] float_ops and integer_ops btree registration. The number -- of entries the extension adds to each family is printed by the catalog -- snapshot in extension_lifecycle ("footprint"), which counts only what -- CREATE EXTENSION added, so core PostgreSQL's own entries do not move it. -- The whole-family counts that used to be here also counted core entries. -- (T013 was removed: PostgreSQL has one float_ops family.) -- T012/T014 per-pair check: every int x float pair, in both directions, must -- have strategies 1-5 held by < <= = >= > of that pair, and support -- FUNCTION 1, in both float_ops and integer_ops. Lists any pair that does -- not; expected result is no rows. WITH fam(opfname) AS (VALUES ('float_ops'), ('integer_ops')), it(t) AS (VALUES ('int2'::regtype), ('int4'::regtype), ('int8'::regtype)), ft(t) AS (VALUES ('float4'::regtype), ('float8'::regtype)), pairs(l, r) AS (SELECT it.t, ft.t FROM it, ft UNION ALL SELECT ft.t, it.t FROM it, ft), f AS (SELECT o.oid, o.opfname FROM pg_opfamily o JOIN fam USING (opfname) WHERE o.opfmethod = (SELECT oid FROM pg_am WHERE amname = 'btree')), got AS ( SELECT f.opfname, p.l, p.r, (SELECT string_agg(a.amopstrategy || ':' || o.oprname, ' ' ORDER BY a.amopstrategy) FROM pg_amop a JOIN pg_operator o ON o.oid = a.amopopr WHERE a.amopfamily = f.oid AND a.amoplefttype = p.l AND a.amoprighttype = p.r AND o.oprleft = p.l AND o.oprright = p.r) AS strategies, EXISTS (SELECT 1 FROM pg_amproc WHERE amprocfamily = f.oid AND amproclefttype = p.l AND amprocrighttype = p.r AND amprocnum = 1) AS has_cmp_func FROM f, pairs p) SELECT opfname, l AS lefttype, r AS righttype, strategies, has_cmp_func FROM got WHERE strategies IS DISTINCT FROM '1:< 2:<= 3:= 4:>= 5:>' OR NOT has_cmp_func ORDER BY 1, 2, 3; -- Every btree entry in the three families the extension extends, core and -- extension alike, must hold the operator its strategy means (1 <, 2 <=, -- 3 =, 4 >=, 5 >) for the entry's own types. Lists any that do not; -- expected result is no rows. SELECT f.opfname, a.amopstrategy, a.amopopr::regoperator AS operator FROM pg_amop a JOIN pg_opfamily f ON f.oid = a.amopfamily JOIN pg_am m ON m.oid = f.opfmethod JOIN pg_operator o ON o.oid = a.amopopr WHERE m.amname = 'btree' AND a.amoppurpose = 's' AND f.opfname IN ('float_ops', 'integer_ops', 'numeric_ops') AND (o.oprname IS DISTINCT FROM (ARRAY['<', '<=', '=', '>=', '>'])[a.amopstrategy] OR o.oprleft <> a.amoplefttype OR o.oprright <> a.amoprighttype) ORDER BY 1, 2, 3; -- T014a [US1] Every int x float equality operator must exist and be MERGES -- (FR-008). Lists any pair whose = is missing or not MERGES; expected -- result is no rows. WITH it(t) AS (VALUES ('int2'::regtype), ('int4'::regtype), ('int8'::regtype)), ft(t) AS (VALUES ('float4'::regtype), ('float8'::regtype)), pairs(l, r) AS (SELECT it.t, ft.t FROM it, ft UNION ALL SELECT ft.t, it.t FROM it, ft) SELECT p.l AS lefttype, p.r AS righttype, o.oid IS NOT NULL AS exists, o.oprcanmerge FROM pairs p LEFT JOIN pg_operator o ON o.oprname = '=' AND o.oprleft = p.l AND o.oprright = p.r WHERE o.oid IS NULL OR NOT o.oprcanmerge ORDER BY 1, 2; -- -- Phase 4: User Story 2 - Transitivity Tests (T016-T020a) -- -- T016 [US2] Transitivity for int4 = float4 = int4 near the float4 precision -- limit. For each stored float4 value, the 7 distinct integers around it are -- compared: at most one may equal it (ints_equal <= 1). Since the candidates -- are distinct, that is what makes a = f AND f = c imply a = c. Rounding -- comparisons would equate several integers with one float4 above 2^24 and -- break this. WITH f(v) AS (VALUES (16777216::float4), (16777218::float4), (16777220::float4), (33554432::float4), (-16777218::float4), (2147483520::float4), (0.5::float4)), cand AS (SELECT f.v, (round(f.v::float8)::int8 + d)::int4 AS n FROM f, generate_series(-3, 3) AS d) SELECT f.v AS stored_float4, (SELECT count(*) FROM cand x WHERE x.v = f.v AND x.n = f.v) AS ints_equal FROM f ORDER BY f.v; -- T017 [US2] Transitivity for int8 = float8 = int8 near the float8 precision -- limit (2^53), same method as T016 WITH f(v) AS (VALUES (9007199254740992::float8), (9007199254740994::float8), (18014398509481984::float8), (-9007199254740994::float8), (9223372036854774784::float8), (0.5::float8)), cand AS (SELECT f.v, (round(f.v::float8)::int8 + d)::int8 AS n FROM f, generate_series(-3, 3) AS d) SELECT f.v AS stored_float8, (SELECT count(*) FROM cand x WHERE x.v = f.v AND x.n = f.v) AS ints_equal FROM f ORDER BY f.v; -- T017a [US2] Transitivity for int8 = float4 = int8, the third pair where the -- int type is wider than the float mantissa, same method as T016. Values run -- from the float4 limit (2^24) to the largest float4 below 2^63. WITH f(v) AS (VALUES (16777216::float4), (16777218::float4), (33554432::float4), (-16777218::float4), (4611686018427387904::float4), (9223371487098961920::float4), (-9223371487098961920::float4), (0.5::float4)), cand AS (SELECT f.v, (round(f.v::float8)::int8 + d)::int8 AS n FROM f, generate_series(-3, 3) AS d) SELECT f.v AS stored_float4, (SELECT count(*) FROM cand x WHERE x.v = f.v AND x.n = f.v) AS ints_equal FROM f ORDER BY f.v; -- T018 [US2] NaN sorts above every integer (btree convention), and -- T019 [US2] -Infinity < every integer < +Infinity: checked for all 6 float x -- int pairs, all six operators and both operand orders, against an exact -- reference, by comparison_exactness Check 3 (ex_float holds NaN and -- +/-Infinity for both float types). Only NaN = NaN, a core float property -- the reference does not cover, stays here. SELECT ('NaN'::float4 = 'NaN'::float4) AS "NaN_eq_NaN_float4", ('NaN'::float8 = 'NaN'::float8) AS "NaN_eq_NaN_float8"; -- T020 [US2] Add precision boundary transitivity test (16777216 boundary for float4) SELECT (16777216::int4 = 16777216::float4) AS "at_float4_limit_true", (16777217::int4 = 16777217::float4) AS "beyond_float4_limit_false", (9007199254740992::int8 = 9007199254740992::float8) AS "at_float8_limit_true", (9007199254740993::int8 = 9007199254740993::float8) AS "beyond_float8_limit_false"; -- Create test table for transitive inference test CREATE TEMPORARY TABLE transitivity_test (val int4); INSERT INTO transitivity_test VALUES (42), (100), (200); -- T020a [US2] Add EXPLAIN test for transitive inference (verify planner simplifies chained conditions) EXPLAIN (COSTS false) SELECT * FROM transitivity_test t WHERE t.val = 42::float4 AND 42::float4 = 42::int4 AND 42::int4 = t.val; -- Cleanup DROP TABLE transitivity_test;