Skip to content

Commit

Permalink
chore: Improve measurement of dw setup (PostHog#29071)
Browse files Browse the repository at this point in the history
  • Loading branch information
robbie-c authored Feb 21, 2025
1 parent 5c1e23e commit 2123d05
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions posthog/hogql/database/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,37 +345,44 @@ def create_hogql_database(
views: dict[str, Table] = {}

with timings.measure("data_warehouse_saved_query"):
for saved_query in DataWarehouseSavedQuery.objects.filter(team_id=team.pk).exclude(deleted=True):
views[saved_query.name] = saved_query.hogql_definition(modifiers)
with timings.measure("select"):
saved_queries = list(DataWarehouseSavedQuery.objects.filter(team_id=team.pk).exclude(deleted=True))
for saved_query in saved_queries:
with timings.measure(f"saved_query_{saved_query.name}"):
views[saved_query.name] = saved_query.hogql_definition(modifiers)

with timings.measure("data_warehouse_tables"):
for table in (
DataWarehouseTable.objects.filter(team_id=team.pk)
.exclude(deleted=True)
.select_related("credential", "external_data_source")
):
with timings.measure("select"):
tables = list(
DataWarehouseTable.objects.filter(team_id=team.pk)
.exclude(deleted=True)
.select_related("credential", "external_data_source")
)

for table in tables:
# Skip adding data warehouse tables that are materialized from views (in this case they have the same names)
if views.get(table.name, None) is not None:
continue

s3_table = table.hogql_definition(modifiers)
with timings.measure(f"table_{table.name}"):
s3_table = table.hogql_definition(modifiers)

# If the warehouse table has no _properties_ field, then set it as a virtual table
if s3_table.fields.get("properties") is None:
# If the warehouse table has no _properties_ field, then set it as a virtual table
if s3_table.fields.get("properties") is None:

class WarehouseProperties(VirtualTable):
fields: dict[str, FieldOrTable] = s3_table.fields
parent_table: S3Table = s3_table
class WarehouseProperties(VirtualTable):
fields: dict[str, FieldOrTable] = s3_table.fields
parent_table: S3Table = s3_table

def to_printed_hogql(self):
return self.parent_table.to_printed_hogql()
def to_printed_hogql(self):
return self.parent_table.to_printed_hogql()

def to_printed_clickhouse(self, context):
return self.parent_table.to_printed_clickhouse(context)
def to_printed_clickhouse(self, context):
return self.parent_table.to_printed_clickhouse(context)

s3_table.fields["properties"] = WarehouseProperties(hidden=True)
s3_table.fields["properties"] = WarehouseProperties(hidden=True)

warehouse_tables[table.name] = s3_table
warehouse_tables[table.name] = s3_table

def define_mappings(warehouse: dict[str, Table], get_table: Callable):
if "id" not in warehouse[warehouse_modifier.table_name].fields.keys():
Expand Down

0 comments on commit 2123d05

Please sign in to comment.