Skip to content

Commit

Permalink
feat(hogql): always provide an alias when printing out clickhouse (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusandra authored Dec 19, 2023
1 parent 1c5e7ad commit 08606ae
Show file tree
Hide file tree
Showing 27 changed files with 1,449 additions and 885 deletions.
108 changes: 54 additions & 54 deletions posthog/api/test/__snapshots__/test_query.ambr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# name: TestQuery.test_event_property_filter
'
/* user_id:0 request:_snapshot_ */
SELECT events.event,
events.distinct_id,
SELECT events.event AS event,
events.distinct_id AS distinct_id,
replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, 'key'), ''), 'null'), '^"|"$', '') AS key,
'a%sd',
concat(ifNull(toString(events.event), ''), ' ', ifNull(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, 'key'), ''), 'null'), '^"|"$', '')), ''))
Expand All @@ -18,8 +18,8 @@
# name: TestQuery.test_event_property_filter.1
'
/* user_id:0 request:_snapshot_ */
SELECT events.event,
events.distinct_id,
SELECT events.event AS event,
events.distinct_id AS distinct_id,
replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, 'key'), ''), 'null'), '^"|"$', '') AS key,
'a%sd',
concat(ifNull(toString(events.event), ''), ' ', ifNull(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, 'key'), ''), 'null'), '^"|"$', '')), ''))
Expand All @@ -35,8 +35,8 @@
# name: TestQuery.test_event_property_filter.2
'
/* user_id:0 request:_snapshot_ */
SELECT events.event,
events.distinct_id,
SELECT events.event AS event,
events.distinct_id AS distinct_id,
replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, 'key'), ''), 'null'), '^"|"$', '') AS key,
'a%sd',
concat(ifNull(toString(events.event), ''), ' ', ifNull(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, 'key'), ''), 'null'), '^"|"$', '')), ''))
Expand All @@ -52,8 +52,8 @@
# name: TestQuery.test_event_property_filter_materialized
'
/* user_id:0 request:_snapshot_ */
SELECT events.event,
events.distinct_id,
SELECT events.event AS event,
events.distinct_id AS distinct_id,
nullIf(nullIf(events.mat_key, ''), 'null') AS key,
'a%sd',
concat(ifNull(toString(events.event), ''), ' ', ifNull(toString(nullIf(nullIf(events.mat_key, ''), 'null')), ''))
Expand All @@ -69,8 +69,8 @@
# name: TestQuery.test_event_property_filter_materialized.1
'
/* user_id:0 request:_snapshot_ */
SELECT events.event,
events.distinct_id,
SELECT events.event AS event,
events.distinct_id AS distinct_id,
nullIf(nullIf(events.mat_key, ''), 'null') AS key,
'a%sd',
concat(ifNull(toString(events.event), ''), ' ', ifNull(toString(nullIf(nullIf(events.mat_key, ''), 'null')), ''))
Expand All @@ -86,8 +86,8 @@
# name: TestQuery.test_event_property_filter_materialized.2
'
/* user_id:0 request:_snapshot_ */
SELECT events.event,
events.distinct_id,
SELECT events.event AS event,
events.distinct_id AS distinct_id,
nullIf(nullIf(events.mat_key, ''), 'null') AS key,
'a%sd',
concat(ifNull(toString(events.event), ''), ' ', ifNull(toString(nullIf(nullIf(events.mat_key, ''), 'null')), ''))
Expand All @@ -103,7 +103,7 @@
# name: TestQuery.test_events_query_all_time_date
'
/* user_id:0 request:_snapshot_ */
SELECT events.event
SELECT events.event AS event
FROM events
WHERE and(equals(events.team_id, 2), less(toTimeZone(events.timestamp, 'UTC'), toDateTime64('2023-01-12 12:14:05.000000', 6, 'UTC')))
ORDER BY events.event ASC
Expand All @@ -116,7 +116,7 @@
# name: TestQuery.test_events_query_all_time_date.1
'
/* user_id:0 request:_snapshot_ */
SELECT events.event
SELECT events.event AS event
FROM events
WHERE and(equals(events.team_id, 2), less(toTimeZone(events.timestamp, 'UTC'), toDateTime64('2022-01-12 12:14:00.000000', 6, 'UTC')))
ORDER BY events.event ASC
Expand All @@ -129,7 +129,7 @@
# name: TestQuery.test_events_query_all_time_date.2
'
/* user_id:0 request:_snapshot_ */
SELECT events.event
SELECT events.event AS event
FROM events
WHERE and(equals(events.team_id, 2), less(toTimeZone(events.timestamp, 'UTC'), toDateTime64('2022-01-01 00:00:00.000000', 6, 'UTC')), greater(toTimeZone(events.timestamp, 'UTC'), toDateTime64('2019-01-12 12:14:00.000000', 6, 'UTC')))
ORDER BY events.event ASC
Expand All @@ -142,8 +142,8 @@
# name: TestQuery.test_full_hogql_query
'
/* user_id:0 request:_snapshot_ */
SELECT events.event,
events.distinct_id,
SELECT events.event AS event,
events.distinct_id AS distinct_id,
replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, 'key'), ''), 'null'), '^"|"$', '') AS key
FROM events
WHERE equals(events.team_id, 2)
Expand All @@ -156,8 +156,8 @@
# name: TestQuery.test_full_hogql_query_materialized
'
/* user_id:0 request:_snapshot_ */
SELECT events.event,
events.distinct_id,
SELECT events.event AS event,
events.distinct_id AS distinct_id,
nullIf(nullIf(events.mat_key, ''), 'null') AS key
FROM events
WHERE equals(events.team_id, 2)
Expand All @@ -184,9 +184,9 @@
# name: TestQuery.test_full_hogql_query_view.1
'
/* user_id:0 request:_snapshot_ */
SELECT event_view.event,
event_view.distinct_id,
event_view.key
SELECT event_view.event AS event,
event_view.distinct_id AS distinct_id,
event_view.key AS key
FROM
(SELECT events.event AS event,
events.distinct_id AS distinct_id,
Expand All @@ -202,8 +202,8 @@
# name: TestQuery.test_hogql_property_filter
'
/* user_id:0 request:_snapshot_ */
SELECT events.event,
events.distinct_id,
SELECT events.event AS event,
events.distinct_id AS distinct_id,
replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, 'key'), ''), 'null'), '^"|"$', '') AS key,
'a%sd',
concat(ifNull(toString(events.event), ''), ' ', ifNull(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, 'key'), ''), 'null'), '^"|"$', '')), ''))
Expand All @@ -219,8 +219,8 @@
# name: TestQuery.test_hogql_property_filter.1
'
/* user_id:0 request:_snapshot_ */
SELECT events.event,
events.distinct_id,
SELECT events.event AS event,
events.distinct_id AS distinct_id,
replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, 'key'), ''), 'null'), '^"|"$', '') AS key,
'a%sd',
concat(ifNull(toString(events.event), ''), ' ', ifNull(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, 'key'), ''), 'null'), '^"|"$', '')), ''))
Expand All @@ -236,8 +236,8 @@
# name: TestQuery.test_hogql_property_filter.2
'
/* user_id:0 request:_snapshot_ */
SELECT events.event,
events.distinct_id,
SELECT events.event AS event,
events.distinct_id AS distinct_id,
replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, 'key'), ''), 'null'), '^"|"$', '') AS key,
'a%sd',
concat(ifNull(toString(events.event), ''), ' ', ifNull(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, 'key'), ''), 'null'), '^"|"$', '')), ''))
Expand All @@ -253,8 +253,8 @@
# name: TestQuery.test_hogql_property_filter.3
'
/* user_id:0 request:_snapshot_ */
SELECT events.event,
events.distinct_id,
SELECT events.event AS event,
events.distinct_id AS distinct_id,
replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, 'key'), ''), 'null'), '^"|"$', '') AS key,
'a%sd',
concat(ifNull(toString(events.event), ''), ' ', ifNull(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, 'key'), ''), 'null'), '^"|"$', '')), ''))
Expand All @@ -270,8 +270,8 @@
# name: TestQuery.test_hogql_property_filter_materialized
'
/* user_id:0 request:_snapshot_ */
SELECT events.event,
events.distinct_id,
SELECT events.event AS event,
events.distinct_id AS distinct_id,
nullIf(nullIf(events.mat_key, ''), 'null') AS key,
'a%sd',
concat(ifNull(toString(events.event), ''), ' ', ifNull(toString(nullIf(nullIf(events.mat_key, ''), 'null')), ''))
Expand All @@ -287,8 +287,8 @@
# name: TestQuery.test_hogql_property_filter_materialized.1
'
/* user_id:0 request:_snapshot_ */
SELECT events.event,
events.distinct_id,
SELECT events.event AS event,
events.distinct_id AS distinct_id,
nullIf(nullIf(events.mat_key, ''), 'null') AS key,
'a%sd',
concat(ifNull(toString(events.event), ''), ' ', ifNull(toString(nullIf(nullIf(events.mat_key, ''), 'null')), ''))
Expand All @@ -304,8 +304,8 @@
# name: TestQuery.test_hogql_property_filter_materialized.2
'
/* user_id:0 request:_snapshot_ */
SELECT events.event,
events.distinct_id,
SELECT events.event AS event,
events.distinct_id AS distinct_id,
nullIf(nullIf(events.mat_key, ''), 'null') AS key,
'a%sd',
concat(ifNull(toString(events.event), ''), ' ', ifNull(toString(nullIf(nullIf(events.mat_key, ''), 'null')), ''))
Expand All @@ -321,8 +321,8 @@
# name: TestQuery.test_hogql_property_filter_materialized.3
'
/* user_id:0 request:_snapshot_ */
SELECT events.event,
events.distinct_id,
SELECT events.event AS event,
events.distinct_id AS distinct_id,
nullIf(nullIf(events.mat_key, ''), 'null') AS key,
'a%sd',
concat(ifNull(toString(events.event), ''), ' ', ifNull(toString(nullIf(nullIf(events.mat_key, ''), 'null')), ''))
Expand All @@ -338,8 +338,8 @@
# name: TestQuery.test_person_property_filter
'
/* user_id:0 request:_snapshot_ */
SELECT events.event,
events.distinct_id,
SELECT events.event AS event,
events.distinct_id AS distinct_id,
replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, 'key'), ''), 'null'), '^"|"$', '') AS key,
'a%sd',
concat(ifNull(toString(events.event), ''), ' ', ifNull(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, 'key'), ''), 'null'), '^"|"$', '')), ''))
Expand All @@ -352,11 +352,11 @@
GROUP BY person_distinct_id2.distinct_id
HAVING ifNull(equals(argMax(person_distinct_id2.is_deleted, person_distinct_id2.version), 0), 0)) AS events__pdi ON equals(events.distinct_id, events__pdi.distinct_id)
INNER JOIN
(SELECT person.id,
(SELECT person.id AS id,
replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'email'), ''), 'null'), '^"|"$', '') AS properties___email
FROM person
WHERE and(equals(person.team_id, 2), ifNull(in(tuple(person.id, person.version),
(SELECT person.id, max(person.version) AS version
(SELECT person.id AS id, max(person.version) AS version
FROM person
WHERE equals(person.team_id, 2)
GROUP BY person.id
Expand All @@ -372,8 +372,8 @@
# name: TestQuery.test_person_property_filter_materialized
'
/* user_id:0 request:_snapshot_ */
SELECT events.event,
events.distinct_id,
SELECT events.event AS event,
events.distinct_id AS distinct_id,
nullIf(nullIf(events.mat_key, ''), 'null') AS key,
'a%sd',
concat(ifNull(toString(events.event), ''), ' ', ifNull(toString(nullIf(nullIf(events.mat_key, ''), 'null')), ''))
Expand All @@ -386,11 +386,11 @@
GROUP BY person_distinct_id2.distinct_id
HAVING ifNull(equals(argMax(person_distinct_id2.is_deleted, person_distinct_id2.version), 0), 0)) AS events__pdi ON equals(events.distinct_id, events__pdi.distinct_id)
INNER JOIN
(SELECT person.id,
(SELECT person.id AS id,
nullIf(nullIf(person.pmat_email, ''), 'null') AS properties___email
FROM person
WHERE and(equals(person.team_id, 2), ifNull(in(tuple(person.id, person.version),
(SELECT person.id, max(person.version) AS version
(SELECT person.id AS id, max(person.version) AS version
FROM person
WHERE equals(person.team_id, 2)
GROUP BY person.id
Expand Down Expand Up @@ -468,9 +468,9 @@
# name: TestQuery.test_select_event_person
'
/* user_id:0 request:_snapshot_ */
SELECT events.event,
events.distinct_id,
events.distinct_id
SELECT events.event AS event,
events.distinct_id AS distinct_id,
events.distinct_id AS distinct_id
FROM events
WHERE and(equals(events.team_id, 2), less(toTimeZone(events.timestamp, 'UTC'), toDateTime64('2020-01-10 12:14:05.000000', 6, 'UTC')), greater(toTimeZone(events.timestamp, 'UTC'), toDateTime64('2020-01-09 12:14:00.000000', 6, 'UTC')))
ORDER BY events.event ASC
Expand All @@ -484,8 +484,8 @@
'
/* user_id:0 request:_snapshot_ */
SELECT replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, 'key'), ''), 'null'), '^"|"$', '') AS key,
events.event,
events.distinct_id,
events.event AS event,
events.distinct_id AS distinct_id,
concat(ifNull(toString(events.event), ''), ' ', ifNull(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, 'key'), ''), 'null'), '^"|"$', '')), ''))
FROM events
WHERE and(equals(events.team_id, 2), less(toTimeZone(events.timestamp, 'UTC'), toDateTime64('2020-01-10 12:14:05.000000', 6, 'UTC')), greater(toTimeZone(events.timestamp, 'UTC'), toDateTime64('2020-01-09 12:14:00.000000', 6, 'UTC')))
Expand All @@ -500,7 +500,7 @@
'
/* 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')),
events.event
events.event AS event
FROM events
WHERE and(equals(events.team_id, 2), less(toTimeZone(events.timestamp, 'UTC'), toDateTime64('2020-01-10 12:14:05.000000', 6, 'UTC')), greater(toTimeZone(events.timestamp, 'UTC'), toDateTime64('2020-01-09 12:14:00.000000', 6, '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
Expand All @@ -514,7 +514,7 @@
'
/* user_id:0 request:_snapshot_ */
SELECT count(),
events.event
events.event AS event
FROM events
WHERE and(equals(events.team_id, 2), less(toTimeZone(events.timestamp, 'UTC'), toDateTime64('2020-01-10 12:14:05.000000', 6, 'UTC')), greater(toTimeZone(events.timestamp, 'UTC'), toDateTime64('2020-01-09 12:14:00.000000', 6, 'UTC')))
GROUP BY events.event
Expand All @@ -529,7 +529,7 @@
'
/* user_id:0 request:_snapshot_ */
SELECT count(),
events.event
events.event AS event
FROM events
WHERE and(equals(events.team_id, 2), or(equals(events.event, 'sign up'), ifNull(like(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, 'key'), ''), 'null'), '^"|"$', ''), '%val2'), 0)), less(toTimeZone(events.timestamp, 'UTC'), toDateTime64('2020-01-10 12:14:05.000000', 6, 'UTC')), greater(toTimeZone(events.timestamp, 'UTC'), toDateTime64('2020-01-09 12:14:00.000000', 6, 'UTC')))
GROUP BY events.event
Expand Down
10 changes: 5 additions & 5 deletions posthog/api/test/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ def test_full_hogql_query(self):
@patch("posthog.hogql.constants.DEFAULT_RETURNED_ROWS", 10)
@patch("posthog.hogql.constants.MAX_SELECT_RETURNED_ROWS", 15)
def test_full_hogql_query_limit(self, MAX_SELECT_RETURNED_ROWS=15, DEFAULT_RETURNED_ROWS=10):
random_uuid = str(UUIDT())
random_uuid = f"RANDOM_TEST_ID::{UUIDT()}"
with freeze_time("2020-01-10 12:00:00"):
for _ in range(20):
_create_event(
Expand All @@ -594,7 +594,7 @@ def test_full_hogql_query_limit(self, MAX_SELECT_RETURNED_ROWS=15, DEFAULT_RETUR
@patch("posthog.hogql.constants.DEFAULT_RETURNED_ROWS", 10)
@patch("posthog.hogql.constants.MAX_SELECT_RETURNED_ROWS", 15)
def test_full_hogql_query_limit_exported(self, MAX_SELECT_RETURNED_ROWS=15, DEFAULT_RETURNED_ROWS=10):
random_uuid = str(UUIDT())
random_uuid = f"RANDOM_TEST_ID::{UUIDT()}"
with freeze_time("2020-01-10 12:00:00"):
for _ in range(20):
_create_event(
Expand All @@ -619,7 +619,7 @@ def test_full_hogql_query_limit_exported(self, MAX_SELECT_RETURNED_ROWS=15, DEFA
@patch("posthog.hogql.constants.DEFAULT_RETURNED_ROWS", 10)
@patch("posthog.hogql.constants.MAX_SELECT_RETURNED_ROWS", 15)
def test_full_events_query_limit(self, MAX_SELECT_RETURNED_ROWS=15, DEFAULT_RETURNED_ROWS=10):
random_uuid = str(UUIDT())
random_uuid = f"RANDOM_TEST_ID::{UUIDT()}"
with freeze_time("2020-01-10 12:00:00"):
for _ in range(20):
_create_event(
Expand All @@ -645,7 +645,7 @@ def test_full_events_query_limit(self, MAX_SELECT_RETURNED_ROWS=15, DEFAULT_RETU
@patch("posthog.hogql.constants.DEFAULT_RETURNED_ROWS", 10)
@patch("posthog.hogql.constants.MAX_SELECT_RETURNED_ROWS", 15)
def test_full_events_query_limit_exported(self, MAX_SELECT_RETURNED_ROWS=15, DEFAULT_RETURNED_ROWS=10):
random_uuid = str(UUIDT())
random_uuid = f"RANDOM_TEST_ID::{UUIDT()}"
with freeze_time("2020-01-10 12:00:00"):
for _ in range(20):
_create_event(
Expand Down Expand Up @@ -764,7 +764,7 @@ def test_full_hogql_query_view(self):
)

def test_full_hogql_query_values(self):
random_uuid = str(UUIDT())
random_uuid = f"RANDOM_TEST_ID::{UUIDT()}"
with freeze_time("2020-01-10 12:00:00"):
for _ in range(20):
_create_event(
Expand Down
11 changes: 7 additions & 4 deletions posthog/hogql/database/test/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def test_database_with_warehouse_tables(self, patch_execute):

self.assertEqual(
response.clickhouse,
f"SELECT whatever.id FROM s3Cluster('posthog', %(hogql_val_0_sensitive)s, %(hogql_val_3_sensitive)s, %(hogql_val_4_sensitive)s, %(hogql_val_1)s, %(hogql_val_2)s) AS whatever LIMIT 100 SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1",
f"SELECT whatever.id AS id FROM s3Cluster('posthog', %(hogql_val_0_sensitive)s, %(hogql_val_3_sensitive)s, %(hogql_val_4_sensitive)s, %(hogql_val_1)s, %(hogql_val_2)s) AS whatever LIMIT 100 SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1",
)

def test_database_group_type_mappings(self):
Expand Down Expand Up @@ -100,17 +100,20 @@ def test_database_expression_fields(self):
query = print_ast(parse_select(sql), context, dialect="clickhouse")
assert (
query
== "SELECT numbers.number, multiply(numbers.number, 2) AS double, plus(plus(1, 1), numbers.number) FROM numbers(2) AS numbers LIMIT 10000"
== "SELECT numbers.number AS number, multiply(numbers.number, 2) AS double, plus(plus(1, 1), numbers.number) FROM numbers(2) AS numbers LIMIT 10000"
), query

sql = "select double from (select double from numbers(2))"
query = print_ast(parse_select(sql), context, dialect="clickhouse")
assert (
query
== "SELECT double FROM (SELECT multiply(numbers.number, 2) AS double FROM numbers(2) AS numbers) LIMIT 10000"
== "SELECT double AS double FROM (SELECT multiply(numbers.number, 2) AS double FROM numbers(2) AS numbers) LIMIT 10000"
), query

# expression fields are not included in select *
sql = "select * from (select * from numbers(2))"
query = print_ast(parse_select(sql), context, dialect="clickhouse")
assert query == "SELECT number FROM (SELECT numbers.number FROM numbers(2) AS numbers) LIMIT 10000", query
assert (
query
== "SELECT number AS number FROM (SELECT numbers.number AS number FROM numbers(2) AS numbers) LIMIT 10000"
), query
Loading

0 comments on commit 08606ae

Please sign in to comment.