Skip to content

Commit efe0713

Browse files
author
Artur Zakirov
committed
Remove duplicated times after collectMatchRumKey
1 parent 790c362 commit efe0713

File tree

2 files changed

+68
-13
lines changed

2 files changed

+68
-13
lines changed

expected/orderby.out

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,12 @@ SELECT id, d, d <-> '2016-05-16 14:21:25' FROM tsts ORDER BY d <-> '2016-05-16 1
194194
(3 rows)
195195

196196
SELECT id, d, d <-> '2016-05-16 14:21:25' FROM tsts ORDER BY d <-> '2016-05-16 14:21:25' LIMIT 5;
197-
id | d | ?column?
198-
-----+---------------------------------+----------
199-
355 | Mon May 16 14:21:22.326724 2016 | 2.673276
200-
355 | Mon May 16 14:21:22.326724 2016 | 2.673276
201-
355 | Mon May 16 14:21:22.326724 2016 | 2.673276
202-
355 | Mon May 16 14:21:22.326724 2016 | 2.673276
203-
355 | Mon May 16 14:21:22.326724 2016 | 2.673276
197+
id | d | ?column?
198+
-----+---------------------------------+-------------
199+
355 | Mon May 16 14:21:22.326724 2016 | 2.673276
200+
356 | Mon May 16 15:21:22.326724 2016 | 3597.326724
201+
354 | Mon May 16 13:21:22.326724 2016 | 3602.673276
202+
357 | Mon May 16 16:21:22.326724 2016 | 7197.326724
203+
353 | Mon May 16 12:21:22.326724 2016 | 7202.673276
204204
(5 rows)
205205

rumget.c

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ static bool scanPage(RumState *rumstate, RumScanEntry entry, ItemPointer item,
3838
Page page, bool equalOk);
3939
static void insertScanItem(RumScanOpaque so, bool recheck);
4040
static int scan_entry_cmp(const void *p1, const void *p2);
41+
static int rum_key_cmp_with_check(const void *p1, const void *p2, void *arg);
4142
static void entryGetItem(RumState *rumstate, RumScanEntry entry);
4243

4344

@@ -414,6 +415,41 @@ collectMatchBitmap(RumBtreeData *btree, RumBtreeStack *stack,
414415
}
415416
}
416417

418+
/*
419+
* Sort array of RumKey and remove duplicates.
420+
*
421+
* Returns new size of the array.
422+
*/
423+
static uint32
424+
sortAndUniqRumKeys(RumKey *list, uint32 nlist)
425+
{
426+
uint32 i, j;
427+
bool haveDups = false;
428+
429+
if (nlist < 2)
430+
return nlist;
431+
432+
qsort_arg(list, nlist, sizeof(RumKey), rum_key_cmp_with_check,
433+
(void *) &haveDups);
434+
435+
/* There are duplicates, remove them */
436+
if (haveDups)
437+
{
438+
j = 1;
439+
for (i = 1; i < nlist; i++)
440+
{
441+
if (rumCompareItemPointers(&list[i - 1].iptr, &list[i].iptr) != 0)
442+
{
443+
list[j] = list[i];
444+
j++;
445+
}
446+
}
447+
return j;
448+
}
449+
else
450+
return nlist;
451+
}
452+
417453
static void
418454
collectMatchRumKey(RumBtreeData *btree, RumBtreeStack *stack,
419455
RumScanEntry entry)
@@ -588,14 +624,16 @@ collectMatchRumKey(RumBtreeData *btree, RumBtreeStack *stack,
588624
}
589625
else if (RumGetNPosting(itup) > 0)
590626
{
591-
uint32 j;
627+
uint32 off, count;
592628

593-
j = entry->nlist;
594-
entry->nlist += RumGetNPosting(itup);
595-
entry->predictNumberResult += RumGetNPosting(itup);
629+
count = RumGetNPosting(itup);
630+
631+
off = entry->nlist;
632+
entry->nlist += count;
633+
entry->predictNumberResult += count;
596634
if (entry->nalloc == 0)
597635
{
598-
entry->nalloc = Max(RumGetNPosting(itup), 32);
636+
entry->nalloc = Max(count, 32);
599637
entry->list = (RumKey *) palloc(entry->nalloc * sizeof(RumKey));
600638
}
601639
else if (entry->nlist > entry->nalloc)
@@ -605,7 +643,7 @@ collectMatchRumKey(RumBtreeData *btree, RumBtreeStack *stack,
605643
repalloc(entry->list, entry->nalloc * sizeof(RumKey));
606644
}
607645

608-
rumReadTuple(btree->rumstate, entry->attnum, itup, entry->list + j);
646+
rumReadTuple(btree->rumstate, entry->attnum, itup, entry->list + off);
609647
entry->isFinished = FALSE;
610648
}
611649

@@ -696,6 +734,7 @@ startScanEntry(RumState *rumstate, RumScanEntry entry)
696734
{
697735
btreeEntry.findItem(&btreeEntry, stackEntry);
698736
collectMatchRumKey(&btreeEntry, stackEntry, entry);
737+
entry->nlist = sortAndUniqRumKeys(entry->list, entry->nlist);
699738
}
700739
else if (btreeEntry.findItem(&btreeEntry, stackEntry))
701740
{
@@ -810,6 +849,22 @@ scan_entry_cmp(const void *p1, const void *p2)
810849
return -cmpEntries(e1, e2);
811850
}
812851

852+
static int
853+
rum_key_cmp_with_check(const void *p1, const void *p2, void *arg)
854+
{
855+
const RumKey *k1 = (const RumKey *) p1;
856+
const RumKey *k2 = (const RumKey *) p2;
857+
bool *haveDups = (bool *) arg;
858+
int res;
859+
860+
res = rumCompareItemPointers(&k1->iptr, &k2->iptr);
861+
862+
if (res == 0)
863+
*haveDups = true;
864+
865+
return res;
866+
}
867+
813868
static void
814869
startScan(IndexScanDesc scan)
815870
{

0 commit comments

Comments
 (0)