Skip to content

Commit e35e136

Browse files
committed
it works
1 parent 62f9bf5 commit e35e136

File tree

8 files changed

+407
-25
lines changed

8 files changed

+407
-25
lines changed

expected/orderby.out

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,183 @@ CREATE INDEX tsts_idx ON tsts USING rum (t rum_tsvector_timestamp_ops, d)
44
WITH (orderby = 'd', addto = 't');
55
INSERT INTO tsts VALUES (-1, 't1 t2', '2016-05-02 02:24:22.326724');
66
INSERT INTO tsts VALUES (-2, 't1 t2 t3', '2016-05-02 02:26:22.326724');
7+
SELECT count(*) FROM tsts WHERE t @@ 'wr|qh';
8+
count
9+
-------
10+
158
11+
(1 row)
12+
13+
SELECT count(*) FROM tsts WHERE t @@ 'wr&qh';
14+
count
15+
-------
16+
17
17+
(1 row)
18+
19+
SELECT count(*) FROM tsts WHERE t @@ 'eq&yt';
20+
count
21+
-------
22+
6
23+
(1 row)
24+
25+
SELECT count(*) FROM tsts WHERE t @@ 'eq|yt';
26+
count
27+
-------
28+
98
29+
(1 row)
30+
31+
SELECT count(*) FROM tsts WHERE t @@ '(eq&yt)|(wr&qh)';
32+
count
33+
-------
34+
23
35+
(1 row)
36+
37+
SELECT count(*) FROM tsts WHERE t @@ '(eq|yt)&(wr|qh)';
38+
count
39+
-------
40+
39
41+
(1 row)
42+
43+
SET enable_indexscan=OFF;
44+
SET enable_indexonlyscan=OFF;
45+
SET enable_bitmapscan=OFF;
46+
SELECT id, d, d <-> '2016-05-16 14:21:25' FROM tsts WHERE t @@ 'wr&qh' ORDER BY d <-> '2016-05-16 14:21:25' LIMIT 5;
47+
id | d | ?column?
48+
-----+---------------------------------+---------------
49+
355 | Mon May 16 14:21:22.326724 2016 | 2.673276
50+
354 | Mon May 16 13:21:22.326724 2016 | 3602.673276
51+
371 | Tue May 17 06:21:22.326724 2016 | 57597.326724
52+
406 | Wed May 18 17:21:22.326724 2016 | 183597.326724
53+
415 | Thu May 19 02:21:22.326724 2016 | 215997.326724
54+
(5 rows)
55+
56+
SELECT id, d, d <-| '2016-05-16 14:21:25' FROM tsts WHERE t @@ 'wr&qh' ORDER BY d <-| '2016-05-16 14:21:25' LIMIT 5;
57+
id | d | ?column?
58+
-----+---------------------------------+---------------
59+
355 | Mon May 16 14:21:22.326724 2016 | 2.673276
60+
354 | Mon May 16 13:21:22.326724 2016 | 3602.673276
61+
252 | Thu May 12 07:21:22.326724 2016 | 370802.673276
62+
232 | Wed May 11 11:21:22.326724 2016 | 442802.673276
63+
168 | Sun May 08 19:21:22.326724 2016 | 673202.673276
64+
(5 rows)
65+
66+
SELECT id, d, d |-> '2016-05-16 14:21:25' FROM tsts WHERE t @@ 'wr&qh' ORDER BY d |-> '2016-05-16 14:21:25' LIMIT 5;
67+
id | d | ?column?
68+
-----+---------------------------------+---------------
69+
371 | Tue May 17 06:21:22.326724 2016 | 57597.326724
70+
406 | Wed May 18 17:21:22.326724 2016 | 183597.326724
71+
415 | Thu May 19 02:21:22.326724 2016 | 215997.326724
72+
428 | Thu May 19 15:21:22.326724 2016 | 262797.326724
73+
457 | Fri May 20 20:21:22.326724 2016 | 367197.326724
74+
(5 rows)
75+
76+
RESET enable_indexscan;
77+
RESET enable_indexonlyscan;
78+
RESET enable_bitmapscan;
79+
SET enable_seqscan = off;
80+
EXPLAIN (costs off)
81+
SELECT count(*) FROM tsts WHERE t @@ 'wr|qh';
82+
QUERY PLAN
83+
-------------------------------------------------------------
84+
Aggregate
85+
-> Bitmap Heap Scan on tsts
86+
Recheck Cond: (t @@ '''wr'' | ''qh'''::tsquery)
87+
-> Bitmap Index Scan on tsts_idx
88+
Index Cond: (t @@ '''wr'' | ''qh'''::tsquery)
89+
(5 rows)
90+
91+
SELECT count(*) FROM tsts WHERE t @@ 'wr|qh';
92+
count
93+
-------
94+
158
95+
(1 row)
96+
97+
SELECT count(*) FROM tsts WHERE t @@ 'wr&qh';
98+
count
99+
-------
100+
17
101+
(1 row)
102+
103+
SELECT count(*) FROM tsts WHERE t @@ 'eq&yt';
104+
count
105+
-------
106+
6
107+
(1 row)
108+
109+
SELECT count(*) FROM tsts WHERE t @@ 'eq|yt';
110+
count
111+
-------
112+
98
113+
(1 row)
114+
115+
SELECT count(*) FROM tsts WHERE t @@ '(eq&yt)|(wr&qh)';
116+
count
117+
-------
118+
23
119+
(1 row)
120+
121+
SELECT count(*) FROM tsts WHERE t @@ '(eq|yt)&(wr|qh)';
122+
count
123+
-------
124+
39
125+
(1 row)
126+
127+
EXPLAIN (costs off)
128+
SELECT id, d, d <-> '2016-05-16 14:21:25' FROM tsts WHERE t @@ 'wr&qh' ORDER BY d <-> '2016-05-16 14:21:25' LIMIT 5;
129+
QUERY PLAN
130+
-----------------------------------------------------------------------------------
131+
Limit
132+
-> Index Scan using tsts_idx on tsts
133+
Index Cond: (t @@ '''wr'' & ''qh'''::tsquery)
134+
Order By: (d <-> 'Mon May 16 14:21:25 2016'::timestamp without time zone)
135+
(4 rows)
136+
137+
SELECT id, d, d <-> '2016-05-16 14:21:25' FROM tsts WHERE t @@ 'wr&qh' ORDER BY d <-> '2016-05-16 14:21:25' LIMIT 5;
138+
id | d | ?column?
139+
-----+---------------------------------+---------------
140+
355 | Mon May 16 14:21:22.326724 2016 | 2.673276
141+
354 | Mon May 16 13:21:22.326724 2016 | 3602.673276
142+
371 | Tue May 17 06:21:22.326724 2016 | 57597.326724
143+
406 | Wed May 18 17:21:22.326724 2016 | 183597.326724
144+
415 | Thu May 19 02:21:22.326724 2016 | 215997.326724
145+
(5 rows)
146+
147+
EXPLAIN (costs off)
148+
SELECT id, d, d <-| '2016-05-16 14:21:25' FROM tsts WHERE t @@ 'wr&qh' ORDER BY d <-| '2016-05-16 14:21:25' LIMIT 5;
149+
QUERY PLAN
150+
-----------------------------------------------------------------------------------
151+
Limit
152+
-> Index Scan using tsts_idx on tsts
153+
Index Cond: (t @@ '''wr'' & ''qh'''::tsquery)
154+
Order By: (d <-| 'Mon May 16 14:21:25 2016'::timestamp without time zone)
155+
(4 rows)
156+
157+
SELECT id, d, d <-| '2016-05-16 14:21:25' FROM tsts WHERE t @@ 'wr&qh' ORDER BY d <-| '2016-05-16 14:21:25' LIMIT 5;
158+
id | d | ?column?
159+
-----+---------------------------------+---------------
160+
355 | Mon May 16 14:21:22.326724 2016 | 2.673276
161+
354 | Mon May 16 13:21:22.326724 2016 | 3602.673276
162+
252 | Thu May 12 07:21:22.326724 2016 | 370802.673276
163+
232 | Wed May 11 11:21:22.326724 2016 | 442802.673276
164+
168 | Sun May 08 19:21:22.326724 2016 | 673202.673276
165+
(5 rows)
166+
167+
EXPLAIN (costs off)
168+
SELECT id, d, d |-> '2016-05-16 14:21:25' FROM tsts WHERE t @@ 'wr&qh' ORDER BY d |-> '2016-05-16 14:21:25' LIMIT 5;
169+
QUERY PLAN
170+
-----------------------------------------------------------------------------------
171+
Limit
172+
-> Index Scan using tsts_idx on tsts
173+
Index Cond: (t @@ '''wr'' & ''qh'''::tsquery)
174+
Order By: (d |-> 'Mon May 16 14:21:25 2016'::timestamp without time zone)
175+
(4 rows)
176+
177+
SELECT id, d, d |-> '2016-05-16 14:21:25' FROM tsts WHERE t @@ 'wr&qh' ORDER BY d |-> '2016-05-16 14:21:25' LIMIT 5;
178+
id | d | ?column?
179+
-----+---------------------------------+---------------
180+
371 | Tue May 17 06:21:22.326724 2016 | 57597.326724
181+
406 | Wed May 18 17:21:22.326724 2016 | 183597.326724
182+
415 | Thu May 19 02:21:22.326724 2016 | 215997.326724
183+
428 | Thu May 19 15:21:22.326724 2016 | 262797.326724
184+
457 | Fri May 20 20:21:22.326724 2016 | 367197.326724
185+
(5 rows)
186+

