Skip to content

Non-selected aggregations from HAVING/WHERE/ORDER BY leak into result columns #55

@fupelaqu

Description

@fupelaqu

Description

When an aggregation is created solely to support a HAVING, WHERE, or ORDER BY clause (i.e., the aggregation is not in the SELECT list), the aggregation result leaks into the output row set as an extra column.

Reproduction

ORDER BY case

SELECT profile.city AS city,
       AVG(age) AS avg_age
FROM dql_users
GROUP BY profile.city
ORDER BY COUNT(*) DESC

Actual result (3 columns instead of 2):

city avg_age count_all
paris 27.5 2.0
lyon 40.0 1.0
marseille 50.0 1.0

HAVING case

SELECT profile.city AS city,
       AVG(age) AS avg_age
FROM dql_users
GROUP BY profile.city
HAVING COUNT(*) >= 1

Expected: 2 columns (city, avg_age). count_all should not appear.

WHERE case (same pattern)

Any aggregation created via Criteria.extractAggregationFields for WHERE filtering that is not in SELECT will also leak.

Root Cause

SingleSearch.aggregates collects aggregations from SELECT, HAVING, WHERE, and ORDER BY (since issues #52 and #53). All collected aggregations are passed to the bridge layer and included in the ES query. The result parser (ElasticConversion.parseAggregations) includes all aggregation values in the output rows without distinguishing between SELECT aggregations and auxiliary ones.

Suggested Fix

The output column filtering (in SearchApi.extractOutputFieldNames or normalizeRow) should exclude aggregation columns that are not present in the SELECT clause.

Possible approaches:

  1. Mark Field instances with their origin (SELECT vs ORDER BY vs HAVING/WHERE) and filter at output time
  2. Use Select.fieldsWithComputedAliases (not aggregates) to determine output columns
  3. Post-filter rows to remove columns not in the SELECT field list

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions