11#include "pathman.h"
22#include "postgres.h"
33#include "fmgr.h"
4+ #include "nodes/nodeFuncs.h"
45#include "nodes/pg_list.h"
56#include "nodes/relation.h"
67#include "nodes/primnodes.h"
@@ -59,6 +60,7 @@ static void change_varnos_in_restrinct_info(RestrictInfo *rinfo, Oid old_varno,
5960static void change_varnos (Node * node ,Oid old_varno ,Oid new_varno );
6061static bool change_varno_walker (Node * node ,change_varno_context * context );
6162
63+ /* callbacks */
6264PG_FUNCTION_INFO_V1 (on_partitions_created );
6365PG_FUNCTION_INFO_V1 (on_partitions_updated );
6466PG_FUNCTION_INFO_V1 (on_partitions_removed );
@@ -173,15 +175,15 @@ my_hook(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
173175// // children = lappend_int(children, prel->children[i]);
174176// children = lappend_int(children, dsm_arr[i]);
175177
176- ranges = list_make1_int (make_range (0 ,prel -> children_count - 1 ));
178+ ranges = list_make1_int (make_irange (0 ,prel -> children_count - 1 , false ));
177179
178180/* Run over restrictions and collect children partitions */
179181ereport (LOG , (errmsg ("Checking restrictions" )));
180182foreach (lc ,rel -> baserestrictinfo )
181183{
182184RestrictInfo * rinfo = (RestrictInfo * )lfirst (lc );
183185List * ret = walk_expr_tree (rinfo -> clause ,prel ,& all );
184- ranges = intersect_ranges (ranges ,ret );
186+ ranges = irange_list_intersect (ranges ,ret );
185187
186188// if (!all)
187189// {
@@ -194,7 +196,7 @@ my_hook(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
194196{
195197int i ;
196198IndexRange range = (IndexRange )lfirst (lc );
197- for (i = range_min (range );i <= range_max (range );i ++ )
199+ for (i = irange_lower (range );i <= irange_upper (range );i ++ )
198200children = lappend_int (children ,dsm_arr [i ]);
199201}
200202
@@ -341,7 +343,7 @@ append_child_relation(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEnt
341343childrel -> baserestrictinfo = NIL ;
342344foreach (lc ,rel -> baserestrictinfo )
343345{
344- RestrictInfo * new_rinfo ;
346+ Node * new_rinfo ;
345347
346348node = (Node * )lfirst (lc );
347349new_rinfo = copyObject (node );
@@ -351,9 +353,6 @@ append_child_relation(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEnt
351353
352354childrel -> baserestrictinfo = lappend (childrel -> baserestrictinfo ,
353355new_rinfo );
354-
355- /* TODO: temporarily commented out */
356- // reconstruct_restrictinfo((Node *) new_rinfo, prel, childOID);
357356}
358357
359358/* Build an AppendRelInfo for this parent and child */
@@ -435,7 +434,6 @@ change_varnos_in_restrinct_info(RestrictInfo *rinfo, Oid old_varno, Oid new_varn
435434}
436435}
437436
438-
439437/*
440438 * Recursive function to walk through conditions tree
441439 */
@@ -473,12 +471,15 @@ handle_binary_opexpr(const PartRelationInfo *prel, const OpExpr *expr,
473471const Var * v ,const Const * c )
474472{
475473HashRelationKey key ;
476- HashRelation * hashrel ;
477474RangeRelation * rangerel ;
478475int int_value ;
479476Datum value ;
480- int i ,j ;
481- int startidx ,endidx ;
477+ int i ,
478+ startidx ,
479+ endidx ;
480+ FmgrInfo * cmp_func ;
481+
482+ * all = false;
482483
483484/* determine operator type */
484485TypeCacheEntry * tce = lookup_type_cache (v -> vartype ,
@@ -493,7 +494,7 @@ handle_binary_opexpr(const PartRelationInfo *prel, const OpExpr *expr,
493494int_value = DatumGetInt32 (c -> constvalue );
494495key .hash = make_hash (prel ,int_value );
495496
496- return list_make1_int ( make_range (key .hash ,key .hash ));
497+ return list_make1_irange ( make_irange (key .hash ,key .hash , true ));
497498
498499// key.parent_oid = prel->oid;
499500// hashrel = (HashRelation *)
@@ -572,7 +573,7 @@ handle_binary_opexpr(const PartRelationInfo *prel, const OpExpr *expr,
572573{
573574case OP_STRATEGY_LT :
574575startidx = 0 ;
575- endidx = ( re -> min == value ) ?i - 1 :i ;
576+ endidx = check_eq ( cmp_func , re -> min , value ) ?i - 1 :i ;
576577break ;
577578case OP_STRATEGY_LE :
578579startidx = 0 ;
@@ -581,19 +582,19 @@ handle_binary_opexpr(const PartRelationInfo *prel, const OpExpr *expr,
581582case BTEqualStrategyNumber :
582583// return list_make1_int(re->child_oid);
583584// return list_make1_int(make_range(prel->oid, prel->oid));
584- return list_make1_int ( make_range (i ,i ));
585+ return list_make1_irange ( make_irange (i ,i , true ));
585586case BTGreaterEqualStrategyNumber :
586587startidx = i ;
587588endidx = rangerel -> nranges - 1 ;
588589break ;
589- case OP_STRATEGY_GT :
590- startidx = ( re -> max == value ) ?i + 1 :i ;
590+ case BTGreaterStrategyNumber :
591+ startidx = check_eq ( cmp_func , re -> max , value ) ?i + 1 :i ;
591592endidx = rangerel -> nranges - 1 ;
592593}
593594// for (j=startidx; j<=endidx; j++)
594595// children = lappend_int(children, ranges[j].child_oid);
595596* all = false;
596- return list_make1_int ( make_range (startidx ,endidx ));
597+ return list_make1_irange ( make_irange (startidx ,endidx , true ));
597598
598599// return children;
599600}
@@ -650,7 +651,7 @@ handle_boolexpr(const BoolExpr *expr, const PartRelationInfo *prel)
650651List * b = ALL ;
651652
652653if (expr -> boolop == AND_EXPR )
653- ret = list_make1_int ( make_range (0 ,RANGE_INFINITY ));
654+ ret = list_make1_irange ( make_irange (0 ,RANGE_INFINITY , false ));
654655
655656foreach (lc ,expr -> args )
656657{
@@ -691,11 +692,11 @@ handle_boolexpr(const BoolExpr *expr, const PartRelationInfo *prel)
691692// break;
692693
693694case OR_EXPR :
694- ret = unite_ranges (ret ,b );
695+ ret = irange_list_union (ret ,b );
695696// list_free(b);
696697break ;
697698case AND_EXPR :
698- ret = intersect_ranges (ret ,b );
699+ ret = irange_list_intersect (ret ,b );
699700// list_free(b);
700701break ;
701702default :
@@ -715,7 +716,6 @@ handle_arrexpr(const ScalarArrayOpExpr *expr, const PartRelationInfo *prel)
715716Node * varnode = (Node * )linitial (expr -> args );
716717Node * arraynode = (Node * )lsecond (expr -> args );
717718HashRelationKey key ;
718- HashRelation * hashrel ;
719719
720720if (varnode == NULL || !IsA (varnode ,Var ))
721721return ALL ;
@@ -730,8 +730,8 @@ handle_arrexpr(const ScalarArrayOpExpr *expr, const PartRelationInfo *prel)
730730int num_elems ;
731731Datum * elem_values ;
732732bool * elem_nulls ;
733- int i ;
734- List * oids = NIL ;
733+ int i ;
734+ List * ret = NIL ;
735735
736736/* extract values from array */
737737arrayval = DatumGetArrayTypeP (((Const * )arraynode )-> constvalue );
@@ -743,7 +743,7 @@ handle_arrexpr(const ScalarArrayOpExpr *expr, const PartRelationInfo *prel)
743743& elem_values ,& elem_nulls ,& num_elems );
744744
745745/* construct OIDs list */
746- for (i = 0 ;i < num_elems ;i ++ )
746+ for (i = 0 ;i < num_elems ;i ++ )
747747{
748748key .hash = make_hash (prel ,elem_values [i ]);
749749// key.parent_oid = prel->oid;
@@ -753,14 +753,15 @@ handle_arrexpr(const ScalarArrayOpExpr *expr, const PartRelationInfo *prel)
753753// if (hashrel != NULL)
754754// oids = list_append_unique_int(oids, hashrel->child_oid);
755755
756- oids = list_append_unique_int (oids , make_range (key .hash ,key .hash ));
756+ ret = list_append_unique_int (ret , make_irange (key .hash ,key .hash , true ));
757757}
758758
759759/* free resources */
760760pfree (elem_values );
761761pfree (elem_nulls );
762762
763- return oids ;
763+ * all = false;
764+ return ret ;
764765}
765766return ALL ;
766767}