@@ -375,7 +375,7 @@ multicornGetForeignPaths(PlannerInfo *root,
375375 /* Try to find parameterized paths */
376376 pathes = findPaths (root , baserel , possiblePaths , planstate -> startupCost ,
377377 planstate , apply_pathkeys , deparsed_pathkeys );
378-
378+
379379 /* Add a simple default path */
380380 pathes = lappend (pathes , create_foreignscan_path (root , baserel ,
381381 NULL , /* default pathtarget */
@@ -406,6 +406,10 @@ multicornGetForeignPaths(PlannerInfo *root,
406406 }
407407 }
408408
409+ /* Determine if the sort is completely pushed down and store the results to be used in the upper paths */
410+ /* Regardless, store the deparsed pathkeys to be used in the upper paths */
411+ planstate -> sort_pushed_down = pathkeys_contained_in (root -> sort_pathkeys , apply_pathkeys );
412+
409413 /* Add each ForeignPath previously found */
410414 foreach (lc , pathes )
411415 {
@@ -419,11 +423,13 @@ multicornGetForeignPaths(PlannerInfo *root,
419423 {
420424 ForeignPath * newpath ;
421425
422- MulticornPathState * pathstate = (MulticornPathState * )calloc ( 1 , sizeof (MulticornPathState ));
426+ MulticornPathState * pathstate = (MulticornPathState * )palloc0 ( sizeof (MulticornPathState ));
423427 pathstate -> pathkeys = deparsed_pathkeys ;
424428 pathstate -> limit = -1 ;
425429 pathstate -> offset = -1 ;
426430
431+ planstate -> pathkeys = deparsed_pathkeys ;
432+
427433 newpath = create_foreignscan_path (root , baserel ,
428434 NULL , /* default pathtarget */
429435 path -> path .rows ,
@@ -458,23 +464,21 @@ static void multicornGetForeignUpperPaths(PlannerInfo *root,
458464 RelOptInfo * output_rel ,
459465 void * extra )
460466{
461- // elog(WARNING, "Got input_rel private: %p", input_rel->fdw_private);
462- // elog(WARNING, "Got output_rel private: %p", output_rel->fdw_private);
463-
464- // If the input_rel has no private, then pushdown wasn't supported for the previous stage
465- // Which means we can't pushdown anything for the the current stage (as least this is true for limit/offset)
467+ // If the input_rel has no private, then pushdown wasn't supported for the previous stage.
468+ // Therefore we can't pushdown anything for the the current stage (as least this is true for limit/offset)
466469 if (!input_rel -> fdw_private )
467470 return ;
468471
469- // elog(WARNING, "Got stage: %d", stage);
470472 switch (stage )
471473 {
472474 case UPPERREL_ORDERED :
473475 add_foreign_ordered_paths (root , input_rel , output_rel , (FinalPathExtraData * )extra );
474476 break ;
477+
475478 case UPPERREL_FINAL :
476479 add_foreign_final_paths (root , input_rel , output_rel , (FinalPathExtraData * )extra );
477480 break ;
481+
478482 default :
479483 break ;
480484 }
@@ -484,51 +488,17 @@ static void multicornGetForeignUpperPaths(PlannerInfo *root,
484488 * add_foreign_ordered_paths
485489 * Add foreign paths for performing the sort processing remotely.
486490 *
487- * Given input_rel contains the source-data Paths. The paths are added to the
488- * given final_rel.
489- *
490491 * Note: Since sorts are already taken care of in the base rel, we only check for pushdown here.
491492 */
492493 static void
493494 add_foreign_ordered_paths (PlannerInfo * root , RelOptInfo * input_rel ,
494495 RelOptInfo * final_rel ,
495496 FinalPathExtraData * extra )
496497 {
497- List * applied_pathkeys = NIL ;
498- ListCell * lc ;
499498 MulticornPlanState * planstate = input_rel -> fdw_private ;
500- ForeignPath * cheapest_path = (ForeignPath * )input_rel -> cheapest_total_path ;
501- if ( planstate )
502- {
503- if (cheapest_path && IsA (cheapest_path , ForeignPath ))
504- {
505- MulticornPathState * pathstate = (MulticornPathState * )cheapest_path -> fdw_private ;
506- if ( pathstate )
507- {
508- planstate -> pathkeys = pathstate -> pathkeys ;
509- }
510- }
511-
512- /* Extract the pathkeys from the input_rel */
513- foreach (lc , input_rel -> pathlist )
514- {
515- Path * path = (Path * ) lfirst (lc );
516- if (IsA (path , ForeignPath ))
517- {
518- ForeignPath * fpath = (ForeignPath * ) path ;
519- if (fpath -> path .pathkeys != NIL )
520- {
521- applied_pathkeys = fpath -> path .pathkeys ;
522- break ;
523- }
524- }
525- }
526-
527- /* We only support limit/offset if the sort is completely pushed down */
528- /* By bailing here, input_rel for the next state will not have planstate, which will cause no more pushdowns */
529- if (!pathkeys_contained_in (root -> sort_pathkeys , applied_pathkeys ))
530- return ;
531499
500+ if ( planstate && planstate -> sort_pushed_down )
501+ {
532502 planstate -> input_rel = input_rel ;
533503 final_rel -> fdw_private = planstate ;
534504 }
@@ -584,7 +554,7 @@ static void multicornGetForeignUpperPaths(PlannerInfo *root,
584554 if (parse -> limitOffset )
585555 limitOffset = DatumGetInt32 (((Const * )parse -> limitOffset )-> constvalue );
586556
587- /* Get the current input_rel and it's planstate */
557+ /* Get the current planstate and its input_rel */
588558 planstate = input_rel -> fdw_private ;
589559 if ( planstate -> input_rel )
590560 input_rel = planstate -> input_rel ;
@@ -593,11 +563,13 @@ static void multicornGetForeignUpperPaths(PlannerInfo *root,
593563 if (!canLimit (planstate , limitCount , limitOffset ))
594564 return ;
595565
596- /* Create foreign final path with the correct number of rows, and include state for limit/offset pushdown */
597- pathstate = (MulticornPathState * )calloc ( 1 , sizeof (MulticornPathState ));
566+ /* Include pathkeys and limit/offset in pathstate */
567+ pathstate = (MulticornPathState * )palloc ( sizeof (MulticornPathState ));
598568 pathstate -> pathkeys = planstate -> pathkeys ;
599569 pathstate -> limit = limitCount ;
600570 pathstate -> offset = limitOffset ;
571+
572+ /* Create foreign final path with the correct number of rows and cost. */
601573 final_path = create_foreign_upper_path (root ,
602574 input_rel ,
603575 root -> upper_targets [UPPERREL_FINAL ],
@@ -1369,7 +1341,7 @@ serializePlanState(MulticornPlanState * state)
13691341 List * result = NULL ;
13701342
13711343 result = lappend (result , makeConst (INT4OID ,
1372- -1 , InvalidOid , 4 , Int32GetDatum (state -> numattrs ), false, true));
1344+ -1 , InvalidOid , 4 , Int32GetDatum (state -> numattrs ), false, true));
13731345 result = lappend (result , makeConst (INT4OID ,
13741346 -1 , InvalidOid , 4 , Int32GetDatum (state -> foreigntableid ), false, true));
13751347 result = lappend (result , state -> target_list );
0 commit comments