diff --git a/lib/src/sparql/plan_builder.rs b/lib/src/sparql/plan_builder.rs
index d223f213..401c2be2 100644
--- a/lib/src/sparql/plan_builder.rs
+++ b/lib/src/sparql/plan_builder.rs
@@ -1128,9 +1128,8 @@ impl<'a> PlanBuilder<'a> {
} => {
let mut child_problematic_set = BTreeSet::new();
for (child_i, output_i) in lateral_mapping.iter() {
- if set.contains(output_i) {
- child_problematic_set.insert(*child_i);
- }
+ set.insert(*output_i);
+ child_problematic_set.insert(*child_i);
}
Self::add_left_join_problematic_variables(child, &mut child_problematic_set);
for (child_i, output_i) in mapping.iter() {
diff --git a/testsuite/oxigraph-tests/sparql/lateral/subselect_inside_optional.srx b/testsuite/oxigraph-tests/sparql/lateral/subselect_inside_optional.srx
index 11630eb9..ce409249 100644
--- a/testsuite/oxigraph-tests/sparql/lateral/subselect_inside_optional.srx
+++ b/testsuite/oxigraph-tests/sparql/lateral/subselect_inside_optional.srx
@@ -26,7 +26,7 @@
http://example.org/s2
- 21
+ 11
@@ -34,13 +34,24 @@
http://example.org/s2
- 22
+ 11
http://example.org/s3
+
+ 11
+
+
+
+
+ http://example.org/s3
+
+
+ 11
+
\ No newline at end of file