rum--1.0.sql

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,27 +139,35 @@ AS
139139
OPERATOR 3 =,
140140
OPERATOR 4 >=,
141141
OPERATOR 5 >,
142-
OPERATOR 20 <-> FOR ORDER BY pg_catalog.float_ops,
143-
OPERATOR 21 <-| FOR ORDER BY pg_catalog.float_ops,
144-
OPERATOR 22 |-> FOR ORDER BY pg_catalog.float_ops,
142+
--support
145143
FUNCTION 1 timestamp_cmp(timestamp,timestamp),
146144
FUNCTION 2 rum_timestamp_extract_value(timestamp,internal,internal,internal,internal),
147145
FUNCTION 3 rum_timestamp_extract_query(timestamp,internal,smallint,internal,internal,internal,internal),
148146
FUNCTION 4 rum_timestamp_consistent(internal,smallint,timestamp,int,internal,internal,internal,internal),
149147
FUNCTION 5 rum_timestamp_compare_prefix(timestamp,timestamp,smallint,internal),
148+
-- support to timestamp disttance in rum_tsvector_timestamp_ops
150149
FUNCTION 10 rum_timestamp_outer_distance(timestamp, timestamp, smallint),
150+
OPERATOR 20 <-> (timestamp,timestamp) FOR ORDER BY pg_catalog.float_ops,
151+
OPERATOR 21 <-| (timestamp,timestamp) FOR ORDER BY pg_catalog.float_ops,
152+
OPERATOR 22 |-> (timestamp,timestamp) FOR ORDER BY pg_catalog.float_ops,
151153
STORAGE timestamp;
152154

