diff --git a/core/lib/Drupal/Core/Entity/Query/Sql/ConditionAggregate.php b/core/lib/Drupal/Core/Entity/Query/Sql/ConditionAggregate.php index d0db922895c6..75784bf11c59 100644 --- a/core/lib/Drupal/Core/Entity/Query/Sql/ConditionAggregate.php +++ b/core/lib/Drupal/Core/Entity/Query/Sql/ConditionAggregate.php @@ -38,7 +38,8 @@ class ConditionAggregate extends ConditionAggregateBase { $condition_class::translateCondition($condition, $sql_query, $tables->isFieldCaseSensitive($condition['field'])); $function = $condition['function']; $placeholder = ':db_placeholder_' . $conditionContainer->nextPlaceholder(); - $conditionContainer->having("$function($field) {$condition['operator']} $placeholder", [$placeholder => $condition['value']]); + $sql_field_escaped = '[' . str_replace('.', '].[', $field) . ']'; + $conditionContainer->having("$function($sql_field_escaped) {$condition['operator']} $placeholder", [$placeholder => $condition['value']]); } } } diff --git a/core/lib/Drupal/Core/Entity/Query/Sql/QueryAggregate.php b/core/lib/Drupal/Core/Entity/Query/Sql/QueryAggregate.php index 0f7ae5b217bc..2e2d7a01c1a2 100644 --- a/core/lib/Drupal/Core/Entity/Query/Sql/QueryAggregate.php +++ b/core/lib/Drupal/Core/Entity/Query/Sql/QueryAggregate.php @@ -75,7 +75,8 @@ class QueryAggregate extends Query implements QueryAggregateInterface { if ($this->aggregate) { foreach ($this->aggregate as $aggregate) { $sql_field = $this->getSqlField($aggregate['field'], $aggregate['langcode']); - $this->sqlExpressions[$aggregate['alias']] = $aggregate['function'] . "($sql_field)"; + $sql_field_escaped = '[' . str_replace('.', '].[', $sql_field) . ']'; + $this->sqlExpressions[$aggregate['alias']] = $aggregate['function'] . "($sql_field_escaped)"; } } return $this; diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryAggregateTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryAggregateTest.php index 02448d6cee7a..f9af54342d77 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryAggregateTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryAggregateTest.php @@ -131,10 +131,14 @@ class EntityQueryAggregateTest extends EntityKernelTestBase { // Apply a simple aggregation for different aggregation functions. foreach ($function_expected as $aggregation_function => $expected) { - $this->queryResult = $this->entityStorage->getAggregateQuery() - ->aggregate('id', $aggregation_function) - ->execute(); - $this->assertEqual($this->queryResult, $expected); + $query = $this->entityStorage->getAggregateQuery() + ->aggregate('id', $aggregation_function); + $this->queryResult = $query->execute(); + // We need to check that a character exists before and after the table, + // column and alias identifiers. These would be the quote characters + // specific for each database system. + $this->assertRegExp('/' . $aggregation_function . '\(.entity_test.\..id.\) AS .id_' . $aggregation_function . './', (string) $query, 'The argument to the aggregation function should be a quoted field.'); + $this->assertEquals($expected, $this->queryResult); } // Apply aggregation and groupby on the same query.