Skip to content

Commit

Permalink
cs: Fix code style of different storage classes. Fixed array aggregation
Browse files Browse the repository at this point in the history
storage for different filters
  • Loading branch information
OliverSkroblin committed Feb 2, 2024
1 parent 4c5767b commit f768107
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 64 deletions.
13 changes: 10 additions & 3 deletions src/Array/ArrayStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,18 @@ public function aggregate(array $aggregations, Criteria $criteria, StorageContex

$result = [];
foreach ($aggregations as $aggregation) {
if ($aggregation->filter) {
$filtered = array_filter($filtered, fn(Document $document): bool => $this->match($document, $aggregation->filter, $context));
// reset to root filter to not apply aggregations specific filters
$documents = $filtered;

if ($aggregation->filters) {
$documents = array_filter($documents, fn(Document $document): bool => $this->match($document, $aggregation->filters, $context));
}

$value = $this->parseAggregation($filtered, $aggregation, $context);
$value = $this->parseAggregation(
filtered: $documents,
aggregation: $aggregation,
context: $context
);

$result[$aggregation->name] = $this->caster->cast(
schema: $this->schema,
Expand Down
9 changes: 5 additions & 4 deletions src/Common/Aggregation/AggregationCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Shopware\Storage\Common\Aggregation\Type\Aggregation;
use Shopware\Storage\Common\Aggregation\Type\Count;
use Shopware\Storage\Common\Aggregation\Type\Distinct;
use Shopware\Storage\Common\Schema\FieldType;
use Shopware\Storage\Common\Schema\Schema;
use Shopware\Storage\Common\Schema\SchemaUtil;

Expand All @@ -15,13 +16,13 @@ public function cast(Schema $schema, Aggregation $aggregation, mixed $data): mix
$type = SchemaUtil::type(schema: $schema, accessor: $aggregation->field);

switch ($type) {
case 'integer':
case FieldType::INT:
$caster = fn($value) => (int) $value;
break;
case 'float':
case FieldType::FLOAT:
$caster = fn($value) => round((float) $value, 6);
break;
case 'bool':
case FieldType::BOOL:
$caster = function ($value) {
return match (true) {
$value === '1', $value === 'true' => true,
Expand All @@ -30,7 +31,7 @@ public function cast(Schema $schema, Aggregation $aggregation, mixed $data): mix
};
};
break;
case 'datetime':
case FieldType::DATETIME:
$caster = function ($value) {
return match (true) {
is_string($value) => (new \DateTimeImmutable($value))->format('Y-m-d H:i:s.v'),
Expand Down
4 changes: 2 additions & 2 deletions src/Common/Aggregation/Type/Aggregation.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
abstract class Aggregation
{
/**
* @param array<Filter> $filter
* @param array<Filter> $filters
*/
public function __construct(
public readonly string $name,
public readonly string $field,
public readonly array $filter = [],
public readonly array $filters = [],
) {}
}
6 changes: 3 additions & 3 deletions src/MongoDB/MongoDBStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,15 @@ private function parseAggregation(Aggregation $aggregation, StorageContext $cont
{
$parsed = [];

if (!empty($aggregation->filter)) {
$parsed[] = ['$match' => $this->parseFilters($aggregation->filter, $context)];
if (!empty($aggregation->filters)) {
$parsed[] = ['$match' => $this->parseFilters($aggregation->filters, $context)];
}

$property = SchemaUtil::property(accessor: $aggregation->field);

$type = SchemaUtil::type(schema: $this->schema, accessor: $property);

if ($type == FieldType::OBJECT_LIST) {
if (in_array($type, [FieldType::OBJECT_LIST, FieldType::LIST], true)) {
$parsed[] = ['$unwind' => '$' . $property];
}
$field = '$' . $aggregation->field;
Expand Down
3 changes: 1 addition & 2 deletions src/MySQL/MySQLStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private function loadAggregation(Aggregation $aggregation, Criteria $criteria, S

$filters = array_merge(
$criteria->filters,
$aggregation->filter
$aggregation->filters
);

//todo@skroblin support of post filters?
Expand Down Expand Up @@ -411,7 +411,6 @@ private function getAccessor(QueryBuilder $query, string $accessor, StorageConte

$root = SchemaUtil::property(accessor: $accessor);


$translated = SchemaUtil::translated(schema: $this->schema, accessor: $root);

$type = SchemaUtil::type(schema: $this->schema, accessor: $accessor);
Expand Down
4 changes: 2 additions & 2 deletions src/Opensearch/OpenSearchStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -685,13 +685,13 @@ private function handleAggregations(
->addAggregation($parsed);
}

if (empty($aggregation->filter)) {
if (empty($aggregation->filters)) {
$search->addAggregation($parsed);
continue;
}

$queries = $this->parseRootFilter(
filters: $aggregation->filter,
filters: $aggregation->filters,
context: $context
);

Expand Down
110 changes: 64 additions & 46 deletions tests/Common/AggregationStorageTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ public static function intCases(): \Generator
]),
['min' => 1],
];

yield 'Avg, int field' => [
new Avg(name: 'avg', field: 'intField'),
new Documents([
Expand All @@ -137,7 +136,6 @@ public static function intCases(): \Generator
]),
['avg' => 2],
];

yield 'Max, int field' => [
new Max(name: 'max', field: 'intField'),
new Documents([
Expand Down Expand Up @@ -173,7 +171,6 @@ public static function intCases(): \Generator
]
],
];

yield 'Distinct, int field' => [
new Distinct(name: 'distinct', field: 'intField'),
new Documents([
Expand Down Expand Up @@ -396,12 +393,68 @@ public static function listFloatCases(): \Generator

public static function listIntCases(): \Generator
{
yield 'Avg, list int field' => [];
yield 'Min, list int field' => [];
yield 'Max, list int field' => [];
yield 'Sum, list int field' => [];
yield 'Count, list int field' => [];
yield 'Distinct, list int field' => [];
yield 'Avg, list int field' => [
new Avg(name: 'avg', field: 'listField'),
new Documents([
self::document('key1', listField: [1, 2, 3]),
self::document('key2', listField: [2, 4, 3]),
self::document('key3', listField: [2, 5, 5]),
]),
['avg' => 3],
];
yield 'Min, list int field' => [
new Min(name: 'min', field: 'listField'),
new Documents([
self::document('key1', listField: [1, 2, 3]),
self::document('key2', listField: [2, 4, 3]),
self::document('key3', listField: [2, 5, 4]),
]),
['min' => 1],
];
yield 'Max, list int field' => [
new Max(name: 'max', field: 'listField'),
new Documents([
self::document('key1', listField: [1, 2, 3]),
self::document('key2', listField: [2, 4, 3]),
self::document('key3', listField: [2, 5, 4]),
]),
['max' => 5],
];
yield 'Sum, list int field' => [
new Sum(name: 'sum', field: 'listField'),
new Documents([
self::document('key1', listField: [1, 2, 3]),
self::document('key2', listField: [2, 4, 3]),
self::document('key3', listField: [2, 5, 4]),
]),
['sum' => 26],
];
yield 'Count, list int field' => [
new Count(name: 'count', field: 'listField'),
new Documents([
self::document('key1', listField: [1, 2, 3]),
self::document('key2', listField: [2, 4, 3]),
self::document('key3', listField: [2, 5, 3]),
]),
[
'count' => [
['key' => 1, 'count' => 1],
['key' => 2, 'count' => 3],
['key' => 3, 'count' => 3],
['key' => 4, 'count' => 1],
['key' => 5, 'count' => 1],
]
],
];
yield 'Distinct, list int field' => [
new Distinct(name: 'distinct', field: 'listField'),
new Documents([
self::document('key1', listField: [1, 2, 3]),
self::document('key2', listField: [2, 4, 3]),
self::document('key3', listField: [2, 5, 3]),
]),
['distinct' => [1, 2, 3, 4, 5]],
];
}

public static function listDateCases(): \Generator
Expand Down Expand Up @@ -645,15 +698,6 @@ public static function objectBoolCases(): \Generator

public static function objectDateCases(): \Generator
{
// yield 'Avg, object date field' => [
// new Avg(name: 'avg', field: 'objectField.dateField'),
// new Documents([
// self::document(key: 'key1', objectField: ['dateField' => '2021-01-01 00:00:00.000']),
// self::document(key: 'key2', objectField: ['dateField' => '2021-01-02 00:00:00.000']),
// self::document(key: 'key3', objectField: ['dateField' => '2021-01-03 00:00:00.000']),
// ]),
// ['avg' => '2021-01-02 00:00:00.000'],
// ];
yield 'Min, object date field' => [
new Min(name: 'min', field: 'objectField.dateField'),
new Documents([
Expand All @@ -672,15 +716,7 @@ public static function objectDateCases(): \Generator
]),
['max' => '2021-01-03 00:00:00.000'],
];
// yield 'Sum, object date field' => [
// new Sum(name: 'sum', field: 'objectField.dateField'),
// new Documents([
// self::document(key: 'key1', objectField: ['dateField' => '2021-01-01 00:00:00.000']),
// self::document(key: 'key2', objectField: ['dateField' => '2021-01-02 00:00:00.000']),
// self::document(key: 'key3', objectField: ['dateField' => '2021-01-03 00:00:00.000']),
// ]),
// ['sum' => '2021-01-06 00:00:00.000'],
// ];

yield 'Count, object date field' => [
new Count(name: 'count', field: 'objectField.dateField'),
new Documents([
Expand Down Expand Up @@ -936,15 +972,6 @@ public static function objectListBoolCases(): \Generator

public static function objectListDateCases(): \Generator
{
// yield 'Avg, object list date field' => [
// new Avg(name: 'avg', field: 'objectListField.dateField'),
// new Documents([
// self::document(key: 'key1', objectListField: [['dateField' => '2021-01-01 00:00:00.000'], ['dateField' => '2021-01-02 00:00:00.000']]),
// self::document(key: 'key2', objectListField: [['dateField' => '2021-01-03 00:00:00.000'], ['dateField' => '2021-01-04 00:00:00.000']]),
// self::document(key: 'key3', objectListField: [['dateField' => '2021-01-05 00:00:00.000'], ['dateField' => '2021-01-06 00:00:00.000']]),
// ]),
// ['avg' => '2021-01-03 00:00:00.000'],
// ];
yield 'Min, object list date field' => [
new Min(name: 'min', field: 'objectListField.dateField'),
new Documents([
Expand Down Expand Up @@ -1214,15 +1241,6 @@ public static function translatedBoolCases(): \Generator

public static function translatedDateCases(): \Generator
{
// yield 'Avg, translated date field' => [
// new Avg(name: 'avg', field: 'translatedDate'),
// new Documents([
// self::document(key: 'key1', translatedDate: ['en' => '2021-01-01 00:00:00.000', 'de' => '2021-01-02 00:00:00.000']),
// self::document(key: 'key2', translatedDate: ['en' => null, 'de' => '2021-01-03 00:00:00.000']),
// self::document(key: 'key3', translatedDate: ['de' => '2021-01-04 00:00:00.000']),
// ]),
// ['avg' => '2021-01-03 00:00:00.000'],
// ];
yield 'Min, translated date field' => [
new Min(name: 'min', field: 'translatedDate'),
new Documents([
Expand Down Expand Up @@ -1271,7 +1289,7 @@ public static function translatedDateCases(): \Generator
/**
* @param array<string, mixed> $expected
*/
#[DataProvider('dateCases')]
#[DataProvider('intCases')]
public function testDebug(
Aggregation $aggregations,
Documents $input,
Expand Down
2 changes: 1 addition & 1 deletion tests/Common/FilterStorageTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ abstract class FilterStorageTestBase extends TestCase

abstract public function getStorage(): FilterAware&Storage;

#[DataProvider('objectBoolCases')]
#[DataProvider('translatedIntCases')]
public function testDebug(
Documents $input,
Criteria $criteria,
Expand Down
2 changes: 1 addition & 1 deletion tests/Opensearch/OpenSearchStorageAggregationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private function createIndex(): void
->exists(['index' => $this->getSchema()->source]);

if ($exists) {
// $this->getClient()->indices()->delete(['index' => $this->getSchema()->source]);
//$this->getClient()->indices()->delete(['index' => $this->getSchema()->source]);
// delete all documents from index
$this->getClient()->deleteByQuery([
'index' => $this->getSchema()->source,
Expand Down

0 comments on commit f768107

Please sign in to comment.