use of org.openrdf.query.algebra.Projection in project incubator-rya by apache.
the class PrecompJoinOptimizerTest2 method testBindingSetAssignment2.
@Test
public void testBindingSetAssignment2() throws Exception {
final SPARQLParser parser = new SPARQLParser();
final ParsedQuery pq1 = parser.parseQuery(q41, null);
final ParsedQuery pq2 = parser.parseQuery(q42, null);
final ParsedQuery pq3 = parser.parseQuery(q43, null);
final SimpleExternalTupleSet extTup1 = new SimpleExternalTupleSet(new Projection(pq2.getTupleExpr()));
final SimpleExternalTupleSet extTup2 = new SimpleExternalTupleSet(new Projection(pq3.getTupleExpr()));
final List<ExternalTupleSet> list = new ArrayList<ExternalTupleSet>();
list.add(extTup1);
list.add(extTup2);
final TupleExpr tup = pq1.getTupleExpr().clone();
provider.setIndices(list);
final PCJOptimizer pcj = new PCJOptimizer(list, false, provider);
pcj.optimize(tup, null, null);
final Set<StatementPattern> qSet = Sets.newHashSet(StatementPatternCollector.process(pq1.getTupleExpr()));
final Set<QueryModelNode> eTupSet = PcjIntegrationTestingUtil.getTupleSets(tup);
final Set<StatementPattern> set = Sets.newHashSet();
for (final QueryModelNode s : eTupSet) {
set.addAll(StatementPatternCollector.process(((ExternalTupleSet) s).getTupleExpr()));
}
Assert.assertEquals(2, eTupSet.size());
Assert.assertEquals(qSet, set);
final BindingSetAssignmentCollector bsac1 = new BindingSetAssignmentCollector();
final BindingSetAssignmentCollector bsac2 = new BindingSetAssignmentCollector();
pq1.getTupleExpr().visit(bsac1);
tup.visit(bsac2);
Assert.assertEquals(bsac1.getBindingSetAssignments(), bsac2.getBindingSetAssignments());
}
use of org.openrdf.query.algebra.Projection in project incubator-rya by apache.
the class ParsedQueryUtilTest method findProjection_projectionIsTopNode.
@Test
public void findProjection_projectionIsTopNode() throws MalformedQueryException {
// A SPARQL query that will result in the Projection node being the top node.
String sparql = "SELECT ?a ?b " + "WHERE {" + "?a <http://talksTo> ?b" + "}";
// Run the test.
SPARQLParser parser = new SPARQLParser();
ParsedQuery query = parser.parseQuery(sparql, null);
Optional<Projection> projection = new ParsedQueryUtil().findProjection(query);
assertTrue(projection.isPresent());
}
use of org.openrdf.query.algebra.Projection in project incubator-rya by apache.
the class SimpleExternalTupleSetTest method getSupportedVariableOrderMap.
@Test
public void getSupportedVariableOrderMap() throws MalformedQueryException {
// Create the PCJ expression.
final String sparql = "SELECT ?f ?m ?d { " + "?f <urn:talksTo> ?m . " + "?m <uri:associatesWith> ?d . " + "}";
final ParsedQuery query = new SPARQLParser().parseQuery(sparql, null);
final Projection pcjExpression = (Projection) query.getTupleExpr();
// Create the object that is being tested.
final SimpleExternalTupleSet testSet = new SimpleExternalTupleSet(pcjExpression);
// Verify the correct Supported Variable Order Map is created.
final Map<String, Set<String>> expected = new HashMap<>();
String varOrder = "f";
Set<String> vars = new HashSet<>();
vars.add("f");
expected.put(varOrder, vars);
varOrder = "f;m";
vars = new HashSet<>();
vars.add("f");
vars.add("m");
expected.put(varOrder, vars);
varOrder = "f;m;d";
vars = new HashSet<>();
vars.add("f");
vars.add("m");
vars.add("d");
expected.put(varOrder, vars);
assertEquals(expected, testSet.getSupportedVariableOrderMap());
}
use of org.openrdf.query.algebra.Projection in project incubator-rya by apache.
the class SimpleExternalTupleSetTest method hashCode_notSame.
public void hashCode_notSame() throws MalformedQueryException {
// Create the first SimpleExternalTupleSet object.
final String sparql1 = "SELECT ?f ?m ?d { " + "?f <urn:talksTo> ?m . " + "?m <uri:associatesWith> ?d . " + "}";
final ParsedQuery query1 = new SPARQLParser().parseQuery(sparql1, null);
final Projection pcjExpression1 = (Projection) query1.getTupleExpr();
final SimpleExternalTupleSet set1 = new SimpleExternalTupleSet(pcjExpression1);
// Create another one using a different expression.
final String sparql2 = "SELECT ?f ?m ?d { " + "?f <urn:talksTo> ?m . " + "?m <uri:friendsWith> ?d . " + "}";
final ParsedQuery query2 = new SPARQLParser().parseQuery(sparql2, null);
final Projection pcjExpression2 = (Projection) query2.getTupleExpr();
final SimpleExternalTupleSet set2 = new SimpleExternalTupleSet(pcjExpression2);
// Show they are not equal.
assertNotEquals(set1.hashCode(), set2.hashCode());
}
use of org.openrdf.query.algebra.Projection in project incubator-rya by apache.
the class IndexWritingTool method setVarOrders.
public void setVarOrders(final String s, final Configuration conf) throws MalformedQueryException {
final SPARQLParser parser = new SPARQLParser();
final TupleExpr query = parser.parseQuery(s, null).getTupleExpr();
final List<String> projList = Lists.newArrayList(((Projection) query).getProjectionElemList().getTargetNames());
final String projElems = Joiner.on(";").join(projList);
conf.set("projElems", projElems);
final Pattern splitPattern1 = Pattern.compile("\n");
final Pattern splitPattern2 = Pattern.compile(",");
final String[] lines = splitPattern1.split(s);
final List<String> varOrders = Lists.newArrayList();
final List<String> varOrderPos = Lists.newArrayList();
int orderNum = 0;
final int projSizeSq = projList.size() * projList.size();
for (String t : lines) {
if (orderNum > projSizeSq) {
break;
}
String[] order = null;
if (t.startsWith("#prefix")) {
t = t.substring(7).trim();
order = splitPattern2.split(t, projList.size());
}
String tempVarOrder = "";
String tempVarOrderPos = "";
if (order != null) {
for (final String u : order) {
if (tempVarOrder.length() == 0) {
tempVarOrder = u.trim();
} else {
tempVarOrder = tempVarOrder + ";" + u.trim();
}
final int pos = projList.indexOf(u.trim());
if (pos < 0) {
throw new IllegalArgumentException("Invalid variable order!");
} else {
if (tempVarOrderPos.length() == 0) {
tempVarOrderPos = tempVarOrderPos + pos;
} else {
tempVarOrderPos = tempVarOrderPos + ";" + pos;
}
}
}
varOrders.add(tempVarOrder);
varOrderPos.add(tempVarOrderPos);
}
if (tempVarOrder.length() > 0) {
orderNum++;
}
}
if (orderNum == 0) {
varOrders.add(projElems);
String tempVarPos = "";
for (int i = 0; i < projList.size(); i++) {
if (i == 0) {
tempVarPos = Integer.toString(0);
} else {
tempVarPos = tempVarPos + ";" + i;
}
}
varOrderPos.add(tempVarPos);
}
final String[] vOrders = varOrders.toArray(new String[varOrders.size()]);
final String[] vOrderPos = varOrderPos.toArray(new String[varOrderPos.size()]);
conf.setStrings("varOrders", vOrders);
conf.setStrings("varOrderPos", vOrderPos);
}
Aggregations