-
Notifications
You must be signed in to change notification settings - Fork 58
Select queries skip filters on unselected attributes #941
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9347,6 +9347,14 @@ public function decode(Document $collection, Document $document, array $selectio | |
| $attributes[] = $attribute; | ||
| } | ||
|
|
||
| $hasRelationshipSelections = false; | ||
| foreach ($selections as $selection) { | ||
| if (\str_contains($selection, '.')) { | ||
| $hasRelationshipSelections = true; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| foreach ($attributes as $attribute) { | ||
| $key = $attribute['$id'] ?? ''; | ||
| $type = $attribute['type'] ?? ''; | ||
|
|
@@ -9379,34 +9387,26 @@ public function decode(Document $collection, Document $document, array $selectio | |
| $value = ($array) ? $value : [$value]; | ||
| $value = (is_null($value)) ? [] : $value; | ||
|
|
||
| foreach ($value as $index => $node) { | ||
| foreach (\array_reverse($filters) as $filter) { | ||
| $node = $this->decodeAttribute($filter, $node, $document, $key); | ||
| $selected = empty($selections) | ||
| || \in_array($key, $selections) | ||
| || \in_array('*', $selections); | ||
|
|
||
| if ($selected || $hasRelationshipSelections) { | ||
| foreach ($value as $index => $node) { | ||
| foreach (\array_reverse($filters) as $filter) { | ||
| $node = $this->decodeAttribute($filter, $node, $document, $key); | ||
| } | ||
| $value[$index] = $node; | ||
| } | ||
| $value[$index] = $node; | ||
| } | ||
|
|
||
| $filteredValue[$key] = ($array) ? $value : $value[0]; | ||
|
|
||
| if ( | ||
| empty($selections) | ||
| || \in_array($key, $selections) | ||
| || \in_array('*', $selections) | ||
| ) { | ||
| if ($selected) { | ||
|
Comment on lines
+9390
to
+9405
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚀 Performance & Scalability | 🟠 Major | ⚡ Quick win Skip filters for unrelated attributes during relationship selections. A dotted selection sets
📍 Affects 2 files
🤖 Prompt for AI Agents |
||
| $document->setAttribute($key, ($array) ? $value : $value[0]); | ||
| } | ||
| } | ||
|
|
||
| $hasRelationshipSelections = false; | ||
| if (!empty($selections)) { | ||
| foreach ($selections as $selection) { | ||
| if (\str_contains($selection, '.')) { | ||
| $hasRelationshipSelections = true; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if ($hasRelationshipSelections && !empty($selections) && !\in_array('*', $selections)) { | ||
| foreach ($collection->getAttribute('attributes', []) as $attribute) { | ||
| $key = $attribute['$id'] ?? ''; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a query selects a nested relationship field such as
children.name,$hasRelationshipSelectionsmakes every attribute pass this condition, so unrelated unselected query-producing filters still execute once per returned document and retain the fan-out cost this change is intended to remove.Knowledge Base Used: Database Core
Prompt To Fix With AI
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct that filters still run when a dotted selection is present, but that's the pre-existing behaviour preserved deliberately, not a leftover.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair point — if the intent is to preserve the pre-existing behaviour for relationship queries rather than optimize it, the current logic makes sense. Thanks for the clarification.