use of org.apache.jena.sparql.engine.binding.Binding in project jena by apache.
the class TestQuery method query_recursive_01.
@Test
public void query_recursive_01() {
String query = "SELECT * WHERE { SERVICE <" + serviceQuery() + "> { ?s ?p ?o . BIND(?o AS ?x) } }";
try (QueryExecution qExec = QueryExecutionFactory.sparqlService(serviceQuery(), query)) {
ResultSet rs = qExec.execSelect();
Var x = Var.alloc("x");
while (rs.hasNext()) {
Binding b = rs.nextBinding();
Assert.assertNotNull(b.get(x));
}
}
}
use of org.apache.jena.sparql.engine.binding.Binding in project jena by apache.
the class AlgebraEx method main.
public static void main(String[] args) {
String s = "SELECT DISTINCT ?s { ?s ?p ?o }";
// Parse
Query query = QueryFactory.create(s);
System.out.println(query);
// Generate algebra
Op op = Algebra.compile(query);
op = Algebra.optimize(op);
System.out.println(op);
// Execute it.
QueryIterator qIter = Algebra.exec(op, ExQuerySelect1.createModel());
// Results
for (; qIter.hasNext(); ) {
Binding b = qIter.nextBinding();
System.out.println(b);
}
qIter.close();
}
use of org.apache.jena.sparql.engine.binding.Binding in project jena by apache.
the class AlgebraGenerator method compileModifiers.
/** Compile query modifiers */
protected Op compileModifiers(Query query, Op pattern) {
/* The modifier order in algebra is:
*
* Limit/Offset
* Distinct/reduce
* project
* OrderBy
* Bindings
* having
* select expressions
* group
*/
// Preparation: sort SELECT clause into assignments and projects.
VarExprList projectVars = query.getProject();
// Assignments to be done.
VarExprList exprs = new VarExprList();
// projection variables
List<Var> vars = new ArrayList<>();
Op op = pattern;
if (query.hasGroupBy()) {
// When there is no GroupBy but there are some aggregates, it's a group of no variables.
op = OpGroup.create(op, query.getGroupBy(), query.getAggregators());
}
// Look for assignments in SELECT expressions.
if (!projectVars.isEmpty() && !query.isQueryResultStar()) {
// through in SELECT *
if (projectVars.size() == 0 && query.isSelectType())
Log.warn(this, "No project variables");
// Separate assignments and variable projection.
for (Var v : query.getProject().getVars()) {
Expr e = query.getProject().getExpr(v);
if (e != null) {
Expr e2 = ExprLib.replaceAggregateByVariable(e);
exprs.add(v, e2);
}
// Include in project
vars.add(v);
}
}
// ---- Assignments from SELECT and other places (so available to ORDER and HAVING)
for (Var v : exprs.getVars()) {
Expr e = exprs.getExpr(v);
op = OpExtend.create(op, v, e);
}
// ---- HAVING
if (query.hasHaving()) {
for (Expr expr : query.getHavingExprs()) {
// HAVING expression to refer to the aggregate via the variable.
Expr expr2 = ExprLib.replaceAggregateByVariable(expr);
op = OpFilter.filter(expr2, op);
}
}
// ---- VALUES
if (query.hasValues()) {
Table table = TableFactory.create(query.getValuesVariables());
for (Binding binding : query.getValuesData()) table.addBinding(binding);
OpTable opTable = OpTable.create(table);
op = OpJoin.create(op, opTable);
}
// ---- ToList
if (context.isTrue(ARQ.generateToList))
// Listify it.
op = new OpList(op);
// ---- ORDER BY
if (query.getOrderBy() != null) {
List<SortCondition> scList = new ArrayList<>();
// Aggregates in ORDER BY
for (SortCondition sc : query.getOrderBy()) {
Expr e = sc.getExpression();
e = ExprLib.replaceAggregateByVariable(e);
scList.add(new SortCondition(e, sc.getDirection()));
}
op = new OpOrder(op, scList);
}
if (vars.size() > 0)
op = new OpProject(op, vars);
// ---- DISTINCT
if (query.isDistinct())
op = OpDistinct.create(op);
// ---- REDUCED
if (query.isReduced())
op = OpReduced.create(op);
// ---- LIMIT/OFFSET
if (query.hasLimit() || query.hasOffset())
op = new OpSlice(op, query.getOffset(), /*start*/
query.getLimit());
return op;
}
use of org.apache.jena.sparql.engine.binding.Binding in project jena by apache.
the class TestSortedDataBag method randomBindings.
private List<Binding> randomBindings(int numBindings) {
random = new Random();
Var[] vars = new Var[] { Var.alloc("1"), Var.alloc("2"), Var.alloc("3"), Var.alloc("4"), Var.alloc("5"), Var.alloc("6"), Var.alloc("7"), Var.alloc("8"), Var.alloc("9"), Var.alloc("0") };
List<Binding> toReturn = new ArrayList<>();
for (int i = 0; i < numBindings; i++) {
toReturn.add(randomBinding(vars));
}
return toReturn;
}
use of org.apache.jena.sparql.engine.binding.Binding in project jena by apache.
the class TestDistinctDataBag method testTemporaryFilesAreCleanedUpAfterCompletion.
@Test
public void testTemporaryFilesAreCleanedUpAfterCompletion() {
List<Binding> undistinct = new ArrayList<>();
random = new Random();
Var[] vars = new Var[] { Var.alloc("1"), Var.alloc("2"), Var.alloc("3"), Var.alloc("4"), Var.alloc("5"), Var.alloc("6"), Var.alloc("7"), Var.alloc("8"), Var.alloc("9"), Var.alloc("0") };
for (int i = 0; i < 500; i++) {
undistinct.add(randomBinding(vars));
}
DistinctDataBag<Binding> db = new DistinctDataBag<>(new ThresholdPolicyCount<Binding>(10), SerializationFactoryFinder.bindingSerializationFactory(), new BindingComparator(new ArrayList<SortCondition>()));
List<File> spillFiles = new ArrayList<>();
try {
db.addAll(undistinct);
spillFiles.addAll(db.getSpillFiles());
int count = 0;
for (File file : spillFiles) {
if (file.exists()) {
count++;
}
}
// 500 bindings divided into 50 chunks (49 in files, and 1 in memory)
assertEquals(49, count);
Iterator<Binding> iter = db.iterator();
while (iter.hasNext()) {
iter.next();
}
Iter.close(iter);
} finally {
db.close();
}
int count = 0;
for (File file : spillFiles) {
if (file.exists()) {
count++;
}
}
assertEquals(0, count);
}
Aggregations