1616
1717import org .eclipse .rdf4j .query .BindingSet ;
1818import org .eclipse .rdf4j .query .Dataset ;
19+ import org .eclipse .rdf4j .query .algebra .Extension ;
1920import org .eclipse .rdf4j .query .algebra .Join ;
2021import org .eclipse .rdf4j .query .algebra .LeftJoin ;
2122import org .eclipse .rdf4j .query .algebra .StatementPattern ;
@@ -88,9 +89,18 @@ public void meet(Join node) {
8889 // Reorder the (recursive) join arguments to a more optimal sequence
8990 List <TupleExpr > orderedJoinArgs = new ArrayList <TupleExpr >(joinArgs .size ());
9091
92+ // Reorder the subselects and extensions to a more optimal sequence
93+ List <TupleExpr > priorityArgs = new ArrayList <TupleExpr >(joinArgs .size ());
94+
9195 // first get all subselects and order them
9296 List <TupleExpr > orderedSubselects = reorderSubselects (getSubSelects (joinArgs ));
9397 joinArgs .removeAll (orderedSubselects );
98+ priorityArgs .addAll (orderedSubselects );
99+
100+ // second get all extensions (BIND clause)
101+ List <Extension > orderedExtensions = getExtensions (joinArgs );
102+ joinArgs .removeAll (orderedExtensions );
103+ priorityArgs .addAll (orderedExtensions );
94104
95105 // We order all remaining join arguments based on cardinality and
96106 // variable frequency statistics
@@ -131,11 +141,11 @@ public void meet(Join node) {
131141 }
132142
133143 // Build new join hierarchy
134- TupleExpr subselectJoins = null ;
135- if (orderedSubselects .size () > 0 ) {
136- subselectJoins = orderedSubselects .get (0 );
137- for (int i = 1 ; i < orderedSubselects .size (); i ++) {
138- subselectJoins = new Join (subselectJoins , orderedSubselects .get (i ));
144+ TupleExpr priorityJoins = null ;
145+ if (priorityArgs .size () > 0 ) {
146+ priorityJoins = priorityArgs .get (0 );
147+ for (int i = 1 ; i < priorityArgs .size (); i ++) {
148+ priorityJoins = new Join (priorityJoins , priorityArgs .get (i ));
139149 }
140150 }
141151
@@ -149,17 +159,17 @@ public void meet(Join node) {
149159 replacement = new Join (orderedJoinArgs .get (i ), replacement );
150160 }
151161
152- if (subselectJoins != null ) {
153- replacement = new Join (subselectJoins , replacement );
162+ if (priorityJoins != null ) {
163+ replacement = new Join (priorityJoins , replacement );
154164 }
155165
156166 // Replace old join hierarchy
157167 node .replaceWith (replacement );
158168
159169 }
160170 else {
161- // only subselect joins involved in this query.
162- node .replaceWith (subselectJoins );
171+ // only subselect/priority joins involved in this query.
172+ node .replaceWith (priorityJoins );
163173 }
164174 }
165175 finally {
@@ -198,6 +208,16 @@ protected <M extends Map<Var, Integer>> M getVarFreqMap(List<Var> varList, M var
198208 return varFreqMap ;
199209 }
200210
211+ protected List <Extension > getExtensions (List <TupleExpr > expressions ) {
212+ List <Extension > extensions = new ArrayList <Extension >();
213+ for (TupleExpr expr : expressions ) {
214+ if (expr instanceof Extension ) {
215+ extensions .add ((Extension )expr );
216+ }
217+ }
218+ return extensions ;
219+ }
220+
201221 protected List <TupleExpr > getSubSelects (List <TupleExpr > expressions ) {
202222 List <TupleExpr > subselects = new ArrayList <TupleExpr >();
203223
0 commit comments