private List<SargIntervalSequence> evaluateChildren(SargSetExpr setExpr) { List<SargIntervalSequence> list = new ArrayList<SargIntervalSequence>(); for (SargExpr child : setExpr.children) { SargIntervalSequence newSeq = child.evaluate(); list.add(newSeq); } return list; }
/** * Adds a child to this expression. * * @param child child to add */ public void addChild(SargExpr child) { assert (child.getDataType() == dataType); if (setOp == SargSetOperator.COMPLEMENT) { assert (children.isEmpty()); } children.add(child); }
// implement SargExpr public SargIntervalSequence evaluate() { if (setOp == SargSetOperator.COMPLEMENT) { assert (children.size() == 1); SargExpr child = children.get(0); return child.evaluateComplemented(); } List<SargIntervalSequence> list = evaluateChildren(this); switch (setOp) { case UNION: return evaluateUnion(list); case INTERSECTION: return evaluateIntersection(list); default: throw Util.newInternal(setOp.toString()); } }
// implement SargExpr public SargIntervalSequence evaluateComplemented() { if (setOp == SargSetOperator.COMPLEMENT) { // Double negation is a nop return children.get(0).evaluate(); } // Use DeMorgan's Law: complement of union is intersection of // complements, and vice versa List<SargIntervalSequence> list = new ArrayList<SargIntervalSequence>(); for (SargExpr child : children) { SargIntervalSequence newSeq = child.evaluateComplemented(); list.add(newSeq); } switch (setOp) { case INTERSECTION: return evaluateUnion(list); case UNION: return evaluateIntersection(list); default: throw Util.newInternal(setOp.toString()); } }
/** * Tests whether we can support the usage of dynamic parameters in a given SargExpr. * * @param sargExpr expression to test * @return true if supported */ private boolean testDynamicParamSupport(SargExpr sargExpr) { // NOTE jvs 18-Mar-2006: we don't currently support boolean operators // over predicates on dynamic parameters. The reason is that we // evaluate sarg expressions at prepare time rather than at execution // time. To support them, we would need a way to defer evaluation // until execution time (Broadbase used to do that). Set<RexDynamicParam> dynamicParams = new HashSet<RexDynamicParam>(); sargExpr.collectDynamicParams(dynamicParams); if (dynamicParams.isEmpty()) { // no dynamic params, no problem return true; } if (sargExpr instanceof SargIntervalExpr) { // We can support a single point or ray, which is the // most that would have been generated by the time this is // called. Should probably assert that. return true; } // Anything else is unsupported. return false; }
/** Reconstructs a rex predicate from a list of SargExprs which will be AND'ed together. */ private void recomposeConjunction() { for (int i = 0; i < sargBindingList.size(); i++) { final SargBinding currBinding = sargBindingList.get(i); final RexInputRef currRef = currBinding.getInputRef(); SargExpr currSargExpr = currBinding.getExpr(); RexNode currAndNode = sarg2RexMap.get(currSargExpr); // don't need this anymore // will be have new mapping put back if currSargExpr remain // unchanged. sarg2RexMap.remove(currSargExpr); boolean recomp = false; // search the rest of the list to find SargExpr on the same col. ListIterator<SargBinding> iter = sargBindingList.listIterator(i + 1); while (iter.hasNext()) { final SargBinding nextBinding = iter.next(); final RexInputRef nextRef = nextBinding.getInputRef(); final SargExpr nextSargExpr = nextBinding.getExpr(); if (nextRef.getIndex() == currRef.getIndex()) { // build new SargExpr SargSetExpr expr = factory.newSetExpr(currSargExpr.getDataType(), SargSetOperator.INTERSECTION); expr.addChild(currSargExpr); expr.addChild(nextSargExpr); // build new RexNode currAndNode = factory .getRexBuilder() .makeCall( SqlStdOperatorTable.andOperator, currAndNode, sarg2RexMap.get(nextSargExpr)); currSargExpr = expr; sarg2RexMap.remove(nextSargExpr); iter.remove(); recomp = true; } } if (recomp) { assert !simpleMode; if (!testDynamicParamSupport(currSargExpr)) { // Oops, we can't actually support the conjunction we // recomposed. Toss it. (We could do a better job by at // least using part of it, but the effort might be better // spent on implementing deferred expression evaluation.) nonSargFilterList.add(currAndNode); sargBindingList.remove(i); continue; } } if (recomp) { SargBinding newBinding = new SargBinding(currSargExpr, currRef); sargBindingList.remove(i); sargBindingList.add(i, newBinding); } sarg2RexMap.put(currSargExpr, currAndNode); } }
// implement SargExpr public void collectDynamicParams(Set<RexDynamicParam> dynamicParams) { for (SargExpr child : children) { child.collectDynamicParams(dynamicParams); } }