153155
--together
154156

157+
CREATE FUNCTION rum_tsquery_timestamp_consistent(internal, smallint, tsvector, integer, internal, internal, internal, internal)
158+
RETURNS bool
159+
AS 'MODULE_PATHNAME'
160+
LANGUAGE C IMMUTABLE STRICT;
161+
155162
CREATE OPERATOR CLASS rum_tsvector_timestamp_ops
156163
FOR TYPE tsvector USING rum
157164
AS
158165
OPERATOR 1 @@ (tsvector, tsquery),
166+
--support function
159167
FUNCTION 1 gin_cmp_tslexeme(text, text),
160168
FUNCTION 2 rum_extract_tsvector(tsvector,internal,internal,internal,internal),
161169
FUNCTION 3 rum_extract_tsquery(tsquery,internal,smallint,internal,internal,internal,internal),
162-
FUNCTION 4 rum_tsquery_consistent(internal,smallint,tsvector,int,internal,internal,internal,internal),
170+
FUNCTION 4 rum_tsquery_timestamp_consistent(internal,smallint,tsvector,int,internal,internal,internal,internal),
163171
FUNCTION 5 gin_cmp_prefix(text,text,smallint,internal),
164172
FUNCTION 6 gin_tsquery_triconsistent(internal,smallint,tsvector,int,internal,internal,internal),
165173
FUNCTION 8 rum_tsquery_pre_consistent(internal,smallint,tsvector,int,internal,internal,internal,internal),

