Skip to content

Commit dff84c9

Browse files
committed
fix crash: add lossy distance checking
1 parent ead26db commit dff84c9

File tree

4 files changed

+40
-15
lines changed

4 files changed

+40
-15
lines changed

expected/rum.out

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,16 @@ SELECT rum_ts_distance(a, to_tsquery('pg_catalog.english', 'way')), *
7878
16.4493 | thinking--“to go or not to go?” We are this far on the way. Reached | 'far':11 'go':3,7 'reach':15 'think':1 'way':14
7979
(4 rows)
8080

81-
-- CRASHES
82-
--SELECT rum_ts_distance(a, to_tsquery('pg_catalog.english', 'way & (go | half)')), *
83-
-- FROM test_rum
84-
-- WHERE a @@ to_tsquery('pg_catalog.english', 'way & (go | half)')
85-
-- ORDER BY a >< to_tsquery('pg_catalog.english', 'way & (go | half)');
81+
SELECT rum_ts_distance(a, to_tsquery('pg_catalog.english', 'way & (go | half)')), *
82+
FROM test_rum
83+
WHERE a @@ to_tsquery('pg_catalog.english', 'way & (go | half)')
84+
ORDER BY a >< to_tsquery('pg_catalog.english', 'way & (go | half)');
85+
rum_ts_distance | t | a
86+
-----------------+---------------------------------------------------------------------+---------------------------------------------------------
87+
9.65659 | thinking--“to go or not to go?” We are this far on the way. Reached | 'far':11 'go':3,7 'reach':15 'think':1 'way':14
88+
10.0905 | itself. Put on your “specs” and look at the castle, half way up the | 'castl':10 'half':11 'look':7 'put':2 'spec':5 'way':12
89+
(2 rows)
90+
8691
INSERT INTO test_rum (t) VALUES ('foo bar foo the over foo qq bar');
8792
INSERT INTO test_rum (t) VALUES ('345 qwerty copyright');
8893
INSERT INTO test_rum (t) VALUES ('345 qwerty');

rumget.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2121,7 +2121,7 @@ rumgetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
21212121

21222122
static float8
21232123
keyGetOrdering(RumState *rumstate, MemoryContext tempCtx, RumScanKey key,
2124-
ItemPointer iptr)
2124+
ItemPointer iptr)
21252125
{
21262126
RumScanEntry entry;
21272127
int i;
@@ -2221,8 +2221,21 @@ rumgettuple(IndexScanDesc scan, ScanDirection direction)
22212221
item = rum_tuplesort_getrum(so->sortstate, true, &should_free);
22222222
if (item)
22232223
{
2224+
int i, j = 0;
2225+
22242226
scan->xs_ctup.t_self = item->iptr;
22252227
scan->xs_recheck = item->recheck;
2228+
scan->xs_recheckorderby = false;
2229+
2230+
for (i = 0; i < so->nkeys; i++)
2231+
{
2232+
if (!so->keys[i].orderBy)
2233+
continue;
2234+
scan->xs_orderbyvals[j] = Float8GetDatum(item->data[j]);
2235+
scan->xs_orderbynulls[j] = false;
2236+
2237+
j++;
2238+
}
22262239

22272240
if (should_free)
22282241
pfree(item);

rumscan.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,19 +380,27 @@ rumNewScanKey(IndexScanDesc scan)
380380

381381
so->isVoidRes = false;
382382

383-
for (i = 0; i < scan->numberOfKeys; i++)
384-
{
383+
for (i = 0; i < scan->numberOfKeys; i++)
384+
{
385385
initScanKey(so, &scan->keyData[i], &hasNullQuery);
386386
if (so->isVoidRes)
387-
break;
387+
break;
388388
}
389389

390390
for (i = 0; i < scan->numberOfOrderBys; i++)
391391
{
392392
initScanKey(so, &scan->orderByData[i], &hasNullQuery);
393393
if (so->isVoidRes)
394394
break;
395-
}
395+
}
396+
397+
if (scan->numberOfOrderBys > 0)
398+
{
399+
scan->xs_orderbyvals = palloc0(sizeof(Datum) * scan->numberOfOrderBys);
400+
scan->xs_orderbynulls = palloc(sizeof(bool) * scan->numberOfOrderBys);
401+
memset(scan->xs_orderbynulls, true, sizeof(bool) *
402+
scan->numberOfOrderBys);
403+
}
396404

397405
/*
398406
* If there are no regular scan keys, generate an EVERYTHING scankey to

sql/rum.sql

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@ SELECT rum_ts_distance(a, to_tsquery('pg_catalog.english', 'way')), *
2828
FROM test_rum
2929
WHERE a @@ to_tsquery('pg_catalog.english', 'way')
3030
ORDER BY a >< to_tsquery('pg_catalog.english', 'way');
31-
-- CRASHES
32-
--SELECT rum_ts_distance(a, to_tsquery('pg_catalog.english', 'way & (go | half)')), *
33-
-- FROM test_rum
34-
-- WHERE a @@ to_tsquery('pg_catalog.english', 'way & (go | half)')
35-
-- ORDER BY a >< to_tsquery('pg_catalog.english', 'way & (go | half)');
31+
SELECT rum_ts_distance(a, to_tsquery('pg_catalog.english', 'way & (go | half)')), *
32+
FROM test_rum
33+
WHERE a @@ to_tsquery('pg_catalog.english', 'way & (go | half)')
34+
ORDER BY a >< to_tsquery('pg_catalog.english', 'way & (go | half)');
3635

3736
INSERT INTO test_rum (t) VALUES ('foo bar foo the over foo qq bar');
3837
INSERT INTO test_rum (t) VALUES ('345 qwerty copyright');

0 commit comments

Comments
 (0)