Skip to content

Commit

Permalink
not so pretty
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusandra committed Jan 25, 2024
1 parent b2d706d commit fdc4f10
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 8 deletions.
2 changes: 1 addition & 1 deletion posthog/hogql/functions/test/test_sparkline.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class TestSparkline(BaseTest):
def test_sparkline(self):
response = execute_hogql_query("select sparkline([1,2,3])", self.team)
response = execute_hogql_query("select sparkline([1,2,3])", self.team, pretty=False)
self.assertEqual(
response.clickhouse,
f"SELECT tuple(%(hogql_val_0)s, %(hogql_val_1)s, %(hogql_val_2)s, [1, 2, 3]) LIMIT 100 SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1",
Expand Down
4 changes: 3 additions & 1 deletion posthog/hogql/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def execute_hogql_query(
limit_context: Optional[LimitContext] = LimitContext.QUERY,
timings: Optional[HogQLTimings] = None,
explain: Optional[bool] = False,
pretty: Optional[bool] = True,
) -> HogQLQueryResponse:
if timings is None:
timings = HogQLTimings()
Expand Down Expand Up @@ -95,7 +96,7 @@ def execute_hogql_query(
)

with timings.measure("print_ast"):
hogql = print_prepared_ast(select_query_hogql, hogql_query_context, "hogql", pretty=True)
hogql = print_prepared_ast(select_query_hogql, hogql_query_context, "hogql", pretty=pretty)

Check failure on line 99 in posthog/hogql/query.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

Argument "pretty" to "print_prepared_ast" has incompatible type "bool | None"; expected "bool"
print_columns = []
columns_query = (
select_query_hogql.select_queries[0]
Expand Down Expand Up @@ -133,6 +134,7 @@ def execute_hogql_query(
context=clickhouse_context,
dialect="clickhouse",
settings=settings,
pretty=pretty,

Check failure on line 137 in posthog/hogql/query.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

Argument "pretty" to "print_ast" has incompatible type "bool | None"; expected "bool"
)

timings_dict = timings.to_dict()
Expand Down
75 changes: 69 additions & 6 deletions posthog/hogql/test/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def test_query(self):
"select count(), event from events where properties.random_uuid = {random_uuid} group by event",
placeholders={"random_uuid": ast.Constant(value=random_uuid)},
team=self.team,
pretty=False,
)
assert pretty_print_response_in_tests(response, self.team.pk) == self.snapshot
self.assertEqual(response.results, [(2, "random event")])
Expand All @@ -77,6 +78,7 @@ def test_subquery(self):
"select count, event from (select count() as count, event from events where properties.random_uuid = {random_uuid} group by event) group by count, event",
placeholders={"random_uuid": ast.Constant(value=random_uuid)},
team=self.team,
pretty=False,
)
assert pretty_print_response_in_tests(response, self.team.pk) == self.snapshot
self.assertEqual(response.results, [(2, "random event")])
Expand All @@ -90,6 +92,7 @@ def test_subquery_alias(self):
"select count, event from (select count(*) as count, event from events where properties.random_uuid = {random_uuid} group by event) as c group by count, event",
placeholders={"random_uuid": ast.Constant(value=random_uuid)},
team=self.team,
pretty=False,
)
assert pretty_print_response_in_tests(response, self.team.pk) == self.snapshot
self.assertEqual(response.results, [(2, "random event")])
Expand All @@ -103,6 +106,7 @@ def test_query_distinct(self):
"select distinct properties.sneaky_mail from persons where properties.random_uuid = {random_uuid}",
placeholders={"random_uuid": ast.Constant(value=random_uuid)},
team=self.team,
pretty=False,
)
assert pretty_print_response_in_tests(response, self.team.pk) == self.snapshot
self.assertEqual(response.results, [("[email protected]",)])
Expand All @@ -114,6 +118,7 @@ def test_query_person_distinct_ids(self):
response = execute_hogql_query(
f"select distinct person_id, distinct_id from person_distinct_ids",
self.team,
pretty=False,
)
assert pretty_print_response_in_tests(response, self.team.pk) == self.snapshot
self.assertTrue(len(response.results) > 0)
Expand All @@ -125,6 +130,7 @@ def test_query_timings(self):
"select count(), event from events where properties.random_uuid = {random_uuid} group by event",
placeholders={"random_uuid": ast.Constant(value=random_uuid)},
team=self.team,
pretty=False,
)
self.assertTrue(isinstance(response.timings, list) and len(response.timings) > 0)
self.assertTrue(isinstance(response.timings[0], QueryTiming))
Expand All @@ -145,6 +151,7 @@ def test_query_joins_simple(self):
ON p.id = pdi.person_id
""",
self.team,
pretty=False,
)
assert pretty_print_response_in_tests(response, self.team.pk) == self.snapshot
self.assertEqual(response.results[0][0], "random event")
Expand All @@ -169,6 +176,7 @@ def test_query_joins_pdi(self):
ON e.distinct_id = pdi.distinct_id
""",
self.team,
pretty=False,
)

assert pretty_print_response_in_tests(response, self.team.pk) == self.snapshot
Expand All @@ -182,6 +190,7 @@ def test_query_joins_events_pdi(self):
response = execute_hogql_query(
"SELECT event, timestamp, pdi.distinct_id, pdi.person_id FROM events LIMIT 10",
self.team,
pretty=False,
)
assert pretty_print_response_in_tests(response, self.team.pk) == self.snapshot
self.assertEqual(response.results[0][0], "random event")
Expand All @@ -196,6 +205,7 @@ def test_query_joins_events_e_pdi(self):
response = execute_hogql_query(
"SELECT event, e.timestamp, e.pdi.distinct_id, pdi.person_id FROM events e LIMIT 10",
self.team,
pretty=False,
)
self.assertEqual(
response.hogql,
Expand All @@ -214,6 +224,7 @@ def test_query_joins_pdi_persons(self):
response = execute_hogql_query(
"SELECT pdi.distinct_id, pdi.person.created_at FROM person_distinct_ids pdi LIMIT 10",
self.team,
pretty=False,
)
self.assertEqual(
response.hogql,
Expand All @@ -234,6 +245,7 @@ def test_query_joins_pdi_person_properties(self):
response = execute_hogql_query(
"SELECT pdi.distinct_id, pdi.person.properties.sneaky_mail FROM person_distinct_ids pdi LIMIT 10",
self.team,
pretty=False,
)
self.assertEqual(
response.hogql,
Expand All @@ -251,6 +263,7 @@ def test_query_joins_events_pdi_person(self):
response = execute_hogql_query(
"SELECT event, timestamp, pdi.distinct_id, pdi.person.id FROM events LIMIT 10",
self.team,
pretty=False,
)
assert pretty_print_response_in_tests(response, self.team.pk) == self.snapshot
self.assertEqual(response.results[0][0], "random event")
Expand All @@ -266,6 +279,7 @@ def test_query_joins_events_pdi_person_properties(self):
response = execute_hogql_query(
"SELECT event, timestamp, pdi.distinct_id, pdi.person.properties.sneaky_mail FROM events LIMIT 10",
self.team,
pretty=False,
)
assert pretty_print_response_in_tests(response, self.team.pk) == self.snapshot
self.assertEqual(response.results[0][0], "random event")
Expand All @@ -280,6 +294,7 @@ def test_query_joins_events_pdi_e_person_properties(self):
response = execute_hogql_query(
"SELECT event, e.timestamp, pdi.distinct_id, e.pdi.person.properties.sneaky_mail FROM events e LIMIT 10",
self.team,
pretty=False,
)
assert pretty_print_response_in_tests(response, self.team.pk) == self.snapshot
self.assertEqual(response.results[0][0], "random event")
Expand All @@ -294,6 +309,7 @@ def test_query_joins_events_person_properties(self):
response = execute_hogql_query(
"SELECT event, e.timestamp, e.pdi.person.properties.sneaky_mail FROM events e LIMIT 10",
self.team,
pretty=False,
)
assert pretty_print_response_in_tests(response, self.team.pk) == self.snapshot
self.assertEqual(response.results[0][0], "random event")
Expand All @@ -306,6 +322,7 @@ def test_query_joins_events_person_properties_in_aggregration(self):
response = execute_hogql_query(
"SELECT s.pdi.person.properties.sneaky_mail, count() FROM events s GROUP BY s.pdi.person.properties.sneaky_mail LIMIT 10",
self.team,
pretty=False,
)
assert pretty_print_response_in_tests(response, self.team.pk) == self.snapshot
self.assertEqual(response.results[0][0], "[email protected]")
Expand All @@ -317,6 +334,7 @@ def test_select_person_on_events(self):
response = execute_hogql_query(
"SELECT poe.properties.sneaky_mail, count() FROM events s GROUP BY poe.properties.sneaky_mail LIMIT 10",
self.team,
pretty=False,
)
assert pretty_print_response_in_tests(response, self.team.pk) == self.snapshot
self.assertEqual(response.results[0][0], "[email protected]")
Expand All @@ -330,6 +348,7 @@ def test_query_select_person_with_joins_without_poe(self):
response = execute_hogql_query(
"SELECT event, timestamp, person.id, person.properties.sneaky_mail FROM events LIMIT 10",
self.team,
pretty=False,
)
assert pretty_print_response_in_tests(response, self.team.pk) == self.snapshot
self.assertEqual(response.results[0][0], "random event")
Expand All @@ -345,6 +364,7 @@ def test_query_select_person_with_poe_without_joins(self):
response = execute_hogql_query(
"SELECT event, timestamp, person.id, person.properties.sneaky_mail FROM events LIMIT 10",
self.team,
pretty=False,
)
assert pretty_print_response_in_tests(response, self.team.pk) == self.snapshot
self.assertEqual(response.results[0][0], "random event")
Expand Down Expand Up @@ -403,6 +423,7 @@ def test_prop_cohort_basic(self):
self.team,
)
},
pretty=False,
)
assert pretty_print_response_in_tests(response, self.team.pk) == self.snapshot
self.assertEqual(response.results, [("$pageview", 2)])
Expand All @@ -417,6 +438,7 @@ def test_prop_cohort_basic(self):
self.team,
)
},
pretty=False,
)
assert pretty_print_response_in_tests(response, self.team.pk) == self.snapshot
self.assertEqual(response.results, [("$pageview", 2)])
Expand Down Expand Up @@ -460,6 +482,7 @@ def test_prop_cohort_static(self):
self.team,
)
},
pretty=False,
)
self.assertEqual(response.results, [("$pageview", 1)])
assert pretty_print_response_in_tests(response, self.team.pk) == self.snapshot
Expand All @@ -474,6 +497,7 @@ def test_prop_cohort_static(self):
self.team,
)
},
pretty=False,
)
assert pretty_print_response_in_tests(response, self.team.pk) == self.snapshot
self.assertEqual(response.results, [("$pageview", 1)])
Expand Down Expand Up @@ -508,6 +532,7 @@ def test_join_with_property_materialized_session_id(self):
response = execute_hogql_query(
"select e.event, s.session_id from events e left join session_replay_events s on s.session_id = e.properties.$session_id where e.properties.$session_id is not null limit 10",
team=self.team,
pretty=False,
)
assert pretty_print_response_in_tests(response, self.team.pk) == self.snapshot
assert pretty_print_in_tests(response.hogql, self.team.pk) == self.snapshot
Expand All @@ -516,6 +541,7 @@ def test_join_with_property_materialized_session_id(self):
response = execute_hogql_query(
"select e.event, s.session_id from session_replay_events s left join events e on e.properties.$session_id = s.session_id where e.properties.$session_id is not null limit 10",
team=self.team,
pretty=False,
)
assert pretty_print_response_in_tests(response, self.team.pk) == self.snapshot
assert pretty_print_in_tests(response.hogql, self.team.pk) == self.snapshot
Expand Down Expand Up @@ -551,6 +577,7 @@ def test_join_with_property_not_materialized(self):
response = execute_hogql_query(
"select e.event, s.session_id from events e left join session_replay_events s on s.session_id = e.properties.$$$session_id where e.properties.$$$session_id is not null limit 10",
team=self.team,
pretty=False,
)
assert pretty_print_response_in_tests(response, self.team.pk) == self.snapshot
assert pretty_print_in_tests(response.hogql, self.team.pk) == self.snapshot
Expand All @@ -559,6 +586,7 @@ def test_join_with_property_not_materialized(self):
response = execute_hogql_query(
"select e.event, s.session_id from session_replay_events s left join events e on e.properties.$$$session_id = s.session_id where e.properties.$$$session_id is not null limit 10",
team=self.team,
pretty=False,
)
assert pretty_print_response_in_tests(response, self.team.pk) == self.snapshot
assert pretty_print_in_tests(response.hogql, self.team.pk) == self.snapshot
Expand All @@ -570,6 +598,7 @@ def test_hogql_lambdas(self):
response = execute_hogql_query(
"SELECT arrayMap(x -> x * 2, [1, 2, 3]), 1",
team=self.team,
pretty=False,
)
self.assertEqual(response.results, [([2, 4, 6], 1)])
assert pretty_print_response_in_tests(response, self.team.pk) == self.snapshot
Expand All @@ -580,6 +609,7 @@ def test_hogql_arrays(self):
response = execute_hogql_query(
"SELECT [1, 2, 3], [10,11,12][1]",
team=self.team,
pretty=False,
)
# Following SQL tradition, ClickHouse array indexes start at 1, not from zero.
self.assertEqual(response.results, [([1, 2, 3], 10)])
Expand Down Expand Up @@ -607,6 +637,7 @@ def test_tuple_access(self):
response = execute_hogql_query(
query,
team=self.team,
pretty=False,
)
self.assertEqual(
response.results,
Expand Down Expand Up @@ -871,6 +902,7 @@ def test_with_pivot_table_1_level(self):
response = execute_hogql_query(
query,
team=self.team,
pretty=False,
)
self.assertEqual(
response.results,
Expand Down Expand Up @@ -910,6 +942,7 @@ def test_with_pivot_table_2_levels(self):
response = execute_hogql_query(
query,
team=self.team,
pretty=False,
)
self.assertEqual(
response.results,
Expand Down Expand Up @@ -955,7 +988,11 @@ def test_property_access_with_arrays(self):
]
columns = ",".join(alternatives)
query = f"SELECT {columns} FROM events WHERE properties.string = '{random_uuid}'"
response = execute_hogql_query(query, team=self.team)
response = execute_hogql_query(
query,
team=self.team,
pretty=False,
)
self.assertEqual(
response.clickhouse,
f"SELECT "
Expand Down Expand Up @@ -1143,6 +1180,7 @@ def test_regex_functions(self):
response = execute_hogql_query(
query,
team=self.team,
pretty=False,
)

self.assertEqual(
Expand Down Expand Up @@ -1332,13 +1370,25 @@ def test_hogql_query_filters(self):
properties=[EventPropertyFilter(key="index", operator="exact", value="4", type="event")]
)
placeholders = {"distinct_id": ast.Constant(value=random_uuid)}
response = execute_hogql_query(query, team=self.team, filters=filters, placeholders=placeholders)
response = execute_hogql_query(
query,
team=self.team,
filters=filters,
placeholders=placeholders,
pretty=False,
)
assert pretty_print_response_in_tests(response, self.team.pk) == self.snapshot
assert pretty_print_in_tests(response.hogql, self.team.pk) == self.snapshot
self.assertEqual(len(response.results), 1)

filters.dateRange = DateRange(date_from="2020-01-01", date_to="2020-01-02")
response = execute_hogql_query(query, team=self.team, filters=filters, placeholders=placeholders)
response = execute_hogql_query(
query,
team=self.team,
filters=filters,
placeholders=placeholders,
pretty=False,
)
assert pretty_print_response_in_tests(response, self.team.pk) == self.snapshot
assert pretty_print_in_tests(response.hogql, self.team.pk) == self.snapshot
self.assertEqual(len(response.results), 0)
Expand All @@ -1349,7 +1399,11 @@ def test_hogql_query_filters(self):

def test_hogql_query_filters_empty_true(self):
query = "SELECT event from events where {filters}"
response = execute_hogql_query(query, team=self.team)
response = execute_hogql_query(
query,
team=self.team,
pretty=False,
)
self.assertEqual(response.hogql, "SELECT event FROM events WHERE true LIMIT 100")

def test_hogql_query_filters_double_error(self):
Expand Down Expand Up @@ -1380,7 +1434,12 @@ def test_hogql_query_filters_alias(self):
)
]
)
response = execute_hogql_query(query, team=self.team, filters=filters)
response = execute_hogql_query(
query,
team=self.team,
filters=filters,
pretty=False,
)
self.assertEqual(
response.hogql,
f"SELECT event, distinct_id FROM events AS e WHERE equals(properties.random_uuid, '{random_uuid}') LIMIT 100",
Expand All @@ -1391,7 +1450,11 @@ def test_hogql_query_filters_alias(self):
@pytest.mark.usefixtures("unittest_snapshot")
def test_hogql_union_all_limits(self):
query = "SELECT event FROM events UNION ALL SELECT event FROM events"
response = execute_hogql_query(query, team=self.team)
response = execute_hogql_query(
query,
team=self.team,
pretty=False,
)
self.assertEqual(
response.hogql,
f"SELECT event FROM events LIMIT 100 UNION ALL SELECT event FROM events LIMIT 100",
Expand Down

0 comments on commit fdc4f10

Please sign in to comment.