diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2001-05-03 19:00:37 +0000 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2001-05-03 19:00:37 +0000 |
commit | 2792374cff361a7a4ec0e750b5fa935d85afc9ac (patch) | |
tree | 98736405bf8af30af89bdf224802f868057b7d2a /src/backend/utils/adt/date.c | |
parent | 77fe28f33e49d752be4e4a1bbc6c112f825e7882 (diff) |
Ensure that btree sort ordering functions and boolean comparison operators
give consistent results for all datatypes. Types float4, float8, and
numeric were broken for NaN values; abstime, timestamp, and interval
were broken for INVALID values; timetz was just plain broken (some
possible pairs of values were neither < nor = nor >). Also clean up
text, bpchar, varchar, and bit/varbit to eliminate duplicate code and
thereby reduce the probability of similar inconsistencies arising in
the future.
Diffstat (limited to 'src/backend/utils/adt/date.c')
-rw-r--r-- | src/backend/utils/adt/date.c | 46 |
1 files changed, 30 insertions, 16 deletions
diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 5e7d3c92f2e..4504f5f043c 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/adt/date.c,v 1.56 2001/03/22 03:59:49 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/date.c,v 1.57 2001/05/03 19:00:36 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -899,13 +899,35 @@ timetz_out(PG_FUNCTION_ARGS) } +static int +timetz_cmp_internal(TimeTzADT *time1, TimeTzADT *time2) +{ + double t1, + t2; + + /* Primary sort is by true (GMT-equivalent) time */ + t1 = time1->time + time1->zone; + t2 = time2->time + time2->zone; + + if (t1 > t2) + return 1; + if (t1 < t2) + return -1; + + /* + * If same GMT time, sort by timezone; we only want to say that two + * timetz's are equal if both the time and zone parts are equal. + */ + return time1->zone - time2->zone; +} + Datum timetz_eq(PG_FUNCTION_ARGS) { TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0); TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1); - PG_RETURN_BOOL(((time1->time + time1->zone) == (time2->time + time2->zone))); + PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) == 0); } Datum @@ -914,7 +936,7 @@ timetz_ne(PG_FUNCTION_ARGS) TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0); TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1); - PG_RETURN_BOOL(((time1->time + time1->zone) != (time2->time + time2->zone))); + PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) != 0); } Datum @@ -923,7 +945,7 @@ timetz_lt(PG_FUNCTION_ARGS) TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0); TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1); - PG_RETURN_BOOL(((time1->time + time1->zone) < (time2->time + time2->zone))); + PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) < 0); } Datum @@ -932,7 +954,7 @@ timetz_le(PG_FUNCTION_ARGS) TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0); TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1); - PG_RETURN_BOOL(((time1->time + time1->zone) <= (time2->time + time2->zone))); + PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) <= 0); } Datum @@ -941,7 +963,7 @@ timetz_gt(PG_FUNCTION_ARGS) TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0); TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1); - PG_RETURN_BOOL(((time1->time + time1->zone) > (time2->time + time2->zone))); + PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) > 0); } Datum @@ -950,7 +972,7 @@ timetz_ge(PG_FUNCTION_ARGS) TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0); TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1); - PG_RETURN_BOOL(((time1->time + time1->zone) >= (time2->time + time2->zone))); + PG_RETURN_BOOL(timetz_cmp_internal(time1, time2) >= 0); } Datum @@ -959,15 +981,7 @@ timetz_cmp(PG_FUNCTION_ARGS) TimeTzADT *time1 = PG_GETARG_TIMETZADT_P(0); TimeTzADT *time2 = PG_GETARG_TIMETZADT_P(1); - if (DatumGetBool(DirectFunctionCall2(timetz_lt, - TimeTzADTPGetDatum(time1), - TimeTzADTPGetDatum(time2)))) - PG_RETURN_INT32(-1); - if (DatumGetBool(DirectFunctionCall2(timetz_gt, - TimeTzADTPGetDatum(time1), - TimeTzADTPGetDatum(time2)))) - PG_RETURN_INT32(1); - PG_RETURN_INT32(0); + PG_RETURN_INT32(timetz_cmp_internal(time1, time2)); } /* |