Skip to content

HAVING COUNT(field) fails when COUNT(field) has no alias #54

@fupelaqu

Description

@fupelaqu

Description

When a GROUP BY query uses COUNT(field) (not COUNT(*)) without an alias and references it in a HAVING clause, the bucket_selector's buckets_path maps the metric name to itself instead of the actual Elasticsearch aggregation name.

Root Cause

In extractMetricsPathForBucket (bridge/…/ElasticAggregation.scala), when the lookup matches via agg.field == metricName, the return value metricName -> metricName is incorrect because:

  • metricName = "field_name" (from Identifier.metricName)
  • aggName (local) = "count_field_name" (from s"${aggType}_${sourceField}")
  • The bucket_selector gets {"field_name": "field_name"} but the actual ES aggregation is named "count_field_name"

The correct return should map metricName to the local aggregation name (last segment of ElasticAggregation.aggName).

Complexity

The fix is non-trivial because ElasticAggregation.aggName in the case class stores the full nested path (e.g., "inner_products.filtered_agg.min_price"), not just the local name. Extracting the local name requires splitting on . which may be fragile. A cleaner approach would be to store the local aggregation name as a separate field in the case class.

Workaround

Always provide an alias for aggregations used in HAVING:

HAVING COUNT(field) > 5        -- broken
HAVING cnt > 5                 -- works (with COUNT(field) AS cnt in SELECT)

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions