Closed shawnsmith closed 3 months ago
initialBinding()
is deprecated because it has far more inconsistent behaviour. It relies upon internal details of Jena's query engine so behaviour has changed )often subtly) over time as internals have changed. Also injecting parameters in this way means they don't have any effect of optimisation so could get worse performance.
Another alternative to substitution is to use parameterised strings which relies on textual replacement so may be another option
Funny that just today I stumbled over the same issue: In ExprTransformNodeElement I had added the following as a fix in my own copy of the class which just today I wanted to remove from my code base :smile:
@Override
public Expr transform(ExprAggregator eAgg) {
return eAgg.applyNodeTransform(nodeTransform);
}
I don't know if that's a bug or intended.
Me neither, that's why I was living with my own copy so far.
@shawnsmith - Thank you for the detailed report.
It's a bug.
@Aklakan's fix should do it - QueryTransformOps.transform
does not take scoping into account.
Thanks for the quick feedback!
Fwiw I tested this change today, and it's not complete:
@Override
public Expr transform(ExprAggregator eAgg) {
return eAgg.applyNodeTransform(nodeTransform);
}
The aggregators in the Query.aggregators
field also need to be transformed, but aren't. Something along these lines:
diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/syntax/syntaxtransform/QueryTransformOps.java b/jena-arq/src/main/java/org/apache/jena/sparql/syntax/syntaxtransform/QueryTransformOps.java
index b461a0a7e0..8d54370290 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/syntax/syntaxtransform/QueryTransformOps.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/syntax/syntaxtransform/QueryTransformOps.java
@@ -37,6 +37,7 @@ import org.apache.jena.shared.impl.PrefixMappingImpl;
import org.apache.jena.sparql.ARQException;
import org.apache.jena.sparql.core.*;
import org.apache.jena.sparql.expr.Expr;
+import org.apache.jena.sparql.expr.ExprAggregator;
import org.apache.jena.sparql.expr.ExprTransform;
import org.apache.jena.sparql.expr.ExprTransformer;
import org.apache.jena.sparql.expr.ExprVar;
@@ -78,6 +79,7 @@ public class QueryTransformOps {
if (q2.getOrderBy() != null)
mutateSortConditions(q2.getOrderBy(), exprTransform);
mutateQueryPattern(q2, transform, exprTransform);
+ transformAggregators(query, q2, exprTransform);
if ( query.isQueryResultStar() ) {
// Reset internal to only what now can be seen.
q2.resetResultVars();
@@ -111,6 +113,12 @@ public class QueryTransformOps {
}
}
+ private static void transformAggregators(Query query, Query newQuery, ExprTransform exprTransform) {
+ for (ExprAggregator aggregator : query.getAggregators()) {
+ newQuery.getAggregators().add((ExprAggregator) exprTransform.transform(aggregator));
+ }
+ }
+
// Do the result form part of the cloned query.
private static void mutateByQueryType(Query q2, ElementTransform transform, ExprTransform exprTransform) {
switch(q2.queryType()) {
@@ -300,9 +308,6 @@ public class QueryTransformOps {
for (String x : desc.getNamedGraphURIs())
newQuery.addNamedGraphURI(x);
}
-
- // Aggregators.
- newQuery.getAggregators().addAll(query.getAggregators());
}
@Override
@shawnsmith - thanks - I've added that to the PR.
Version
5.1.0
What happened?
https://github.com/apache/jena/issues/2028 deprecated
QueryExecutionDatasetBuilder.initialBinding()
and https://github.com/apache/jena/issues/2435 deprecated it for removal. But thesubstitution()
method isn't a perfect replacement forinitialBinding()
. I ran across an edge case whereinitialBinding()
appears to work correctly butsubstitution()
does not:Substitute
?a <- 123
in this query:and you get this, where
?a
in the aggregation expression is not defined:It looks like
substitution()
doesn't replace variable references in aggregation expressions. I don't know if that's a bug or intended. For now I'm sticking withinitialBinding()
, so I'm concerned that it's marked for removal.Relevant output and stacktrace
Are you interested in making a pull request?
None