Skip to content

SQL heavy-hitter gate misses count-ranked topk when COUNT is aliased #20

Description

@milindsrivastava1997

Summary

Two SQL queries with identical semantics produce different L3 IR depending on whether the COUNT(*) is aliased in the ORDER BY. The heavy-hitter gate only fires when ORDER BY COUNT(*) DESC appears literally; referencing the count via an alias defeats it.

Reproduction

-- S1: alias — falls through to Sort+Limit (WRONG: should be heavy-hitter)
SELECT service, COUNT(*) AS cnt
FROM metrics
GROUP BY service
ORDER BY cnt DESC LIMIT 5

L3 output:

Limit { Sort { Project { Aggregate(Count) } } }
-- S2: inline — correctly hits heavy-hitter path
SELECT service, COUNT(*)
FROM metrics
GROUP BY service
ORDER BY COUNT(*) DESC LIMIT 5

L3 output:

Aggregate(TopK { k:5 })

Expected

Both queries should produce the same L3 IR — the heavy-hitter Aggregate(TopK) — since they are semantically identical.

Reproduce via example

cargo run -p asap-control-lower --example topk_ir

Labels: S1, S2

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions