1 package com.daffodilwoods.daffodildb.server.sql99.dql.plan.condition; 2 3 12 import com.daffodilwoods.daffodildb.server.sql99.common.*; 13 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*; 14 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.predicates.*; 15 import com.daffodilwoods.daffodildb.server.sql99.token.*; 16 import com.daffodilwoods.database.resource.*; 17 18 29 public class BVEPlanMerger implements ExecutionPlanConstants, BVEConstants { 30 31 79 public static booleanvalueexpression addAndConditions(booleanvalueexpression bve1, booleanvalueexpression bve2) throws DException { 80 SRESERVEDWORD1206543922 sRES = new SRESERVEDWORD1206543922(); 81 sRES._SRESERVEDWORD12065439220 = "AND"; 82 if (bve1 == null) 83 return bve2; 84 if (bve2 == null) 85 return bve1; 86 int type1 = bve1.getConditionType (); 87 int type2 = bve2.getConditionType (); 88 if(type1 == CONTAINS){ 89 return bve1; 90 } 91 if(type2 == CONTAINS){ 92 return bve2; 93 } 94 if(type1 == PREDICATE){ 95 if(type2 == PREDICATE){ 96 return new booleantermSRESERVEDWORD1206543922booleanfactor((booleanterm)bve1,(booleanfactor)bve2 ,sRES); 97 } 98 if(type2 == BTANDBF) 99 return new booleantermSRESERVEDWORD1206543922booleanfactor((booleanterm)bve2,(booleanfactor)bve1 , sRES); 100 if(type2 == BVEORBT) 101 return new booleantermSRESERVEDWORD1206543922booleanfactor((booleanterm) bve1 ,getBooleanFactor(new parenthesizedbooleanvalueexpression(bve2)) , sRES ); 102 throw new DException("DSE527", new Object []{new Integer (type2)}); 103 } 104 if(type1 == BTANDBF){ 105 if(type2 == PREDICATE) 106 return new booleantermSRESERVEDWORD1206543922booleanfactor((booleanterm) bve1,(booleanfactor)bve2 , sRES ); 107 if(type2 == BTANDBF) 108 return new booleantermSRESERVEDWORD1206543922booleanfactor ((booleanterm)bve1,getBooleanFactor((booleanprimary)(new parenthesizedbooleanvalueexpression(bve2))) , sRES ); 109 if(type2 == BVEORBT) 110 return new booleantermSRESERVEDWORD1206543922booleanfactor ((booleanterm)bve1,getBooleanFactor((booleanprimary)(new parenthesizedbooleanvalueexpression(bve2)) ) , sRES); 111 throw new DException("DSE527", new Object []{new Integer (type2)}); 112 } 113 if(type1 == BVEORBT){ 114 if(type2 == PREDICATE) 115 return new booleantermSRESERVEDWORD1206543922booleanfactor ((booleanterm)bve2,getBooleanFactor((booleanprimary)(new parenthesizedbooleanvalueexpression(bve1))) , sRES); 116 if(type2 == BTANDBF) 117 return new booleantermSRESERVEDWORD1206543922booleanfactor ((booleanterm)bve2 , getBooleanFactor((booleanprimary)(new parenthesizedbooleanvalueexpression(bve1)) ) , sRES); 118 if(type2 == BVEORBT) 119 return new booleantermSRESERVEDWORD1206543922booleanfactor (getBooleanFactor(new parenthesizedbooleanvalueexpression(bve1)) , getBooleanFactor(new parenthesizedbooleanvalueexpression(bve2)) , sRES); 120 throw new DException("DSE527", new Object []{new Integer (type2)}); 121 } 122 throw new DException("DSE527", new Object []{new Integer (type1)}); 123 } 125 172 public static booleanvalueexpression addOrConditions(booleanvalueexpression bve1, booleanvalueexpression bve2) throws DException { 173 SRESERVEDWORD1206543922 sRES = new SRESERVEDWORD1206543922(); 174 sRES._SRESERVEDWORD12065439220 = "OR"; 175 if (bve1 == null) { 176 return bve2; 177 } 178 if (bve2 == null) { 179 return bve1; 180 } 181 int type1 = bve1.getConditionType(); 182 int type2 = bve2.getConditionType(); 183 if (type1 == PREDICATE) { 184 if (type2 == PREDICATE) { 185 return new booleanvalueexpressionSRESERVEDWORD1206543922booleanterm( (booleanterm) bve1, (booleanterm) bve2, sRES); 186 } 187 if (type2 == BTANDBF) { 188 return new booleanvalueexpressionSRESERVEDWORD1206543922booleanterm(bve2, (booleanterm) bve1, sRES); 189 } 190 if (type2 == BVEORBT) { 191 return new booleanvalueexpressionSRESERVEDWORD1206543922booleanterm(bve2, (booleanterm) bve1, sRES); 192 } 193 throw new DException("DSE527", new Object [] {new Integer (type2)}); 194 } 195 if (type1 == BTANDBF) { 196 if (type2 == PREDICATE) { 197 return new booleanvalueexpressionSRESERVEDWORD1206543922booleanterm(bve1, (booleanterm) bve2, sRES); 198 } 199 if (type2 == BTANDBF) { 200 return new booleanvalueexpressionSRESERVEDWORD1206543922booleanterm( (booleanvalueexpression) bve1, (booleanterm) bve2, sRES); 201 } 202 if (type2 == BVEORBT) { 203 return new booleanvalueexpressionSRESERVEDWORD1206543922booleanterm(bve2, (booleanterm) bve1, sRES); 204 } 205 throw new DException("DSE527", new Object [] {new Integer (type2)}); 206 } 207 if (type1 == BVEORBT) { 208 if (type2 == PREDICATE) { 209 return new booleanvalueexpressionSRESERVEDWORD1206543922booleanterm(bve1, (booleanterm) bve2, sRES); 210 } 211 if (type2 == BTANDBF) { 212 return new booleanvalueexpressionSRESERVEDWORD1206543922booleanterm(bve1, (booleanterm) bve2, sRES); 213 } 214 if (type2 == BVEORBT) { 215 return new booleanvalueexpressionSRESERVEDWORD1206543922booleanterm(bve1, getBooleanFactor( (booleanprimary) (new parenthesizedbooleanvalueexpression(bve2))), sRES); 216 } 217 throw new DException("DSE527", new Object [] {new Integer (type2)}); 218 } 219 throw new DException("DSE527", new Object [] {new Integer (type1)}); 220 } 222 233 public static _BVEPlan mergeSamePlanWithOr(_BVESingleTablePlan tablePlan1, _BVESingleTablePlan tablePlan2) throws DException { 234 try { 235 booleanvalueexpression bve = addOrConditions( (booleanvalueexpression) tablePlan1.getCondition().clone(), (booleanvalueexpression) tablePlan2.getCondition().clone()); 236 tablePlan1.setCondition(bve); 237 return (_BVEPlan) tablePlan1; 238 } catch (CloneNotSupportedException ex) { 239 throw new DException("DSE0", new Object [] {"Clone Not Supported"}); 240 } 241 } 243 252 public static _BVEPlan mergeSamePlanWithAnd(_BVESingleTablePlan tablePlan1, _BVESingleTablePlan tablePlan2) throws DException { 253 booleanvalueexpression bve = addAndConditions(tablePlan1.getCondition(), tablePlan2.getCondition()); 254 tablePlan1.setCondition(bve); 255 return tablePlan1; 256 } 258 274 public static _BVEPlan mergeTablePlansWithAnd(_BVEPlan tablePlan1, _BVEPlan tablePlan2) throws DException { 275 276 if (tablePlan1 == null) { 277 return tablePlan2; 278 } 279 if (tablePlan2 == null) { 280 return tablePlan1; 281 } 282 int type1 = tablePlan1.getType(); 283 int type2 = tablePlan2.getType(); 284 if (type1 == BVEConstants.BVESINGLETABLEPLAN) { 285 if (type2 == BVEConstants.BVESINGLETABLEPLAN) { 286 return mergeTwoBveSingleTablePlan( (_BVESingleTablePlan) tablePlan1, (_BVESingleTablePlan) tablePlan2); 287 } 288 if (type2 == BVEConstants.BVEALLTABLEPLAN) { 289 ( (BVEAllTablePlan) tablePlan2).andBVESingleTablePlan(new BVESingleTablePlan[] { (BVESingleTablePlan) tablePlan1}); 290 return tablePlan2; 291 } 292 if (type2 == BVEConstants.BVEAGGREGATEPLAN) { 293 BVEAllTablePlan bveAllTablePlan = new BVEAllTablePlan(new _BVESingleTablePlan[] { (BVESingleTablePlan) tablePlan1} 294 , null, null); 295 bveAllTablePlan.andAggregateCondition( ( (BVEAggregatePlan) tablePlan2).getAggregateCondition()); 296 return bveAllTablePlan; 297 } 298 } 299 300 if (type1 == BVEConstants.BVEALLTABLEPLAN) { 301 if (type2 == BVEConstants.BVESINGLETABLEPLAN) { 302 ( (BVEAllTablePlan) tablePlan1).andBVESingleTablePlan(new BVESingleTablePlan[] { (BVESingleTablePlan) tablePlan2}); 303 return tablePlan1; 304 } 305 if (type2 == BVEConstants.BVEALLTABLEPLAN) { 306 return mergeTwoAllTablePlan(tablePlan1, tablePlan2); 307 } 308 if (type2 == BVEConstants.BVEAGGREGATEPLAN) { 309 return mergeAllTableAndAggregateCondition(tablePlan1, tablePlan2); 310 } 311 } 312 if (type1 == BVEConstants.BVEAGGREGATEPLAN) { 313 if (type2 == BVEConstants.BVESINGLETABLEPLAN) { 314 BVEAllTablePlan bveAllTablePlan = new BVEAllTablePlan(new _BVESingleTablePlan[] { (BVESingleTablePlan) tablePlan2} 315 , null, null); 316 bveAllTablePlan.andAggregateCondition( ( (BVEAggregatePlan) tablePlan1).getAggregateCondition()); 317 return bveAllTablePlan; 318 } 319 if (type2 == BVEConstants.BVEALLTABLEPLAN) { 320 return mergeAllTableAndAggregateCondition(tablePlan2, tablePlan1); 321 } 322 if (type2 == BVEConstants.BVEAGGREGATEPLAN) { 323 return mergeAggregatePlanAndAggregatePlan(tablePlan1, tablePlan2); 324 } 325 326 } 327 throw new DException("DSE527", new Object [] {new Integer (type1)}); 328 } 329 330 347 public static _BVEPlan mergeTablePlansWithOr(_BVEPlan tablePlan1, _BVEPlan tablePlan2, booleanvalueexpression bve) throws DException { 348 int type1 = tablePlan1.getType(); 349 int type2 = tablePlan2.getType(); 350 if (type1 == BVEConstants.BVESINGLETABLEPLAN) { 351 if (type2 == BVEConstants.BVESINGLETABLEPLAN) { 352 TableDetails table1 = ( (BVESingleTablePlan) tablePlan1).getTableDetails()[0]; 353 TableDetails table2 = ( (BVESingleTablePlan) tablePlan2).getTableDetails()[0]; 354 if (table1 == table2) { 355 return mergeSamePlanWithOr( (BVESingleTablePlan) tablePlan1, (BVESingleTablePlan) tablePlan2); 356 } else { 357 ( (BVESingleTablePlan) tablePlan1).setCondition(null); 358 ( (BVESingleTablePlan) tablePlan2).setCondition(null); 359 return (_BVEPlan)new BVEAllTablePlan(null, null, bve); 360 } 361 } 362 if (type2 == BVEConstants.BVEALLTABLEPLAN) { 363 ( (BVEAllTablePlan) tablePlan2).orBVESingleTablePlan(new BVESingleTablePlan[] { (BVESingleTablePlan) tablePlan1}); 364 ( (BVEAllTablePlan) tablePlan2).orRelation(null); 365 ( (BVEAllTablePlan) tablePlan2).setRemainingCondition(bve); 366 return tablePlan2; 367 } 368 if (type2 == BVEConstants.BVEAGGREGATEPLAN) { 369 ( (BVEAggregatePlan) tablePlan2).setAggregateCondition(bve); 370 return tablePlan2; 371 } 372 } 373 if (type1 == BVEConstants.BVEALLTABLEPLAN) { 374 if (type2 == BVEConstants.BVESINGLETABLEPLAN) { 375 ( (BVEAllTablePlan) tablePlan1).orBVESingleTablePlan(new BVESingleTablePlan[] { (BVESingleTablePlan) tablePlan2}); 376 ( (BVEAllTablePlan) tablePlan1).orRelation(null); 377 ( (BVEAllTablePlan) tablePlan1).setRemainingCondition(bve); 378 return tablePlan1; 379 } 380 if (type2 == BVEConstants.BVEALLTABLEPLAN) { 381 ( (BVEAllTablePlan) tablePlan1).orBVESingleTablePlan( ( (BVEAllTablePlan) tablePlan2).getBVESingleTablePlans()); 382 ( (BVEAllTablePlan) tablePlan1).orRelation( ( (BVEAllTablePlan) tablePlan2).getAllTableJoinRelation()); 383 ( (BVEAllTablePlan) tablePlan1).setRemainingCondition(bve); 384 return tablePlan1; 385 } 386 if (type2 == BVEConstants.BVEAGGREGATEPLAN) { 387 ( (BVEAggregatePlan) tablePlan2).setAggregateCondition(bve); 388 return tablePlan2; 389 } 390 } 391 if (type1 == BVEConstants.BVEAGGREGATEPLAN) { 392 ( (BVEAggregatePlan) tablePlan1).setAggregateCondition(bve); 393 return tablePlan1; 394 395 } 396 throw new DException("DSE527", new Object [] {new Integer (type1)}); 397 } 398 399 406 public static booleanfactor getBooleanFactor(booleanprimary bp) throws DException { 407 return new booleanfactor(new booleantest(bp)); 408 } 409 415 public static booleanfactor getBooleanFactor(predicate predicate0) throws DException { 416 return new booleanfactor(new booleantest(predicate0)); 417 } 418 426 public static _BVESingleTablePlan mergeSameSinglePlanWithAnd(_BVESingleTablePlan tablePlan1, _BVESingleTablePlan tablePlan2) throws DException { 427 booleanvalueexpression bve = addAndConditions(tablePlan1.getCondition(), tablePlan2.getCondition()); 428 tablePlan1.setCondition(bve); 429 return tablePlan1; 430 } 431 443 private static _BVEPlan mergeTwoBveSingleTablePlan(_BVESingleTablePlan tablePlan1, _BVESingleTablePlan tablePlan2) throws DException { 444 TableDetails table1 = tablePlan1.getTableDetails()[0]; 445 TableDetails table2 = tablePlan2.getTableDetails()[0]; 446 if (table1 == table2) { 447 return mergeSamePlanWithAnd( (_BVESingleTablePlan) tablePlan1, (_BVESingleTablePlan) tablePlan2); 448 } 449 return new BVEAllTablePlan(new _BVESingleTablePlan[] { (_BVESingleTablePlan) tablePlan1, (_BVESingleTablePlan) tablePlan2} 450 , null, null); 451 } 452 464 private static _BVEPlan mergeTwoAllTablePlan(_BVEPlan tablePlan1, _BVEPlan tablePlan2) throws DException { 465 ( (BVEAllTablePlan) tablePlan1).andBVESingleTablePlan( ( (BVEAllTablePlan) tablePlan2).getBVESingleTablePlans()); 466 ( (BVEAllTablePlan) tablePlan1).andRemainingCondition( ( (BVEAllTablePlan) tablePlan2).getRemainingCondition()); 467 ( (BVEAllTablePlan) tablePlan1).andRelation( ( (BVEAllTablePlan) tablePlan2).getAllTableJoinRelation()); 468 ( (BVEAllTablePlan) tablePlan1).andAggregateCondition( ( (BVEAllTablePlan) tablePlan2).aggregateCondition); 469 return tablePlan1; 470 } 471 478 private static _BVEPlan mergeAllTableAndAggregateCondition(_BVEPlan tablePlan1, _BVEPlan tablePlan2) throws DException { 479 ( (BVEAllTablePlan) tablePlan1).andAggregateCondition( ( (BVEAggregatePlan) tablePlan2).getAggregateCondition()); 480 return tablePlan1; 481 } 482 490 private static _BVEPlan mergeAggregatePlanAndAggregatePlan(_BVEPlan tablePlan1, _BVEPlan tablePlan2) throws DException { 491 ( (BVEAggregatePlan) tablePlan1).addAggregateCondition( ( (BVEAggregatePlan) tablePlan2).getAggregateCondition()); 492 return tablePlan1; 493 } 494 } 495 | Popular Tags |