1212#include "funcapi.h"
1313
1414#include "access/htup_details.h"
15+ #include "access/heapam.h"
16+
1517#include "point.h"
18+ #include "crossmatch.h"
1619
1720extern void _PG_init (void );
1821
@@ -37,6 +40,9 @@ typedef struct
3740HeapTupleData scan_tuple ;/* buffer to fetch tuple */
3841List * dev_tlist ;/* tlist to be returned from the device */
3942List * dev_quals ;/* quals to be run on the device */
43+
44+ Relation left ;
45+ Relation right ;
4046}CrossmatchScanState ;
4147
4248static CustomPathMethods crossmatch_path_methods ;
@@ -91,6 +97,7 @@ create_crossmatch_path(PlannerInfo *root,
9197result -> cpath .path .parent = joinrel ;
9298result -> cpath .path .param_info = param_info ;
9399result -> cpath .path .pathkeys = NIL ;
100+ result -> cpath .path .pathtarget = & joinrel -> reltarget ;
94101result -> cpath .path .rows = joinrel -> rows ;
95102result -> cpath .flags = 0 ;
96103result -> cpath .methods = & crossmatch_path_methods ;
@@ -221,7 +228,7 @@ create_crossmatch_plan(PlannerInfo *root,
221228{
222229CrossmatchJoinPath * gpath = (CrossmatchJoinPath * )best_path ;
223230List * joinrestrictclauses = gpath -> joinrestrictinfo ;
224- List * joinclauses ;
231+ List * joinclauses ;/* NOTE: do we really need it? */
225232List * otherclauses ;
226233CustomScan * cscan ;
227234
@@ -249,7 +256,6 @@ create_crossmatch_plan(PlannerInfo *root,
249256
250257cscan -> flags = best_path -> flags ;
251258cscan -> methods = & crossmatch_plan_methods ;
252- cscan -> custom_plans = list_copy_tail (custom_plans ,1 );
253259
254260return & cscan -> scan .plan ;
255261}
@@ -261,45 +267,57 @@ crossmatch_create_scan_state(CustomScan *node)
261267
262268NodeSetTag (scan_state ,T_CustomScanState );
263269scan_state -> css .flags = node -> flags ;
264- if (node -> methods == & crossmatch_plan_methods )
265- scan_state -> css .methods = & crossmatch_exec_methods ;
266- else
267- elog (ERROR ,"Bug? unexpected CustomPlanMethods" );
270+ scan_state -> css .methods = & crossmatch_exec_methods ;
271+
272+ scan_state -> css .ss .ps .ps_TupFromTlist = false;
268273
269274return (Node * )scan_state ;
270275}
271276
272- /* HACK: remove this */
273- static int i = 0 ;
274-
275277static void
276278crossmatch_begin (CustomScanState * node ,EState * estate ,int eflags )
277279{
278- i = 0 ;
280+
279281}
280282
281283static TupleTableSlot *
282284crossmatch_exec (CustomScanState * node )
283285{
284- TupleTableSlot * slot = node -> ss .ss_ScanTupleSlot ;
285- TupleDesc tupdesc = node -> ss .ss_ScanTupleSlot -> tts_tupleDescriptor ;
286+ TupleTableSlot * slot = node -> ss .ps .ps_ResultTupleSlot ;
287+ TupleDesc tupdesc = node -> ss .ps .ps_ResultTupleSlot -> tts_tupleDescriptor ;
288+ ExprContext * econtext = node -> ss .ps .ps_ProjInfo -> pi_exprContext ;
286289HeapTuple htup ;
287290
291+ HeapTupleData fetched_tup ;
292+
293+ ResetExprContext (econtext );
294+
288295/* TODO: fill with real data from joined tables */
289- Datum values [2 ]= {DirectFunctionCall1 (spherepoint_in ,CStringGetDatum ("(0d, 0d)" )),
296+ Datum values [4 ]= {DirectFunctionCall1 (spherepoint_in ,CStringGetDatum ("(0d, 0d)" )),
290297DirectFunctionCall1 (spherepoint_in ,CStringGetDatum ("(0d, 0d)" )) };
291- bool nulls [2 ]= {0 ,0 };
298+ bool nulls [4 ]= {0 ,1 ,1 ,1 };
299+
300+ elog (LOG ,"slot.natts: %d" ,tupdesc -> natts );
292301
293302htup = heap_form_tuple (tupdesc ,values ,nulls );
294303
295- elog (LOG ,"natts: %d" ,tupdesc -> natts );
304+ if (node -> ss .ps .ps_ProjInfo -> pi_itemIsDone != ExprEndResult )
305+ {
306+ TupleTableSlot * result ;
307+ ExprDoneCond isDone ;
308+
309+ econtext -> ecxt_scantuple = ExecStoreTuple (htup ,slot ,InvalidBuffer , false);
296310
297- i ++ ;
311+ result = ExecProject ( node -> ss . ps . ps_ProjInfo , & isDone ) ;
298312
299- if (i > 10 )
300- ExecClearTuple (slot );
313+ if (isDone != ExprEndResult )
314+ {
315+ node -> ss .ps .ps_TupFromTlist = (isDone == ExprMultipleResult );
316+ return result ;
317+ }
318+ }
301319else
302- ExecStoreTuple ( htup , slot , InvalidBuffer , false );
320+ ExecClearTuple ( slot );
303321
304322return slot ;
305323}