rum.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,13 +561,16 @@ typedef struct RumScanKeyData
561561
bool *entryRes;
562562
Datum *addInfo;
563563
bool *addInfoIsNull;
564+
bool useAddToColumn;
565+
Datum outerAddInfo;
566+
bool outerAddInfoIsNull;
564567

565568
/* other data needed for calling consistentFn */
566569
Datum query;
567570
/* NB: these three arrays have only nuserentries elements! */
568571
Datum *queryValues;
569572
RumNullCategory *queryCategories;
570-
Pointer *extra_data;
573+
Pointer *extra_data;
571574
StrategyNumber strategy;
572575
int32 searchMode;
573576
OffsetNumber attnum;

rum_timestamp.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
#include "utils/builtins.h"
77
#include "utils/timestamp.h"
88

9+
#define RUM_TMST_DISTANCE 20
10+
#define RUM_TMST_LEFT_DISTANCE 21
11+
#define RUM_TMST_RIGHT_DISTANCE 22
12+
913
typedef struct QueryInfo
1014
{
1115
StrategyNumber strategy;
@@ -59,6 +63,9 @@ rum_timestamp_extract_query(PG_FUNCTION_ARGS)
5963
case BTGreaterStrategyNumber:
6064
*ptr_partialmatch = true;
6165
case BTEqualStrategyNumber:
66+
case RUM_TMST_DISTANCE:
67+
case RUM_TMST_LEFT_DISTANCE:
68+
case RUM_TMST_RIGHT_DISTANCE:
6269
entries[0] = datum;
6370
break;
6471
default:
@@ -215,10 +222,6 @@ timestamp_right_distance(PG_FUNCTION_ARGS)
215222
PG_RETURN_FLOAT8(diff);
216223
}
217224

218-
#define RUM_TMST_DISTANCE 20
219-
#define RUM_TMST_LEFT_DISTANCE 21
220-
#define RUM_TMST_RIGHT_DISTANCE 22
221-
222225
PG_FUNCTION_INFO_V1(rum_timestamp_outer_distance);
223226
Datum
224227
rum_timestamp_outer_distance(PG_FUNCTION_ARGS)

rum_ts_utils.c

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ PG_FUNCTION_INFO_V1(rum_extract_tsquery);
2626
PG_FUNCTION_INFO_V1(rum_tsvector_config);
2727
PG_FUNCTION_INFO_V1(rum_tsquery_pre_consistent);
2828
PG_FUNCTION_INFO_V1(rum_tsquery_consistent);
29+
PG_FUNCTION_INFO_V1(rum_tsquery_timestamp_consistent);
2930
PG_FUNCTION_INFO_V1(rum_tsquery_distance);
3031
PG_FUNCTION_INFO_V1(rum_ts_distance);
3132

