Fix parent reference conflicts in SPARQL aggregation query parsing #5493
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes an issue where SPARQL queries with aggregation functions would create inconsistent parent references in the query algebra tree, causing problems for query optimizers that need to replace nodes.
Problem
When parsing SPARQL queries with aggregation functions, the
TupleExprBuilder
was sharing the same aggregate operator instance between multiple parent nodes (GroupElem
andExtensionElem
). This caused parent reference conflicts that would be detected by theParentReferenceChecker
:The issue occurred in both simple aggregations like
COUNT(?s)
and complex expressions likeCOUNT(?s)/30
.Root Cause
In
TupleExprBuilder.visit(ASTSelect)
, when processing aggregations for implicit grouping:GroupElem
for grouping semanticsExtensionElem
for SELECT projectionsetParentNode()
on the same operator, creating conflicting parent referencesSolution
Fixed the issue by strategically cloning aggregate operators at three key points:
GroupElem
(line 572)ExtensionElem
in anonymous extensions (line 581)valueExpr
if it contains aggregates when creating the main extension (line 600)This ensures each parent node has its own copy of the aggregate operator, eliminating parent reference conflicts while preserving query semantics.
Testing
Fixes #[issue-number]
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.