@@ -151,6 +151,23 @@ callConsistentFn(RumState * rumstate, RumScanKey key)
151151 return res && callAddInfoConsistentFn (rumstate , key );
152152}
153153
154+ /*
155+ * Extract key value for ordering
156+ */
157+ static void
158+ scanEntryGetKey (RumScanEntry entry , RumState * rumstate , IndexTuple itup )
159+ {
160+ if (entry -> useCurKey == false)
161+ return ;
162+
163+ /*
164+ * XXX FIXME only pass-by-value!!! Value should be copied to
165+ * long-lived memory context and, somehow, freeed. Seems, the
166+ * last is real problem.
167+ */
168+ entry -> curKey = rumtuple_get_key (rumstate , itup , & entry -> curKeyCategory );
169+ }
170+
154171/*
155172 * Goes to the next page if current offset is outside of bounds
156173 */
@@ -641,6 +658,8 @@ startScanEntry(RumState * rumstate, RumScanEntry entry)
641658 if (entry -> queryCategory == RUM_CAT_EMPTY_QUERY &&
642659 entry -> scanWithAddInfo )
643660 entry -> stack = stackEntry ;
661+
662+ scanEntryGetKey (entry , rumstate , itup );
644663 }
645664
646665 if (needUnlock )
@@ -1047,6 +1066,8 @@ entryGetNextItemList(RumState * rumstate, RumScanEntry entry)
10471066 entry -> curItem = entry -> list [entry -> offset ];
10481067 entry -> offset += entry -> scanDirection ;
10491068
1069+ scanEntryGetKey (entry , rumstate , itup );
1070+
10501071 /*
10511072 * Done with this entry, go to the next for the future.
10521073 */
@@ -1207,6 +1228,7 @@ entryGetItem(RumState * rumstate, RumScanEntry entry, bool *nextEntryList)
12071228 entry -> isFinished = TRUE;
12081229 }
12091230 }
1231+ /* Get next item from posting tree */
12101232 else
12111233 {
12121234 do
@@ -2093,9 +2115,11 @@ keyGetOrdering(RumState * rumstate, MemoryContext tempCtx, RumScanKey key,
20932115 }
20942116 }
20952117
2096- return DatumGetFloat8 (FunctionCall10Coll (& rumstate -> orderingFn [key -> attnum - 1 ],
2118+ return DatumGetFloat8 (FunctionCall12Coll (& rumstate -> orderingFn [key -> attnum - 1 ],
20972119 rumstate -> supportCollation [key -> attnum - 1 ],
20982120 PointerGetDatum (key -> entryRes ),
2121+ DatumGetChar (key -> curKeyCategory ),
2122+ key -> curKey ,
20992123 UInt16GetDatum (key -> strategy ),
21002124 key -> query ,
21012125 UInt32GetDatum (key -> nuserentries ),
@@ -2121,10 +2145,12 @@ insertScanItem(RumScanOpaque so, bool recheck)
21212145 item -> iptr = so -> item .iptr ;
21222146 item -> recheck = recheck ;
21232147
2124- if (AttributeNumberIsValid (so -> rumstate .attrnAddToColumn ))
2148+ if (AttributeNumberIsValid (so -> rumstate .attrnAddToColumn ) || so -> willSort )
21252149 {
21262150 int nOrderByAnother = 0 ,
2127- count = 0 ;
2151+ nOrderByKey = 0 ,
2152+ countByAnother = 0 ,
2153+ countByKey = 0 ;
21282154
21292155 for (i = 0 ; i < so -> nkeys ; i ++ )
21302156 {
@@ -2133,11 +2159,15 @@ insertScanItem(RumScanOpaque so, bool recheck)
21332159 so -> keys [i ]-> outerAddInfoIsNull = true;
21342160 nOrderByAnother ++ ;
21352161 }
2162+ else if (so -> keys [i ]-> useCurKey )
2163+ nOrderByKey ++ ;
21362164 }
21372165
2138- for (i = 0 ; count < nOrderByAnother && i < so -> nkeys ; i ++ )
2166+ for (i = 0 ; (countByAnother < nOrderByAnother || countByKey < nOrderByKey ) &&
2167+ i < so -> nkeys ; i ++ )
21392168 {
2140- if (so -> keys [i ]-> attnum == so -> rumstate .attrnAddToColumn &&
2169+ if (countByAnother < nOrderByAnother &&
2170+ so -> keys [i ]-> attnum == so -> rumstate .attrnAddToColumn &&
21412171 so -> keys [i ]-> outerAddInfoIsNull == false)
21422172 {
21432173 Assert (!so -> keys [i ]-> orderBy );
@@ -2150,7 +2180,23 @@ insertScanItem(RumScanOpaque so, bool recheck)
21502180 {
21512181 so -> keys [j ]-> outerAddInfoIsNull = false;
21522182 so -> keys [j ]-> outerAddInfo = so -> keys [i ]-> outerAddInfo ;
2153- count ++ ;
2183+ countByAnother ++ ;
2184+ }
2185+ }
2186+ }
2187+ else if (countByKey < nOrderByKey && so -> keys [i ]-> nentries > 0 &&
2188+ so -> keys [i ]-> scanEntry [0 ]-> useCurKey )
2189+ {
2190+ Assert (!so -> keys [i ]-> orderBy );
2191+
2192+ for (j = i + 1 ; j < so -> nkeys ; j ++ )
2193+ {
2194+ if (so -> keys [j ]-> useCurKey )
2195+ {
2196+ so -> keys [j ]-> curKey = so -> keys [i ]-> scanEntry [0 ]-> curKey ;
2197+ so -> keys [j ]-> curKeyCategory =
2198+ so -> keys [i ]-> scanEntry [0 ]-> curKeyCategory ;
2199+ countByKey ++ ;
21542200 }
21552201 }
21562202 }
@@ -2222,10 +2268,7 @@ rumgettuple(IndexScanDesc scan, ScanDirection direction)
22222268 if (so -> naturalOrder == NoMovementScanDirection )
22232269 {
22242270 so -> sortstate = rum_tuplesort_begin_rum (work_mem , so -> norderbys ,
2225- false,
2226- so -> totalentries > 0 &&
2227- so -> entries [0 ]-> queryCategory == RUM_CAT_EMPTY_QUERY &&
2228- so -> entries [0 ]-> scanWithAddInfo );
2271+ false, so -> scanType == RumFullScan );
22292272
22302273
22312274 while (scanGetItem (scan , & so -> item , & so -> item , & recheck ))
0 commit comments