@@ -45,6 +46,7 @@ typedef struct
4546
bool *need_recheck;
4647
Datum *addInfo;
4748
bool *addInfoIsNull;
49+
bool notPhrase;
4850
} RumChkVal;
4951

5052
static bool
@@ -115,13 +117,21 @@ checkcondition_rum(void *checkval, QueryOperand *val, ExecPhraseData *data)
115117
if (!gcv->check[j])
116118
return false;
117119

120+
/*
121+
* Fill position list for phrase operator if it's needed
122+
* end it exists
123+
*/
118124
if (data && gcv->addInfo && gcv->addInfoIsNull[j] == false)
119125
{
120-
bytea *positions = DatumGetByteaP(gcv->addInfo[j]);
126+
bytea *positions;
121127
int32 i;
122128
char *ptrt;
123129
WordEntryPos post;
124130

131+
if (gcv->notPhrase)
132+
elog(ERROR, "phrase search isn't supported yet");
133+
134+
positions = DatumGetByteaP(gcv->addInfo[j]);
125135
data->npos = count_pos(VARDATA_ANY(positions),
126136
VARSIZE_ANY_EXHDR(positions));
127137
data->pos = palloc(sizeof(*data->pos) * data->npos);
@@ -172,14 +182,53 @@ rum_tsquery_consistent(PG_FUNCTION_ARGS)
172182
gcv.need_recheck = recheck;
173183
gcv.addInfo = addInfo;
174184
gcv.addInfoIsNull = addInfoIsNull;
185+
gcv.notPhrase = false;
175186

176187
res = TS_execute(GETQUERY(query), &gcv, true, checkcondition_rum);
177188
}
178189

179190
PG_RETURN_BOOL(res);
180191
}
181192

193+
Datum
194+
rum_tsquery_timestamp_consistent(PG_FUNCTION_ARGS)
195+
{
196+
bool *check = (bool *) PG_GETARG_POINTER(0);
197+
/* StrategyNumber strategy = PG_GETARG_UINT16(1); */
198+
TSQuery query = PG_GETARG_TSQUERY(2);
199+
/* int32 nkeys = PG_GETARG_INT32(3); */
200+
Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4);
201+
bool *recheck = (bool *) PG_GETARG_POINTER(5);
202+
Datum *addInfo = (Datum *) PG_GETARG_POINTER(8);
203+
bool *addInfoIsNull = (bool *) PG_GETARG_POINTER(9);
204+
bool res = FALSE;
182205

206+
/* The query requires recheck only if it involves
207+
* weights */
208+
*recheck = false;
209+
210+
if (query->size > 0)
211+
{
212+
QueryItem *item;
213+
RumChkVal gcv;
214+
215+
/*
216+
* check-parameter array has one entry for each value
217+
* (operand) in the query.
218+
*/
219+
gcv.first_item = item = GETQUERY(query);
220+
gcv.check = check;
221+
gcv.map_item_operand = (int *) (extra_data[0]);
222+
gcv.need_recheck = recheck;
223+
gcv.addInfo = addInfo;
224+
gcv.addInfoIsNull = addInfoIsNull;
225+
gcv.notPhrase = true;
226+
227+
res = TS_execute(GETQUERY(query), &gcv, true, checkcondition_rum);
228+
}
229+
230+
PG_RETURN_BOOL(res);
231+
}
183232

184233
static float weights[] = {0.1f, 0.2f, 0.4f, 1.0f};
185234

@@ -648,7 +697,7 @@ rum_tsquery_distance(PG_FUNCTION_ARGS)
648697
TSQuery query = PG_GETARG_TSQUERY(2);
649698

650699
int32 nkeys = PG_GETARG_INT32(3);
651-
/* Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4); */
700+
/* Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4); */
652701
Datum *addInfo = (Datum *) PG_GETARG_POINTER(8);
653702
bool *addInfoIsNull = (bool *) PG_GETARG_POINTER(9);
654703
float8 res;

0 commit comments

Comments
 (0)