-
Notifications
You must be signed in to change notification settings - Fork 1.4k
PHPLARA-268 Fix AND/OR precedence in compileWheres() #3567
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
base: 5.x
Are you sure you want to change the base?
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 |
|---|---|---|
|
|
@@ -238,6 +238,142 @@ public static function provideQueryBuilderToMql(): iterable | |
| ->orWhere('email', '=', 'foo'), | ||
| ]; | ||
|
|
||
| // "a and b or c" must mean "(a and b) or c", matching SQL's AND-before-OR precedence. | ||
| yield 'where where orWhere' => [ | ||
| [ | ||
| 'find' => [ | ||
| [ | ||
| '$or' => [ | ||
| [ | ||
| '$and' => [ | ||
| ['age' => 1], | ||
| ['name' => 'bar'], | ||
| ], | ||
| ], | ||
| ['email' => 'foo'], | ||
| ], | ||
| ], | ||
| [], // options | ||
| ], | ||
| ], | ||
| fn (Builder $builder) => $builder | ||
| ->where('age', '=', 1) | ||
| ->where('name', '=', 'bar') | ||
| ->orWhere('email', '=', 'foo'), | ||
| ]; | ||
|
|
||
| // "a or b and c" must mean "a or (b and c)", matching SQL's AND-before-OR precedence. | ||
| yield 'where orWhere where' => [ | ||
|
Member
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. ->where('age', 1)->orWhere('email', 'foo')->where('name', 'bar')Before this PR: {"$or":[{"age":1},{"email":"foo"}],"$and":[{"name":"bar"}]}MongoDB ANDs the sibling top-level keys, so this was evaluated as With this PR: {"$or":[{"age":1},{"$and":[{"email":"foo"},{"name":"bar"}]}]}which is The symmetric case. Here the old query was too restrictive instead of too permissive: the trailing |
||
| [ | ||
| 'find' => [ | ||
| [ | ||
| '$or' => [ | ||
| ['age' => 1], | ||
| [ | ||
| '$and' => [ | ||
| ['email' => 'foo'], | ||
| ['name' => 'bar'], | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| [], // options | ||
| ], | ||
| ], | ||
| fn (Builder $builder) => $builder | ||
| ->where('age', '=', 1) | ||
| ->orWhere('email', '=', 'foo') | ||
| ->where('name', '=', 'bar'), | ||
| ]; | ||
|
|
||
| // "a or b and c or d and e" must mean "a or (b and c) or (d and e)". | ||
| yield 'alternating where and orWhere' => [ | ||
|
Member
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. ->where('a', 1)->orWhere('b', 2)->where('c', 3)->orWhere('d', 4)->where('e', 5)Before this PR: {"$or":[{"a":1},{"b":2},{"d":4}],"$and":[{"c":3},{"e":5}]}MongoDB ANDs the sibling top-level keys, so this was evaluated as With this PR: {"$or":[{"a":1},{"$and":[{"b":2},{"c":3}]},{"$and":[{"d":4},{"e":5}]}]}which is This is the case that shows the old bucketing most clearly: every |
||
| [ | ||
| 'find' => [ | ||
| [ | ||
| '$or' => [ | ||
| ['a' => 1], | ||
| ['$and' => [['b' => 2], ['c' => 3]]], | ||
| ['$and' => [['d' => 4], ['e' => 5]]], | ||
| ], | ||
| ], | ||
| [], // options | ||
| ], | ||
| ], | ||
| fn (Builder $builder) => $builder | ||
| ->where('a', 1) | ||
| ->orWhere('b', 2) | ||
| ->where('c', 3) | ||
| ->orWhere('d', 4) | ||
| ->where('e', 5), | ||
| ]; | ||
|
|
||
| // A leading "orWhere" behaves like a "where". | ||
| yield 'leading orWhere then where' => [ | ||
|
Member
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. ->orWhere('a', 1)->where('b', 2)Before this PR: {"$or":[{"a":1}],"$and":[{"b":2}]}MongoDB ANDs the sibling top-level keys, so this was evaluated as With this PR: {"$and":[{"a":1},{"b":2}]}which is Semantics were already correct here, a leading |
||
| [ | ||
| 'find' => [ | ||
| [ | ||
| '$and' => [ | ||
| ['a' => 1], | ||
| ['b' => 2], | ||
| ], | ||
| ], | ||
| [], // options | ||
| ], | ||
| ], | ||
| fn (Builder $builder) => $builder | ||
| ->orWhere('a', 1) | ||
| ->where('b', 2), | ||
| ]; | ||
|
|
||
| // "a or not b and c" must mean "a or (not b and c)". | ||
| yield 'where orWhereNot where' => [ | ||
|
Member
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. ->where('a', 1)->orWhereNot('b', 2)->where('c', 3)Before this PR: {"$or":[{"a":1},{"$nor":[{"b":2}]}],"$and":[{"c":3}]}MongoDB ANDs the sibling top-level keys, so this was evaluated as With this PR: {"$or":[{"a":1},{"$and":[{"$nor":[{"b":2}]},{"c":3}]}]}which is Checks that the |
||
| [ | ||
| 'find' => [ | ||
| [ | ||
| '$or' => [ | ||
| ['a' => 1], | ||
| [ | ||
| '$and' => [ | ||
| ['$nor' => [['b' => 2]]], | ||
| ['c' => 3], | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| [], // options | ||
| ], | ||
| ], | ||
| fn (Builder $builder) => $builder | ||
| ->where('a', 1) | ||
| ->orWhereNot('b', 2) | ||
| ->where('c', 3), | ||
| ]; | ||
|
|
||
| // "a or (b or c) and d" must mean "a or ((b or c) and d)". | ||
| yield 'where orWhere nested where' => [ | ||
|
Member
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. ->where('a', 1)->orWhere(fn ($q) => $q->where('b', 2)->orWhere('c', 3))->where('d', 4)Before this PR: {"$or":[{"a":1},{"$or":[{"b":2},{"c":3}]}],"$and":[{"d":4}]}MongoDB ANDs the sibling top-level keys, so this was evaluated as With this PR: {"$or":[{"a":1},{"$and":[{"$or":[{"b":2},{"c":3}]},{"d":4}]}]}which is Nested groups built by a closure compose with the new grouping, and an explicit closure keeps behaving as its own unit. This is the pattern to recommend to anyone who wants the old grouping back. |
||
| [ | ||
| 'find' => [ | ||
| [ | ||
| '$or' => [ | ||
| ['a' => 1], | ||
| [ | ||
| '$and' => [ | ||
| ['$or' => [['b' => 2], ['c' => 3]]], | ||
| ['d' => 4], | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| [], // options | ||
| ], | ||
| ], | ||
| fn (Builder $builder) => $builder | ||
| ->where('a', 1) | ||
| ->orWhere(fn (Builder $query) => $query->where('b', 2)->orWhere('c', 3)) | ||
| ->where('d', 4), | ||
| ]; | ||
|
|
||
| /** @see DatabaseQueryBuilderTest::testBasicOrWhereNot() */ | ||
| yield 'orWhereNot' => [ | ||
| [ | ||
|
|
||
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.
Before this PR:
{"$and":[{"age":1},{"name":"bar"}],"$or":[{"email":"foo"}]}MongoDB ANDs the sibling top-level keys, so this was evaluated as
age AND name AND email.With this PR:
{"$or":[{"$and":[{"age":1},{"name":"bar"}]},{"email":"foo"}]}which is
(age AND name) OR email, the meaning of the SQL that Laravel generates for the same chain.Documents matching only on
emailwere dropped.