1 22 23 package org.xquark.extractor.progress; 24 25 import java.util.*; 26 27 import org.xquark.extractor.algebra.*; 28 import org.xquark.extractor.runtime.IDProvider; 29 30 36 public class SimplifyOuterJoinVisitor extends DefaultCompleteVisitor { 39 40 private static final String RCSRevision = "$Revision: 1.5 $"; 41 private static final String RCSName = "$Name: $"; 42 43 protected IDProvider _attIDProvider = null; 44 protected IDProvider _relIDProvider = null; 45 46 public SimplifyOuterJoinVisitor(IDProvider attIDProvider, IDProvider relIDProvider) { 47 _attIDProvider = attIDProvider; 48 _relIDProvider = relIDProvider; 49 } 50 51 public void visit(BinOpOuterJoin arg) { 52 54 super.visit(arg); 55 Set participants = arg.participants(); 56 57 Expression lOprnd = arg.getLeftOperand(); 58 Set lTis = ((Relation) lOprnd).providedTableInstances(); 59 boolean lOprndTobeSimplyfied = containsManyElementsof(participants, lTis); 60 61 Expression rOprnd = arg.getRightOperand(); 62 Set rTis = ((Relation) rOprnd).providedTableInstances(); 63 boolean rOprndTobeSimplyfied = containsManyElementsof(participants, rTis); 64 65 if (lOprndTobeSimplyfied || rOprndTobeSimplyfied) { 66 67 Map replaceMap = new Hashtable(); 68 if (lOprndTobeSimplyfied) { 69 RenameRelation renameRelation = createRenameRelationProject(lOprnd, replaceMap); 70 arg.replaceChild(lOprnd, renameRelation); 71 } 72 73 if (rOprndTobeSimplyfied) { 74 RenameRelation renameRelation = createRenameRelationProject(rOprnd, replaceMap); 75 arg.replaceChild(rOprnd, renameRelation); 76 } 77 78 TopDownReplacer replacer = new TopDownReplacer(replaceMap); 79 BottomUpTractor tractor = new BottomUpTractor((AlgebraVisitor) replacer); 80 arg.accept(tractor); 81 } else { 82 83 } 84 85 } 87 88 private RenameRelation createRenameRelationProject(Expression relation, Map replaceMap) { 89 91 UnOpProject project = new UnOpProject(relation, _attIDProvider); 92 RenameRelation retVal = new RenameRelation(project, _relIDProvider); 93 94 List attributeList = new ArrayList(); 95 AttributesVisitor visitor = new AttributesVisitor(attributeList); 96 relation.accept(visitor); 97 98 AttributeExpression attrExpr = null; 99 Expression newExpr = null; 100 AttributeExpression newAttrExpr = null; 101 for (int i = 0; i < attributeList.size(); i++) { 102 attrExpr = (AttributeExpression) attributeList.get(i); 103 newExpr = project.addItem(attrExpr); 105 newAttrExpr = new AttributeExpression(retVal, newExpr.getName()); 106 newAttrExpr.setUnderlyingExpr(newExpr); 107 108 replaceMap.put(attrExpr, newAttrExpr); 109 } 110 111 return retVal; 113 } 114 115 120 123 private boolean containsManyElementsof(Collection colA, Collection colB) { 124 boolean retVal = false; 125 int counter = 0; 126 127 Iterator iter = colB.iterator(); 128 while (iter.hasNext()) { 129 Object item = iter.next(); 130 if (colA.contains(item)) { 131 counter++; 132 } 133 } 134 if (1 < counter) { 135 retVal = true; 136 } 137 138 return retVal; 139 } 140 141 class AttributesVisitor extends DefaultSimpleVisitor { 142 143 144 private List _attributeList = null; 145 146 private RenameRelation _curTI = null; 147 148 public AttributesVisitor(List list) { 149 _attributeList = list; 150 } 151 152 public void visit(UnOpProject arg) { 153 155 List itemList = arg.getItemList(); 156 Iterator iter = itemList.iterator(); 157 Expression attribute = null; 158 AttributeExpression attrExpr = null; 159 while (iter.hasNext()) { 160 attribute = (Expression) iter.next(); 161 attrExpr = new AttributeExpression(_curTI, attribute.getName()); 162 attrExpr.setUnderlyingExpr(attribute); 163 _attributeList.add(attrExpr); 164 } 165 166 } 168 169 public void visit(Table arg) { 170 172 173 Set attributeSet = new HashSet(); 174 attributeSet.addAll(arg.getAttributes()); 175 if (null != arg.getUsedKeyAttributeList()) { 176 attributeSet.addAll(arg.getUsedKeyAttributeList()); 177 } 178 179 180 Iterator iter = attributeSet.iterator(); 181 Attribute attribute = null; 182 AttributeExpression attrExpr = null; 183 while (iter.hasNext()) { 184 attribute = (Attribute) iter.next(); 185 if (attribute instanceof PseudoAttribute) { 186 attrExpr = new Rowid(_curTI, attribute.getName()); 187 } else { 188 attrExpr = new AttributeExpression(_curTI, attribute.getName()); 189 } 190 attrExpr.setUnderlyingExpr(attribute); 191 _attributeList.add(attrExpr); 192 } 193 194 } 196 197 public void visit(RenameRelation arg) { 198 _curTI = arg; 200 super.visit((UnaryOperator) arg); 201 _curTI = null; 202 } 204 } 205 } 206 | Popular Tags |