21
21
#include "utils/lsyscache.h"
22
22
#include "utils/bytea.h"
23
23
#include "utils/snapmgr.h"
24
+ #include "optimizer/clauses.h"
24
25
25
26
26
27
HTAB * relations = NULL ;
@@ -32,6 +33,7 @@ static bool globalByVal;
32
33
33
34
static bool validate_range_constraint (Expr * ,PartRelationInfo * ,Datum * ,Datum * );
34
35
static bool validate_hash_constraint (Expr * expr ,PartRelationInfo * prel ,int * hash );
36
+ static bool read_opexpr_const (OpExpr * opexpr ,int varattno ,Datum * val );
35
37
static int cmp_range_entries (const void * p1 ,const void * p2 );
36
38
37
39
Size
@@ -453,11 +455,7 @@ validate_range_constraint(Expr *expr, PartRelationInfo *prel, Datum *min, Datum
453
455
OpExpr * opexpr ;
454
456
455
457
/* 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 ))
461
459
return false;
462
460
463
461
tce = 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
466
464
opexpr = (OpExpr * )linitial (boolexpr -> args );
467
465
if (get_op_opfamily_strategy (opexpr -> opno ,tce -> btree_opf )== BTGreaterEqualStrategyNumber )
468
466
{
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 ))
474
468
return false;
475
- * min = ((Const * )right )-> constvalue ;
476
469
}
477
470
else
478
471
return false;
479
472
480
- /* TODO: rewrite this */
481
473
/* check that right operand is < operator */
482
474
opexpr = (OpExpr * )lsecond (boolexpr -> args );
483
475
if (get_op_opfamily_strategy (opexpr -> opno ,tce -> btree_opf )== BTLessStrategyNumber )
484
476
{
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 ))
490
478
return false;
491
- * max = ((Const * )right )-> constvalue ;
492
479
}
493
480
else
494
481
return false;
495
482
496
483
return true;
497
484
}
498
485
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
+
499
504
/*
500
505
* Validate hash constraint. It MUST have the exact format
501
506
* VARIABLE % CONST = CONST