11
22#include "kv_fdw.h"
3- #include "kv_storage.h"
43
54#include <pthread.h>
65
@@ -54,6 +53,7 @@ static void GetForeignRelSize(PlannerInfo *root,
5453 * min & max will call GetForeignRelSize & GetForeignPaths multiple times,
5554 * we should open & close db multiple times.
5655 */
56+ printf ("\n-----------------%s open----------------------\n" , __func__ );
5757 ptr = OpenRequest (foreignTableId , ptr );
5858
5959 /* TODO better estimation */
@@ -146,6 +146,88 @@ static ForeignScan *GetForeignPlan(PlannerInfo *root,
146146 NULL );
147147}
148148
149+ #ifdef VidarDB
150+ static void GetKeyRangeQual (Plan * plan ,
151+ TupleDesc tupleDescriptor ,
152+ TableReadState * readState ) {
153+ if (!plan ) {
154+ return ;
155+ }
156+
157+ int qualSize = list_length (plan -> qual );
158+ if (qualSize < 2 ) {
159+ return ;
160+ }
161+
162+ bool startFlag = false;
163+ bool limitFlag = false;
164+ StringInfo startInfo = makeStringInfo ();
165+ StringInfo limitInfo = makeStringInfo ();
166+
167+ ListCell * lc ;
168+ foreach (lc , plan -> qual ) {
169+ Expr * state = lfirst (lc );
170+ if (!state || !IsA (state , OpExpr )) {
171+ continue ;
172+ }
173+ OpExpr * op = (OpExpr * ) state ;
174+ if (list_length (op -> args ) != 2 ) {
175+ continue ;
176+ }
177+
178+ Node * left = list_nth (op -> args , 0 );
179+ if (!IsA (left , Var )) {
180+ continue ;
181+ }
182+
183+ Node * right = list_nth (op -> args , 1 );
184+ if (!IsA (right , Const )) {
185+ continue ;
186+ }
187+
188+ Index varattno = ((Var * ) left )-> varattno ;
189+ if (varattno != 1 ) {
190+ continue ;
191+ }
192+
193+ HeapTuple opertup = SearchSysCache1 (OPEROID , ObjectIdGetDatum (op -> opno ));
194+ if (!HeapTupleIsValid (opertup )) {
195+ ereport (ERROR , (errmsg ("cache lookup failed for operator %u" , op -> opno )));
196+ }
197+ Form_pg_operator operform = (Form_pg_operator ) GETSTRUCT (opertup );
198+ char * oprname = NameStr (operform -> oprname );
199+ if (strncmp (oprname , ">=" , NAMEDATALEN ) == 0 ) {
200+ startFlag = true;
201+ Const * constNode = ((Const * ) right );
202+ Datum datum = constNode -> constvalue ;
203+ TypeCacheEntry * typeEntry = lookup_type_cache (constNode -> consttype , 0 );
204+ datum = ShortVarlena (datum , typeEntry -> typlen , typeEntry -> typstorage );
205+ SerializeAttribute (tupleDescriptor , varattno - 1 , datum , startInfo );
206+ ReleaseSysCache (opertup );
207+ }else if (strncmp (oprname , "<=" , NAMEDATALEN ) == 0 ) {
208+ limitFlag = true;
209+ Const * constNode = ((Const * ) right );
210+ Datum datum = constNode -> constvalue ;
211+ TypeCacheEntry * typeEntry = lookup_type_cache (constNode -> consttype , 0 );
212+ datum = ShortVarlena (datum , typeEntry -> typlen , typeEntry -> typstorage );
213+ SerializeAttribute (tupleDescriptor , varattno - 1 , datum , limitInfo );
214+ ReleaseSysCache (opertup );
215+ }else {
216+ ReleaseSysCache (opertup );
217+ continue ;
218+ }
219+
220+ if (startFlag && limitFlag ) {
221+ readState -> isRangeQueryUsed = true;
222+ readState -> rangeSpec .start = startInfo -> data ;
223+ readState -> rangeSpec .startLen = startInfo -> len ;
224+ readState -> rangeSpec .limit = limitInfo -> data ;
225+ readState -> rangeSpec .limitLen = limitInfo -> len ;
226+ }
227+ }
228+ }
229+ #endif
230+
149231static void GetKeyBasedQual (Node * node ,
150232 TupleDesc tupleDescriptor ,
151233 TableReadState * readState ) {
@@ -242,9 +324,13 @@ static void BeginForeignScan(ForeignScanState *scanState, int executorFlags) {
242324 return ;
243325 }
244326
327+ //printf("\n-----------------Plan Type: %d----------------------\n", scanState->ss.ps.plan->type);
328+
245329 ListCell * lc ;
246330 foreach (lc , scanState -> ss .ps .plan -> qual ) {
247331 Expr * state = lfirst (lc );
332+ //printf("\n-----------------Qual Type: %d----------------------\n", state->type);
333+
248334 GetKeyBasedQual ((Node * ) state ,
249335 scanState -> ss .ss_currentRelation -> rd_att ,
250336 readState );
@@ -255,8 +341,18 @@ static void BeginForeignScan(ForeignScanState *scanState, int executorFlags) {
255341 }
256342
257343 if (!readState -> isKeyBased ) {
258- Oid relationId = RelationGetRelid (scanState -> ss .ss_currentRelation );
259- GetIterRequest (relationId , ptr );
344+ #ifdef VidarDB
345+ GetKeyRangeQual (scanState -> ss .ps .plan ,
346+ scanState -> ss .ss_currentRelation -> rd_att ,
347+ readState );
348+ #endif
349+ //if(readState->isRangeQueryUsed) {
350+
351+ //}else
352+ //{
353+ Oid relationId = RelationGetRelid (scanState -> ss .ss_currentRelation );
354+ GetIterRequest (relationId , ptr );
355+ //}
260356 }
261357}
262358
@@ -359,6 +455,9 @@ static TupleTableSlot *IterateForeignScan(ForeignScanState *scanState) {
359455 found = GetRequest (relationId , ptr , k , kLen , & v , & vLen );
360456 readState -> done = true;
361457 }
458+ /*} else if (readState->isRangeQueryUsed)
459+ {*/
460+
362461 } else {
363462 found = NextRequest (relationId , ptr , & k , & kLen , & v , & vLen );
364463 }
0 commit comments