From fcb8d55dd0fb494a18fb1495dc427c38c69696d3 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Tue, 1 Dec 2026 11:38:00 +1300 Subject: [PATCH 1/5] fix(query): allow and/or over aggregate aliases in having Having refused logical groups that compared aggregate aliases. Each nested condition is now validated as a having condition, so aliases keep their value and operator checks at any depth. Co-Authored-By: Claude Opus 5.5 --- src/Database/Validator/Query/Having.php | 48 +++++++++++++------ tests/e2e/Adapter/Scopes/AggregationTests.php | 32 ++++++++++++- tests/unit/QueryShapeDatabaseTest.php | 6 +-- tests/unit/Validator/QueryShapeHavingTest.php | 20 ++++++-- 4 files changed, 84 insertions(+), 22 deletions(-) diff --git a/src/Database/Validator/Query/Having.php b/src/Database/Validator/Query/Having.php index 04928451e..e490561fb 100644 --- a/src/Database/Validator/Query/Having.php +++ b/src/Database/Validator/Query/Having.php @@ -119,7 +119,7 @@ protected function isValidQuery(Query $query): bool } foreach ($queries as $condition) { - if (! $this->isValidCondition($condition, $this->filter, nested: false)) { + if (! $this->isValidCondition($condition, $this->filter)) { return false; } } @@ -127,7 +127,7 @@ protected function isValidQuery(Query $query): bool return true; } - private function isValidCondition(Query $condition, Filter $filter, bool $nested): bool + private function isValidCondition(Query $condition, Filter $filter): bool { $method = $condition->getMethod(); @@ -138,25 +138,13 @@ private function isValidCondition(Query $condition, Filter $filter, bool $nested } if ($method === Method::And || $method === Method::Or) { - foreach ($condition->getValues() as $child) { - if ($child instanceof Query && ! $this->isValidCondition($child, $filter, nested: true)) { - return false; - } - } - - return $this->isValidFilter($condition, $filter); + return $this->isValidLogicalCondition($condition, $filter); } $attribute = $condition->getAttribute(); $aggregation = $this->aggregations[$attribute] ?? null; if ($aggregation !== null) { - if ($nested) { - $this->message = 'Aggregate alias "'.$attribute.'" can only be compared at the top level of having'; - - return false; - } - return $this->isValidAliasCondition($condition, $aggregation, $filter); } @@ -229,6 +217,36 @@ private function isValidValueCount(Method $method, array $values): bool return true; } + /** + * Each condition of an and() or or() is checked as a having condition, so it may compare an + * aggregate alias, which the filter rules would look up as an attribute. + */ + private function isValidLogicalCondition(Query $condition, Filter $filter): bool + { + $method = $condition->getMethod(); + $children = $condition->getValues(); + + foreach ($children as $child) { + if (! $child instanceof Query) { + $this->message = \ucfirst($method->value).' queries can only contain filter queries'; + + return false; + } + + if (! $this->isValidCondition($child, $filter)) { + return false; + } + } + + if (\count($children) < 2) { + $this->message = \ucfirst($method->value).' queries require at least two queries'; + + return false; + } + + return true; + } + private function isValidFilter(Query $condition, Filter $filter): bool { if (! $filter->isValid($condition)) { diff --git a/tests/e2e/Adapter/Scopes/AggregationTests.php b/tests/e2e/Adapter/Scopes/AggregationTests.php index 0622a04fd..cec571744 100644 --- a/tests/e2e/Adapter/Scopes/AggregationTests.php +++ b/tests/e2e/Adapter/Scopes/AggregationTests.php @@ -901,6 +901,37 @@ public function testHavingWithCount(): void $database->deleteCollection('having_cnt'); } + public function testHavingComparesAliasesInsideLogicalGroups(): void + { + $database = static::getDatabase(); + if (! $database->getAdapter()->supports(Capability::Aggregations)) { + $this->expectNotToPerformAssertions(); + return; + } + + $collection = 'having_logical'; + $this->createProducts($database, $collection); + + $categories = fn (array $having): array => \array_map( + fn (Document $group): string => $group->getAttribute('category'), + $database->aggregate($collection, [ + Query::sum('price', 'total'), + Query::min('price', 'cheapest'), + Query::max('price', 'priciest'), + Query::groupBy(['category']), + Query::having($having), + Query::orderAsc('category'), + ]), + ); + + $this->assertSame(['books', 'electronics'], $categories([Query::or([Query::greaterThan('total', 1000), Query::lessThan('total', 100)])])); + $this->assertSame(['clothing', 'electronics'], $categories([Query::or([Query::greaterThan('total', 1000), Query::and([Query::equal('category', ['clothing']), Query::lessThan('total', 300)])])])); + $this->assertSame(['books', 'electronics'], $categories([Query::or([Query::lessThan('cheapest', 12), Query::greaterThan('priciest', 1000)])])); + $this->assertSame(['clothing'], $categories([Query::greaterThan('total', 100), Query::or([Query::lessThan('total', 1000), Query::equal('category', ['books'])])])); + + $database->deleteCollection($collection); + } + public function testInnerJoinBasic(): void { $database = static::getDatabase(); @@ -1967,7 +1998,6 @@ public function testHavingConditionsFollowTheFilterRules(): void 'Searching by attribute "name" requires a fulltext index.' => [Query::count('*', 'rows'), Query::groupBy(['name']), Query::having([Query::search('name', 'Laptop')])], 'Invalid query: Having can only compare an aggregate alias or a groupBy attribute: no_such_attribute' => [Query::sum('price', 'total'), Query::groupBy(['category']), Query::having([Query::equal('no_such_attribute', ['x'])])], 'Invalid query: Having can only compare an aggregate alias or a groupBy attribute: name' => [Query::sum('price', 'total'), Query::groupBy(['category']), Query::having([Query::equal('name', ['Laptop'])])], - 'Invalid query: Aggregate alias "total" can only be compared at the top level of having' => [Query::sum('price', 'total'), Query::groupBy(['category']), Query::having([Query::or([Query::greaterThan('total', 1000), Query::lessThan('total', 100)])])], 'Invalid query: Query value is invalid for aggregate alias "total"' => [Query::sum('price', 'total'), Query::groupBy(['category']), Query::having([Query::greaterThan('total', 'abc')])], 'Invalid query: Query value is invalid for attribute "name"' => [Query::max('name', 'last'), Query::groupBy(['category']), Query::having([Query::greaterThan('last', 5)])], ]; diff --git a/tests/unit/QueryShapeDatabaseTest.php b/tests/unit/QueryShapeDatabaseTest.php index e5081535b..e20ec9e95 100644 --- a/tests/unit/QueryShapeDatabaseTest.php +++ b/tests/unit/QueryShapeDatabaseTest.php @@ -104,9 +104,9 @@ public static function rejectedShapeProvider(): array [Query::sum('amount', 'total'), Query::groupBy(['status']), Query::having([Query::greaterThan('total', 'abc')])], 'Invalid query: Query value is invalid for aggregate alias "total"', ], - 'having an alias inside a logical group' => [ - [Query::sum('amount', 'total'), Query::groupBy(['status']), Query::having([Query::or([Query::greaterThan('total', 10), Query::lessThan('total', 1)])])], - 'Invalid query: Aggregate alias "total" can only be compared at the top level of having', + 'having an alias inside a logical group compared with text' => [ + [Query::sum('amount', 'total'), Query::groupBy(['status']), Query::having([Query::or([Query::greaterThan('total', 10), Query::lessThan('total', 'abc')])])], + 'Invalid query: Query value is invalid for aggregate alias "total"', ], 'sum of a string attribute' => [ [Query::sum('status', 'total')], diff --git a/tests/unit/Validator/QueryShapeHavingTest.php b/tests/unit/Validator/QueryShapeHavingTest.php index 7ce1a1544..b64751ecf 100644 --- a/tests/unit/Validator/QueryShapeHavingTest.php +++ b/tests/unit/Validator/QueryShapeHavingTest.php @@ -34,6 +34,8 @@ public static function validHavingProvider(): array 'maximum compared as its attribute' => [[Query::max('name', 'last'), Query::groupBy(['price']), Query::having([Query::greaterThan('last', 'm')])]], 'grouped attribute' => [[Query::sum('price', 'total'), Query::groupBy(['name']), Query::having([Query::equal('name', ['a', 'b'])])]], 'grouped attributes in a logical group' => [[Query::sum('price', 'total'), Query::groupBy(['name', 'price']), Query::having([Query::or([Query::equal('name', ['a']), Query::greaterThan('price', 5)])])]], + 'aliases in a logical group' => [[Query::sum('price', 'total'), Query::groupBy(['name']), Query::having([Query::or([Query::greaterThan('total', 10), Query::lessThan('total', 1)])])]], + 'aliases and grouped attributes in nested logical groups' => [[Query::count('*', 'rows'), Query::max('name', 'last'), Query::groupBy(['name']), Query::having([Query::or([Query::greaterThan('rows', 2), Query::and([Query::equal('name', ['a']), Query::lessThan('last', 'm')])])])]], 'several conditions' => [[Query::count('*', 'rows'), Query::sum('price', 'total'), Query::groupBy(['name']), Query::having([Query::greaterThanEqual('rows', 2), Query::lessThan('total', 500)])]], 'grouped attribute of a join' => [[Query::leftJoin('orders', 'ord', [Query::on('$id', 'customer')]), Query::count('*', 'rows'), Query::groupBy(['ord.status']), Query::having([Query::equal('ord.status', ['paid'])])]], ]; @@ -104,9 +106,21 @@ public static function invalidHavingProvider(): array [Query::sum('price', 'total'), Query::groupBy(['name']), Query::having([Query::startsWith('total', '1')])], 'Aggregate alias "total" cannot be compared with startsWith', ], - 'alias inside a logical group' => [ - [Query::sum('price', 'total'), Query::groupBy(['name']), Query::having([Query::or([Query::greaterThan('total', 10), Query::lessThan('total', 1)])])], - 'Aggregate alias "total" can only be compared at the top level of having', + 'alias inside a logical group compared with text' => [ + [Query::sum('price', 'total'), Query::groupBy(['name']), Query::having([Query::or([Query::greaterThan('total', 10), Query::lessThan('total', 'abc')])])], + 'Query value is invalid for aggregate alias "total"', + ], + 'logical group with one condition' => [ + [Query::sum('price', 'total'), Query::groupBy(['name']), Query::having([Query::or([Query::greaterThan('total', 10)])])], + 'Or queries require at least two queries', + ], + 'logical group of a value that is not a query' => [ + [Query::sum('price', 'total'), Query::groupBy(['name']), Query::having([new Query(Method::And, '', ['total', Query::greaterThan('total', 10)])])], + 'And queries can only contain filter queries', + ], + 'alias inside a logical group with an operator an aggregate cannot take' => [ + [Query::sum('price', 'total'), Query::groupBy(['name']), Query::having([Query::and([Query::equal('name', ['x']), Query::startsWith('total', '1')])])], + 'Aggregate alias "total" cannot be compared with startsWith', ], 'alias comparison with too many values' => [ [Query::sum('price', 'total'), Query::groupBy(['name']), Query::having([new Query(Method::GreaterThan, 'total', [1, 2])])], From cb73156f85515aad2dc590aa3c4d9b67d3a97216 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Tue, 1 Dec 2026 11:48:55 +1300 Subject: [PATCH 2/5] test(query): read aggregate rows as arrays in the having logical-group test aggregate() returns rows as arrays, not Documents, so the closure errored on every adapter and failed PHPStan. Co-Authored-By: Claude Opus 5.5 --- tests/e2e/Adapter/Scopes/AggregationTests.php | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/tests/e2e/Adapter/Scopes/AggregationTests.php b/tests/e2e/Adapter/Scopes/AggregationTests.php index cec571744..ee2ee630b 100644 --- a/tests/e2e/Adapter/Scopes/AggregationTests.php +++ b/tests/e2e/Adapter/Scopes/AggregationTests.php @@ -912,17 +912,15 @@ public function testHavingComparesAliasesInsideLogicalGroups(): void $collection = 'having_logical'; $this->createProducts($database, $collection); - $categories = fn (array $having): array => \array_map( - fn (Document $group): string => $group->getAttribute('category'), - $database->aggregate($collection, [ - Query::sum('price', 'total'), - Query::min('price', 'cheapest'), - Query::max('price', 'priciest'), - Query::groupBy(['category']), - Query::having($having), - Query::orderAsc('category'), - ]), - ); + /** @param array $having */ + $categories = fn (array $having): array => \array_column($database->aggregate($collection, [ + Query::sum('price', 'total'), + Query::min('price', 'cheapest'), + Query::max('price', 'priciest'), + Query::groupBy(['category']), + Query::having($having), + Query::orderAsc('category'), + ]), 'category'); $this->assertSame(['books', 'electronics'], $categories([Query::or([Query::greaterThan('total', 1000), Query::lessThan('total', 100)])])); $this->assertSame(['clothing', 'electronics'], $categories([Query::or([Query::greaterThan('total', 1000), Query::and([Query::equal('category', ['clothing']), Query::lessThan('total', 300)])])])); From 170f85f69b6777c5598422fac762173dc448b349 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Tue, 1 Dec 2026 12:01:02 +1300 Subject: [PATCH 3/5] test(query): type the having queries passed by the logical-group test PHPStan does not read a docblock on an arrow function, so the having queries reached Query::having() as a plain array. Co-Authored-By: Claude Opus 5.5 --- tests/e2e/Adapter/Scopes/AggregationTests.php | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/tests/e2e/Adapter/Scopes/AggregationTests.php b/tests/e2e/Adapter/Scopes/AggregationTests.php index ee2ee630b..689bc9abb 100644 --- a/tests/e2e/Adapter/Scopes/AggregationTests.php +++ b/tests/e2e/Adapter/Scopes/AggregationTests.php @@ -912,8 +912,21 @@ public function testHavingComparesAliasesInsideLogicalGroups(): void $collection = 'having_logical'; $this->createProducts($database, $collection); - /** @param array $having */ - $categories = fn (array $having): array => \array_column($database->aggregate($collection, [ + $this->assertSame(['books', 'electronics'], $this->havingCategories($database, $collection, [Query::or([Query::greaterThan('total', 1000), Query::lessThan('total', 100)])])); + $this->assertSame(['clothing', 'electronics'], $this->havingCategories($database, $collection, [Query::or([Query::greaterThan('total', 1000), Query::and([Query::equal('category', ['clothing']), Query::lessThan('total', 300)])])])); + $this->assertSame(['books', 'electronics'], $this->havingCategories($database, $collection, [Query::or([Query::lessThan('cheapest', 12), Query::greaterThan('priciest', 1000)])])); + $this->assertSame(['clothing'], $this->havingCategories($database, $collection, [Query::greaterThan('total', 100), Query::or([Query::lessThan('total', 1000), Query::equal('category', ['books'])])])); + + $database->deleteCollection($collection); + } + + /** + * @param array $having + * @return array + */ + private function havingCategories(Database $database, string $collection, array $having): array + { + return \array_column($database->aggregate($collection, [ Query::sum('price', 'total'), Query::min('price', 'cheapest'), Query::max('price', 'priciest'), @@ -921,13 +934,6 @@ public function testHavingComparesAliasesInsideLogicalGroups(): void Query::having($having), Query::orderAsc('category'), ]), 'category'); - - $this->assertSame(['books', 'electronics'], $categories([Query::or([Query::greaterThan('total', 1000), Query::lessThan('total', 100)])])); - $this->assertSame(['clothing', 'electronics'], $categories([Query::or([Query::greaterThan('total', 1000), Query::and([Query::equal('category', ['clothing']), Query::lessThan('total', 300)])])])); - $this->assertSame(['books', 'electronics'], $categories([Query::or([Query::lessThan('cheapest', 12), Query::greaterThan('priciest', 1000)])])); - $this->assertSame(['clothing'], $categories([Query::greaterThan('total', 100), Query::or([Query::lessThan('total', 1000), Query::equal('category', ['books'])])])); - - $database->deleteCollection($collection); } public function testInnerJoinBasic(): void From 1d9e70dba9674d428176337bca162755b7503e1b Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Tue, 1 Dec 2026 13:53:59 +1300 Subject: [PATCH 4/5] chore(deps): pin the query extension with nested having aliases The new image compiles aggregate aliases inside having and()/or() groups (appwrite/appwrite#16353). Co-Authored-By: Claude Opus 5.5 --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index c55181ccb..f24fafacf 100755 --- a/Dockerfile +++ b/Dockerfile @@ -121,7 +121,7 @@ RUN echo extension=pcov.so >> /usr/local/etc/php/conf.d/pcov.ini # but not loaded. The extension lane in .github/workflows/tests.yml loads it by adding # /usr/local/etc/php/utopia to PHP_INI_SCAN_DIR. utopia.libraries limits it to the query classes; # without it the extension would also replace utopia-php/validators. -COPY --from=appwrite/utopia-extension:main@sha256:9c15ea24ee5764b89902af6cc03b9f75cccab7fe25c748bd81767080b557d23b /utopia.so /usr/local/lib/php/utopia.so +COPY --from=appwrite/utopia-extension:main@sha256:f134e25a41550434c15afbd696a9a7c917662b3b2217d2839df6f7eafef88cf0 /utopia.so /usr/local/lib/php/utopia.so RUN apk add --no-cache libheif libstdc++ \ && mkdir /usr/local/etc/php/utopia \ && printf 'extension=/usr/local/lib/php/utopia.so\nutopia.libraries=query\n' > /usr/local/etc/php/utopia/utopia.ini From 39a230c57c022e2d5bc93bb25b0a649b10116529 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Tue, 1 Dec 2026 14:29:19 +1300 Subject: [PATCH 5/5] chore(deps): require utopia-php/query 0.8.1 for nested having aliases 0.8.1 compiles aggregate aliases inside having and()/or() groups on the PHP builder, matching the extension. Co-Authored-By: Claude Opus 5.5 --- composer.json | 2 +- composer.lock | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index 5e56a8cec..78de09739 100755 --- a/composer.json +++ b/composer.json @@ -54,7 +54,7 @@ "utopia-php/cache": "^8.0", "utopia-php/pools": "^2.0", "utopia-php/mongo": "^1.5.4", - "utopia-php/query": "^0.8.0 || ^0.9.0", + "utopia-php/query": "^0.8.1 || ^0.9.0", "utopia-php/async": "^0.2" }, "require-dev": { diff --git a/composer.lock b/composer.lock index 1de90433b..1bd6280b6 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "b171b6109cafaeb5236b1c7ad07960a6", + "content-hash": "edd24ce1023eaf651aa3ead92495a5d2", "packages": [ { "name": "brick/math", @@ -2292,16 +2292,16 @@ }, { "name": "utopia-php/query", - "version": "0.8.0", + "version": "0.8.1", "source": { "type": "git", "url": "https://github.com/utopia-php/query.git", - "reference": "aa6ea0415beae93abac10c36c3c644a9402e0c02" + "reference": "8ff24d006c864392a51b1ff14d39c964fe7de184" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/query/zipball/aa6ea0415beae93abac10c36c3c644a9402e0c02", - "reference": "aa6ea0415beae93abac10c36c3c644a9402e0c02", + "url": "https://api.github.com/repos/utopia-php/query/zipball/8ff24d006c864392a51b1ff14d39c964fe7de184", + "reference": "8ff24d006c864392a51b1ff14d39c964fe7de184", "shasum": "" }, "require": { @@ -2333,10 +2333,10 @@ ], "support": { "issues": "https://github.com/utopia-php/query/issues", - "source": "https://github.com/utopia-php/query/tree/0.8.0" + "source": "https://github.com/utopia-php/query/tree/0.8.1" }, - "time": "2026-11-24T16:14:05+00:00", - "published-time": "2026-11-24T16:17:02+00:00" + "time": "2026-12-01T01:16:26+00:00", + "published-time": "2026-12-01T01:16:38+00:00" }, { "name": "utopia-php/telemetry",