Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
16 changes: 8 additions & 8 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 33 additions & 15 deletions src/Database/Validator/Query/Having.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ 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;
}
}

return true;
}

private function isValidCondition(Query $condition, Filter $filter, bool $nested): bool
private function isValidCondition(Query $condition, Filter $filter): bool
{
$method = $condition->getMethod();

Expand All @@ -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);
}

Expand Down Expand Up @@ -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)) {
Expand Down
36 changes: 35 additions & 1 deletion tests/e2e/Adapter/Scopes/AggregationTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<Query> $having
* @return array<mixed>
*/
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();
Expand Down Expand Up @@ -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)])],
];
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/QueryShapeDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')],
Expand Down
20 changes: 17 additions & 3 deletions tests/unit/Validator/QueryShapeHavingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])])]],
];
Expand Down Expand Up @@ -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])])],
Expand Down
Loading