2021-09-07 13:53:57 +02:00
|
|
|
from celery.utils.log import get_task_logger
|
|
|
|
|
|
|
|
from ee.clickhouse.materialized_columns.columns import TRIM_AND_EXTRACT_PROPERTY, ColumnName, get_materialized_columns
|
2022-06-22 23:24:03 +02:00
|
|
|
from posthog.clickhouse.replication.utils import clickhouse_is_replicated
|
2022-03-22 21:27:11 +01:00
|
|
|
from posthog.client import sync_execute
|
2022-03-16 09:27:39 +01:00
|
|
|
from posthog.settings import CLICKHOUSE_CLUSTER, CLICKHOUSE_DATABASE
|
2021-09-07 13:53:57 +02:00
|
|
|
|
|
|
|
logger = get_task_logger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def mark_all_materialized() -> None:
|
|
|
|
if any_ongoing_mutations():
|
|
|
|
logger.info("There are running mutations, skipping marking as materialized")
|
|
|
|
return
|
|
|
|
|
2022-08-30 13:31:32 +02:00
|
|
|
for table, property_name, table_column, column_name in get_materialized_columns_with_default_expression():
|
2022-03-16 09:27:39 +01:00
|
|
|
updated_table = "sharded_events" if clickhouse_is_replicated() and table == "events" else table
|
2021-09-07 13:53:57 +02:00
|
|
|
|
|
|
|
# :TRICKY: On cloud, we ON CLUSTER updates to events/sharded_events but not to persons. Why? ¯\_(ツ)_/¯
|
2022-03-03 12:29:39 +01:00
|
|
|
execute_on_cluster = f"ON CLUSTER '{CLICKHOUSE_CLUSTER}'" if table == "events" else ""
|
2021-09-07 13:53:57 +02:00
|
|
|
|
|
|
|
sync_execute(
|
|
|
|
f"""
|
|
|
|
ALTER TABLE {updated_table}
|
|
|
|
{execute_on_cluster}
|
|
|
|
MODIFY COLUMN
|
2022-08-30 13:31:32 +02:00
|
|
|
{column_name} VARCHAR MATERIALIZED {TRIM_AND_EXTRACT_PROPERTY.format(table_column=table_column)}
|
2021-09-07 13:53:57 +02:00
|
|
|
""",
|
|
|
|
{"property": property_name},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def get_materialized_columns_with_default_expression():
|
|
|
|
for table in ["events", "person"]:
|
|
|
|
materialized_columns = get_materialized_columns(table, use_cache=False)
|
2022-08-30 13:31:32 +02:00
|
|
|
for (property_name, table_column), column_name in materialized_columns.items():
|
2021-09-07 13:53:57 +02:00
|
|
|
if is_default_expression(table, column_name):
|
2022-08-30 13:31:32 +02:00
|
|
|
yield table, property_name, table_column, column_name
|
2021-09-07 13:53:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
def any_ongoing_mutations() -> bool:
|
|
|
|
running_mutations_count = sync_execute("SELECT count(*) FROM system.mutations WHERE is_done = 0")[0][0]
|
|
|
|
return running_mutations_count > 0
|
|
|
|
|
|
|
|
|
|
|
|
def is_default_expression(table: str, column_name: ColumnName) -> bool:
|
2022-03-16 09:27:39 +01:00
|
|
|
updated_table = "sharded_events" if clickhouse_is_replicated and table == "events" else table
|
2021-09-07 13:53:57 +02:00
|
|
|
column_query = sync_execute(
|
|
|
|
"SELECT default_kind FROM system.columns WHERE table = %(table)s AND name = %(name)s AND database = %(database)s",
|
2022-09-05 14:38:54 +02:00
|
|
|
{"table": updated_table, "name": column_name, "database": CLICKHOUSE_DATABASE},
|
2021-09-07 13:53:57 +02:00
|
|
|
)
|
|
|
|
return len(column_query) > 0 and column_query[0][0] == "DEFAULT"
|