Skip to content

Commit 8e5a063

Browse files
authored
Merge pull request #750 from bserdar/749
Fix #749: do not set query limits if there are queries for non-root n…
2 parents 5c90509 + 2ed1beb commit 8e5a063

File tree

6 files changed

+79
-15
lines changed

6 files changed

+79
-15
lines changed

crud/src/main/java/com/redhat/lightblue/assoc/ep/ExecutionPlan.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,17 @@ public ExecutionPlan(QueryExpression requestQuery,
114114
// This is set to true when we identify the step that sets matchcount
115115
boolean matchCountSet=false;
116116

117+
118+
// This is set if there are queries associated with the nodes
119+
// that are not the root node of the retrieval plan
120+
boolean queries_in_non_root_nodes=false;
121+
for(QueryPlanNode node:retrievalQueryPlan.getAllNodes()) {
122+
if(node.getSources().length!=0&& // non-root
123+
!node.getData().getConjuncts().isEmpty()) {
124+
queries_in_non_root_nodes=true;
125+
break;
126+
}
127+
}
117128
// First, create execution blocks for every node in the search and
118129
// retrieval plans. We keep a map of query plan nodes to execution
119130
// blocks to keep query plan immutable.
@@ -197,7 +208,7 @@ public ExecutionPlan(QueryExpression requestQuery,
197208
if (block == rootEntityInQueryPlan) {
198209
// This is the block for the root entity
199210
if (rootIsTheOnlySource) {
200-
if (unassigned.isEmpty()) {
211+
if (unassigned.isEmpty()&&!queries_in_non_root_nodes) {
201212
// Root is the only source, and there are no unassigned clauses
202213
// We can sort/limit here
203214
search.setLimit(from, to);
@@ -230,7 +241,7 @@ public ExecutionPlan(QueryExpression requestQuery,
230241
last = new Source<>(new Skip(block, from.intValue(), last));
231242
}
232243
if (to != null) {
233-
last = new Source<>(new Limit(block, to.intValue() - from.intValue() + 1, last));
244+
last = new Source<>(new Limit(block, to.intValue() - (from==null?0:from.intValue()) + 1, last));
234245
}
235246
}
236247
block.setResultStep(last);
@@ -258,17 +269,6 @@ public ExecutionPlan(QueryExpression requestQuery,
258269
// Done with the search plan. Now we build the execution plan for retrieval
259270
LOGGER.debug("Building execution plan from retrieval query plan:{}", retrievalQueryPlan);
260271
List<Conjunct> unassigned = retrievalQueryPlan.getUnassignedClauses();
261-
262-
// This is set if there are queries associated with the nodes
263-
// that are not the root node of the retrieval plan
264-
boolean queries_in_non_root_nodes=false;
265-
for(QueryPlanNode node:retrievalQueryPlan.getAllNodes()) {
266-
if(node.getSources().length!=0&& // non-root
267-
!node.getData().getConjuncts().isEmpty()) {
268-
queries_in_non_root_nodes=true;
269-
break;
270-
}
271-
}
272272
if(qfi==null) {
273273
qfi = getAllQueryFieldInfo(retrievalQueryPlan);
274274
} else {
@@ -329,7 +329,7 @@ public ExecutionPlan(QueryExpression requestQuery,
329329
resultStep = new Skip(block, from.intValue(), new Source<>(resultStep));
330330
}
331331
if (to != null) {
332-
resultStep = new Limit(block, to.intValue() - from.intValue() + 1, new Source<>(resultStep));
332+
resultStep = new Limit(block, to.intValue() - (from==null?0:from.intValue()) + 1, new Source<>(resultStep));
333333
}
334334
}
335335
resultStep = new Project(block, new Source<>(resultStep), requestProjection);

crud/src/test/java/com/redhat/lightblue/mediator/CompositeFinderTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,4 +1088,17 @@ public void test_self_ref_with_parents() throws Exception {
10881088
Assert.assertEquals(1,response.getEntityData().size());
10891089
Assert.assertTrue(response.getEntityData().get(0).get("vulnerabilities").get(0).get("packages").get(0).get("fixed_by_images").size()==1);
10901090
}
1091+
1092+
@Test
1093+
public void limited_retrieval_with_queries_on_sub() throws Exception {
1094+
FindRequest fr=new FindRequest();
1095+
fr.setQuery(query("{'$and':[{'array':'repositories','elemMatch':{'field':'repositories.*._id','op':'>','rvalue':'1'}},"+
1096+
"{'array':'repositories','elemMatch':{'field':'published','op':'=','rvalue':true}}]}"));
1097+
fr.setProjection(projection("[{'field':'*','recursive':true},{'field':'repositories.*.repositories.*.vendors','recursive':true}]"));
1098+
fr.setEntityVersion(new EntityVersion("containerImage","1.0.0"));
1099+
fr.setTo(2l);
1100+
Response response=mediator.find(fr);
1101+
System.out.println(response.getEntityData());
1102+
Assert.assertEquals(3,response.getEntityData().size());
1103+
}
10911104
}

crud/src/test/resources/composite/containerImage.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@
55
"backend":"mongo",
66
"datasource": "mongodata",
77
"collection": "containerImage"
8-
}
8+
},
9+
"indexes":[
10+
{
11+
"fields":[
12+
{"field":"repositories.*.published","dir":"$asc"}
13+
]
14+
}
15+
]
916
},
1017
"schema" : {
1118
"name" : "containerImage",

crud/src/test/resources/composite/containerRepository.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@
2828
"repository": { "type": "string" },
2929
"registry": { "type": "string" },
3030
"vendorLabel": { "type":"string" },
31+
"vendors":{
32+
"type":"reference",
33+
"entity":"vendors",
34+
"versionValue":"1.0.0",
35+
"query":{
36+
"field":"label",
37+
"op":"=",
38+
"rfield":"$parent.vendorLabel"
39+
}
40+
},
3141
"images": {
3242
"type":"reference",
3343
"entity":"containerImage",
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"entityInfo" : {
3+
"name": "vendors",
4+
"datastore": {
5+
"backend":"mongo",
6+
"datasource": "mongodata",
7+
"collection": "vendors"
8+
}
9+
},
10+
"schema" : {
11+
"name" : "vendors",
12+
"version": {
13+
"value": "1.0.0",
14+
"changelog": "Test"
15+
},
16+
"status": {
17+
"value": "active"
18+
},
19+
"access" : {
20+
"insert": ["anyone"],
21+
"find":["anyone"],
22+
"update":["anyone"],
23+
"delete":["anyone"]
24+
},
25+
"fields": {
26+
"_id": {"type": "string", "constraints":{ "identity":1 } },
27+
"objectType": {"type": "string"},
28+
"label": { "type": "string" }
29+
}
30+
}
31+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[
2+
{"_id":"1","objectType":"vendors","label":"Red Hat" }
3+
]

0 commit comments

Comments
 (0)