@Test public void testWrong() { assertNull("not count", getPlan("SELECT * { ?s ?p ?o }")); assertNull("distinct + Triple Pattern",getPlan("SELECT COUNT(distinct ?p) { <hello> ?p ?o }")); assertNull("unused variable", getPlan("SELECT COUNT(distinct ?NO) { ?s ?p ?o }")); assertNull("triple pattern join", getPlan("SELECT COUNT(distinct ?o) { ?s ?SAME ?SAME }")); assertNull("filter", getPlan("SELECT COUNT(*) { ?s ?p ?o FILTER (isLiteral(?o))}")); assertNull("having", getPlan("SELECT COUNT(*) { ?s ?p ?o } HAVING(?o>0)")); assertNull("limit", getPlan("SELECT COUNT(*) { ?s ?p ?o } LIMIT 0")); assertNull("offset", getPlan("SELECT COUNT(*) { ?s ?p ?o } OFFSET 10")); }
public static Plan getPlan(HDTQueryEngine engine, Query query, DatasetGraph dataset, Binding input, Context context) { if(query.getAggregators().size()!=1) return null; Aggregator ag = query.getAggregators().get(0).getAggregator(); if(ag==null || !query.getHavingExprs().isEmpty() || !query.getGroupBy().isEmpty() || query.hasLimit() || query.hasOffset() || !( (ag instanceof AggCount) || (ag instanceof AggCountVar) || (ag instanceof AggCountDistinct) || (ag instanceof AggCountVarDistinct)) ) { return null; } ElementGroup el = (ElementGroup) query.getQueryPattern(); List<Element> list = el.getElements(); if(list.size()!=1) { return null; } Element ele = list.get(0); Node graphName=null; if(ele instanceof ElementNamedGraph) { graphName = ((ElementNamedGraph) ele).getGraphNameNode(); if(graphName.isVariable()) { return null; } ele = ((ElementNamedGraph) ele).getElement(); if(ele instanceof ElementGroup) { ElementGroup elGroup = (ElementGroup) ele; if(elGroup.getElements().size()!=1) { return null; } ele = elGroup.getElements().get(0); } } if(!(ele instanceof ElementPathBlock)) { return null; } PathBlock pb = ((ElementPathBlock) ele).getPattern(); if(pb.size()!=1) { return null; } TriplePath tp = pb.get(0); Triple triple= tp.asTriple(); if(triple==null) { return null; } if(triple.getSubject().equals(triple.getPredicate()) || triple.getPredicate().equals(triple.getObject()) || triple.getSubject().equals(triple.getObject()) ) { return null; } if(query.getProjectVars().size()!=1) { return null; } Var varOutput= query.getProjectVars().get(0); Graph g=null; if(dataset instanceof DatasetGraphOne ) { g = dataset.getDefaultGraph(); } else if(dataset instanceof DatasetGraphMapLink) { if(graphName!=null) { g = dataset.getGraph(graphName); } else { g = dataset.getDefaultGraph(); } } if((g==null) || !(g instanceof HDTGraph)) { return null; } HDTGraph hdtg = (HDTGraph) g; long count; if(ag instanceof AggCountVarDistinct) { ExprList exprList = ag.getExprList(); if(exprList.size()!=1) { return null; } Expr expr = exprList.get(0); if(!(expr instanceof ExprVar)) { return null; } Var countVar = expr.asVar(); if(!triple.getSubject().isVariable() || !triple.getPredicate().isVariable() || ! triple.getObject().isVariable()) { return null; } Dictionary dictionary = hdtg.getHDT().getDictionary(); if(countVar.equals(triple.getSubject())) { count = dictionary.getNsubjects(); } else if(countVar.equals(triple.getPredicate())) { count = dictionary.getNpredicates(); } else if(countVar.equals(triple.getObject())) { count = dictionary.getNobjects(); } else { return null; } } else { if(ag instanceof AggCountVar) { ExprList exprList = ag.getExprList(); if(exprList.size()!=1) { return null; } Expr expr = exprList.get(0); if(!(expr instanceof ExprVar)) { return null; } Var countVar = expr.asVar(); if( !(triple.getSubject().equals(countVar) || triple.getPredicate().equals(countVar) || triple.getObject().equals(countVar)) ) { return null; } } TripleID patternID = hdtg.getNodeDictionary().getTriplePatID(triple); if(patternID.isEmpty()) { count = hdtg.getHDT().getTriples().getNumberOfElements(); } else if(!patternID.isNoMatch()) { IteratorTripleID it = hdtg.getHDT().getTriples().search(patternID); if(it.numResultEstimation()==ResultEstimationType.EXACT) { count = it.estimatedNumResults(); } else { count = 0; while(it.hasNext()) { it.next(); count++; } } } else { count=0; } } Binding bindingResult = new BindingOne( varOutput, NodeFactory.createLiteral(Long.toString(count), XSDDatatype.XSDinteger) ); return new PlanOp(new HDTOptimizedOp(), engine, new QueryIterYieldN(1, bindingResult)); }
OptimizedCount { public static Plan getPlan(HDTQueryEngine engine, Query query, DatasetGraph dataset, Binding input, Context context) { if(query.getAggregators().size()!=1) return null; Aggregator ag = query.getAggregators().get(0).getAggregator(); if(ag==null || !query.getHavingExprs().isEmpty() || !query.getGroupBy().isEmpty() || query.hasLimit() || query.hasOffset() || !( (ag instanceof AggCount) || (ag instanceof AggCountVar) || (ag instanceof AggCountDistinct) || (ag instanceof AggCountVarDistinct)) ) { return null; } ElementGroup el = (ElementGroup) query.getQueryPattern(); List<Element> list = el.getElements(); if(list.size()!=1) { return null; } Element ele = list.get(0); Node graphName=null; if(ele instanceof ElementNamedGraph) { graphName = ((ElementNamedGraph) ele).getGraphNameNode(); if(graphName.isVariable()) { return null; } ele = ((ElementNamedGraph) ele).getElement(); if(ele instanceof ElementGroup) { ElementGroup elGroup = (ElementGroup) ele; if(elGroup.getElements().size()!=1) { return null; } ele = elGroup.getElements().get(0); } } if(!(ele instanceof ElementPathBlock)) { return null; } PathBlock pb = ((ElementPathBlock) ele).getPattern(); if(pb.size()!=1) { return null; } TriplePath tp = pb.get(0); Triple triple= tp.asTriple(); if(triple==null) { return null; } if(triple.getSubject().equals(triple.getPredicate()) || triple.getPredicate().equals(triple.getObject()) || triple.getSubject().equals(triple.getObject()) ) { return null; } if(query.getProjectVars().size()!=1) { return null; } Var varOutput= query.getProjectVars().get(0); Graph g=null; if(dataset instanceof DatasetGraphOne ) { g = dataset.getDefaultGraph(); } else if(dataset instanceof DatasetGraphMapLink) { if(graphName!=null) { g = dataset.getGraph(graphName); } else { g = dataset.getDefaultGraph(); } } if((g==null) || !(g instanceof HDTGraph)) { return null; } HDTGraph hdtg = (HDTGraph) g; long count; if(ag instanceof AggCountVarDistinct) { ExprList exprList = ag.getExprList(); if(exprList.size()!=1) { return null; } Expr expr = exprList.get(0); if(!(expr instanceof ExprVar)) { return null; } Var countVar = expr.asVar(); if(!triple.getSubject().isVariable() || !triple.getPredicate().isVariable() || ! triple.getObject().isVariable()) { return null; } Dictionary dictionary = hdtg.getHDT().getDictionary(); if(countVar.equals(triple.getSubject())) { count = dictionary.getNsubjects(); } else if(countVar.equals(triple.getPredicate())) { count = dictionary.getNpredicates(); } else if(countVar.equals(triple.getObject())) { count = dictionary.getNobjects(); } else { return null; } } else { if(ag instanceof AggCountVar) { ExprList exprList = ag.getExprList(); if(exprList.size()!=1) { return null; } Expr expr = exprList.get(0); if(!(expr instanceof ExprVar)) { return null; } Var countVar = expr.asVar(); if( !(triple.getSubject().equals(countVar) || triple.getPredicate().equals(countVar) || triple.getObject().equals(countVar)) ) { return null; } } TripleID patternID = hdtg.getNodeDictionary().getTriplePatID(triple); if(patternID.isEmpty()) { count = hdtg.getHDT().getTriples().getNumberOfElements(); } else if(!patternID.isNoMatch()) { IteratorTripleID it = hdtg.getHDT().getTriples().search(patternID); if(it.numResultEstimation()==ResultEstimationType.EXACT) { count = it.estimatedNumResults(); } else { count = 0; while(it.hasNext()) { it.next(); count++; } } } else { count=0; } } Binding bindingResult = new BindingOne( varOutput, NodeFactory.createLiteral(Long.toString(count), XSDDatatype.XSDinteger) ); return new PlanOp(new HDTOptimizedOp(), engine, new QueryIterYieldN(1, bindingResult)); } }
OptimizedCount { public static Plan getPlan(HDTQueryEngine engine, Query query, DatasetGraph dataset, Binding input, Context context) { if(query.getAggregators().size()!=1) return null; Aggregator ag = query.getAggregators().get(0).getAggregator(); if(ag==null || !query.getHavingExprs().isEmpty() || !query.getGroupBy().isEmpty() || query.hasLimit() || query.hasOffset() || !( (ag instanceof AggCount) || (ag instanceof AggCountVar) || (ag instanceof AggCountDistinct) || (ag instanceof AggCountVarDistinct)) ) { return null; } ElementGroup el = (ElementGroup) query.getQueryPattern(); List<Element> list = el.getElements(); if(list.size()!=1) { return null; } Element ele = list.get(0); Node graphName=null; if(ele instanceof ElementNamedGraph) { graphName = ((ElementNamedGraph) ele).getGraphNameNode(); if(graphName.isVariable()) { return null; } ele = ((ElementNamedGraph) ele).getElement(); if(ele instanceof ElementGroup) { ElementGroup elGroup = (ElementGroup) ele; if(elGroup.getElements().size()!=1) { return null; } ele = elGroup.getElements().get(0); } } if(!(ele instanceof ElementPathBlock)) { return null; } PathBlock pb = ((ElementPathBlock) ele).getPattern(); if(pb.size()!=1) { return null; } TriplePath tp = pb.get(0); Triple triple= tp.asTriple(); if(triple==null) { return null; } if(triple.getSubject().equals(triple.getPredicate()) || triple.getPredicate().equals(triple.getObject()) || triple.getSubject().equals(triple.getObject()) ) { return null; } if(query.getProjectVars().size()!=1) { return null; } Var varOutput= query.getProjectVars().get(0); Graph g=null; if(dataset instanceof DatasetGraphOne ) { g = dataset.getDefaultGraph(); } else if(dataset instanceof DatasetGraphMapLink) { if(graphName!=null) { g = dataset.getGraph(graphName); } else { g = dataset.getDefaultGraph(); } } if((g==null) || !(g instanceof HDTGraph)) { return null; } HDTGraph hdtg = (HDTGraph) g; long count; if(ag instanceof AggCountVarDistinct) { ExprList exprList = ag.getExprList(); if(exprList.size()!=1) { return null; } Expr expr = exprList.get(0); if(!(expr instanceof ExprVar)) { return null; } Var countVar = expr.asVar(); if(!triple.getSubject().isVariable() || !triple.getPredicate().isVariable() || ! triple.getObject().isVariable()) { return null; } Dictionary dictionary = hdtg.getHDT().getDictionary(); if(countVar.equals(triple.getSubject())) { count = dictionary.getNsubjects(); } else if(countVar.equals(triple.getPredicate())) { count = dictionary.getNpredicates(); } else if(countVar.equals(triple.getObject())) { count = dictionary.getNobjects(); } else { return null; } } else { if(ag instanceof AggCountVar) { ExprList exprList = ag.getExprList(); if(exprList.size()!=1) { return null; } Expr expr = exprList.get(0); if(!(expr instanceof ExprVar)) { return null; } Var countVar = expr.asVar(); if( !(triple.getSubject().equals(countVar) || triple.getPredicate().equals(countVar) || triple.getObject().equals(countVar)) ) { return null; } } TripleID patternID = hdtg.getNodeDictionary().getTriplePatID(triple); if(patternID.isEmpty()) { count = hdtg.getHDT().getTriples().getNumberOfElements(); } else if(!patternID.isNoMatch()) { IteratorTripleID it = hdtg.getHDT().getTriples().search(patternID); if(it.numResultEstimation()==ResultEstimationType.EXACT) { count = it.estimatedNumResults(); } else { count = 0; while(it.hasNext()) { it.next(); count++; } } } else { count=0; } } Binding bindingResult = new BindingOne( varOutput, NodeFactory.createLiteral(Long.toString(count), XSDDatatype.XSDinteger) ); return new PlanOp(new HDTOptimizedOp(), engine, new QueryIterYieldN(1, bindingResult)); } private  OptimizedCount(); }
OptimizedCount { public static Plan getPlan(HDTQueryEngine engine, Query query, DatasetGraph dataset, Binding input, Context context) { if(query.getAggregators().size()!=1) return null; Aggregator ag = query.getAggregators().get(0).getAggregator(); if(ag==null || !query.getHavingExprs().isEmpty() || !query.getGroupBy().isEmpty() || query.hasLimit() || query.hasOffset() || !( (ag instanceof AggCount) || (ag instanceof AggCountVar) || (ag instanceof AggCountDistinct) || (ag instanceof AggCountVarDistinct)) ) { return null; } ElementGroup el = (ElementGroup) query.getQueryPattern(); List<Element> list = el.getElements(); if(list.size()!=1) { return null; } Element ele = list.get(0); Node graphName=null; if(ele instanceof ElementNamedGraph) { graphName = ((ElementNamedGraph) ele).getGraphNameNode(); if(graphName.isVariable()) { return null; } ele = ((ElementNamedGraph) ele).getElement(); if(ele instanceof ElementGroup) { ElementGroup elGroup = (ElementGroup) ele; if(elGroup.getElements().size()!=1) { return null; } ele = elGroup.getElements().get(0); } } if(!(ele instanceof ElementPathBlock)) { return null; } PathBlock pb = ((ElementPathBlock) ele).getPattern(); if(pb.size()!=1) { return null; } TriplePath tp = pb.get(0); Triple triple= tp.asTriple(); if(triple==null) { return null; } if(triple.getSubject().equals(triple.getPredicate()) || triple.getPredicate().equals(triple.getObject()) || triple.getSubject().equals(triple.getObject()) ) { return null; } if(query.getProjectVars().size()!=1) { return null; } Var varOutput= query.getProjectVars().get(0); Graph g=null; if(dataset instanceof DatasetGraphOne ) { g = dataset.getDefaultGraph(); } else if(dataset instanceof DatasetGraphMapLink) { if(graphName!=null) { g = dataset.getGraph(graphName); } else { g = dataset.getDefaultGraph(); } } if((g==null) || !(g instanceof HDTGraph)) { return null; } HDTGraph hdtg = (HDTGraph) g; long count; if(ag instanceof AggCountVarDistinct) { ExprList exprList = ag.getExprList(); if(exprList.size()!=1) { return null; } Expr expr = exprList.get(0); if(!(expr instanceof ExprVar)) { return null; } Var countVar = expr.asVar(); if(!triple.getSubject().isVariable() || !triple.getPredicate().isVariable() || ! triple.getObject().isVariable()) { return null; } Dictionary dictionary = hdtg.getHDT().getDictionary(); if(countVar.equals(triple.getSubject())) { count = dictionary.getNsubjects(); } else if(countVar.equals(triple.getPredicate())) { count = dictionary.getNpredicates(); } else if(countVar.equals(triple.getObject())) { count = dictionary.getNobjects(); } else { return null; } } else { if(ag instanceof AggCountVar) { ExprList exprList = ag.getExprList(); if(exprList.size()!=1) { return null; } Expr expr = exprList.get(0); if(!(expr instanceof ExprVar)) { return null; } Var countVar = expr.asVar(); if( !(triple.getSubject().equals(countVar) || triple.getPredicate().equals(countVar) || triple.getObject().equals(countVar)) ) { return null; } } TripleID patternID = hdtg.getNodeDictionary().getTriplePatID(triple); if(patternID.isEmpty()) { count = hdtg.getHDT().getTriples().getNumberOfElements(); } else if(!patternID.isNoMatch()) { IteratorTripleID it = hdtg.getHDT().getTriples().search(patternID); if(it.numResultEstimation()==ResultEstimationType.EXACT) { count = it.estimatedNumResults(); } else { count = 0; while(it.hasNext()) { it.next(); count++; } } } else { count=0; } } Binding bindingResult = new BindingOne( varOutput, NodeFactory.createLiteral(Long.toString(count), XSDDatatype.XSDinteger) ); return new PlanOp(new HDTOptimizedOp(), engine, new QueryIterYieldN(1, bindingResult)); } private  OptimizedCount(); static Plan getPlan(HDTQueryEngine engine, Query query, DatasetGraph dataset, Binding input, Context context); }
OptimizedCount { public static Plan getPlan(HDTQueryEngine engine, Query query, DatasetGraph dataset, Binding input, Context context) { if(query.getAggregators().size()!=1) return null; Aggregator ag = query.getAggregators().get(0).getAggregator(); if(ag==null || !query.getHavingExprs().isEmpty() || !query.getGroupBy().isEmpty() || query.hasLimit() || query.hasOffset() || !( (ag instanceof AggCount) || (ag instanceof AggCountVar) || (ag instanceof AggCountDistinct) || (ag instanceof AggCountVarDistinct)) ) { return null; } ElementGroup el = (ElementGroup) query.getQueryPattern(); List<Element> list = el.getElements(); if(list.size()!=1) { return null; } Element ele = list.get(0); Node graphName=null; if(ele instanceof ElementNamedGraph) { graphName = ((ElementNamedGraph) ele).getGraphNameNode(); if(graphName.isVariable()) { return null; } ele = ((ElementNamedGraph) ele).getElement(); if(ele instanceof ElementGroup) { ElementGroup elGroup = (ElementGroup) ele; if(elGroup.getElements().size()!=1) { return null; } ele = elGroup.getElements().get(0); } } if(!(ele instanceof ElementPathBlock)) { return null; } PathBlock pb = ((ElementPathBlock) ele).getPattern(); if(pb.size()!=1) { return null; } TriplePath tp = pb.get(0); Triple triple= tp.asTriple(); if(triple==null) { return null; } if(triple.getSubject().equals(triple.getPredicate()) || triple.getPredicate().equals(triple.getObject()) || triple.getSubject().equals(triple.getObject()) ) { return null; } if(query.getProjectVars().size()!=1) { return null; } Var varOutput= query.getProjectVars().get(0); Graph g=null; if(dataset instanceof DatasetGraphOne ) { g = dataset.getDefaultGraph(); } else if(dataset instanceof DatasetGraphMapLink) { if(graphName!=null) { g = dataset.getGraph(graphName); } else { g = dataset.getDefaultGraph(); } } if((g==null) || !(g instanceof HDTGraph)) { return null; } HDTGraph hdtg = (HDTGraph) g; long count; if(ag instanceof AggCountVarDistinct) { ExprList exprList = ag.getExprList(); if(exprList.size()!=1) { return null; } Expr expr = exprList.get(0); if(!(expr instanceof ExprVar)) { return null; } Var countVar = expr.asVar(); if(!triple.getSubject().isVariable() || !triple.getPredicate().isVariable() || ! triple.getObject().isVariable()) { return null; } Dictionary dictionary = hdtg.getHDT().getDictionary(); if(countVar.equals(triple.getSubject())) { count = dictionary.getNsubjects(); } else if(countVar.equals(triple.getPredicate())) { count = dictionary.getNpredicates(); } else if(countVar.equals(triple.getObject())) { count = dictionary.getNobjects(); } else { return null; } } else { if(ag instanceof AggCountVar) { ExprList exprList = ag.getExprList(); if(exprList.size()!=1) { return null; } Expr expr = exprList.get(0); if(!(expr instanceof ExprVar)) { return null; } Var countVar = expr.asVar(); if( !(triple.getSubject().equals(countVar) || triple.getPredicate().equals(countVar) || triple.getObject().equals(countVar)) ) { return null; } } TripleID patternID = hdtg.getNodeDictionary().getTriplePatID(triple); if(patternID.isEmpty()) { count = hdtg.getHDT().getTriples().getNumberOfElements(); } else if(!patternID.isNoMatch()) { IteratorTripleID it = hdtg.getHDT().getTriples().search(patternID); if(it.numResultEstimation()==ResultEstimationType.EXACT) { count = it.estimatedNumResults(); } else { count = 0; while(it.hasNext()) { it.next(); count++; } } } else { count=0; } } Binding bindingResult = new BindingOne( varOutput, NodeFactory.createLiteral(Long.toString(count), XSDDatatype.XSDinteger) ); return new PlanOp(new HDTOptimizedOp(), engine, new QueryIterYieldN(1, bindingResult)); } private  OptimizedCount(); static Plan getPlan(HDTQueryEngine engine, Query query, DatasetGraph dataset, Binding input, Context context);  }