root.getChildren().set(i, convertToCNF(root.getChildren().get(i))); if (checkCombinationsThreshold(andList)) { root = new ExpressionTree(ExpressionTree.Operator.AND); generateAllCombinations(root.getChildren(), andList, nonAndList); } else { root = new ExpressionTree(TruthValue.YES_NO_NULL);
ExpressionTree child = flatten(root.getChildren().get(i));
if (expr.getChildren() != null) { for(int i=0; i < expr.getChildren().size(); ++i) { ExpressionTree child = foldMaybe(expr.getChildren().get(i)); if (child.getConstant() == TruthValue.YES_NO_NULL) { switch (expr.getOperator()) {
generateAllCombinations(result, andList.subList(1, andList.size()), nonAndList);
switch (child.getOperator()) { case NOT: return pushDownNot(child.getChildren().get(0)); case CONSTANT: return new ExpressionTree(child.getConstant().not()); root = new ExpressionTree(ExpressionTree.Operator.OR); for(ExpressionTree kid: child.getChildren()) { root.getChildren().add(pushDownNot(new ExpressionTree(ExpressionTree.Operator.NOT, kid))); root = new ExpressionTree(ExpressionTree.Operator.AND); for(ExpressionTree kid: child.getChildren()) { root.getChildren().add(pushDownNot(new ExpressionTree (ExpressionTree.Operator.NOT, kid))); root.getChildren().set(i, pushDownNot(root.getChildren().get(i)));
root.getChildren().set(i, convertToCNF(root.getChildren().get(i))); if (checkCombinationsThreshold(andList)) { root = new ExpressionTree(ExpressionTree.Operator.AND); generateAllCombinations(root.getChildren(), andList, nonAndList); } else { root = new ExpressionTree(TruthValue.YES_NO_NULL);
ExpressionTree child = flatten(root.getChildren().get(i));
if (expr.getChildren() != null) { for(int i=0; i < expr.getChildren().size(); ++i) { ExpressionTree child = foldMaybe(expr.getChildren().get(i)); if (child.getConstant() == TruthValue.YES_NO_NULL) { switch (expr.getOperator()) {
@Override public SearchArgument build() { if (currentTree.size() != 1) { throw new IllegalArgumentException("Failed to end " + currentTree.size() + " operations."); } ExpressionTree optimized = pushDownNot(root); optimized = foldMaybe(optimized); optimized = flatten(optimized); optimized = convertToCNF(optimized); optimized = flatten(optimized); int leafReorder[] = new int[leaves.size()]; Arrays.fill(leafReorder, -1); int newLeafCount = compactLeaves(optimized, 0, leafReorder); optimized = rewriteLeaves(optimized, leafReorder); ArrayList<PredicateLeaf> leafList = new ArrayList<>(newLeafCount); // expand list to correct size for(int i=0; i < newLeafCount; ++i) { leafList.add(null); } // build the new list for(Map.Entry<PredicateLeaf, Integer> elem: leaves.entrySet()) { int newLoc = leafReorder[elem.getValue()]; if (newLoc != -1) { leafList.set(newLoc, elem.getKey()); } } return new SearchArgumentImpl(optimized, leafList); }
/** * Recursively explore the tree to find the leaves that are still reachable * after optimizations. * @param tree the node to check next * @param next the next available leaf id * @param leafReorder * @return the next available leaf id */ static int compactLeaves(ExpressionTree tree, int next, int[] leafReorder) { if (tree.getOperator() == ExpressionTree.Operator.LEAF) { int oldLeaf = tree.getLeaf(); if (leafReorder[oldLeaf] == -1) { leafReorder[oldLeaf] = next++; } } else if (tree.getChildren() != null){ for(ExpressionTree child: tree.getChildren()) { next = compactLeaves(child, next, leafReorder); } } return next; }
@Override public Builder in(String column, PredicateLeaf.Type type, Object... literal) { ExpressionTree parent = currentTree.getFirst(); if (column == null || literal == null) { parent.getChildren().add(new ExpressionTree(TruthValue.YES_NO_NULL)); } else { if (literal.length == 0) { throw new IllegalArgumentException("Can't create in expression with " + "no arguments"); } List<Object> argList = new ArrayList<Object>(); argList.addAll(Arrays.asList(literal)); PredicateLeaf leaf = new PredicateLeafImpl(PredicateLeaf.Operator.IN, type, column, null, argList, conf); parent.getChildren().add(new ExpressionTree(addLeaf(leaf))); } return this; }
@Override public Builder in(String column, Object... literal) { ExpressionTree parent = currentTree.getFirst(); if (literal.length == 0) { throw new IllegalArgumentException("Can't create in expression with " + "no arguments"); } List<Object> argList = new ArrayList<Object>(); for(Object lit: literal){ argList.add(boxLiteral(lit)); } PredicateLeaf leaf = new PredicateLeafImpl(PredicateLeaf.Operator.IN, getType(argList.get(0)), column, null, argList); leaves.add(leaf); parent.children.add(new ExpressionTree(leaves.size() - 1)); return this; }
@Override public Builder between(String column, PredicateLeaf.Type type, Object lower, Object upper) { ExpressionTree parent = currentTree.getFirst(); if (column == null || lower == null || upper == null) { parent.getChildren().add(new ExpressionTree(TruthValue.YES_NO_NULL)); } else { List<Object> argList = new ArrayList<Object>(); argList.add(lower); argList.add(upper); PredicateLeaf leaf = new PredicateLeafImpl(PredicateLeaf.Operator.BETWEEN, type, column, null, argList, conf); parent.getChildren().add(new ExpressionTree(addLeaf(leaf))); } return this; }
@Override public Builder equals(String column, PredicateLeaf.Type type, Object literal) { ExpressionTree parent = currentTree.getFirst(); if (column == null || literal == null) { parent.getChildren().add(new ExpressionTree(TruthValue.YES_NO_NULL)); } else { PredicateLeaf leaf = new PredicateLeafImpl(PredicateLeaf.Operator.EQUALS, type, column, literal, null, conf); parent.getChildren().add(new ExpressionTree(addLeaf(leaf))); } return this; }
@Override public Builder lessThan(String column, PredicateLeaf.Type type, Object literal) { ExpressionTree parent = currentTree.getFirst(); if (column == null || literal == null) { parent.getChildren().add(new ExpressionTree(TruthValue.YES_NO_NULL)); } else { PredicateLeaf leaf = new PredicateLeafImpl(PredicateLeaf.Operator.LESS_THAN, type, column, literal, null, conf); parent.getChildren().add(new ExpressionTree(addLeaf(leaf))); } return this; }
public static SearchArgument.Builder newBuilder(Configuration conf) { return new SearchArgumentImpl.BuilderImpl(conf); }
@Override public Builder lessThanEquals(String column, PredicateLeaf.Type type, Object literal) { ExpressionTree parent = currentTree.getFirst(); if (column == null || literal == null) { parent.getChildren().add(new ExpressionTree(TruthValue.YES_NO_NULL)); } else { PredicateLeaf leaf = new PredicateLeafImpl(PredicateLeaf.Operator.LESS_THAN_EQUALS, type, column, literal, null, conf); parent.getChildren().add(new ExpressionTree(addLeaf(leaf))); } return this; }
@Override public Builder isNull(String column, PredicateLeaf.Type type) { ExpressionTree parent = currentTree.getFirst(); if (column == null) { parent.getChildren().add(new ExpressionTree(TruthValue.YES_NO_NULL)); } else { PredicateLeaf leaf = new PredicateLeafImpl(PredicateLeaf.Operator.IS_NULL, type, column, null, null, conf); parent.getChildren().add(new ExpressionTree(addLeaf(leaf))); } return this; }
@Override public Builder between(String column, Object lower, Object upper) { ExpressionTree parent = currentTree.getFirst(); List<Object> argList = new ArrayList<Object>(); argList.add(boxLiteral(lower)); argList.add(boxLiteral(upper)); PredicateLeaf leaf = new PredicateLeafImpl(PredicateLeaf.Operator.BETWEEN, getType(argList.get(0)), column, null, argList); leaves.add(leaf); parent.children.add(new ExpressionTree(leaves.size() - 1)); return this; }
@Override public Builder nullSafeEquals(String column, PredicateLeaf.Type type, Object literal) { ExpressionTree parent = currentTree.getFirst(); if (column == null || literal == null) { parent.getChildren().add(new ExpressionTree(TruthValue.YES_NO_NULL)); } else { PredicateLeaf leaf = new PredicateLeafImpl(PredicateLeaf.Operator.NULL_SAFE_EQUALS, type, column, literal, null, conf); parent.getChildren().add(new ExpressionTree(addLeaf(leaf))); } return this; }