diff --git a/Dockerfile b/Dockerfile index 234eb5302..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:9e1af6bb8eead0d0f883207b140b0d973e287c263011fc966ffe041c295f9212 /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 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", 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..689bc9abb 100644 --- a/tests/e2e/Adapter/Scopes/AggregationTests.php +++ b/tests/e2e/Adapter/Scopes/AggregationTests.php @@ -901,6 +901,41 @@ 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); + + $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'), + Query::groupBy(['category']), + Query::having($having), + Query::orderAsc('category'), + ]), 'category'); + } + public function testInnerJoinBasic(): void { $database = static::getDatabase(); @@ -1967,7 +2002,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])])],