List of usage examples for com.google.common.collect Sets powerSet
@GwtCompatible(serializable = false) public static <E> Set<Set<E>> powerSet(Set<E> set)
From source file:org.apache.tajo.plan.rewrite.rules.FilterPushDownRule.java
@Override public LogicalNode visitScan(FilterPushDownContext context, LogicalPlan plan, LogicalPlan.QueryBlock block, final ScanNode scanNode, Stack<LogicalNode> stack) throws TajoException { List<EvalNode> matched = new ArrayList<>(); // find partition column and check matching Set<String> partitionColumns = new HashSet<>(); TableDesc table = scanNode.getTableDesc(); boolean hasQualifiedName = false; if (table.hasPartition()) { for (Column c : table.getPartitionMethod().getExpressionSchema().getRootColumns()) { partitionColumns.add(c.getQualifiedName()); hasQualifiedName = c.hasQualifier(); }// w w w .j a v a 2 s.c om } Set<EvalNode> partitionEvals = new HashSet<>(); for (EvalNode eval : context.pushingDownFilters) { if (table.hasPartition()) { Set<Column> columns = EvalTreeUtil.findUniqueColumns(eval); if (columns.size() != 1) { continue; } Column column = columns.iterator().next(); // If catalog runs with HiveCatalogStore, partition column is a qualified name // Else partition column is a simple name boolean isPartitionColumn = false; if (hasQualifiedName) { isPartitionColumn = partitionColumns .contains(IdentifierUtil.buildFQName(table.getName(), column.getSimpleName())); } else { isPartitionColumn = partitionColumns.contains(column.getSimpleName()); } if (isPartitionColumn) { EvalNode copy; try { copy = (EvalNode) eval.clone(); } catch (CloneNotSupportedException e) { throw new TajoInternalError(e); } EvalTreeUtil.changeColumnRef(copy, column.getQualifiedName(), scanNode.getCanonicalName() + "." + column.getSimpleName()); matched.add(copy); partitionEvals.add(eval); } } } context.pushingDownFilters.removeAll(partitionEvals); List<EvalNode> notMatched = new ArrayList<>(); // transform Map<EvalNode, EvalNode> transformed = findCanPushdownAndTransform(context, block, scanNode, null, notMatched, partitionColumns, 0); for (EvalNode eval : transformed.keySet()) { if (LogicalPlanner.checkIfBeEvaluatedAtRelation(block, eval, scanNode)) { matched.add(eval); } } EvalNode qual = null; if (matched.size() > 1) { // merged into one eval tree qual = AlgebraicUtil.createSingletonExprFromCNF(matched.toArray(new EvalNode[matched.size()])); } else if (matched.size() == 1) { // if the number of matched expr is one qual = matched.iterator().next(); } block.addAccessPath(scanNode, new SeqScanInfo(table)); if (qual != null) { // if a matched qual exists scanNode.setQual(qual); // Index path can be identified only after filters are pushed into each scan. if (context.rewriteRuleContext.getQueryContext().getBool(SessionVars.INDEX_ENABLED)) { String databaseName, tableName; databaseName = IdentifierUtil.extractQualifier(table.getName()); tableName = IdentifierUtil.extractSimpleName(table.getName()); Set<Predicate> predicates = new HashSet<>(); for (EvalNode eval : PlannerUtil.getAllEqualEvals(qual)) { BinaryEval binaryEval = (BinaryEval) eval; // TODO: consider more complex predicates if (binaryEval.getLeftExpr().getType() == EvalType.FIELD && binaryEval.getRightExpr().getType() == EvalType.CONST) { predicates.add(new Predicate(binaryEval.getType(), ((FieldEval) binaryEval.getLeftExpr()).getColumnRef(), ((ConstEval) binaryEval.getRightExpr()).getValue())); } else if (binaryEval.getLeftExpr().getType() == EvalType.CONST && binaryEval.getRightExpr().getType() == EvalType.FIELD) { predicates.add(new Predicate(binaryEval.getType(), ((FieldEval) binaryEval.getRightExpr()).getColumnRef(), ((ConstEval) binaryEval.getLeftExpr()).getValue())); } } // for every subset of the set of columns, find all matched index paths for (Set<Predicate> subset : Sets.powerSet(predicates)) { if (subset.size() == 0) continue; Column[] columns = extractColumns(subset); if (catalog.existIndexByColumns(databaseName, tableName, columns)) { IndexDesc indexDesc = catalog.getIndexByColumns(databaseName, tableName, columns); block.addAccessPath(scanNode, new IndexScanInfo(table.getStats(), indexDesc, getSimplePredicates(indexDesc, subset))); } } } } for (EvalNode matchedEval : matched) { transformed.remove(matchedEval); } context.setToOrigin(transformed); context.addFiltersTobePushed(notMatched); return scanNode; }
From source file:org.jamocha.dn.compiler.pathblocks.PathBlocks.java
protected static void vertical(final UndirectedGraph<FilterInstance, ConflictEdge> graph, final Set<Set<FilterInstance>> filterInstancesGroupedByRule, final PathBlockSet resultBlocks) { final Set<Set<Set<FilterInstance>>> filterInstancesPowerSet = Sets.powerSet(filterInstancesGroupedByRule); final Iterator<Set<Set<FilterInstance>>> iterator = filterInstancesPowerSet.iterator(); // skip empty set iterator.next();/* w w w .j ava 2 s .c om*/ while (iterator.hasNext()) { final Set<Set<FilterInstance>> powerSetElement = iterator.next(); final Set<List<FilterInstance>> cartesianProduct = Sets .cartesianProduct(ImmutableList.copyOf(powerSetElement)); for (final List<FilterInstance> filterInstances : cartesianProduct) { final Block newBlock = new Block(graph); newBlock.addFilterInstances(filterInstances.stream().collect(toMap(FilterInstance::getRuleOrProxy, fi -> Collections.singleton(new FilterInstancesSideBySide(fi))))); horizontalRecursion(newBlock, new Stack<>(), resultBlocks); } } }
From source file:org.dllearner.algorithms.qtl.experiments.QTLEvaluation.java
private List<String> generateNoiseCandidatesSimilar(List<String> examples, String queryString, int limit) { List<String> negExamples = new ArrayList<>(); Query query = QueryFactory.create(queryString); QueryUtils queryUtils = new QueryUtils(); Set<Triple> triplePatterns = queryUtils.extractTriplePattern(query); Set<String> negExamplesSet = new TreeSet<>(); if (triplePatterns.size() == 1) { Triple tp = triplePatterns.iterator().next(); Node var = NodeFactory.createVariable("var"); Triple newTp = Triple.create(tp.getSubject(), tp.getPredicate(), var); ElementTriplesBlock triplesBlock = new ElementTriplesBlock(); triplesBlock.addTriple(newTp);//from w w w . ja v a 2 s . c o m ElementFilter filter = new ElementFilter( new E_NotEquals(new ExprVar(var), NodeValue.makeNode(tp.getObject()))); ElementGroup eg = new ElementGroup(); eg.addElement(triplesBlock); eg.addElementFilter(filter); Query q = new Query(); q.setQuerySelectType(); q.setDistinct(true); q.addProjectVars(query.getProjectVars()); q.setQueryPattern(eg); // System.out.println(q); List<String> result = getResult(q.toString()); negExamplesSet.addAll(result); } else { // we modify each triple pattern <s p o> by <s p ?var> . ?var != o Set<Set<Triple>> powerSet = new TreeSet<>((o1, o2) -> { return ComparisonChain.start().compare(o1.size(), o2.size()).compare(o1.hashCode(), o2.hashCode()) .result(); }); powerSet.addAll(Sets.powerSet(triplePatterns)); for (Set<Triple> set : powerSet) { if (!set.isEmpty() && set.size() != triplePatterns.size()) { List<Triple> existingTriplePatterns = new ArrayList<>(triplePatterns); List<Triple> newTriplePatterns = new ArrayList<>(); List<ElementFilter> filters = new ArrayList<>(); int cnt = 0; for (Triple tp : set) { if (tp.getObject().isURI() || tp.getObject().isLiteral()) { Node var = NodeFactory.createVariable("var" + cnt++); Triple newTp = Triple.create(tp.getSubject(), tp.getPredicate(), var); existingTriplePatterns.remove(tp); newTriplePatterns.add(newTp); ElementTriplesBlock triplesBlock = new ElementTriplesBlock(); triplesBlock.addTriple(tp); ElementGroup eg = new ElementGroup(); eg.addElement(triplesBlock); ElementFilter filter = new ElementFilter(new E_NotExists(eg)); filters.add(filter); } } Query q = new Query(); q.setQuerySelectType(); q.setDistinct(true); q.addProjectVars(query.getProjectVars()); List<Triple> allTriplePatterns = new ArrayList<>(existingTriplePatterns); allTriplePatterns.addAll(newTriplePatterns); ElementTriplesBlock tripleBlock = new ElementTriplesBlock(BasicPattern.wrap(allTriplePatterns)); ElementGroup eg = new ElementGroup(); eg.addElement(tripleBlock); for (ElementFilter filter : filters) { eg.addElementFilter(filter); } q.setQueryPattern(eg); // System.out.println(q); List<String> result = getResult(q.toString()); result.removeAll(examples); if (result.isEmpty()) { q = new Query(); q.setQuerySelectType(); q.setDistinct(true); q.addProjectVars(query.getProjectVars()); tripleBlock = new ElementTriplesBlock(BasicPattern.wrap(existingTriplePatterns)); eg = new ElementGroup(); eg.addElement(tripleBlock); for (ElementFilter filter : filters) { eg.addElementFilter(filter); } q.setQueryPattern(eg); // System.out.println(q); result = getResult(q.toString()); result.removeAll(examples); } negExamplesSet.addAll(result); } } } negExamplesSet.removeAll(examples); if (negExamples.isEmpty()) { logger.error("Found no negative example."); System.exit(0); } negExamples.addAll(negExamplesSet); return new ArrayList<>(negExamples).subList(0, Math.min(negExamples.size(), limit)); }