Skip to content

Commit f06d95f

Browse files
GH-4769 ArrayBindingSet access to non existing variables should not fail, but return as if
Signed-off-by: Jerven Bolleman <jerven.bolleman@sib.swiss>
1 parent 20114d4 commit f06d95f

1 file changed

Lines changed: 48 additions & 27 deletions

File tree

core/queryalgebra/evaluation/src/main/java/org/eclipse/rdf4j/query/algebra/evaluation/impl/ArrayBindingBasedQueryEvaluationContext.java

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public final class ArrayBindingBasedQueryEvaluationContext implements QueryEvalu
5555
private final BiConsumer<Value, MutableBindingSet>[] setBinding;
5656
private final BiConsumer<Value, MutableBindingSet>[] addBinding;
5757

58-
boolean initialized;
58+
private final boolean initialized;
5959

6060
ArrayBindingBasedQueryEvaluationContext(QueryEvaluationContext context, String[] allVariables) {
6161
assert new HashSet<>(Arrays.asList(allVariables)).size() == allVariables.length;
@@ -109,7 +109,12 @@ public Predicate<BindingSet> hasBinding(String variableName) {
109109

110110
assert variableName != null && !variableName.isEmpty();
111111
Function<ArrayBindingSet, Boolean> directHasVariable = defaultArrayBindingSet.getDirectHasBinding(variableName);
112-
return new HasBinding(variableName, directHasVariable);
112+
if (directHasVariable != null) {
113+
return new HasBinding(variableName, directHasVariable);
114+
} else {
115+
// If the variable is not in the default set, it can never be part of this array binding
116+
return (bs) -> false;
117+
}
113118
}
114119

115120
static private class HasBinding implements Predicate<BindingSet> {
@@ -147,16 +152,19 @@ public Function<BindingSet, Binding> getBinding(String variableName) {
147152

148153
Function<ArrayBindingSet, Binding> directAccessForVariable = defaultArrayBindingSet
149154
.getDirectGetBinding(variableName);
150-
return (bs) -> {
151-
if (bs.isEmpty()) {
152-
return null;
153-
}
154-
if (bs instanceof ArrayBindingSet) {
155-
return directAccessForVariable.apply((ArrayBindingSet) bs);
156-
} else {
157-
return bs.getBinding(variableName);
158-
}
159-
};
155+
if (directAccessForVariable != null) {
156+
return (bs) -> {
157+
if (bs instanceof ArrayBindingSet) {
158+
return directAccessForVariable.apply((ArrayBindingSet) bs);
159+
} else if (bs.isEmpty()) {
160+
return null;
161+
} else {
162+
return bs.getBinding(variableName);
163+
}
164+
};
165+
} else {
166+
return (bs) -> null;
167+
}
160168
}
161169

162170
@Override
@@ -171,7 +179,12 @@ public Function<BindingSet, Value> getValue(String variableName) {
171179

172180
Function<ArrayBindingSet, Value> directAccessForVariable = defaultArrayBindingSet
173181
.getDirectGetValue(variableName);
174-
return new ValueGetter(variableName, directAccessForVariable);
182+
if (directAccessForVariable != null) {
183+
return new ValueGetter(variableName, directAccessForVariable);
184+
} else {
185+
// If the variable is not in the default set, it can never be part of this array binding
186+
return (bs) -> null;
187+
}
175188
}
176189

177190
private static class ValueGetter implements Function<BindingSet, Value> {
@@ -210,13 +223,17 @@ public BiConsumer<Value, MutableBindingSet> setBinding(String variableName) {
210223

211224
BiConsumer<Value, ArrayBindingSet> directAccessForVariable = defaultArrayBindingSet
212225
.getDirectSetBinding(variableName);
213-
return (val, bs) -> {
214-
if (bs instanceof ArrayBindingSet) {
215-
directAccessForVariable.accept(val, (ArrayBindingSet) bs);
216-
} else {
217-
bs.setBinding(variableName, val);
218-
}
219-
};
226+
if (directAccessForVariable != null) {
227+
return (val, bs) -> {
228+
if (bs instanceof ArrayBindingSet) {
229+
directAccessForVariable.accept(val, (ArrayBindingSet) bs);
230+
} else {
231+
bs.setBinding(variableName, val);
232+
}
233+
};
234+
} else {
235+
return (val, bs) -> bs.setBinding(variableName, val);
236+
}
220237
}
221238

222239
@Override
@@ -231,13 +248,17 @@ public BiConsumer<Value, MutableBindingSet> addBinding(String variableName) {
231248

232249
BiConsumer<Value, ArrayBindingSet> wrapped = defaultArrayBindingSet
233250
.getDirectAddBinding(variableName);
234-
return (val, bs) -> {
235-
if (bs instanceof ArrayBindingSet) {
236-
wrapped.accept(val, (ArrayBindingSet) bs);
237-
} else {
238-
bs.addBinding(variableName, val);
239-
}
240-
};
251+
if (wrapped != null) {
252+
return (val, bs) -> {
253+
if (bs instanceof ArrayBindingSet) {
254+
wrapped.accept(val, (ArrayBindingSet) bs);
255+
} else {
256+
bs.addBinding(variableName, val);
257+
}
258+
};
259+
} else {
260+
return (val, bs) -> bs.addBinding(variableName, val);
261+
}
241262
}
242263

243264
@Override

0 commit comments

Comments
 (0)