Hi Andy, thanks for your reply! Another similar way is to create a SELECT query, use > Query.getQueryPattern and then use Query.setQueryPattern on CONSTRUCT > query you want or on a UpdateModify. The query structure isn't modified > during execution so sharing is safe. >
After thinking a bit about your response, and digging some more into the code, I've realized a couple of probably unrelated things: 1. I think some of the inconsistency is due to the fact that OpAsQuery elevates certain operators into the SELECT binding. For example: `extend` will often translate to a BIND, but if wrapped in a `project` operator, it gets elevated into the SELECT binding instead. For such a query, my simple approach of passing WhereHandlers would drop the select bindings, which would lead to problems in the CONSTRUCT. Without additional intervention, I think using Query.getQueryPattern and UpdateModify would run into the same edge cases. I also suspect there's more than just SELECT bindings to worry about. Unfortunately I don't understand the heuristics of how the algebra is partitioned between the where clause, and the other query components. Any ideas for a robust way to handle this? 2. Using Algebra.compile, I can see that the UpdateBuilder + WhereHandler approach (A) that I was using does not preserve `group` operations. In contrast, the UpdateModify + Query.getQueryPattern approach (B) you recommended does: Specifically the following expression comes back the same under approach B: (join (filter (< ?classage "40"^^<http://www.w3.org/2001/XMLSchema#long>) (group (?class) ((?classage (avg ?age))) (bgp (triple ?e <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?class) (triple ?e <http://xmlns.com/foaf/0.1/age> ?age) ))) (bgp (triple ?e <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?class) (triple ?e <http://xmlns.com/foaf/0.1/age> ?age) )) But approach A returns: (join (filter (< ?classage "40"^^<http://www.w3.org/2001/XMLSchema#long>) (group (?class) ((?.0 (avg ?age))) <====== CHANGED VAR: ?.0 (bgp (triple ?e <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?class) (triple ?e <http://xmlns.com/foaf/0.1/age> ?age) ))) (bgp (triple ?e <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?class) (triple ?e <http://xmlns.com/foaf/0.1/age> ?age) )) If that aggregate var is not preserved, then the parent filter will fail to unify. I have no idea if this is a bug.
