2121#include "utils/lsyscache.h"
2222#include "utils/bytea.h"
2323#include "utils/snapmgr.h"
24+ #include "optimizer/clauses.h"
2425
2526
2627HTAB * relations = NULL ;
@@ -32,6 +33,7 @@ static bool globalByVal;
3233
3334static bool validate_range_constraint (Expr * ,PartRelationInfo * ,Datum * ,Datum * );
3435static bool validate_hash_constraint (Expr * expr ,PartRelationInfo * prel ,int * hash );
36+ static bool read_opexpr_const (OpExpr * opexpr ,int varattno ,Datum * val );
3537static int cmp_range_entries (const void * p1 ,const void * p2 );
3638
3739Size
@@ -453,11 +455,7 @@ validate_range_constraint(Expr *expr, PartRelationInfo *prel, Datum *min, Datum
453455OpExpr * opexpr ;
454456
455457/* it should be an AND operator on top */
456- if ( !(IsA (expr ,BoolExpr )&& boolexpr -> boolop == AND_EXPR ) )
457- return false;
458-
459- /* and it should have exactly two operands */
460- if (list_length (boolexpr -> args )!= 2 )
458+ if (!and_clause ((Node * )expr ))
461459return false;
462460
463461tce = lookup_type_cache (prel -> atttype ,TYPECACHE_EQ_OPR |TYPECACHE_LT_OPR |TYPECACHE_GT_OPR );
@@ -466,36 +464,43 @@ validate_range_constraint(Expr *expr, PartRelationInfo *prel, Datum *min, Datum
466464opexpr = (OpExpr * )linitial (boolexpr -> args );
467465if (get_op_opfamily_strategy (opexpr -> opno ,tce -> btree_opf )== BTGreaterEqualStrategyNumber )
468466{
469- Node * left = linitial (opexpr -> args );
470- Node * right = lsecond (opexpr -> args );
471- if ( !IsA (left ,Var )|| !IsA (right ,Const ) )
472- return false;
473- if ( ((Var * )left )-> varattno != prel -> attnum )
467+ if (!read_opexpr_const (opexpr ,prel -> attnum ,min ))
474468return false;
475- * min = ((Const * )right )-> constvalue ;
476469}
477470else
478471return false;
479472
480- /* TODO: rewrite this */
481473/* check that right operand is < operator */
482474opexpr = (OpExpr * )lsecond (boolexpr -> args );
483475if (get_op_opfamily_strategy (opexpr -> opno ,tce -> btree_opf )== BTLessStrategyNumber )
484476{
485- Node * left = linitial (opexpr -> args );
486- Node * right = lsecond (opexpr -> args );
487- if ( !IsA (left ,Var )|| !IsA (right ,Const ) )
488- return false;
489- if ( ((Var * )left )-> varattno != prel -> attnum )
477+ if (!read_opexpr_const (opexpr ,prel -> attnum ,max ))
490478return false;
491- * max = ((Const * )right )-> constvalue ;
492479}
493480else
494481return false;
495482
496483return true;
497484}
498485
486+ /*
487+ * Reads const value from expressions of kind: VAR >= CONST or VAR < CONST
488+ */
489+ static bool
490+ read_opexpr_const (OpExpr * opexpr ,int varattno ,Datum * val )
491+ {
492+ Node * left = linitial (opexpr -> args );
493+ Node * right = lsecond (opexpr -> args );
494+
495+ if ( !IsA (left ,Var )|| !IsA (right ,Const ) )
496+ return false;
497+ if ( ((Var * )left )-> varattno != varattno )
498+ return false;
499+ * val = ((Const * )right )-> constvalue ;
500+
501+ return true;
502+ }
503+
499504/*
500505 * Validate hash constraint. It MUST have the exact format
501506 * VARIABLE % CONST = CONST