Skip to content

Commit

Permalink
fix(hogql): nested macros (#15126)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusandra authored Apr 18, 2023
1 parent 959f8de commit 6b0b0f7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 29 deletions.
58 changes: 29 additions & 29 deletions posthog/api/test/__snapshots__/test_query.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
'a%sd',
concat(events.event, ' ', replaceRegexpAll(JSONExtractRaw(events.properties, 'key'), '^"|"$', ''))
FROM events
WHERE and(equals(events.team_id, 68), less(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
WHERE and(equals(events.team_id, 68), less(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
ORDER BY events.event ASC
LIMIT 101
OFFSET 0 SETTINGS readonly=1,
Expand All @@ -23,7 +23,7 @@
'a%sd',
concat(events.event, ' ', replaceRegexpAll(JSONExtractRaw(events.properties, 'key'), '^"|"$', ''))
FROM events
WHERE and(equals(events.team_id, 68), equals(replaceRegexpAll(JSONExtractRaw(events.properties, 'key'), '^"|"$', ''), 'test_val3'), less(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
WHERE and(equals(events.team_id, 68), equals(replaceRegexpAll(JSONExtractRaw(events.properties, 'key'), '^"|"$', ''), 'test_val3'), less(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
ORDER BY events.event ASC
LIMIT 101
OFFSET 0 SETTINGS readonly=1,
Expand All @@ -39,7 +39,7 @@
'a%sd',
concat(events.event, ' ', replaceRegexpAll(JSONExtractRaw(events.properties, 'key'), '^"|"$', ''))
FROM events
WHERE and(equals(events.team_id, 68), ilike(replaceRegexpAll(JSONExtractRaw(events.properties, 'path'), '^"|"$', ''), '%/%'), less(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
WHERE and(equals(events.team_id, 68), ilike(replaceRegexpAll(JSONExtractRaw(events.properties, 'path'), '^"|"$', ''), '%/%'), less(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
ORDER BY events.event ASC
LIMIT 101
OFFSET 0 SETTINGS readonly=1,
Expand All @@ -55,7 +55,7 @@
'a%sd',
concat(events.event, ' ', events.mat_key)
FROM events
WHERE and(equals(events.team_id, 69), less(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
WHERE and(equals(events.team_id, 69), less(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
ORDER BY events.event ASC
LIMIT 101
OFFSET 0 SETTINGS readonly=1,
Expand All @@ -71,7 +71,7 @@
'a%sd',
concat(events.event, ' ', events.mat_key)
FROM events
WHERE and(equals(events.team_id, 69), equals(events.mat_key, 'test_val3'), less(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
WHERE and(equals(events.team_id, 69), equals(events.mat_key, 'test_val3'), less(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
ORDER BY events.event ASC
LIMIT 101
OFFSET 0 SETTINGS readonly=1,
Expand All @@ -87,7 +87,7 @@
'a%sd',
concat(events.event, ' ', events.mat_key)
FROM events
WHERE and(equals(events.team_id, 69), ilike(events.mat_path, '%/%'), less(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
WHERE and(equals(events.team_id, 69), ilike(events.mat_path, '%/%'), less(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
ORDER BY events.event ASC
LIMIT 101
OFFSET 0 SETTINGS readonly=1,
Expand All @@ -102,7 +102,7 @@
replaceRegexpAll(JSONExtractRaw(events.properties, 'key'), '^"|"$', '')
FROM events
WHERE equals(events.team_id, 70)
ORDER BY toTimezone(events.timestamp, 'UTC') ASC
ORDER BY toTimeZone(events.timestamp, 'UTC') ASC
LIMIT 100 SETTINGS readonly=1,
max_execution_time=60
'
Expand All @@ -115,7 +115,7 @@
events.mat_key
FROM events
WHERE equals(events.team_id, 71)
ORDER BY toTimezone(events.timestamp, 'UTC') ASC
ORDER BY toTimeZone(events.timestamp, 'UTC') ASC
LIMIT 100 SETTINGS readonly=1,
max_execution_time=60
'
Expand All @@ -129,7 +129,7 @@
'a%sd',
concat(events.event, ' ', replaceRegexpAll(JSONExtractRaw(events.properties, 'key'), '^"|"$', ''))
FROM events
WHERE and(equals(events.team_id, 72), less(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
WHERE and(equals(events.team_id, 72), less(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
ORDER BY events.event ASC
LIMIT 101
OFFSET 0 SETTINGS readonly=1,
Expand All @@ -145,7 +145,7 @@
'a%sd',
concat(events.event, ' ', replaceRegexpAll(JSONExtractRaw(events.properties, 'key'), '^"|"$', ''))
FROM events
WHERE and(equals(events.team_id, 72), equals('a%sd', 'foo'), less(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
WHERE and(equals(events.team_id, 72), equals('a%sd', 'foo'), less(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
ORDER BY events.event ASC
LIMIT 101
OFFSET 0 SETTINGS readonly=1,
Expand All @@ -161,7 +161,7 @@
'a%sd',
concat(events.event, ' ', replaceRegexpAll(JSONExtractRaw(events.properties, 'key'), '^"|"$', ''))
FROM events
WHERE and(equals(events.team_id, 72), equals('a%sd', 'a%sd'), less(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
WHERE and(equals(events.team_id, 72), equals('a%sd', 'a%sd'), less(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
ORDER BY events.event ASC
LIMIT 101
OFFSET 0 SETTINGS readonly=1,
Expand All @@ -177,7 +177,7 @@
'a%sd',
concat(events.event, ' ', replaceRegexpAll(JSONExtractRaw(events.properties, 'key'), '^"|"$', ''))
FROM events
WHERE and(equals(events.team_id, 72), equals(replaceRegexpAll(JSONExtractRaw(events.properties, 'key'), '^"|"$', ''), 'test_val2'), less(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
WHERE and(equals(events.team_id, 72), equals(replaceRegexpAll(JSONExtractRaw(events.properties, 'key'), '^"|"$', ''), 'test_val2'), less(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
ORDER BY events.event ASC
LIMIT 101
OFFSET 0 SETTINGS readonly=1,
Expand All @@ -193,7 +193,7 @@
'a%sd',
concat(events.event, ' ', events.mat_key)
FROM events
WHERE and(equals(events.team_id, 73), less(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
WHERE and(equals(events.team_id, 73), less(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
ORDER BY events.event ASC
LIMIT 101
OFFSET 0 SETTINGS readonly=1,
Expand All @@ -209,7 +209,7 @@
'a%sd',
concat(events.event, ' ', events.mat_key)
FROM events
WHERE and(equals(events.team_id, 73), equals('a%sd', 'foo'), less(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
WHERE and(equals(events.team_id, 73), equals('a%sd', 'foo'), less(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
ORDER BY events.event ASC
LIMIT 101
OFFSET 0 SETTINGS readonly=1,
Expand All @@ -225,7 +225,7 @@
'a%sd',
concat(events.event, ' ', events.mat_key)
FROM events
WHERE and(equals(events.team_id, 73), equals('a%sd', 'a%sd'), less(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
WHERE and(equals(events.team_id, 73), equals('a%sd', 'a%sd'), less(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
ORDER BY events.event ASC
LIMIT 101
OFFSET 0 SETTINGS readonly=1,
Expand All @@ -241,7 +241,7 @@
'a%sd',
concat(events.event, ' ', events.mat_key)
FROM events
WHERE and(equals(events.team_id, 73), equals(events.mat_key, 'test_val2'), less(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
WHERE and(equals(events.team_id, 73), equals(events.mat_key, 'test_val2'), less(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
ORDER BY events.event ASC
LIMIT 101
OFFSET 0 SETTINGS readonly=1,
Expand Down Expand Up @@ -271,7 +271,7 @@
WHERE equals(person.team_id, 77)
GROUP BY person.id
HAVING equals(argMax(person.is_deleted, person.version), 0)) AS events__pdi__person ON equals(events__pdi.person_id, events__pdi__person.id)
WHERE and(equals(events.team_id, 77), equals(events__pdi__person.properties___email, '[email protected]'), less(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
WHERE and(equals(events.team_id, 77), equals(events__pdi__person.properties___email, '[email protected]'), less(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
ORDER BY events.event ASC
LIMIT 101
OFFSET 0 SETTINGS readonly=1,
Expand Down Expand Up @@ -301,7 +301,7 @@
WHERE equals(person.team_id, 78)
GROUP BY person.id
HAVING equals(argMax(person.is_deleted, person.version), 0)) AS events__pdi__person ON equals(events__pdi.person_id, events__pdi__person.id)
WHERE and(equals(events.team_id, 78), equals(events__pdi__person.properties___email, '[email protected]'), less(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
WHERE and(equals(events.team_id, 78), equals(events__pdi__person.properties___email, '[email protected]'), less(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
ORDER BY events.event ASC
LIMIT 101
OFFSET 0 SETTINGS readonly=1,
Expand All @@ -314,7 +314,7 @@
SELECT replaceRegexpAll(JSONExtractRaw(events.properties, 'key'), '^"|"$', ''),
count()
FROM events
WHERE and(equals(events.team_id, 79), less(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
WHERE and(equals(events.team_id, 79), less(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
GROUP BY replaceRegexpAll(JSONExtractRaw(events.properties, 'key'), '^"|"$', '')
ORDER BY count() DESC
LIMIT 101
Expand All @@ -328,7 +328,7 @@
SELECT replaceRegexpAll(JSONExtractRaw(events.properties, 'key'), '^"|"$', ''),
count()
FROM events
WHERE and(equals(events.team_id, 79), less(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
WHERE and(equals(events.team_id, 79), less(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
GROUP BY replaceRegexpAll(JSONExtractRaw(events.properties, 'key'), '^"|"$', '')
HAVING and(greater(count(), 1))
ORDER BY count() DESC
Expand All @@ -343,7 +343,7 @@
SELECT events.mat_key,
count()
FROM events
WHERE and(equals(events.team_id, 80), less(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
WHERE and(equals(events.team_id, 80), less(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
GROUP BY events.mat_key
ORDER BY count() DESC
LIMIT 101
Expand All @@ -357,7 +357,7 @@
SELECT events.mat_key,
count()
FROM events
WHERE and(equals(events.team_id, 80), less(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
WHERE and(equals(events.team_id, 80), less(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
GROUP BY events.mat_key
HAVING and(greater(count(), 1))
ORDER BY count() DESC
Expand All @@ -373,7 +373,7 @@
events.distinct_id,
events.distinct_id
FROM events
WHERE and(equals(events.team_id, 81), less(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
WHERE and(equals(events.team_id, 81), less(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
ORDER BY events.event ASC
LIMIT 101
OFFSET 0 SETTINGS readonly=1,
Expand All @@ -388,7 +388,7 @@
events.distinct_id,
concat(events.event, ' ', replaceRegexpAll(JSONExtractRaw(events.properties, 'key'), '^"|"$', ''))
FROM events
WHERE and(equals(events.team_id, 82), less(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
WHERE and(equals(events.team_id, 82), less(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
ORDER BY replaceRegexpAll(JSONExtractRaw(events.properties, 'key'), '^"|"$', '') ASC
LIMIT 101
OFFSET 0 SETTINGS readonly=1,
Expand All @@ -398,11 +398,11 @@
# name: TestQuery.test_select_hogql_expressions.1
'
/* user_id:0 request:_snapshot_ */
SELECT tuple(events.uuid, events.event, events.properties, toTimezone(events.timestamp, 'UTC'), events.team_id, events.distinct_id, events.elements_chain, toTimezone(events.created_at, 'UTC')),
SELECT tuple(events.uuid, events.event, events.properties, toTimeZone(events.timestamp, 'UTC'), events.team_id, events.distinct_id, events.elements_chain, toTimeZone(events.created_at, 'UTC')),
events.event
FROM events
WHERE and(equals(events.team_id, 82), less(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
ORDER BY tuple(events.uuid, events.event, events.properties, toTimezone(events.timestamp, 'UTC'), events.team_id, events.distinct_id, events.elements_chain, toTimezone(events.created_at, 'UTC')) ASC
WHERE and(equals(events.team_id, 82), less(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
ORDER BY tuple(events.uuid, events.event, events.properties, toTimeZone(events.timestamp, 'UTC'), events.team_id, events.distinct_id, events.elements_chain, toTimeZone(events.created_at, 'UTC')) ASC
LIMIT 101
OFFSET 0 SETTINGS readonly=1,
max_execution_time=60
Expand All @@ -414,7 +414,7 @@
SELECT count(),
events.event
FROM events
WHERE and(equals(events.team_id, 82), less(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
WHERE and(equals(events.team_id, 82), less(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
GROUP BY events.event
ORDER BY count() DESC
LIMIT 101
Expand All @@ -428,7 +428,7 @@
SELECT count(),
events.event
FROM events
WHERE and(equals(events.team_id, 82), or(equals(events.event, 'sign up'), like(replaceRegexpAll(JSONExtractRaw(events.properties, 'key'), '^"|"$', ''), '%val2')), less(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimezone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
WHERE and(equals(events.team_id, 82), or(equals(events.event, 'sign up'), like(replaceRegexpAll(JSONExtractRaw(events.properties, 'key'), '^"|"$', ''), '%val2')), less(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-10 12:14:05', 'UTC')), greater(toTimeZone(events.timestamp, 'UTC'), toDateTime('2020-01-09 12:00:00', 'UTC')))
GROUP BY events.event
ORDER BY count() DESC, events.event ASC
LIMIT 101
Expand Down
1 change: 1 addition & 0 deletions posthog/hogql/transforms/macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def visit_field(self, node: ast.Field):
return node

def visit_join_expr(self, node: ast.JoinExpr):
node = super().visit_join_expr(node)
if len(self.scopes) > 0 and isinstance(node.table, ast.Field):
for scope in reversed(self.scopes):
if scope.macros and len(node.table.chain) == 1 and node.table.chain[0] in scope.macros:
Expand Down
14 changes: 14 additions & 0 deletions posthog/hogql/transforms/test/test_macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ def test_macros_subqueries(self):
parse_select("select timestamp from (select * from events) my_table"),
)

def test_macros_subquery_deep(self):
self.assertEqual(
expand_macros(
parse_select(
"with my_table as (select * from events), "
"other_table as (select * from (select * from (select * from my_table))) "
"select * from other_table"
)
),
parse_select(
"select * from (select * from (select * from (select * from (select * from events) as my_table))) as other_table"
),
)

def test_macros_subquery_recursion(self):
query = "with users as (select event, timestamp as tt from events ), final as ( select tt from users ) select * from final"
self.assertEqual(
Expand Down

0 comments on commit 6b0b0f7

Please sign in to comment.