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
);
102 char *c
= (char *) opnd
;
105 if (*c
== '_') *c
= '?';
106 else if(*c
== '%') *c
= '*';
115 logicalOp
= OpInvalidLogicalOp
;
116 comp2Op
= OpInvalidComparisionOp
;
120 void PredicateImpl::setTerm(const char* fName1
, ComparisionOp op
,bool nullFlag
)
122 strcpy(fldName1
, fName1
);
128 logicalOp
= OpInvalidLogicalOp
;
129 comp2Op
= OpInvalidComparisionOp
;
134 void PredicateImpl::setTerm(const char* fName1
, ComparisionOp op
, void **opnd
)
136 strcpy(fldName1
, fName1
);
142 logicalOp
= OpInvalidLogicalOp
;
143 comp2Op
= OpInvalidComparisionOp
;
147 void PredicateImpl::setTerm(const char* fName1
, ComparisionOp op
, void **opnd
, AggType aType
)
149 strcpy(fldName1
, fName1
);
156 logicalOp
= OpInvalidLogicalOp
;
157 comp2Op
= OpInvalidComparisionOp
;
162 void PredicateImpl::setTerm(const char* fName1
, ComparisionOp op
, void **opnd
,
163 ComparisionOp op2
, void **opnd2
)
165 strcpy(fldName1
, fName1
);
171 logicalOp
= OpInvalidLogicalOp
;
178 void PredicateImpl::setParent(PredicateImpl
*pImpl
)
180 //if (parent != NULL) printf("Parent already set\n");
184 void PredicateImpl::setTerm(Predicate
*p1
, LogicalOp op
, Predicate
*p2
)
186 if (p2
== NULL
&& op
!= OpNot
|| op
== OpNot
&& p2
!= NULL
)
189 printError(ErrBadArg
, "Wrong argument passed\n");
192 lhs
= (PredicateImpl
*)p1
;
193 rhs
= (PredicateImpl
*)p2
;
195 compOp
= OpInvalidComparisionOp
;
196 if (lhs
!= NULL
) lhs
->setParent(this);
197 if (rhs
!= NULL
) rhs
->setParent(this);
201 void PredicateImpl::setTable(Table
*tbl
)
209 void PredicateImpl::setIfNoLeftRight()
212 lhs
->setIfNoLeftRight();
214 rhs
->setIfNoLeftRight();
215 if(NULL
== lhs
&& NULL
== rhs
) isNoLeftRight
=true;
219 void PredicateImpl::setTuple(void *tpl
)
231 void PredicateImpl::setProjectionList(List
*lst
)
234 lhs
->setProjectionList(lst
);
236 rhs
->setProjectionList(lst
);
238 isBindBufSet
= false;
240 bool PredicateImpl::isSingleTerm()
242 if (NULL
== lhs
&& NULL
== rhs
&& comp2Op
== OpInvalidComparisionOp
)
247 bool PredicateImpl::appendIfSameFld(char *fName
, ComparisionOp op
, void *buf
)
249 char fieldName1
[IDENTIFIER_LENGTH
];
250 Table::getFieldNameAlone(fldName1
, fieldName1
);
251 if (strcmp(fName
,fieldName1
) == 0)
253 printDebug(DM_Predicate
, "Field name matched");
255 //switching so that in case of joins, first other conditions are
256 //evaluated first and then matching tuples for join is evaluated
257 //otherwise it may give wrong result set
258 if (operand) {operand2 = operand; operand2Ptr = NULL; }
259 if (operandPtr) {operand2Ptr = operandPtr; operand2 = NULL; }
272 bool PredicateImpl::isIsNullInvolved()
274 bool lhsResult
= true, rhsResult
= true;
277 lhsResult
= lhs
->isIsNullInvolved();
281 rhsResult
= rhs
->isIsNullInvolved();
285 if (lhsResult
|| rhsResult
) return true;
286 if(compOp
== isNull
) return true;
290 bool PredicateImpl::isNotOrInvolved()
292 bool lhsResult
= true, rhsResult
= true;
295 lhsResult
= lhs
->isNotOrInvolved();
299 rhsResult
= rhs
->isNotOrInvolved();
303 //Means it involves only Logical operator
307 if (lhsResult
|| rhsResult
) return true; else return false;
321 DbRetVal
PredicateImpl::evaluateLogical(bool &result
)
323 bool rhsResult
= false, lhsResult
=false;
324 DbRetVal retCode
=OK
;
328 retCode
= lhs
->evaluate(lhsResult
);
329 if (retCode
!= OK
) return ErrInvalidExpr
;
330 }else lhsResult
= true;
333 retCode
= rhs
->evaluate(rhsResult
);
334 if (retCode
!= OK
) return ErrInvalidExpr
;
335 } else rhsResult
= true;
338 //Means it involves only Logical operator
339 if (OpAnd
== logicalOp
) {
340 if (lhsResult
&& rhsResult
) result
= true;
341 }else if (OpOr
== logicalOp
) {
342 if (lhsResult
|| rhsResult
) result
= true;
343 }else if (OpNot
== logicalOp
){
344 if (lhsResult
) result
= false; else result
= true;
346 printDebug(DM_Predicate
, "result is %d", result
);
351 DbRetVal
PredicateImpl::evaluateForHaving(bool &result
, AggTableImpl
*aImpl
, void *aggElement
)
353 bool rhsResult
= false, lhsResult
=false;
354 DbRetVal retCode
=OK
;
358 retCode
= lhs
->evaluateForHaving(lhsResult
, aImpl
, aggElement
);
359 if (retCode
!= OK
) return ErrInvalidExpr
;
360 }else lhsResult
= true;
363 retCode
= rhs
->evaluateForHaving(rhsResult
, aImpl
, aggElement
);
364 if (retCode
!= OK
) return ErrInvalidExpr
;
365 } else rhsResult
= true;
368 if (OpAnd
== logicalOp
) {
369 if (lhsResult
&& rhsResult
) result
= true;
370 }else if (OpOr
== logicalOp
) {
371 if (lhsResult
|| rhsResult
) result
= true;
372 }else if (OpNot
== logicalOp
){
373 if (lhsResult
) result
= false; else result
= true;
375 printDebug(DM_Predicate
, "result is %d", result
);
379 void *val1
= NULL
, *val2
=NULL
;
380 int offset
= aImpl
->getAggOffset(fldName1
, aggType
);
381 val1
= (void*)((char*)aggElement
+ offset
);
383 //sets the type and length when it is called first time
384 FieldInfo
*info
= new FieldInfo();
385 DbRetVal rv
= aImpl
->getFieldInfo(fldName1
, info
);
386 if (aggType
== AGG_AVG
) {
388 length
= sizeof(double);
389 } else if (aggType
== AGG_COUNT
) {
391 length
= sizeof(int);
394 length
= info
->length
;
399 if(operand
!= NULL
&& operandPtr
== NULL
)
401 val2
= (char*) operand
;
403 else if(operand
== NULL
&& operandPtr
!= NULL
)
405 val2
= *(char**)operandPtr
;
407 if (aggType
== AGG_AVG
) {
409 AllDataType::convertToDouble(&dVal2
, val2
, type
);
410 result
= AllDataType::compareVal(val1
, &dVal2
, compOp
, typeDouble
, length
);
412 else if (aggType
== AGG_COUNT
) {
414 AllDataType::convertToInt(&dVal2
, val2
, type
);
415 result
= AllDataType::compareVal(val1
, &dVal2
, compOp
, typeInt
, length
);
418 result
= AllDataType::compareVal(val1
, val2
, compOp
, type
, length
);
422 DbRetVal
PredicateImpl::evaluateLogicalForTable(bool &result
, char *tuple
)
424 bool rhsResult
= false, lhsResult
=false;
425 DbRetVal retCode
=OK
;
429 lhs
->evaluateForTable(lhsResult
, tuple
);
430 }else lhsResult
= true;
433 rhs
->evaluateForTable(rhsResult
, tuple
);
434 } else rhsResult
= true;
437 //Means it involves only Logical operator
438 if (OpAnd
== logicalOp
) {
439 if (lhsResult
&& rhsResult
) result
= true;
440 }else if (OpOr
== logicalOp
) {
441 if (lhsResult
|| rhsResult
) result
= true;
442 }else if (OpNot
== logicalOp
){
443 if (lhsResult
) result
= false; else result
= true;
445 printDebug(DM_Predicate
, "result is %d", result
);
449 void PredicateImpl::evaluateForTable(bool &result
, char *tuple
)
451 if (!isNoLeftRight
) {
452 bool rhsResult
= false;
455 rhs
->evaluateForTable(rhsResult
, tuple
);
456 if(rhsResult
== false && OpAnd
== logicalOp
) {//do early return
459 } else rhsResult
= true;
460 bool lhsResult
= false;
463 lhs
->evaluateForTable(lhsResult
, tuple
);
464 }else lhsResult
= true;
467 //Means it involves only Logical operator
468 if (OpAnd
== logicalOp
) {
469 if (lhsResult
&& rhsResult
) result
= true;
470 }else if (OpOr
== logicalOp
) {
471 if (lhsResult
|| rhsResult
) result
= true;
472 }else if (OpNot
== logicalOp
){
473 if (lhsResult
) result
= false; else result
= true;
475 printDebug(DM_Predicate
, "result is %d", result
);
479 //Table null check of condition
481 TableImpl
*tImpl
= (TableImpl
*) table
;
482 tImpl
->setCurTuple(tuple
);
483 bool isValueNull
= table
->isFldNull(fldPos
);
484 if(compOp
== OpIsNull
)
486 if( (isValueNull
&& isNull
) || (!isValueNull
&& !isNull
) )
498 //the below code works only for single table
499 val1
= tuple
+ offset1
;
500 if(offset2
!= -1 && operand
== NULL
&& operandPtr
== NULL
)
501 val2
= tuple
+ offset2
;
503 //Assumes that fldName2 data type is also same for expr f1 <f2
504 //Note:Perf: Do not change the order below
505 if(operand
== NULL
&& operandPtr
!= NULL
)
507 val2
= *(char**)operandPtr
;
508 if (compOp
== OpLike
) {
509 char *c
= (char *)val2
;
510 //OPT:If LIKE has only %, then no need to evaluate
511 if (*c
== '%' && *(c
+1) == '\0') {result
=true; return;}
512 Util::changeWildcardChar(val2
);
515 } else if (operand
== NULL
&& operandPtr
== NULL
)
518 val2
= tuple
+ offset2
;
519 } else if(operand
!= NULL
&& operandPtr
== NULL
)
521 val2
= (char*) operand
;
522 if (compOp
== OpLike
) {
523 char *c
= (char *)val2
;
524 if (*c
== '%' && *(c
+1) == '\0') {result
=true; return;}
525 Util::changeWildcardChar(val2
);
528 if(operand2
== NULL
&& operand2Ptr
!= NULL
)
530 val3
= *(char**)operand2Ptr
;
531 } else if(operand2
!= NULL
&& operand2Ptr
== NULL
)
533 val3
= (char*) operand2
;
539 //printf(" val1 %d val3 %d\n", *(int*)val1, *(int*)val3);
540 result
= AllDataType::compareVal(val1
, val3
, comp2Op
, type
,length
);
541 if(result
==false) return;
546 //printf(" val1 %d val2 %d\n", *(int*)val1, *(int*)val2);
547 result
= AllDataType::compareVal(val1
, val2
, compOp
, type
,length
);
548 //if (!result && val3) AllDataType::copyVal(val3,
551 void* PredicateImpl::getValIfPointLookupOnInt(int &offset
)
553 if (NULL
!= lhs
&& NULL
!= rhs
) return NULL
;
554 if(typeInt
!= type
|| comp2Op
!=OpInvalidComparisionOp
) return NULL
;
555 if (compOp
!= OpEquals
) return NULL
;
558 if(operand
== NULL
&& operandPtr
!= NULL
)
560 val
= *(void**)operandPtr
;
561 } else if(operand
!= NULL
&& operandPtr
== NULL
)
563 val
= (void*) operand
;
567 void* PredicateImpl::getVal1IfBetweenOnInt(int &offset
)
569 if (NULL
!= lhs
&& NULL
!= rhs
) return NULL
;
570 if(typeInt
!= type
) return NULL
;
571 if (compOp
!= OpGreaterThanEquals
||
572 comp2Op
!=OpLessThanEquals
) return NULL
;
575 if(operand
== NULL
&& operandPtr
!= NULL
)
577 val
= *(void**)operandPtr
;
578 } else if(operand
!= NULL
&& operandPtr
== NULL
)
580 val
= (void*) operand
;
584 void* PredicateImpl::getVal2IfBetweenOnInt(int &offset
)
586 if (NULL
!= lhs
&& NULL
!= rhs
) return NULL
;
587 if(typeInt
!= type
) return NULL
;
588 if (compOp
!= OpGreaterThanEquals
||
589 comp2Op
!=OpLessThanEquals
) return NULL
;
592 if(operand2
== NULL
&& operand2Ptr
!= NULL
)
594 val
= *(void**)operand2Ptr
;
595 } else if(operand2
!= NULL
&& operand2Ptr
== NULL
)
597 val
= (void*) operand2
;
601 DbRetVal
PredicateImpl::evaluate(bool &result
)
603 if (!isNoLeftRight
) {
604 bool rhsResult
= false, lhsResult
=false;
605 DbRetVal retCode
=OK
;
609 retCode
= lhs
->evaluate(lhsResult
);
610 if (retCode
< OK
) return ErrInvalidExpr
;
611 }else lhsResult
= true;
614 retCode
= rhs
->evaluate(rhsResult
);
615 if (retCode
< OK
) return ErrInvalidExpr
;
616 } else rhsResult
= true;
619 //Means it involves only Logical operator
620 if (OpAnd
== logicalOp
) {
621 if (lhsResult
&& rhsResult
) result
= true;
622 }else if (OpOr
== logicalOp
) {
623 if (lhsResult
|| rhsResult
) result
= true;
624 }else if (OpNot
== logicalOp
){
625 if (lhsResult
) result
= false; else result
= true;
626 if ( ErrNullValues
== retCode
) result
= false;
628 printDebug(DM_Predicate
, "result is %d", result
);
632 //Means it is relational expression
633 //first operand is always field identifier
634 //get the value in the tuple
636 if (dontEvaluate
) {result
= true; return OK
; }
639 //for join node evaluation
640 ListIterator fIter
= projList
->getIterator();
641 JoinProjFieldInfo
*def
;
643 while (fIter
.hasElement())
645 def
= (JoinProjFieldInfo
*) fIter
.nextElement();
646 if (NULL
!= def
->bindBuf
) {
647 if (0 == strcmp(fldName1
, def
->tabFieldName
))
649 val1
= (char*)def
->bindBuf
;
651 length
= def
->length
;
655 printError(ErrNotExists
, "Field not binded %s.%s\n",
656 def
->tableName
, def
->fieldName
);
660 if (operand
== NULL
&& operandPtr
== NULL
)
662 char fieldName2
[IDENTIFIER_LENGTH
];
663 memset(fieldName2
, 0, IDENTIFIER_LENGTH
);
664 Table::getFieldNameAlone(fldName2
, fieldName2
);
667 while (fIter
.hasElement())
669 def
= (JoinProjFieldInfo
*) fIter
.nextElement();
670 if (NULL
!= def
->bindBuf
) {
671 if (0 == strcmp(fldName2
, def
->tabFieldName
))
673 val2
= (char*)def
->bindBuf
;
677 printError(ErrNotExists
, "Field not binded %s.%s\n",
678 def
->tableName
, def
->fieldName
);
684 else if(operand
!= NULL
&& operandPtr
== NULL
)
686 val2
= (char*) operand
;
688 else if(operand
== NULL
&& operandPtr
!= NULL
)
690 val2
= *(char**)operandPtr
;
694 JoinTableImpl
*jTable
= (JoinTableImpl
*) table
;
695 if (jTable
->isFldNullInt(fldName1
) || jTable
->isFldNullInt(fldName2
))
698 return ErrNullValues
;
700 result
= AllDataType::compareVal(val1
, val2
, compOp
, type
,
705 printf("PRABA::wrong method call\n");
706 //the below code works only for single table
707 val1
= (char*)tuple
+ offset1
;
708 if(offset2
!= -1 && operand
== NULL
&& operandPtr
== NULL
)
709 val2
= ((char*)tuple
) + offset2
;
711 //Assumes that fldName2 data type is also same for expr f1 <f2
712 //Note:Perf: Do not change the order below
713 if(operand
== NULL
&& operandPtr
!= NULL
)
715 val2
= *(char**)operandPtr
;
717 else if (operand
== NULL
&& operandPtr
== NULL
)
720 val2
= ((char*)tuple
) + offset2
;
722 else if(operand
!= NULL
&& operandPtr
== NULL
)
724 val2
= (char*) operand
;
728 result
= AllDataType::compareVal(val1
, val2
, compOp
, type
,length
);
732 void PredicateImpl::solveForProjList(Table
*tab
)
736 lhs
->solveForProjList(tab
);
740 rhs
->solveForProjList(tab
);
743 if (NULL
!= lhs
) return ;
748 ListIterator fIter
= projList
->getIterator();
749 JoinProjFieldInfo
*def
;
750 while (fIter
.hasElement())
752 def
= (JoinProjFieldInfo
*) fIter
.nextElement();
753 if (!isExist1
&& 0 == strcmp(fldName1
, def
->tabFieldName
))
757 if (!isExist2
&& 0 == strcmp(fldName2
, def
->tabFieldName
) )
764 tab
->bindFld(fldName1
, NULL
);
766 if (!isExist2
&& strcmp(fldName2
, "")!=0)
768 tab
->bindFld(fldName2
, NULL
);
773 void PredicateImpl::setOffsetAndType()
777 lhs
->setOffsetAndType();
781 rhs
->setOffsetAndType();
783 char fieldName1
[IDENTIFIER_LENGTH
];
784 char fieldName2
[IDENTIFIER_LENGTH
];
785 memset(fieldName1
, 0, IDENTIFIER_LENGTH
);
786 memset(fieldName2
, 0, IDENTIFIER_LENGTH
);
787 Table::getFieldNameAlone(fldName1
, fieldName1
);
788 Table::getFieldNameAlone(fldName2
, fieldName2
);
789 //this function is called only from TableImpl
790 TableImpl
*tImpl
= (TableImpl
*) table
;
792 FieldInfo
*info
= new FieldInfo();
793 tImpl
->getFieldInfo(fieldName1
, info
);
794 offset1
= tImpl
->getFieldOffset(fieldName1
);
795 fldPos
= tImpl
->getFldPos(fieldName1
);
797 length
= info
->length
;
799 if (info
->isNull
|| info
->isPrimary
||
800 info
->isDefault
|| info
->isAutoIncrement
)
802 //printf("isNullable is set to %d\n", isNullable);
807 offset2
= tImpl
->getFieldOffset(fieldName2
);
808 if(typeUnknown
== type
)
809 type
= tImpl
->getFieldType(fieldName2
);
814 bool PredicateImpl::pointLookupInvolved(const char *fname
)
816 bool rhsResult
, lhsResult
;
819 lhsResult
= lhs
->pointLookupInvolved(fname
);
823 rhsResult
= rhs
->pointLookupInvolved(fname
);
827 //Means it involves only Logical operator
832 if (lhsResult
|| rhsResult
) return true; else return false;
843 //Means it is relational expression
844 //first operand is always field identifier
845 char fieldName1
[IDENTIFIER_LENGTH
];
846 Table::getFieldNameAlone(fldName1
, fieldName1
);
847 if (OpEquals
== compOp
)
849 //for expressions f1 == f2 use full scan, so return false
850 if(NULL
== operand
&& NULL
== operandPtr
) return false;
851 if(0 == strcmp(fieldName1
, fname
))
859 bool PredicateImpl::isBetweenInvolved(const char *fname
)
861 bool rhsResult
, lhsResult
;
864 lhsResult
= lhs
->isBetweenInvolved(fname
);
868 rhsResult
= rhs
->isBetweenInvolved(fname
);
875 if (lhsResult
&& rhsResult
) return true; else return false;
882 char fieldName1
[IDENTIFIER_LENGTH
];
883 Table::getFieldNameAlone(fldName1
, fieldName1
);
884 if ( OpGreaterThan
== compOp
|| OpGreaterThanEquals
== compOp
)
886 if(0 == strcmp(fieldName1
, fname
))
894 bool PredicateImpl::rangeQueryInvolved(const char *fname
)
896 bool rhsResult
, lhsResult
;
899 lhsResult
= lhs
->rangeQueryInvolved(fname
);
903 rhsResult
= rhs
->rangeQueryInvolved(fname
);
910 if (lhsResult
|| rhsResult
) return true; else return false;
921 //Means it is relational expression
922 //first operand is always field identifier
923 char fieldName1
[IDENTIFIER_LENGTH
];
924 Table::getFieldNameAlone(fldName1
, fieldName1
);
925 if (OpLessThan
== compOp
|| OpLessThanEquals
== compOp
||
926 OpGreaterThan
== compOp
|| OpGreaterThanEquals
== compOp
)
928 //for expressions f1 == f2 use full scan, so return false
929 if(NULL
== operand
&& NULL
== operandPtr
) return false;
930 if(0 == strcmp(fieldName1
, fname
))
938 void* PredicateImpl::opAndValPtrForIndexField(const char *fname
, bool isUnique
,ComparisionOp
&op
)
940 ComparisionOp lhsOp
= OpInvalidComparisionOp
, rhsOp
= OpInvalidComparisionOp
;
941 void *lhsRet
=NULL
, *rhsRet
=NULL
;
944 lhsRet
= lhs
->opAndValPtrForIndexField(fname
, isUnique
, lhsOp
);
948 rhsRet
= rhs
->opAndValPtrForIndexField(fname
, isUnique
,rhsOp
);
950 if (lhsRet
&& lhsOp
== OpEquals
) { op
= lhsOp
; return lhsRet
;}
951 if (rhsRet
&& rhsOp
== OpEquals
) { op
= rhsOp
; return rhsRet
;}
954 if( lhsRet
) { op
= lhsOp
; return lhsRet
; }
955 if( rhsRet
) { op
= rhsOp
; return rhsRet
; }
957 char fieldName1
[IDENTIFIER_LENGTH
];
958 Table::getFieldNameAlone(fldName1
, fieldName1
);
959 //Means it is relational expression
960 //first operand is always field identifier
961 if(0 == strcmp(fieldName1
, fname
))
964 if (isUnique
&& compOp
!= OpLessThan
&&
965 compOp
!= OpLessThanEquals
) isPushedDown
= true;
966 if (operand
) return operand
; else return *(void**)operandPtr
;
968 op
= OpInvalidComparisionOp
;
973 //called only in case of hash index scan
974 void* PredicateImpl::valPtrForIndexField(const char *fname
, bool isUnique
)
976 void *lhsRet
=NULL
, *rhsRet
=NULL
;
979 lhsRet
= lhs
->valPtrForIndexField(fname
, isUnique
);
980 if ( lhsRet
!= NULL
) return lhsRet
;
984 rhsRet
= rhs
->valPtrForIndexField(fname
, isUnique
);
985 if ( rhsRet
!= NULL
) return rhsRet
;
987 char fieldName1
[IDENTIFIER_LENGTH
];
988 Table::getFieldNameAlone(fldName1
, fieldName1
);
989 //Means it is relational expression
990 //first operand is always field identifier
991 if (OpEquals
== compOp
)
993 if(0 == strcmp(fieldName1
, fname
))
995 if (isUnique
) isPushedDown
= true;
996 if (operand
) return operand
; else return *(void**)operandPtr
;
1001 ComparisionOp
PredicateImpl::opForIndexField(const char *fname
)
1003 ComparisionOp lhsRet
= OpInvalidComparisionOp
, rhsRet
= OpInvalidComparisionOp
;
1006 lhsRet
= lhs
->opForIndexField(fname
);
1007 if ( lhsRet
!= OpInvalidComparisionOp
) return lhsRet
;
1012 rhsRet
= rhs
->opForIndexField(fname
);
1013 if ( rhsRet
!= OpInvalidComparisionOp
) return rhsRet
;
1015 char fieldName1
[IDENTIFIER_LENGTH
];
1016 Table::getFieldNameAlone(fldName1
, fieldName1
);
1017 if(0 == strcmp(fieldName1
, fname
))
1021 return OpInvalidComparisionOp
;
1023 PredicateImpl
* PredicateImpl::getTablePredicate()
1025 PredicateImpl
*lhsRet
= NULL
, *rhsRet
= NULL
;
1028 lhsRet
= lhs
->getTablePredicate();
1029 if ( lhsRet
!= NULL
) return lhsRet
;
1033 rhsRet
= rhs
->getTablePredicate();
1034 if ( rhsRet
!= NULL
) return rhsRet
;
1036 if (operand
|| operandPtr
)
1038 //printf("PRABA::getTablePredicate returning %s %d\n", fldName1, compOp);
1041 if (this == parent
->lhs
) {
1053 PredicateImpl
* PredicateImpl::getJoinPredicate()
1055 PredicateImpl
*lhsRet
= NULL
, *rhsRet
= NULL
;
1058 lhsRet
= lhs
->getJoinPredicate();
1059 if ( lhsRet
!= NULL
) return lhsRet
;
1063 rhsRet
= rhs
->getJoinPredicate();
1064 if ( rhsRet
!= NULL
) return rhsRet
;
1066 if (0 != strcmp(fldName2
, ""))
1068 //printf("PRABA::getJoinPredicate returning %s %s\n", fldName1, fldName2);
1071 if (this == parent
->lhs
)
1081 void PredicateImpl::removeIfNotNecessary()
1085 lhs
->removeIfNotNecessary();
1089 rhs
->removeIfNotNecessary();
1091 if (logicalOp
!= OpAnd
) return;
1092 if (NULL
== lhs
&& NULL
== rhs
)
1098 if (this == parent
->rhs
) parent
->rhs
= NULL
;
1099 else if (this == parent
->lhs
) parent
->lhs
= NULL
;
1100 //TODO::PRABA::fix the leak below. if uncommented dumps core
1102 //WARNINGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG
1103 //current object is deleted. do not any code here
1106 else if (NULL
== lhs
)
1108 //left side of the node is empty means we can remove this AND node
1109 //and place it as left or right of my parent where i am currently placed
1114 if (this == parent
->rhs
) parent
->rhs
=this->rhs
;
1115 else if (this == parent
->lhs
) parent
->lhs
= this->rhs
;
1116 //TODO::PRABA::fix the leak below. if uncommented dumps core
1118 //WARNINGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG
1119 //current object is deleted. do not any code here
1122 else if (NULL
== rhs
)
1124 //right side of the node is empty means we can remove this AND node
1125 //and place it as left or right of my parent where i am currently placed
1130 if (this == parent
->rhs
) parent
->rhs
=this->lhs
;
1131 else if (this == parent
->lhs
) parent
->lhs
= this->lhs
;
1132 //TODO::PRABA::fix the leak below. if uncommented dumps core
1134 //WARNINGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG
1135 //current object is deleted. do not any code here
1140 bool PredicateImpl::isDummyPredicate()
1142 if (NULL
== lhs
&& NULL
== rhs
&& NULL
== parent
1143 && NULL
== operand
&& NULL
== operandPtr
&&
1144 (0 == strcmp(fldName1
, "")) && (0==strcmp(fldName2
, "")))
1149 PredicateImpl
* PredicateImpl::getIfOneSidedPredicate()
1151 if (logicalOp
!= OpAnd
) return NULL
;
1152 if (NULL
== lhs
&& NULL
!=rhs
)
1156 if (NULL
!= lhs
&& NULL
==rhs
)