Project: YugabyteDB (App Definition and Development / Database)
File: test-date-time-comparison-overloads.md

title: Test the date-time comparison overloads [YSQL] headerTitle: Test the date-time comparison overloads linkTitle: Test comparison overloads description: Presents code that tests the date-time comparison overloads. [YSQL] menu: v2.18: identifier: test-date-time-comparison-overloads parent: date-time-operators weight: 10 type: docs Tests (not shown here) confirm that if the `>` operator is legal between a particular pair of different date-time data types, then all of the other comparison operators, `<=`, `=`, `>=`, `>`, and `<>`, are also legal. Consider these two overloads of some operator, A: ```output datatype1value A datatype2value ``` and: ```output datatype2value A datatype1value ``` The fact that one is legal does not imply that the other is. The legality of each is informed by its own mental model; and each requires its own test. The of the overall date-time section explains why timetz is not covered. So there are five date-time data types to consider here and therefore twenty-five overloads to test. (You can't assume that you can always compare values of the same data type. For example, if the data type represents the latitude and longitude of a location, then you can't ask which location is greater than the other.) The following code implements all of the tests. The design of the code (it tests the legal comparisons and the illegal comparisons separately) was informed by ad hoc tests during its development. Try this: ```plpgsql drop procedure if exists testdatetimecomparisonoverloads(text) cascade; create procedure testdatetimecomparisonoverloads(i in text) language plpgsql as $body$ declare d1 constant date not null := '01-01-2020'; d2 constant date not null := '02-01-2020'; t1 constant time not null := '12:00:00'; t2 constant time not null := '13:00:00'; ts1 constant timestamp not null := '01-01-2020 12:00:00'; ts2 constant timestamp not null := '02-01-2020 12:00:00'; tstz1 constant timestamptz not null := '01-01-2020 12:00:00 UTC'; tstz2 constant timestamptz not null := '02-01-2020 12:00:00 UTC'; i1 constant interval not null := '12 hours'; i2 constant interval not null := '13 hours'; begin case i -- "date"

when 'date-date' then if d2 > d1 then null; end if; when 'date-time' then if d2 > t1 then null; end if; when 'date-ts' then if d2 > ts1 then null; end if; when 'date-tstz' then if d2 > tstz1 then null; end if; when 'date-interval' then if d2 > i1 then null; end if; -- "time" row. when 'time-date' then if t2 > d1 then null; end if; when 'time-time' then if t2 > t1 then null; end if; when 'time-ts' then if t2 > ts1 then null; end if; when 'time-tstz' then if t2 > tstz1 then null; end if; when 'time-interval' then if t2 > i1 then null; end if; -- Plain "timestamp" row. when 'ts-date' then if ts2 > d1 then null; end if; when 'ts-time' then if ts2 > t1 then null; end if; when 'ts-ts' then if ts2 > ts1 then null; end if; when 'ts-tstz' then if ts2 > tstz1 then null; end if; when 'ts-interval' then if ts2 > i1 then null; end if; -- "timestamptz" row. when 'tstz-date' then if tstz2 > d1 then null; end if; when 'tstz-time' then if tstz2 > t1 then null; end if; when 'tstz-ts' then if tstz2 > ts1 then null; end if; when 'tstz-tstz' then if tstz2 > tstz1 then null; end if; when 'tstz-interval' then if tstz2 > i1 then null; end if; -- "interval" row. when 'interval-date' then if i2 > d1 then null; end if; when 'interval-time' then if i2 > t1 then null; end if; when 'interval-ts' then if i2 > ts1 then null; end if; when 'interval-tstz' then if i2 > tstz1 then null; end if; when 'interval-interval' then if i2 > i1 then null; end if; end case; end; $body$; drop procedure if exists confirmexpected42883(text) cascade; create procedure confirmexpected42883(i in text) language plpgsql as $body$ begin call testdatetimecomparisonoverloads(i); assert true, 'Unexpected'; -- 42883: operator does not exist... exception when undefined_function then null; end; $body$; do $body$ begin -- 13 legal comparisons in all call testdatetimecomparisonoverloads('date-date'); call testdatetimecomparisonoverloads('date-ts'); call testdatetimecomparisonoverloads('date-tstz'); call testdatetimecomparisonoverloads('time-time'); call testdatetimecomparisonoverloads('time-interval'); call testdatetimecomparisonoverloads('ts-date'); call testdatetimecomparisonoverloads('ts-ts'); call testdatetimecomparisonoverloads('ts-tstz'); call testdatetimecomparisonoverloads('tstz-date'); call testdatetimecomparisonoverloads('tstz-ts'); call testdatetimecomparisonoverloads('tstz-tstz'); call testdatetimecomparisonoverloads('interval-time'); call testdatetimecomparisonoverloads('interval-interval'); -- 12 illegal comparisons in all call confirmexpected42883('date-time'); call confirmexpected42883('date-interval'); call confirmexpected42883('time-date'); call confirmexpected42883('time-ts'); call confirmexpected42883('time-tstz'); call confirmexpected42883('ts-time'); call confirmexpected42883('ts-interval'); call confirmexpected42883('tstz-time'); call confirmexpected42883('tstz-interval'); call confirmexpected42883('interval-date'); call confirmexpected42883('interval-ts'); call confirmexpected42883('interval-tstz'); end; $body$; ``` The final anonymous block finishes silently, confirming the legality of the first thirteen comparisons and the illegality of the second twelve comparisons.