1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 ***************************************************************************/
18 #include<CatalogTables.h>
21 #include<PredicateImpl.h>
24 #include<JoinTableImpl.h>
26 static char aggNames
[][10] =
28 "MIN", "MAX", "SUM", "AVG", "COUNT", ""
31 PredicateImpl::~PredicateImpl()
33 // if (lhs) {delete lhs; lhs = NULL; }
34 // if (rhs) { delete rhs; rhs = NULL; }
36 void PredicateImpl::print(int space
)
38 char spaceBuf
[IDENTIFIER_LENGTH
];
39 memset(spaceBuf
, 32, IDENTIFIER_LENGTH
);
40 spaceBuf
[space
] = '\0';
42 printf("%s <PREDICATE>\n", spaceBuf
);
43 if (0 != strcmp(fldName1
, "")) {
44 if (aggType
== AGG_UNKNOWN
)
45 printf("%s <FieldName1> %s </FieldName1>\n", spaceBuf
, fldName1
);
47 printf("%s <FieldName1> %s(%s) </FieldName1>\n", spaceBuf
,
48 aggNames
[aggType
-1], fldName1
);
50 if (0 != strcmp(fldName2
, ""))
51 printf("%s <FieldName2> %s </FieldName2>\n", spaceBuf
, fldName2
);
52 if (compOp
!= OpInvalidComparisionOp
)
53 printf("%s <CompOp> %s </CompOp>\n", spaceBuf
, CompOpNames
[compOp
]);
54 if (logicalOp
!= OpInvalidLogicalOp
)
55 printf("%s <LogOp> %s </LogOp>\n", spaceBuf
, LogOpNames
[logicalOp
]);
56 if (operand
) printf("%s <Operand> VALUE </Operand>\n", spaceBuf
);
57 if (operandPtr
) printf("%s <OperandPtr> VALUE </OperandPtr>\n", spaceBuf
);
58 if (comp2Op
!= OpInvalidComparisionOp
)
59 printf("%s <Comp2Op> %s </Comp2Op>\n", spaceBuf
, CompOpNames
[comp2Op
]);
60 if (operand2
) printf("%s <Operand2> VALUE </Operand2>\n", spaceBuf
);
61 if (operand2Ptr
) printf("%s <Operand2Ptr> VALUE </Operand2Ptr>\n", spaceBuf
);
63 //printf("<ISPUSHEDDOWN> %d </ISPUSHEDDOWN>\n", isPushedDown);
66 printf("%s <PRED-LEFT>\n", spaceBuf
);
68 printf("%s </PRED-LEFT>\n", spaceBuf
);
72 printf("%s <PRED-RIGHT>\n", spaceBuf
);
74 printf("%s </PRED-RIGHT>\n", spaceBuf
);
76 printf("%s </PREDICATE>\n", spaceBuf
);
80 void PredicateImpl::setTerm(const char* fName1
, ComparisionOp op
,
83 strcpy(fldName1
, fName1
);
84 strcpy(fldName2
, fName2
);
90 logicalOp
= OpInvalidLogicalOp
;
91 comp2Op
= OpInvalidComparisionOp
;
96 //Operand should be of the same type of the field.This is must
97 void PredicateImpl::setTerm(const char* fName1
, ComparisionOp op
, void *opnd
)
99 strcpy(fldName1
, fName1
);
105 logicalOp
= OpInvalidLogicalOp
;
106 comp2Op
= OpInvalidComparisionOp
;
110 void PredicateImpl::setTerm(Expression
*exp
, ComparisionOp op
, void **opnd
)
116 logicalOp
= OpInvalidLogicalOp
;
117 comp2Op
= OpInvalidComparisionOp
;
122 void PredicateImpl::setTerm(Expression
*exp1
, ComparisionOp op
, Expression
*exp2
)
128 logicalOp
= OpInvalidLogicalOp
;
129 comp2Op
= OpInvalidComparisionOp
;
135 void PredicateImpl::setTerm(Expression
*exp
, ComparisionOp op
, const char *fName2
)
137 strcpy(fldName2
, fName2
);
143 logicalOp
= OpInvalidLogicalOp
;
144 comp2Op
= OpInvalidComparisionOp
;
151 void PredicateImpl::setTerm(const char* fName1
, ComparisionOp op
,bool nullFlag
)
153 strcpy(fldName1
, fName1
);
159 logicalOp
= OpInvalidLogicalOp
;
160 comp2Op
= OpInvalidComparisionOp
;
165 void PredicateImpl::setTerm(const char* fName1
, ComparisionOp op
, void **opnd
)
167 strcpy(fldName1
, fName1
);
173 logicalOp
= OpInvalidLogicalOp
;
174 comp2Op
= OpInvalidComparisionOp
;
178 void PredicateImpl::setTerm(const char* fName1
, ComparisionOp op
, void **opnd
, AggType aType
)
180 strcpy(fldName1
, fName1
);
187 logicalOp
= OpInvalidLogicalOp
;
188 comp2Op
= OpInvalidComparisionOp
;
193 void PredicateImpl::setTerm(const char* fName1
, ComparisionOp op
, void **opnd
,
194 ComparisionOp op2
, void **opnd2
)
196 strcpy(fldName1
, fName1
);
202 logicalOp
= OpInvalidLogicalOp
;
209 void PredicateImpl::setParent(PredicateImpl
*pImpl
)
211 //if (parent != NULL) printf("Parent already set\n");
215 void PredicateImpl::setTerm(Predicate
*p1
, LogicalOp op
, Predicate
*p2
)
217 if (p2
== NULL
&& op
!= OpNot
|| op
== OpNot
&& p2
!= NULL
)
220 printError(ErrBadArg
, "Wrong argument passed\n");
223 lhs
= (PredicateImpl
*)p1
;
224 rhs
= (PredicateImpl
*)p2
;
226 compOp
= OpInvalidComparisionOp
;
227 if (lhs
!= NULL
) lhs
->setParent(this);
228 if (rhs
!= NULL
) rhs
->setParent(this);
232 void PredicateImpl::setTable(Table
*tbl
)
240 void PredicateImpl::setIfNoLeftRight()
243 lhs
->setIfNoLeftRight();
245 rhs
->setIfNoLeftRight();
246 if(NULL
== lhs
&& NULL
== rhs
) isNoLeftRight
=true;
250 void PredicateImpl::setTuple(void *tpl
)
262 void PredicateImpl::setProjectionList(List
*lst
)
265 lhs
->setProjectionList(lst
);
267 rhs
->setProjectionList(lst
);
269 isBindBufSet
= false;
271 bool PredicateImpl::isSingleTerm()
273 if (NULL
== lhs
&& NULL
== rhs
&& comp2Op
== OpInvalidComparisionOp
)
278 bool PredicateImpl::appendIfSameFld(char *fName
, ComparisionOp op
, void *buf
)
280 char fieldName1
[IDENTIFIER_LENGTH
];
281 Table::getFieldNameAlone(fldName1
, fieldName1
);
282 if (strcmp(fName
,fieldName1
) == 0)
284 printDebug(DM_Predicate
, "Field name matched");
286 //switching so that in case of joins, first other conditions are
287 //evaluated first and then matching tuples for join is evaluated
288 //otherwise it may give wrong result set
289 if (operand) {operand2 = operand; operand2Ptr = NULL; }
290 if (operandPtr) {operand2Ptr = operandPtr; operand2 = NULL; }
303 bool PredicateImpl::isIsNullInvolved()
305 bool lhsResult
= true, rhsResult
= true;
308 lhsResult
= lhs
->isIsNullInvolved();
312 rhsResult
= rhs
->isIsNullInvolved();
316 if (lhsResult
|| rhsResult
) return true;
317 if(compOp
== isNull
) return true;
321 bool PredicateImpl::isNotOrInvolved()
323 bool lhsResult
= true, rhsResult
= true;
326 lhsResult
= lhs
->isNotOrInvolved();
330 rhsResult
= rhs
->isNotOrInvolved();
334 //Means it involves only Logical operator
338 if (lhsResult
|| rhsResult
) return true; else return false;
352 DbRetVal
PredicateImpl::evaluateLogical(bool &result
)
354 bool rhsResult
= false, lhsResult
=false;
355 DbRetVal retCode
=OK
;
359 retCode
= lhs
->evaluate(lhsResult
);
360 if (retCode
!= OK
) return ErrInvalidExpr
;
361 }else lhsResult
= true;
364 retCode
= rhs
->evaluate(rhsResult
);
365 if (retCode
!= OK
) return ErrInvalidExpr
;
366 } else rhsResult
= true;
369 //Means it involves only Logical operator
370 if (OpAnd
== logicalOp
) {
371 if (lhsResult
&& rhsResult
) result
= true;
372 }else if (OpOr
== logicalOp
) {
373 if (lhsResult
|| rhsResult
) result
= true;
374 }else if (OpNot
== logicalOp
){
375 if (lhsResult
) result
= false; else result
= true;
377 printDebug(DM_Predicate
, "result is %d", result
);
382 DbRetVal
PredicateImpl::evaluateForHaving(bool &result
, AggTableImpl
*aImpl
, void *aggElement
)
384 bool rhsResult
= false, lhsResult
=false;
385 DbRetVal retCode
=OK
;
389 retCode
= lhs
->evaluateForHaving(lhsResult
, aImpl
, aggElement
);
390 if (retCode
!= OK
) return ErrInvalidExpr
;
391 }else lhsResult
= true;
394 retCode
= rhs
->evaluateForHaving(rhsResult
, aImpl
, aggElement
);
395 if (retCode
!= OK
) return ErrInvalidExpr
;
396 } else rhsResult
= true;
399 if (OpAnd
== logicalOp
) {
400 if (lhsResult
&& rhsResult
) result
= true;
401 }else if (OpOr
== logicalOp
) {
402 if (lhsResult
|| rhsResult
) result
= true;
403 }else if (OpNot
== logicalOp
){
404 if (lhsResult
) result
= false; else result
= true;
406 printDebug(DM_Predicate
, "result is %d", result
);
410 void *val1
= NULL
, *val2
=NULL
;
411 int offset
= aImpl
->getAggOffset(fldName1
, aggType
);
412 val1
= (void*)((char*)aggElement
+ offset
);
414 //sets the type and length when it is called first time
415 FieldInfo
*info
= new FieldInfo();
416 DbRetVal rv
= aImpl
->getFieldInfo(fldName1
, info
);
417 if (aggType
== AGG_AVG
) {
419 length
= sizeof(double);
420 } else if (aggType
== AGG_COUNT
) {
422 length
= sizeof(int);
425 length
= info
->length
;
430 if(operand
!= NULL
&& operandPtr
== NULL
)
432 val2
= (char*) operand
;
434 else if(operand
== NULL
&& operandPtr
!= NULL
)
436 val2
= *(char**)operandPtr
;
438 if (aggType
== AGG_AVG
) {
440 AllDataType::convertToDouble(&dVal2
, val2
, type
);
441 result
= AllDataType::compareVal(val1
, &dVal2
, compOp
, typeDouble
, length
);
443 else if (aggType
== AGG_COUNT
) {
445 AllDataType::convertToInt(&dVal2
, val2
, type
);
446 result
= AllDataType::compareVal(val1
, &dVal2
, compOp
, typeInt
, length
);
449 result
= AllDataType::compareVal(val1
, val2
, compOp
, type
, length
);
453 DbRetVal
PredicateImpl::evaluateLogicalForTable(bool &result
, char *tuple
)
455 bool rhsResult
= false, lhsResult
=false;
456 DbRetVal retCode
=OK
;
460 lhs
->evaluateForTable(lhsResult
, tuple
);
461 }else lhsResult
= true;
464 rhs
->evaluateForTable(rhsResult
, tuple
);
465 } else rhsResult
= true;
468 //Means it involves only Logical operator
469 if (OpAnd
== logicalOp
) {
470 if (lhsResult
&& rhsResult
) result
= true;
471 }else if (OpOr
== logicalOp
) {
472 if (lhsResult
|| rhsResult
) result
= true;
473 }else if (OpNot
== logicalOp
){
474 if (lhsResult
) result
= false; else result
= true;
476 printDebug(DM_Predicate
, "result is %d", result
);
480 void PredicateImpl::evaluateForTable(bool &result
, char *tuple
)
482 if (!isNoLeftRight
) {
483 bool rhsResult
= false;
486 rhs
->evaluateForTable(rhsResult
, tuple
);
487 if(rhsResult
== false && OpAnd
== logicalOp
) {//do early return
491 } else rhsResult
= true;
492 bool lhsResult
= false;
495 lhs
->evaluateForTable(lhsResult
, tuple
);
496 }else lhsResult
= true;
499 //Means it involves only Logical operator
500 if (OpAnd
== logicalOp
) {
501 if (lhsResult
&& rhsResult
) result
= true;
502 }else if (OpOr
== logicalOp
) {
503 if (lhsResult
|| rhsResult
) result
= true;
504 }else if (OpNot
== logicalOp
){
505 if (lhsResult
) result
= false; else result
= true;
507 printDebug(DM_Predicate
, "result is %d", result
);
511 //Table null check of condition
515 TableImpl
*tImpl
= (TableImpl
*) table
;
517 lExp
->setTable(tImpl
);
518 lExp
->setTuple(tuple
);
519 val
= lExp
->evaluateForFunction(AllDataType::getCsqlTypeFromFunctionType(lExp
->getFunctionType()));
522 rExp
->setTable(tImpl
);
523 rExp
->setTuple(tuple
);
524 rval
= rExp
->evaluateForFunction(AllDataType::getCsqlTypeFromFunctionType(rExp
->getFunctionType()));
527 result
= AllDataType::compareVal(val
, rval
, compOp
, AllDataType::getCsqlTypeFromFunctionTypeForComparision(lExp
->getFunctionType()),length
);
528 }else if( val
&& operandPtr
!=NULL
){
529 val2
= *(char**)operandPtr
;
530 result
= AllDataType::compareVal(val
, val2
, compOp
, AllDataType::getCsqlTypeFromFunctionTypeForComparision(lExp
->getFunctionType()),length
);
531 }else if(val
&& (offset2
!= -1 && operand
== NULL
&& operandPtr
== NULL
)){
532 val2
= tuple
+ offset2
;
533 result
= AllDataType::compareVal(val
, val2
, compOp
, AllDataType::getCsqlTypeFromFunctionTypeForComparision(lExp
->getFunctionType()),length
);
541 TableImpl
*tImpl
= (TableImpl
*) table
;
542 tImpl
->setCurTuple(tuple
);
543 bool isValueNull
= table
->isFldNull(fldPos
);
544 if(compOp
== OpIsNull
)
546 if( (isValueNull
&& isNull
) || (!isValueNull
&& !isNull
) )
558 //the below code works only for single table
559 val1
= tuple
+ offset1
;
560 if(offset2
!= -1 && operand
== NULL
&& operandPtr
== NULL
)
561 val2
= tuple
+ offset2
;
563 //Assumes that fldName2 data type is also same for expr f1 <f2
564 //Note:Perf: Do not change the order below
565 if(operand
== NULL
&& operandPtr
!= NULL
)
567 val2
= *(char**)operandPtr
;
568 if (compOp
== OpLike
) {
569 char *c
= (char *)val2
;
570 //OPT:If LIKE has only %, then no need to evaluate
571 if (*c
== '%' && *(c
+1) == '\0') {result
=true; return;}
572 Util::changeWildcardChar(val2
);
575 } else if (operand
== NULL
&& operandPtr
== NULL
)
578 val2
= tuple
+ offset2
;
579 } else if(operand
!= NULL
&& operandPtr
== NULL
)
581 val2
= (char*) operand
;
582 if (compOp
== OpLike
) {
583 char *c
= (char *)val2
;
584 if (*c
== '%' && *(c
+1) == '\0') {result
=true; return;}
585 Util::changeWildcardChar(val2
);
588 if(operand2
== NULL
&& operand2Ptr
!= NULL
)
590 val3
= *(char**)operand2Ptr
;
591 } else if(operand2
!= NULL
&& operand2Ptr
== NULL
)
593 val3
= (char*) operand2
;
599 //printf(" val1 %d val3 %d\n", *(int*)val1, *(int*)val3);
600 result
= AllDataType::compareVal(val1
, val3
, comp2Op
, type
,length
);
601 if(result
==false) return;
606 //printf(" val1 %d val2 %d\n", *(int*)val1, *(int*)val2);
607 if (type
!= typeVarchar
)
608 result
= AllDataType::compareVal(val1
, val2
, compOp
, type
,length
);
610 result
= AllDataType::compareVal((void *) *(long *) val1
, val2
,
611 compOp
, type
,length
);
614 void* PredicateImpl::getValIfPointLookupOnInt(int &offset
)
616 if (NULL
!= lhs
&& NULL
!= rhs
) return NULL
;
617 if(typeInt
!= type
|| comp2Op
!=OpInvalidComparisionOp
) return NULL
;
618 if (compOp
!= OpEquals
) return NULL
;
621 if(operand
== NULL
&& operandPtr
!= NULL
)
623 val
= *(void**)operandPtr
;
624 } else if(operand
!= NULL
&& operandPtr
== NULL
)
626 val
= (void*) operand
;
630 void* PredicateImpl::getVal1IfBetweenOnInt(int &offset
)
632 if (NULL
!= lhs
&& NULL
!= rhs
) return NULL
;
633 if(typeInt
!= type
) return NULL
;
634 if (compOp
!= OpGreaterThanEquals
||
635 comp2Op
!=OpLessThanEquals
) return NULL
;
638 if(operand
== NULL
&& operandPtr
!= NULL
)
640 val
= *(void**)operandPtr
;
641 } else if(operand
!= NULL
&& operandPtr
== NULL
)
643 val
= (void*) operand
;
647 void* PredicateImpl::getVal2IfBetweenOnInt(int &offset
)
649 if (NULL
!= lhs
&& NULL
!= rhs
) return NULL
;
650 if(typeInt
!= type
) return NULL
;
651 if (compOp
!= OpGreaterThanEquals
||
652 comp2Op
!=OpLessThanEquals
) return NULL
;
655 if(operand2
== NULL
&& operand2Ptr
!= NULL
)
657 val
= *(void**)operand2Ptr
;
658 } else if(operand2
!= NULL
&& operand2Ptr
== NULL
)
660 val
= (void*) operand2
;
664 DbRetVal
PredicateImpl::evaluate(bool &result
)
666 if (!isNoLeftRight
) {
667 bool rhsResult
= false, lhsResult
=false;
668 DbRetVal retCode
=OK
;
672 retCode
= lhs
->evaluate(lhsResult
);
673 if (retCode
< OK
) return ErrInvalidExpr
;
674 }else lhsResult
= true;
677 retCode
= rhs
->evaluate(rhsResult
);
678 if (retCode
< OK
) return ErrInvalidExpr
;
679 } else rhsResult
= true;
682 //Means it involves only Logical operator
683 if (OpAnd
== logicalOp
) {
684 if (lhsResult
&& rhsResult
) result
= true;
685 }else if (OpOr
== logicalOp
) {
686 if (lhsResult
|| rhsResult
) result
= true;
687 }else if (OpNot
== logicalOp
){
688 if (lhsResult
) result
= false; else result
= true;
689 if ( ErrNullValues
== retCode
) result
= false;
691 printDebug(DM_Predicate
, "result is %d", result
);
695 //Means it is relational expression
696 //first operand is always field identifier
697 //get the value in the tuple
699 if (dontEvaluate
) {result
= true; return OK
; }
702 //for join node evaluation
703 ListIterator fIter
= projList
->getIterator();
704 JoinProjFieldInfo
*def
;
706 while (fIter
.hasElement())
708 def
= (JoinProjFieldInfo
*) fIter
.nextElement();
709 if (NULL
!= def
->bindBuf
) {
710 if (0 == strcmp(fldName1
, def
->tabFieldName
))
712 val1
= (char*)def
->bindBuf
;
714 length
= def
->length
;
718 printError(ErrNotExists
, "Field not binded %s.%s\n",
719 def
->tableName
, def
->fieldName
);
723 if (operand
== NULL
&& operandPtr
== NULL
)
725 char fieldName2
[IDENTIFIER_LENGTH
];
726 memset(fieldName2
, 0, IDENTIFIER_LENGTH
);
727 Table::getFieldNameAlone(fldName2
, fieldName2
);
730 while (fIter
.hasElement())
732 def
= (JoinProjFieldInfo
*) fIter
.nextElement();
733 if (NULL
!= def
->bindBuf
) {
734 if (0 == strcmp(fldName2
, def
->tabFieldName
))
736 val2
= (char*)def
->bindBuf
;
740 printError(ErrNotExists
, "Field not binded %s.%s\n",
741 def
->tableName
, def
->fieldName
);
747 else if(operand
!= NULL
&& operandPtr
== NULL
)
749 val2
= (char*) operand
;
751 else if(operand
== NULL
&& operandPtr
!= NULL
)
753 val2
= *(char**)operandPtr
;
757 JoinTableImpl
*jTable
= (JoinTableImpl
*) table
;
758 if (jTable
->isFldNullInt(fldName1
) || jTable
->isFldNullInt(fldName2
))
761 return ErrNullValues
;
763 result
= AllDataType::compareVal(val1
, val2
, compOp
, type
,
768 printf("PRABA::wrong method call\n");
769 //the below code works only for single table
770 val1
= (char*)tuple
+ offset1
;
771 if(offset2
!= -1 && operand
== NULL
&& operandPtr
== NULL
)
772 val2
= ((char*)tuple
) + offset2
;
774 //Assumes that fldName2 data type is also same for expr f1 <f2
775 //Note:Perf: Do not change the order below
776 if(operand
== NULL
&& operandPtr
!= NULL
)
778 val2
= *(char**)operandPtr
;
780 else if (operand
== NULL
&& operandPtr
== NULL
)
783 val2
= ((char*)tuple
) + offset2
;
785 else if(operand
!= NULL
&& operandPtr
== NULL
)
787 val2
= (char*) operand
;
791 result
= AllDataType::compareVal(val1
, val2
, compOp
, type
,length
);
795 void PredicateImpl::solveForProjList(Table
*tab
)
799 lhs
->solveForProjList(tab
);
803 rhs
->solveForProjList(tab
);
806 if (NULL
!= lhs
) return ;
811 ListIterator fIter
= projList
->getIterator();
812 JoinProjFieldInfo
*def
;
813 while (fIter
.hasElement())
815 def
= (JoinProjFieldInfo
*) fIter
.nextElement();
816 if (!isExist1
&& 0 == strcmp(fldName1
, def
->tabFieldName
))
820 if (!isExist2
&& 0 == strcmp(fldName2
, def
->tabFieldName
) )
827 tab
->bindFld(fldName1
, NULL
);
829 if (!isExist2
&& strcmp(fldName2
, "")!=0)
831 tab
->bindFld(fldName2
, NULL
);
836 void PredicateImpl::setOffsetAndType()
840 lhs
->setOffsetAndType();
844 rhs
->setOffsetAndType();
846 char fieldName1
[IDENTIFIER_LENGTH
];
847 char fieldName2
[IDENTIFIER_LENGTH
];
848 memset(fieldName1
, 0, IDENTIFIER_LENGTH
);
849 memset(fieldName2
, 0, IDENTIFIER_LENGTH
);
850 Table::getFieldNameAlone(fldName1
, fieldName1
);
851 Table::getFieldNameAlone(fldName2
, fieldName2
);
852 //this function is called only from TableImpl
853 TableImpl
*tImpl
= (TableImpl
*) table
;
855 FieldInfo
*info
= new FieldInfo();
856 tImpl
->getFieldInfo(fieldName1
, info
);
857 offset1
= tImpl
->getFieldOffset(fieldName1
);
858 fldPos
= tImpl
->getFldPos(fieldName1
);
860 length
= info
->length
;
862 if (info
->isNull
|| info
->isPrimary
|| info
->isAutoIncrement
)
864 //printf("isNullable is set to %d\n", isNullable);
869 offset2
= tImpl
->getFieldOffset(fieldName2
);
870 if(typeUnknown
== type
)
871 type
= tImpl
->getFieldType(fieldName2
);
876 bool PredicateImpl::pointLookupInvolved(const char *fname
)
878 bool rhsResult
, lhsResult
;
881 lhsResult
= lhs
->pointLookupInvolved(fname
);
885 rhsResult
= rhs
->pointLookupInvolved(fname
);
889 //Means it involves only Logical operator
894 if (lhsResult
|| rhsResult
) return true; else return false;
905 //Means it is relational expression
906 //first operand is always field identifier
907 char fieldName1
[IDENTIFIER_LENGTH
];
908 Table::getFieldNameAlone(fldName1
, fieldName1
);
909 if (OpEquals
== compOp
)
911 //for expressions f1 == f2 use full scan, so return false
912 if(NULL
== operand
&& NULL
== operandPtr
) return false;
913 if(0 == strcmp(fieldName1
, fname
))
921 bool PredicateImpl::isBetweenInvolved(const char *fname
)
923 bool rhsResult
, lhsResult
;
926 lhsResult
= lhs
->isBetweenInvolved(fname
);
930 rhsResult
= rhs
->isBetweenInvolved(fname
);
937 if (lhsResult
&& rhsResult
) return true; else return false;
944 char fieldName1
[IDENTIFIER_LENGTH
];
945 Table::getFieldNameAlone(fldName1
, fieldName1
);
946 if ( OpGreaterThan
== compOp
|| OpGreaterThanEquals
== compOp
)
948 if(0 == strcmp(fieldName1
, fname
))
956 bool PredicateImpl::rangeQueryInvolved(const char *fname
)
958 bool rhsResult
, lhsResult
;
961 lhsResult
= lhs
->rangeQueryInvolved(fname
);
965 rhsResult
= rhs
->rangeQueryInvolved(fname
);
972 if (lhsResult
|| rhsResult
) return true; else return false;
983 //Means it is relational expression
984 //first operand is always field identifier
985 char fieldName1
[IDENTIFIER_LENGTH
];
986 Table::getFieldNameAlone(fldName1
, fieldName1
);
987 if (OpLessThan
== compOp
|| OpLessThanEquals
== compOp
||
988 OpGreaterThan
== compOp
|| OpGreaterThanEquals
== compOp
)
990 //for expressions f1 == f2 use full scan, so return false
991 if(NULL
== operand
&& NULL
== operandPtr
) return false;
992 if(0 == strcmp(fieldName1
, fname
))
1000 void* PredicateImpl::opAndValPtrForIndexField(const char *fname
, bool isUnique
,ComparisionOp
&op
)
1002 ComparisionOp lhsOp
= OpInvalidComparisionOp
, rhsOp
= OpInvalidComparisionOp
;
1003 void *lhsRet
=NULL
, *rhsRet
=NULL
;
1006 lhsRet
= lhs
->opAndValPtrForIndexField(fname
, isUnique
, lhsOp
);
1010 rhsRet
= rhs
->opAndValPtrForIndexField(fname
, isUnique
,rhsOp
);
1012 if (lhsRet
&& lhsOp
== OpEquals
) { op
= lhsOp
; return lhsRet
;}
1013 if (rhsRet
&& rhsOp
== OpEquals
) { op
= rhsOp
; return rhsRet
;}
1016 if( lhsRet
) { op
= lhsOp
; return lhsRet
; }
1017 if( rhsRet
) { op
= rhsOp
; return rhsRet
; }
1019 char fieldName1
[IDENTIFIER_LENGTH
];
1020 Table::getFieldNameAlone(fldName1
, fieldName1
);
1021 //Means it is relational expression
1022 //first operand is always field identifier
1023 if(0 == strcmp(fieldName1
, fname
))
1026 if (isUnique
&& compOp
!= OpLessThan
&&
1027 compOp
!= OpLessThanEquals
&&
1028 compOp
!= OpNotEquals
) isPushedDown
= true;
1029 if (operand
) return operand
; else return *(void**)operandPtr
;
1031 op
= OpInvalidComparisionOp
;
1036 //called only in case of hash index scan
1037 void* PredicateImpl::valPtrForIndexField(const char *fname
, bool isUnique
)
1039 void *lhsRet
=NULL
, *rhsRet
=NULL
;
1042 lhsRet
= lhs
->valPtrForIndexField(fname
, isUnique
);
1043 if ( lhsRet
!= NULL
) return lhsRet
;
1047 rhsRet
= rhs
->valPtrForIndexField(fname
, isUnique
);
1048 if ( rhsRet
!= NULL
) return rhsRet
;
1050 char fieldName1
[IDENTIFIER_LENGTH
];
1051 Table::getFieldNameAlone(fldName1
, fieldName1
);
1052 //Means it is relational expression
1053 //first operand is always field identifier
1054 if (OpEquals
== compOp
)
1056 if(0 == strcmp(fieldName1
, fname
))
1058 if (isUnique
) isPushedDown
= true;
1059 if (operand
) return operand
; else return *(void**)operandPtr
;
1064 ComparisionOp
PredicateImpl::opForIndexField(const char *fname
)
1066 ComparisionOp lhsRet
= OpInvalidComparisionOp
, rhsRet
= OpInvalidComparisionOp
;
1069 lhsRet
= lhs
->opForIndexField(fname
);
1070 if ( lhsRet
!= OpInvalidComparisionOp
) return lhsRet
;
1075 rhsRet
= rhs
->opForIndexField(fname
);
1076 if ( rhsRet
!= OpInvalidComparisionOp
) return rhsRet
;
1078 char fieldName1
[IDENTIFIER_LENGTH
];
1079 Table::getFieldNameAlone(fldName1
, fieldName1
);
1080 if(0 == strcmp(fieldName1
, fname
))
1084 return OpInvalidComparisionOp
;
1086 PredicateImpl
* PredicateImpl::getTablePredicate()
1088 PredicateImpl
*lhsRet
= NULL
, *rhsRet
= NULL
;
1091 lhsRet
= lhs
->getTablePredicate();
1092 if ( lhsRet
!= NULL
) return lhsRet
;
1096 rhsRet
= rhs
->getTablePredicate();
1097 if ( rhsRet
!= NULL
) return rhsRet
;
1099 if (operand
|| operandPtr
)
1101 //printf("PRABA::getTablePredicate returning %s %d\n", fldName1, compOp);
1104 if (this == parent
->lhs
) {
1116 PredicateImpl
* PredicateImpl::getJoinPredicate()
1118 PredicateImpl
*lhsRet
= NULL
, *rhsRet
= NULL
;
1121 lhsRet
= lhs
->getJoinPredicate();
1122 if ( lhsRet
!= NULL
) return lhsRet
;
1126 rhsRet
= rhs
->getJoinPredicate();
1127 if ( rhsRet
!= NULL
) return rhsRet
;
1129 if (0 != strcmp(fldName2
, ""))
1131 //printf("PRABA::getJoinPredicate returning %s %s\n", fldName1, fldName2);
1134 if (this == parent
->lhs
)
1144 void PredicateImpl::removeIfNotNecessary()
1148 lhs
->removeIfNotNecessary();
1152 rhs
->removeIfNotNecessary();
1154 if (logicalOp
!= OpAnd
) return;
1155 if (NULL
== lhs
&& NULL
== rhs
)
1161 if (this == parent
->rhs
) parent
->rhs
= NULL
;
1162 else if (this == parent
->lhs
) parent
->lhs
= NULL
;
1163 //TODO::PRABA::fix the leak below. if uncommented dumps core
1165 //WARNINGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG
1166 //current object is deleted. do not any code here
1169 else if (NULL
== lhs
)
1171 //left side of the node is empty means we can remove this AND node
1172 //and place it as left or right of my parent where i am currently placed
1177 if (this == parent
->rhs
) parent
->rhs
=this->rhs
;
1178 else if (this == parent
->lhs
) parent
->lhs
= this->rhs
;
1179 //TODO::PRABA::fix the leak below. if uncommented dumps core
1181 //WARNINGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG
1182 //current object is deleted. do not any code here
1185 else if (NULL
== rhs
)
1187 //right side of the node is empty means we can remove this AND node
1188 //and place it as left or right of my parent where i am currently placed
1193 if (this == parent
->rhs
) parent
->rhs
=this->lhs
;
1194 else if (this == parent
->lhs
) parent
->lhs
= this->lhs
;
1195 //TODO::PRABA::fix the leak below. if uncommented dumps core
1197 //WARNINGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG
1198 //current object is deleted. do not any code here
1203 bool PredicateImpl::isDummyPredicate()
1205 if (NULL
== lhs
&& NULL
== rhs
&& NULL
== parent
1206 && NULL
== operand
&& NULL
== operandPtr
&&
1207 (0 == strcmp(fldName1
, "")) && (0==strcmp(fldName2
, "")))
1212 PredicateImpl
* PredicateImpl::getIfOneSidedPredicate()
1214 if (logicalOp
!= OpAnd
) return NULL
;
1215 if (NULL
== lhs
&& NULL
!=rhs
)
1219 if (NULL
!= lhs
&& NULL
==rhs
)