Problem/Motivation
Drupal\Core\Entity\Query\Sql\Query::__clone() re-parents and resets the regular $condition object, but leaves $conditionAggregate pointing at the same ConditionAggregate instance as the original query. As a result, cloning an aggregate query produces a clone that shares its aggregate condition tree with the source: calling conditionAggregate() on either query changes both, and the still-bound query reference means executing the clone can build SQL against the wrong query object.
Proposed resolution
Clone $conditionAggregate and set the query on the copy (mirroring the existing handling of $condition), so a cloned aggregate query gets its own isolated aggregate condition tree.
Remaining tasks
Review.
User interface changes
Nope.
Introduced terminology
N/A
API changes
Nope.
Data model changes
Nope.
Release notes snippet
N/A
Issue fork drupal-3593233
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3593233-11.4.x
changes, plain diff MR !16287
- 3593233-cloning-an-aggregate
changes, plain diff MR !15960
Comments
Comment #3
amateescu commentedFound this while reviewing #2875033: Optimize joins and table selection in SQL entity query implementation.
Comment #4
catchVisually looks great. There was one random functional test failure but on re-running it passed. Can't think of anything else.
Comment #5
amateescu commentedRan the test-only job as well to prove the failure :)
Comment #6
godotislateQuestions on the MR.
Comment #7
amateescu commentedThanks for reviewing!
I fixed those points by adding both
setQuery()andgetQuery()toConditionAggregateInterface, which should be fine because they're both implemented inConditionFundamentals.If we ever decide in a followup that
ConditionAggregateInterfaceshould extendConditionInterface(not sure whether we actually want that tbh) we can remove them, but until then I think this is the easiest way forward here.And while doing the change in
QueryBase, I discovered that this is actually the sister issue for something I found 14 years ago :)) #1829942: Cloning an entity query doesn't clone its conditionsComment #8
godotislateI think this is fine, and we should have a CR for the interface change.
Comment #9
amitgoyal commentedCreated a Change Record for the ConditionAggregateInterface API addition in this MR: https://www.drupal.org/node/3603991
With all MR threads resolved, CI passing, and the CR in place, this should be ready for RTBC.
Comment #10
godotislate@amitgoyal can you disclose whether you used AI to help you compose the CR?
Comment #11
amitgoyal commented@godotislate - yes, I have used AI to compose the CR but reviewed it myself before submitting it.
Comment #12
alexpottI've fixed up the CR by formatting it properly and removing extraneous info.
Comment #13
alexpottDiscussed with @catch we agreed this belongs in 11.5.x too.
Committed and pushed 3e4104d1b00 to main and fb8ea960b77 to 11.x. Thanks!
Comment #17
amateescu commentedAs a bug fix, this should go into 11.4.x as well. It's actually a followup for #2875033: Optimize joins and table selection in SQL entity query implementation, because it fixes something that was missed there :)
Comment #18
catchIt's a bugfix but it adds new methods to an interface which we'd normally only do in a minor release.
Comment #19
amateescu commentedRight, it adds the same methods that were added by #2875033: Optimize joins and table selection in SQL entity query implementation on 11.4.x
Comment #20
godotislateI think we could backport it if we left out the interface change and checked that the method exists?
Comment #23
godotislateMR 16287 for 11.4.x per #20.
Comment #24
smustgrave commentedSeems like a good backport.
Comment #25
catchCommitted/pushed to 11.4.x, thanks!