Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(insights): pretty print more sql #19963

Merged
merged 12 commits into from
Jan 26, 2024
13 changes: 11 additions & 2 deletions posthog/hogql/database/test/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,22 @@ def test_can_select_from_each_table_at_all(self, poe_enabled: bool) -> None:
serialized_database = serialize_database(create_hogql_database(team_id=self.team.pk))
for table, possible_columns in serialized_database.items():
if table == "numbers":
execute_hogql_query("SELECT number FROM numbers(10) LIMIT 100", self.team)
execute_hogql_query(
"SELECT number FROM numbers(10) LIMIT 100",
self.team,
pretty=False,
)
else:
columns = [
x["key"]
for x in possible_columns
if "table" not in x and "chain" not in x and "fields" not in x
]
execute_hogql_query(f"SELECT {','.join(columns)} FROM {table}", team=self.team)
execute_hogql_query(
f"SELECT {','.join(columns)} FROM {table}",
team=self.team,
pretty=False,
)

@patch("posthog.hogql.query.sync_execute", return_value=(None, None))
@pytest.mark.usefixtures("unittest_snapshot")
Expand All @@ -66,6 +74,7 @@ def test_database_with_warehouse_tables(self, patch_execute):
response = execute_hogql_query(
"select * from whatever",
team=self.team,
pretty=False,
)

self.assertEqual(
Expand Down
3 changes: 3 additions & 0 deletions posthog/hogql/functions/test/test_cohort.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def test_in_cohort_dynamic(self):
f"SELECT event FROM events WHERE person_id IN COHORT {cohort.pk} AND event='{random_uuid}'",
self.team,
modifiers=HogQLQueryModifiers(inCohortVia="subquery"),
pretty=False,
)
assert pretty_print_response_in_tests(response, self.team.pk) == self.snapshot
self.assertEqual(len(response.results), 1)
Expand All @@ -65,6 +66,7 @@ def test_in_cohort_static(self):
f"SELECT event FROM events WHERE person_id IN COHORT {cohort.pk}",
self.team,
modifiers=HogQLQueryModifiers(inCohortVia="subquery"),
pretty=False,
)
assert pretty_print_response_in_tests(response, self.team.pk) == self.snapshot

Expand All @@ -80,6 +82,7 @@ def test_in_cohort_strings(self):
f"SELECT event FROM events WHERE person_id IN COHORT 'my cohort'",
self.team,
modifiers=HogQLQueryModifiers(inCohortVia="subquery"),
pretty=False,
)
assert pretty_print_response_in_tests(response, self.team.pk) == self.snapshot

Expand Down
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
2 changes: 1 addition & 1 deletion posthog/hogql/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ def visit_select_query(self, node: ast.SelectQuery):

clauses = [
f"SELECT{space}{'DISTINCT ' if node.distinct else ''}{comma.join(columns)}",
f"FROM{space}{' '.join(joined_tables)}" if len(joined_tables) > 0 else None,
f"FROM{space}{space.join(joined_tables)}" if len(joined_tables) > 0 else None,
array_join if array_join else None,
f"PREWHERE{space}" + prewhere if prewhere else None,
f"WHERE{space}" + where if where else None,
Expand Down
6 changes: 5 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,9 @@ def execute_hogql_query(
)

with timings.measure("print_ast"):
hogql = print_prepared_ast(select_query_hogql, hogql_query_context, "hogql")
hogql = print_prepared_ast(
select_query_hogql, hogql_query_context, "hogql", pretty=pretty if pretty is not None else True
)
print_columns = []
columns_query = (
select_query_hogql.select_queries[0]
Expand Down Expand Up @@ -133,6 +136,7 @@ def execute_hogql_query(
context=clickhouse_context,
dialect="clickhouse",
settings=settings,
pretty=pretty if pretty is not None else True,
)

timings_dict = timings.to_dict()
Expand Down
3 changes: 2 additions & 1 deletion posthog/hogql/test/__snapshots__/test_printer.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
(SELECT
minus(dateTrunc('day', assumeNotNull(toDateTime('2023-10-19 23:59:59'))), toIntervalDay(number)) AS start_of_period
FROM
numbers(dateDiff('day', dateTrunc('day', assumeNotNull(toDateTime('2023-09-19 00:00:00'))), dateTrunc('day', plus(assumeNotNull(toDateTime('2023-10-19 23:59:59')), toIntervalDay(1))))) AS numbers) AS periods CROSS JOIN (SELECT
numbers(dateDiff('day', dateTrunc('day', assumeNotNull(toDateTime('2023-09-19 00:00:00'))), dateTrunc('day', plus(assumeNotNull(toDateTime('2023-10-19 23:59:59')), toIntervalDay(1))))) AS numbers) AS periods
CROSS JOIN (SELECT
status
FROM
(SELECT
Expand Down
5 changes: 5 additions & 0 deletions posthog/hogql/test/test_modifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def test_modifiers_persons_on_events_mode_mapping(self):
query,
team=self.team,
modifiers=HogQLQueryModifiers(personsOnEventsMode=mode),
pretty=False,
)
assert f"SELECT {', '.join(expected)} FROM" in response.clickhouse, f"PoE mode: {mode}"

Expand Down Expand Up @@ -158,6 +159,7 @@ def test_modifiers_materialization_mode(self):
"SELECT properties.$browser FROM events",
team=self.team,
modifiers=HogQLQueryModifiers(materializationMode=MaterializationMode.auto),
pretty=False,
)
assert (
"SELECT nullIf(nullIf(events.`mat_$browser`, ''), 'null') AS `$browser` FROM events" in response.clickhouse
Expand All @@ -167,6 +169,7 @@ def test_modifiers_materialization_mode(self):
"SELECT properties.$browser FROM events",
team=self.team,
modifiers=HogQLQueryModifiers(materializationMode=MaterializationMode.legacy_null_as_null),
pretty=False,
)
assert (
"SELECT nullIf(nullIf(events.`mat_$browser`, ''), 'null') AS `$browser` FROM events" in response.clickhouse
Expand All @@ -176,13 +179,15 @@ def test_modifiers_materialization_mode(self):
"SELECT properties.$browser FROM events",
team=self.team,
modifiers=HogQLQueryModifiers(materializationMode=MaterializationMode.legacy_null_as_string),
pretty=False,
)
assert "SELECT nullIf(events.`mat_$browser`, '') AS `$browser` FROM events" in response.clickhouse

response = execute_hogql_query(
"SELECT properties.$browser FROM events",
team=self.team,
modifiers=HogQLQueryModifiers(materializationMode=MaterializationMode.disabled),
pretty=False,
)
assert (
"SELECT replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_0)s), ''), 'null'), '^\"|\"$', '') AS `$browser` FROM events"
Expand Down
Loading
Loading