Skip to content

Commit

Permalink
bug(query): Fix alias mangling in nested subqueries (#5854)
Browse files Browse the repository at this point in the history
* fix alias mangling for sub queries

* fix test
  • Loading branch information
enochtangg committed May 6, 2024
1 parent a77595f commit 2d60584
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion snuba/query/snql/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,7 @@ def mangle_column_value(exp: Expression) -> Expression:

# Check if this query has a subquery. If it does, we need to mangle the column name as well
# and keep track of what we mangled by updating the mappings in memory.
if isinstance(query.get_from_clause(), LogicalQuery):
if isinstance(query.get_from_clause(), (LogicalQuery, CompositeQuery)):
query.transform_expressions(mangle_column_value)


Expand Down
2 changes: 1 addition & 1 deletion tests/query/snql/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -1498,7 +1498,7 @@ def build_cond(tn: str) -> str:
FunctionCall(
"_snuba_min_count",
"min",
(Column("_snuba_max_count", None, "max_count"),),
(Column("_snuba_max_count", None, "_snuba_max_count"),),
),
),
],
Expand Down
31 changes: 30 additions & 1 deletion tests/test_snql_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def test_sub_query(self) -> None:
data=json.dumps(
{
"query": """MATCH {
MATCH (discover_events )
MATCH (discover_events)
SELECT count() AS count BY project_id, tags[custom_tag]
WHERE type != 'transaction' AND project_id = %s
AND timestamp >= toDateTime('%s')
Expand All @@ -240,6 +240,35 @@ def test_sub_query(self) -> None:
assert response.status_code == 200, data
assert data["data"] == [{"avg_count": 1.0}]

def test_join_query_in_sub_query(self) -> None:
response = self.post(
"/events/snql",
data=json.dumps(
{
"query": """MATCH {
MATCH (events: events) -[attributes]-> (group_attributes: group_attributes)
SELECT events.group_id, count() AS `event_count`, max(events.timestamp) AS `last_seen`
BY events.group_id
WHERE events.project_id IN array(4553884953739266)
AND group_attributes.project_id IN array(4553884953739266)
AND events.timestamp >= toDateTime('2024-01-24T16:59:49.431129')
AND events.timestamp < toDateTime('2024-04-23T16:59:49.431129')
AND group_attributes.group_status = 0
}
SELECT events.group_id WHERE last_seen >= toDateTime('2024-04-09T16:59:49.431129')
ORDER BY event_count DESC LIMIT 10000""",
"turbo": False,
"consistent": False,
"debug": True,
"tenant_ids": {"referrer": "r", "organization_id": 123},
}
),
)
data = json.loads(response.data)

assert response.status_code == 200
assert data["data"] == []

def test_max_limit(self) -> None:
response = self.post(
"/discover/snql",
Expand Down

0 comments on commit 2d60584

Please sign in to comment.