Add more cross-type comparisons to contrib/btree_gin. · postgres/postgres@fc89682 · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

Commit fc89682

Browse files
committed
Add more cross-type comparisons to contrib/btree_gin.
Using the just-added infrastructure, extend btree_gin to support cross-type operators in its other opclasses. All of the cross-type comparison operators supported by the core btree opclasses for these datatypes are now available for btree_gin indexes as well. Author: Tom Lane <tgl@sss.pgh.pa.us> Reviewed-by: Arseniy Mukhin <arseniy.mukhin.dev@gmail.com> Discussion: https://postgr.es/m/262624.1738460652@sss.pgh.pa.us
1 parent e2b64fc commit fc89682

File tree

16 files changed

+1725
-37
lines changed

16 files changed

+1725
-37
lines changed

contrib/btree_gin/btree_gin--1.3--1.4.sql

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,91 @@ ADD
6161
OPERATOR 0x24 >= (int8, int4),
6262
OPERATOR 0x25 > (int8, int4)
6363
;
64+
65+
ALTER OPERATOR FAMILY float4_ops USING gin
66+
ADD
67+
-- Code 1: RHS is float8
68+
OPERATOR 0x11 < (float4, float8),
69+
OPERATOR 0x12 <= (float4, float8),
70+
OPERATOR 0x13 = (float4, float8),
71+
OPERATOR 0x14 >= (float4, float8),
72+
OPERATOR 0x15 > (float4, float8)
73+
;
74+
75+
ALTER OPERATOR FAMILY float8_ops USING gin
76+
ADD
77+
-- Code 1: RHS is float4
78+
OPERATOR 0x11 < (float8, float4),
79+
OPERATOR 0x12 <= (float8, float4),
80+
OPERATOR 0x13 = (float8, float4),
81+
OPERATOR 0x14 >= (float8, float4),
82+
OPERATOR 0x15 > (float8, float4)
83+
;
84+
85+
ALTER OPERATOR FAMILY text_ops USING gin
86+
ADD
87+
-- Code 1: RHS is name
88+
OPERATOR 0x11 < (text, name),
89+
OPERATOR 0x12 <= (text, name),
90+
OPERATOR 0x13 = (text, name),
91+
OPERATOR 0x14 >= (text, name),
92+
OPERATOR 0x15 > (text, name)
93+
;
94+
95+
ALTER OPERATOR FAMILY name_ops USING gin
96+
ADD
97+
-- Code 1: RHS is text
98+
OPERATOR 0x11 < (name, text),
99+
OPERATOR 0x12 <= (name, text),
100+
OPERATOR 0x13 = (name, text),
101+
OPERATOR 0x14 >= (name, text),
102+
OPERATOR 0x15 > (name, text)
103+
;
104+
105+
ALTER OPERATOR FAMILY date_ops USING gin
106+
ADD
107+
-- Code 1: RHS is timestamp
108+
OPERATOR 0x11 < (date, timestamp),
109+
OPERATOR 0x12 <= (date, timestamp),
110+
OPERATOR 0x13 = (date, timestamp),
111+
OPERATOR 0x14 >= (date, timestamp),
112+
OPERATOR 0x15 > (date, timestamp),
113+
-- Code 2: RHS is timestamptz
114+
OPERATOR 0x21 < (date, timestamptz),
115+
OPERATOR 0x22 <= (date, timestamptz),
116+
OPERATOR 0x23 = (date, timestamptz),
117+
OPERATOR 0x24 >= (date, timestamptz),
118+
OPERATOR 0x25 > (date, timestamptz)
119+
;
120+
121+
ALTER OPERATOR FAMILY timestamp_ops USING gin
122+
ADD
123+
-- Code 1: RHS is date
124+
OPERATOR 0x11 < (timestamp, date),
125+
OPERATOR 0x12 <= (timestamp, date),
126+
OPERATOR 0x13 = (timestamp, date),
127+
OPERATOR 0x14 >= (timestamp, date),
128+
OPERATOR 0x15 > (timestamp, date),
129+
-- Code 2: RHS is timestamptz
130+
OPERATOR 0x21 < (timestamp, timestamptz),
131+
OPERATOR 0x22 <= (timestamp, timestamptz),
132+
OPERATOR 0x23 = (timestamp, timestamptz),
133+
OPERATOR 0x24 >= (timestamp, timestamptz),
134+
OPERATOR 0x25 > (timestamp, timestamptz)
135+
;
136+
137+
ALTER OPERATOR FAMILY timestamptz_ops USING gin
138+
ADD
139+
-- Code 1: RHS is date
140+
OPERATOR 0x11 < (timestamptz, date),
141+
OPERATOR 0x12 <= (timestamptz, date),
142+
OPERATOR 0x13 = (timestamptz, date),
143+
OPERATOR 0x14 >= (timestamptz, date),
144+
OPERATOR 0x15 > (timestamptz, date),
145+
-- Code 2: RHS is timestamp
146+
OPERATOR 0x21 < (timestamptz, timestamp),
147+
OPERATOR 0x22 <= (timestamptz, timestamp),
148+
OPERATOR 0x23 = (timestamptz, timestamp),
149+
OPERATOR 0x24 >= (timestamptz, timestamp),
150+
OPERATOR 0x25 > (timestamptz, timestamp)
151+
;

contrib/btree_gin/btree_gin.c

Lines changed: 171 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
#include <limits.h>
77

88
#include "access/stratnum.h"
9+
#include "mb/pg_wchar.h"
910
#include "utils/builtins.h"
1011
#include "utils/date.h"
1112
#include "utils/float.h"
1213
#include "utils/inet.h"
1314
#include "utils/numeric.h"
1415
#include "utils/timestamp.h"
1516
#include "utils/uuid.h"
17+
#include "varatt.h"
1618

1719
PG_MODULE_MAGIC_EXT(
1820
.name = "btree_gin",
@@ -401,27 +403,59 @@ leftmostvalue_float4(void)
401403
return Float4GetDatum(-get_float4_infinity());
402404
}
403405

406+
static Datum
407+
cvt_float8_float4(Datum input)
408+
{
409+
float8 val = DatumGetFloat8(input);
410+
float4 result;
411+
412+
/*
413+
* Assume that ordinary C conversion will produce a usable result.
414+
* (Compare dtof(), which raises error conditions that we don't need.)
415+
* Note that for inputs that aren't exactly representable as float4, it
416+
* doesn't matter whether the conversion rounds up or down. That might
417+
* cause us to scan a few index entries that we'll reject as not matching,
418+
* but we won't miss any that should match.
419+
*/
420+
result = (float4) val;
421+
return Float4GetDatum(result);
422+
}
423+
404424
static const bool float4_rhs_is_varlena[] =
405-
{false};
425+
{false, false};
426+
427+
static const btree_gin_convert_function float4_cvt_fns[] =
428+
{NULL, cvt_float8_float4};
406429

407430
static const PGFunction float4_cmp_fns[] =
408-
{btfloat4cmp};
431+
{btfloat4cmp, btfloat84cmp};
409432

410-
GIN_SUPPORT(float4, leftmostvalue_float4, float4_rhs_is_varlena, NULL, float4_cmp_fns)
433+
GIN_SUPPORT(float4, leftmostvalue_float4, float4_rhs_is_varlena, float4_cvt_fns, float4_cmp_fns)
411434

412435
static Datum
413436
leftmostvalue_float8(void)
414437
{
415438
return Float8GetDatum(-get_float8_infinity());
416439
}
417440

441+
static Datum
442+
cvt_float4_float8(Datum input)
443+
{
444+
float4 val = DatumGetFloat4(input);
445+
446+
return Float8GetDatum((float8) val);
447+
}
448+
418449
static const bool float8_rhs_is_varlena[] =
419-
{false};
450+
{false, false};
451+
452+
static const btree_gin_convert_function float8_cvt_fns[] =
453+
{NULL, cvt_float4_float8};
420454

421455
static const PGFunction float8_cmp_fns[] =
422-
{btfloat8cmp};
456+
{btfloat8cmp, btfloat48cmp};
423457

424-
GIN_SUPPORT(float8, leftmostvalue_float8, float8_rhs_is_varlena, NULL, float8_cmp_fns)
458+
GIN_SUPPORT(float8, leftmostvalue_float8, float8_rhs_is_varlena, float8_cvt_fns, float8_cmp_fns)
425459

426460
static Datum
427461
leftmostvalue_money(void)
@@ -457,21 +491,75 @@ leftmostvalue_timestamp(void)
457491
return TimestampGetDatum(DT_NOBEGIN);
458492
}
459493

494+
static Datum
495+
cvt_date_timestamp(Datum input)
496+
{
497+
DateADT val = DatumGetDateADT(input);
498+
Timestamp result;
499+
int overflow;
500+
501+
result = date2timestamp_opt_overflow(val, &overflow);
502+
/* We can ignore the overflow result, since result is useful as-is */
503+
return TimestampGetDatum(result);
504+
}
505+
506+
static Datum
507+
cvt_timestamptz_timestamp(Datum input)
508+
{
509+
TimestampTz val = DatumGetTimestampTz(input);
510+
Timestamp result;
511+
int overflow;
512+
513+
result = timestamptz2timestamp_opt_overflow(val, &overflow);
514+
/* We can ignore the overflow result, since result is useful as-is */
515+
return TimestampGetDatum(result);
516+
}
517+
460518
static const bool timestamp_rhs_is_varlena[] =
461-
{false};
519+
{false, false, false};
520+
521+
static const btree_gin_convert_function timestamp_cvt_fns[] =
522+
{NULL, cvt_date_timestamp, cvt_timestamptz_timestamp};
462523

463524
static const PGFunction timestamp_cmp_fns[] =
464-
{timestamp_cmp};
525+
{timestamp_cmp, date_cmp_timestamp, timestamptz_cmp_timestamp};
465526

466-
GIN_SUPPORT(timestamp, leftmostvalue_timestamp, timestamp_rhs_is_varlena, NULL, timestamp_cmp_fns)
527+
GIN_SUPPORT(timestamp, leftmostvalue_timestamp, timestamp_rhs_is_varlena, timestamp_cvt_fns, timestamp_cmp_fns)
528+
529+
static Datum
530+
cvt_date_timestamptz(Datum input)
531+
{
532+
DateADT val = DatumGetDateADT(input);
533+
TimestampTz result;
534+
int overflow;
535+
536+
result = date2timestamptz_opt_overflow(val, &overflow);
537+
/* We can ignore the overflow result, since result is useful as-is */
538+
return TimestampTzGetDatum(result);
539+
}
540+
541+
static Datum
542+
cvt_timestamp_timestamptz(Datum input)
543+
{
544+
Timestamp val = DatumGetTimestamp(input);
545+
TimestampTz result;
546+
int overflow;
547+
548+
result = timestamp2timestamptz_opt_overflow(val, &overflow);
549+
/* We can ignore the overflow result, since result is useful as-is */
550+
return TimestampTzGetDatum(result);
551+
}
467552

468553
static const bool timestamptz_rhs_is_varlena[] =
469-
{false};
554+
{false, false, false};
555+
556+
static const btree_gin_convert_function timestamptz_cvt_fns[] =
557+
{NULL, cvt_date_timestamptz, cvt_timestamp_timestamptz};
470558

471559
static const PGFunction timestamptz_cmp_fns[] =
472-
{timestamp_cmp};
560+
{timestamp_cmp, date_cmp_timestamptz, timestamp_cmp_timestamptz};
473561

474-
GIN_SUPPORT(timestamptz, leftmostvalue_timestamp, timestamptz_rhs_is_varlena, NULL, timestamptz_cmp_fns)
562+
GIN_SUPPORT(timestamptz, leftmostvalue_timestamp, timestamptz_rhs_is_varlena, timestamptz_cvt_fns, timestamptz_cmp_fns)
475563

476564
static Datum
477565
leftmostvalue_time(void)
@@ -512,13 +600,40 @@ leftmostvalue_date(void)
512600
return DateADTGetDatum(DATEVAL_NOBEGIN);
513601
}
514602

603+
static Datum
604+
cvt_timestamp_date(Datum input)
605+
{
606+
Timestamp val = DatumGetTimestamp(input);
607+
DateADT result;
608+
int overflow;
609+
610+
result = timestamp2date_opt_overflow(val, &overflow);
611+
/* We can ignore the overflow result, since result is useful as-is */
612+
return DateADTGetDatum(result);
613+
}
614+
615+
static Datum
616+
cvt_timestamptz_date(Datum input)
617+
{
618+
TimestampTz val = DatumGetTimestampTz(input);
619+
DateADT result;
620+
int overflow;
621+
622+
result = timestamptz2date_opt_overflow(val, &overflow);
623+
/* We can ignore the overflow result, since result is useful as-is */
624+
return DateADTGetDatum(result);
625+
}
626+
515627
static const bool date_rhs_is_varlena[] =
516-
{false};
628+
{false, false, false};
629+
630+
static const btree_gin_convert_function date_cvt_fns[] =
631+
{NULL, cvt_timestamp_date, cvt_timestamptz_date};
517632

518633
static const PGFunction date_cmp_fns[] =
519-
{date_cmp};
634+
{date_cmp, timestamp_cmp_date, timestamptz_cmp_date};
520635

521-
GIN_SUPPORT(date, leftmostvalue_date, date_rhs_is_varlena, NULL, date_cmp_fns)
636+
GIN_SUPPORT(date, leftmostvalue_date, date_rhs_is_varlena, date_cvt_fns, date_cmp_fns)
522637

523638
static Datum
524639
leftmostvalue_interval(void)
@@ -598,13 +713,24 @@ leftmostvalue_text(void)
598713
return PointerGetDatum(cstring_to_text_with_len("", 0));
599714
}
600715

716+
static Datum
717+
cvt_name_text(Datum input)
718+
{
719+
Name val = DatumGetName(input);
720+
721+
return PointerGetDatum(cstring_to_text(NameStr(*val)));
722+
}
723+
601724
static const bool text_rhs_is_varlena[] =
602-
{true};
725+
{true, false};
726+
727+
static const btree_gin_convert_function text_cvt_fns[] =
728+
{NULL, cvt_name_text};
603729

604730
static const PGFunction text_cmp_fns[] =
605-
{bttextcmp};
731+
{bttextcmp, btnametextcmp};
606732

607-
GIN_SUPPORT(text, leftmostvalue_text, text_rhs_is_varlena, NULL, text_cmp_fns)
733+
GIN_SUPPORT(text, leftmostvalue_text, text_rhs_is_varlena, text_cvt_fns, text_cmp_fns)
608734

609735
static const bool bpchar_rhs_is_varlena[] =
610736
{true};
@@ -804,13 +930,37 @@ leftmostvalue_name(void)
804930
return NameGetDatum(result);
805931
}
806932

933+
static Datum
934+
cvt_text_name(Datum input)
935+
{
936+
text *val = DatumGetTextPP(input);
937+
NameData *result = (NameData *) palloc0(NAMEDATALEN);
938+
int len = VARSIZE_ANY_EXHDR(val);
939+
940+
/*
941+
* Truncate oversize input. We're assuming this will produce a result
942+
* considered less than the original. That could be a bad assumption in
943+
* some collations, but fortunately an index on "name" is generally going
944+
* to use C collation.
945+
*/
946+
if (len >= NAMEDATALEN)
947+
len = pg_mbcliplen(VARDATA_ANY(val), len, NAMEDATALEN - 1);
948+
949+
memcpy(NameStr(*result), VARDATA_ANY(val), len);
950+
951+
return NameGetDatum(result);
952+
}
953+
807954
static const bool name_rhs_is_varlena[] =
808-
{false};
955+
{false, true};
956+
957+
static const btree_gin_convert_function name_cvt_fns[] =
958+
{NULL, cvt_text_name};
809959

810960
static const PGFunction name_cmp_fns[] =
811-
{btnamecmp};
961+
{btnamecmp, bttextnamecmp};
812962

813-
GIN_SUPPORT(name, leftmostvalue_name, name_rhs_is_varlena, NULL, name_cmp_fns)
963+
GIN_SUPPORT(name, leftmostvalue_name, name_rhs_is_varlena, name_cvt_fns, name_cmp_fns)
814964

815965
static Datum
816966
leftmostvalue_bool(void)

0 commit comments

Comments
 (0)

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

🎥 Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.