Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

refactor(hogql): relocate property constants and enums to pure modules (#71913)

George Munyorof4367d587a4c
diff --git a/posthog/clickhouse/events_json.py b/posthog/clickhouse/events_json.py
new file mode 100644
index 000000000000..62222b022d7b
--- /dev/null
+++ b/posthog/clickhouse/events_json.py
@@ -0,0 +1,264 @@
+"""ClickHouse schema declarations for the native-JSON events tables.
+
+Table names, JSON subcolumn type declarations, and index DDL for the events-JSON schema.
+Django-free so the HogQL engine (printer, property planner) can import these without booting
+Django; posthog.models.event.sql re-exports the public names for existing callers.
+"""
+
+import re
+from functools import cache
+
+EVENTS_JSON_DATA_TABLE = "sharded_events_json"
+WRITABLE_EVENTS_JSON_TABLE = "writable_events_json"
+DISTRIBUTED_EVENTS_JSON_TABLE = "events_json"
+KAFKA_EVENTS_NATIVE_JSON_TABLE = "kafka_events_json_native_json"
+
+
+def _json_subcolumn_type_supports_nullable(column_type: str) -> bool:
+ return not column_type.startswith(("Array(", "Map("))
+
+
+def _nullable_json_subcolumn_types(subcolumns: dict[str, str]) -> dict[str, str]:
+ return {
+ path: column_type
+ if column_type.startswith("Nullable(") or not _json_subcolumn_type_supports_nullable(column_type)
+ else f"Nullable({column_type})"
+ for path, column_type in subcolumns.items()
+ }
+
+
+# Scalar JSON subcolumns are wrapped as Nullable so missing/JSON-null values stay distinct from scalar defaults such as
+# an empty string. Arrays and maps stay non-nullable because ClickHouse does not support Nullable(Array(...)) or
+# Nullable(Map(...)).
+EVENTS_PROPERTIES_JSON_SUBCOLUMN_DECLARED_TYPES: dict[str, str] = {
+ "$active_feature_flags": "Array(String)",
+ "$ai_experiment_id": "Nullable(String)",
+ "$ai_http_status": "Nullable(String)",
+ "$ai_is_error": "Nullable(String)",
+ "$ai_model": "Nullable(String)",
+ "$ai_parent_id": "Nullable(String)",
+ "$ai_prompt_name": "Nullable(String)",
+ "$ai_provider": "Nullable(String)",
+ "$ai_session_id": "Nullable(String)",
+ "$ai_span_id": "Nullable(String)",
+ "$ai_total_cost_usd": "Nullable(String)",
+ "$ai_trace_id": "Nullable(String)",
+ "$anon_distinct_id": "Nullable(String)",
+ "$app_build": "String",
+ "$app_namespace": "String",
+ "$app_version": "String",
+ "$browser": "String",
+ "$browser_version": "String",
+ "$current_url": "String",
+ "$device": "String",
+ "$device_id": "String",
+ "$device_model": "String",
+ "$device_type": "String",
+ "$el_text": "String",
+ "$event_type": "String",
+ "$exception_fingerprint": "Nullable(String)",
+ "$exception_functions": "Array(String)",
+ "$exception_issue_id": "Nullable(String)",
+ "$exception_sources": "Array(String)",
+ "$exception_types": "Array(String)",
+ "$exception_values": "Array(String)",
+ "$feature_flag": "String",
+ "$feature_flag_payloads": "String",
+ "$feature_flag_response": "String",
+ "$geoip_city_name": "String",
+ "$geoip_country_code": "String",
+ "$geoip_country_name": "String",
+ "$geoip_subdivision_1_code": "String",
+ "$group_0": "String",
+ "$group_1": "String",
+ "$group_2": "String",
+ "$group_3": "String",
+ "$group_4": "String",
+ "$groups": "String",
+ "$host": "String",
+ "$initial_pathname": "String",
+ "$initial_referrer": "String",
+ "$initial_referring_domain": "String",
+ "$ip": "String",
+ "$is_identified": "Nullable(String)",
+ "$lib": "String",
+ "$lib_custom_api_host": "String",
+ "$lib_version": "String",
+ "$lib_version__minor": "String",
+ "$os": "String",
+ "$os_name": "String",
+ "$os_version": "String",
+ "$pathname": "String",
+ "$prev_pageview_max_content_percentage": "String",
+ "$prev_pageview_max_scroll_percentage": "String",
+ "$prev_pageview_pathname": "String",
+ "$process_person_profile": "Nullable(String)",
+ "$referrer": "String",
+ "$referring_domain": "String",
+ "$screen_height": "String",
+ "$screen_name": "String",
+ "$screen_width": "String",
+ "$sent_at": "String",
+ "$session_id": "String",
+ "$survey_id": "String",
+ "$survey_response": "String",
+ "$survey_response_1": "String",
+ "$time": "String",
+ "$user_id": "String",
+ "$viewport_height": "Nullable(String)",
+ "$viewport_width": "Nullable(String)",
+ "$web_vitals_CLS_value": "Nullable(String)",
+ "$web_vitals_FCP_value": "Nullable(String)",
+ "$web_vitals_INP_value": "Nullable(String)",
+ "$web_vitals_LCP_value": "Nullable(String)",
+ "$window_id": "String",
+}
+
+EVENTS_PROPERTIES_JSON_SUBCOLUMNS: dict[str, str] = _nullable_json_subcolumn_types(
+ EVENTS_PROPERTIES_JSON_SUBCOLUMN_DECLARED_TYPES
+)
+
+
+PERSON_PROPERTIES_JSON_SUBCOLUMN_DECLARED_TYPES: dict[str, str] = {
+ "$app_version": "String",
+ "$browser": "String",
+ "$current_url": "String",
+ "$geoip_continent_name": "String",
+ "$geoip_country_code": "String",
+ "$geoip_country_name": "String",
+ "$initial_current_url": "String",
+ "$initial_fbclid": "String",
+ "$initial_gad_source": "String",
+ "$initial_gbraid": "String",
+ "$initial_gclid": "String",
+ "$initial_msclkid": "String",
+ "$initial_pathname": "String",
+ "$initial_referring_domain": "String",
+ "$initial_utm_campaign": "String",
+ "$initial_utm_content": "String",
+ "$initial_utm_medium": "String",
+ "$initial_utm_source": "String",
+ "$initial_utm_term": "String",
+ "$initial_wbraid": "String",
+ "$os_name": "String",
+ "$referring_domain": "String",
+}
+
+PERSON_PROPERTIES_JSON_SUBCOLUMNS: dict[str, str] = _nullable_json_subcolumn_types(
+ PERSON_PROPERTIES_JSON_SUBCOLUMN_DECLARED_TYPES
+)
+
+
+def EVENTS_JSON_DATA_TABLE_INDEXES() -> str:
+ indexes = [
+ "INDEX kafka_timestamp_minmax_sharded_events _timestamp TYPE minmax GRANULARITY 3",
+ "INDEX `minmax_$group_0` properties.`$group_0` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_$group_1` properties.`$group_1` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_$group_2` properties.`$group_2` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_$group_3` properties.`$group_3` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_$group_4` properties.`$group_4` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_$window_id` properties.`$window_id` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_$session_id` properties.`$session_id` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$sent_at` properties.`$sent_at` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$initial_pathname` properties.`$initial_pathname` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$lib_version` properties.`$lib_version` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_pp_$initial_utm_campaign` person_properties.`$initial_utm_campaign` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_pp_$initial_utm_medium` person_properties.`$initial_utm_medium` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_pp_$initial_gclid` person_properties.`$initial_gclid` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_pp_$initial_gad_source` person_properties.`$initial_gad_source` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_pp_$initial_utm_source` person_properties.`$initial_utm_source` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_pp_$initial_referring_domain` person_properties.`$initial_referring_domain` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_pp_$initial_utm_term` person_properties.`$initial_utm_term` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_pp_$initial_utm_content` person_properties.`$initial_utm_content` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_pp_$initial_gbraid` person_properties.`$initial_gbraid` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_pp_$initial_wbraid` person_properties.`$initial_wbraid` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_pp_$initial_msclkid` person_properties.`$initial_msclkid` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_pp_$initial_fbclid` person_properties.`$initial_fbclid` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$geoip_subdivision_1_code` properties.`$geoip_subdivision_1_code` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$prev_pageview_max_scroll_percentage` properties.`$prev_pageview_max_scroll_percentage` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$prev_pageview_max_content_percentage` properties.`$prev_pageview_max_content_percentage` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$prev_pageview_pathname` properties.`$prev_pageview_pathname` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_pp_$initial_pathname` person_properties.`$initial_pathname` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_pp_$geoip_country_code` person_properties.`$geoip_country_code` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$browser_version` properties.`$browser_version` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_pp_$initial_current_url` person_properties.`$initial_current_url` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_pp_$current_url` person_properties.`$current_url` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$app_namespace` properties.`$app_namespace` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$os_name` properties.`$os_name` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_pp_$os_name` person_properties.`$os_name` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_pp_$app_version` person_properties.`$app_version` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$screen_height` properties.`$screen_height` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$screen_width` properties.`$screen_width` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$app_build` properties.`$app_build` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$geoip_country_code` properties.`$geoip_country_code` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$survey_id` properties.`$survey_id` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$survey_response_1` properties.`$survey_response_1` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$survey_response` properties.`$survey_response` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$el_text` properties.`$el_text` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$os_version` properties.`$os_version` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$feature_flag_payloads` properties.`$feature_flag_payloads` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$groups` properties.`$groups` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$feature_flag` properties.`$feature_flag` TYPE minmax GRANULARITY 1",
+ "INDEX bf_active_feature_flags properties.`$active_feature_flags` TYPE bloom_filter(0.01) GRANULARITY 1",
+ "INDEX `minmax_mat_$device_id` properties.`$device_id` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_pp_$geoip_continent_name` person_properties.`$geoip_continent_name` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$feature_flag_response` properties.`$feature_flag_response` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_pp_$referring_domain` person_properties.`$referring_domain` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$lib_version__minor` properties.`$lib_version__minor` TYPE minmax GRANULARITY 1",
+ "INDEX minmax_inserted_at coalesce(inserted_at, _timestamp) TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$lib_custom_api_host` properties.`$lib_custom_api_host` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_pp_$geoip_country_name` person_properties.`$geoip_country_name` TYPE minmax GRANULARITY 1",
+ "INDEX is_deleted_idx is_deleted TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$device` properties.`$device` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$exception_issue_id` properties.`$exception_issue_id` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$exception_fingerprint` properties.`$exception_fingerprint` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$web_vitals_LCP_value` properties.`$web_vitals_LCP_value` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$web_vitals_FCP_value` properties.`$web_vitals_FCP_value` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$web_vitals_CLS_value` properties.`$web_vitals_CLS_value` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$web_vitals_INP_value` properties.`$web_vitals_INP_value` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$viewport_width` properties.`$viewport_width` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$viewport_height` properties.`$viewport_height` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$anon_distinct_id` properties.`$anon_distinct_id` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$ai_trace_id` properties.`$ai_trace_id` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$ai_model` properties.`$ai_model` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$ai_provider` properties.`$ai_provider` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$ai_parent_id` properties.`$ai_parent_id` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$ai_span_id` properties.`$ai_span_id` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$ai_http_status` properties.`$ai_http_status` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$process_person_profile` properties.`$process_person_profile` TYPE minmax GRANULARITY 1",
+ "INDEX `minmax_mat_$app_version` properties.`$app_version` TYPE minmax GRANULARITY 1",
+ "INDEX `bloom_mat_$is_identified` properties.`$is_identified` TYPE bloom_filter GRANULARITY 1",
+ "INDEX `minmax_$session_id_uuid` toUInt128(toUUIDOrNull(properties.`$session_id`)) TYPE minmax GRANULARITY 1",
+ "INDEX `bloom_filter_$ai_trace_id` properties.`$ai_trace_id` TYPE bloom_filter(0.001) GRANULARITY 2",
+ "INDEX `bloom_filter_$ai_session_id` properties.`$ai_session_id` TYPE bloom_filter GRANULARITY 1",
+ "INDEX `minmax_$ai_session_id` properties.`$ai_session_id` TYPE minmax GRANULARITY 1",
+ "INDEX `set_$ai_is_error` properties.`$ai_is_error` TYPE set(7) GRANULARITY 1",
+ "INDEX `minmax_mat_$ai_total_cost_usd` properties.`$ai_total_cost_usd` TYPE minmax GRANULARITY 1",
+ "INDEX bloom_filter_distinct_id distinct_id TYPE bloom_filter GRANULARITY 1",
+ "INDEX minmax_sharded_events_timestamp timestamp TYPE minmax GRANULARITY 1",
+ "INDEX minmax_historical_migration historical_migration TYPE minmax GRANULARITY 1",
+ "INDEX `bloom_mat_$feature_flag` properties.`$feature_flag` TYPE bloom_filter GRANULARITY 1",
+ "INDEX `bloom_filter_$ai_prompt_name` properties.`$ai_prompt_name` TYPE bloom_filter GRANULARITY 1",
+ "INDEX `minmax_$ai_prompt_name` properties.`$ai_prompt_name` TYPE minmax GRANULARITY 1",
+ "INDEX `bloom_filter_$ai_experiment_id` properties.`$ai_experiment_id` TYPE bloom_filter GRANULARITY 1",
+ "INDEX `minmax_$ai_experiment_id` properties.`$ai_experiment_id` TYPE minmax GRANULARITY 1",
+ ]
+ return " , " + "\n , ".join(indexes)
+
+
+@cache
+def EVENTS_JSON_INDEXED_PROPERTY_NAMES(field_name: str, index_type: str) -> frozenset[str]:
+ indexed_property_names: set[str] = set()
+ column_pattern = re.compile(
+ rf"\b{re.escape(field_name)}\.(?:`(?P<quoted>[^`]+)`|(?P<identifier>[A-Za-z_][A-Za-z0-9_]*))"
+ )
+
+ for index_definition in EVENTS_JSON_DATA_TABLE_INDEXES().splitlines():
+ type_match = re.search(r"\bTYPE\s+(?P<type>[A-Za-z_][A-Za-z0-9_]*)", index_definition)
+ if type_match is None or type_match.group("type") != index_type:
+ continue
+
+ for column_match in column_pattern.finditer(index_definition):
+ indexed_property_names.add(column_match.group("quoted") or column_match.group("identifier"))
+
+ return frozenset(indexed_property_names)
diff --git a/posthog/hogql/database/database.py b/posthog/hogql/database/database.py
index caa8258641ea..67c2c17b7a79 100644
--- a/posthog/hogql/database/database.py
+++ b/posthog/hogql/database/database.py
@@ -148,6 +148,7 @@ from posthog.ph_client import feature_enabled_or_false
from posthog.schema_enums import DatabaseSerializedFieldType, PersonsOnEventsMode, SessionTableVersion
from posthog.scopes import APIScopeObject
from posthog.synthetic_user import SyntheticUser
+from posthog.week_start_day import WeekStartDay
# The Django ORM / products models below are imported lazily inside the functions that build a
# Database (Database._fetch_sources / _build_from_sources / serialize and their helpers) so this
@@ -168,7 +169,7 @@ if TYPE_CHECKING:
)
from posthog.models import User
- from posthog.models.team.team import Team, WeekStartDay
+ from posthog.models.team.team import Team
from posthog.rbac.user_access_control import UserAccessControl
from posthog.shared_link_user import SharedLinkUser
@@ -550,8 +551,6 @@ class Database(BaseModel):
return self._timezone or "UTC"
def get_week_start_day(self) -> WeekStartDay:
- from posthog.models.team.team import WeekStartDay # noqa: PLC0415
-
return self._week_start_day or WeekStartDay.SUNDAY
def get_serialization_errors(self) -> dict[str, str]:
diff --git a/posthog/hogql/database/schema/events.py b/posthog/hogql/database/schema/events.py
index b6174a1cab85..f11f34f044f8 100644
--- a/posthog/hogql/database/schema/events.py
+++ b/posthog/hogql/database/schema/events.py
@@ -22,11 +22,10 @@ from posthog.hogql.database.schema.person_distinct_ids import PersonDistinctIdsT
from posthog.hogql.database.schema.persons_revenue_analytics import PersonsRevenueAnalyticsTable
from posthog.hogql.database.schema.sessions_v1 import SessionsTableV1
+from posthog.clickhouse.events_json import DISTRIBUTED_EVENTS_JSON_TABLE
-def events_table_clickhouse_table_ref(context) -> str:
- # lazy import keeps the events SQL module (Django ORM) off this module's import path
- from posthog.models.event.sql import DISTRIBUTED_EVENTS_JSON_TABLE # noqa: PLC0415
+def events_table_clickhouse_table_ref(context) -> str:
return DISTRIBUTED_EVENTS_JSON_TABLE if context.uses_new_events_schema() else "events"
diff --git a/posthog/hogql/printer/base.py b/posthog/hogql/printer/base.py
index 71fface8998a..cf62b489b38c 100644
--- a/posthog/hogql/printer/base.py
+++ b/posthog/hogql/printer/base.py
@@ -2,7 +2,7 @@ import re
from collections.abc import Iterable
from datetime import date, datetime
from difflib import get_close_matches
-from typing import TYPE_CHECKING, Any, ClassVar, Optional, cast, get_args
+from typing import Any, ClassVar, Optional, cast, get_args
from uuid import UUID
from django.conf import settings as django_settings
@@ -36,9 +36,7 @@ from posthog.hogql.visitor import Visitor, clone_expr
from posthog.clickhouse.kafka_engine import json_extract_trim_quotes
from posthog.schema_enums import PersonsOnEventsMode
from posthog.uuidt import UUIDT
-
-if TYPE_CHECKING:
- from posthog.models.team.team import WeekStartDay
+from posthog.week_start_day import WeekStartDay
MAX_PLACEHOLDER_MACRO_EXPANSION_DEPTH = 8
@@ -1527,9 +1525,7 @@ class BasePrinter(Visitor[str]):
return "UTC"
return self.context.database.get_timezone() if self.context.database else "UTC"
- def _get_week_start_day(self) -> "WeekStartDay":
- from posthog.models.team.team import WeekStartDay # noqa: PLC0415
-
+ def _get_week_start_day(self) -> WeekStartDay:
return self.context.database.get_week_start_day() if self.context.database else WeekStartDay.SUNDAY
def _is_type_nullable(self, node_type: ast.Type) -> bool | None:
diff --git a/posthog/hogql/printer/clickhouse.py b/posthog/hogql/printer/clickhouse.py
index 53232642c72f..d1672c286c13 100644
--- a/posthog/hogql/printer/clickhouse.py
+++ b/posthog/hogql/printer/clickhouse.py
@@ -34,9 +34,10 @@ from posthog.hogql.restricted_properties import restricted_property_keys_for_tab
from posthog.hogql.type_system import parse_sql_runtime_type
from posthog.hogql.visitor import GetFieldsTraverser, clone_expr
+from posthog.clickhouse.events_json import EVENTS_PROPERTIES_JSON_SUBCOLUMNS, PERSON_PROPERTIES_JSON_SUBCOLUMNS
from posthog.exchange_rate_constants import EXCHANGE_RATE_DECIMAL_PRECISION, EXCHANGE_RATE_DICTIONARY_NAME
-from posthog.models.event.sql import EVENTS_PROPERTIES_JSON_SUBCOLUMNS, PERSON_PROPERTIES_JSON_SUBCOLUMNS
from posthog.uuidt import UUIDT
+from posthog.week_start_day import WeekStartDay
def _table_filter_type(table_type: ast.TableOrSelectType) -> ast.TableOrSelectType:
@@ -277,8 +278,6 @@ class ClickHousePrinter(BasePrinter):
args = [*args[:-1], "6", *args[-1:]]
if node.name == "toStartOfWeek" and len(node.args) == 1:
- from posthog.models.team.team import WeekStartDay # noqa: PLC0415
-
# If week mode hasn't been specified, use the project's default.
# For Monday-based weeks mode 3 is used (which is ISO 8601), for Sunday-based mode 0 (CH default)
args.insert(1, WeekStartDay(self._get_week_start_day()).clickhouse_mode)
diff --git a/posthog/hogql/property_planner.py b/posthog/hogql/property_planner.py
index 2f6c6d89692a..5794d99dd533 100644
--- a/posthog/hogql/property_planner.py
+++ b/posthog/hogql/property_planner.py
@@ -23,6 +23,11 @@ from posthog.hogql.type_system import (
runtime_type_from_constant_type,
)
+from posthog.clickhouse.events_json import (
+ EVENTS_JSON_INDEXED_PROPERTY_NAMES,
+ EVENTS_PROPERTIES_JSON_SUBCOLUMNS,
+ PERSON_PROPERTIES_JSON_SUBCOLUMNS,
+)
from posthog.clickhouse.materialized_columns import (
MATERIALIZATION_VALID_TABLES,
MaterializedColumn,
@@ -30,15 +35,10 @@ from posthog.clickhouse.materialized_columns import (
get_materialized_column_for_property,
)
from posthog.clickhouse.property_groups import property_groups
-from posthog.models.event.sql import (
- EVENTS_JSON_INDEXED_PROPERTY_NAMES,
- EVENTS_PROPERTIES_JSON_SUBCOLUMNS,
- PERSON_PROPERTIES_JSON_SUBCOLUMNS,
-)
-from posthog.models.property import PropertyName, TableColumn
+from posthog.property_columns import PropertyName, TableColumn
from posthog.schema_enums import PropertyGroupsMode
-from products.event_definitions.backend.models.property_definition import PropertyType
+from products.event_definitions.backend.property_type import PropertyType
class PropertyScope(StrEnum):
diff --git a/posthog/hogql/test/test_no_django_imports.py b/posthog/hogql/test/test_no_django_imports.py
index 6234f36770b6..c8de0a19cdb4 100644
--- a/posthog/hogql/test/test_no_django_imports.py
+++ b/posthog/hogql/test/test_no_django_imports.py
@@ -11,6 +11,10 @@ DJANGO_FREE_MODULES = [
"posthog.uuidt",
"posthog.exchange_rate_constants",
"posthog.raw_sessions_v3_ad_ids",
+ "posthog.clickhouse.events_json",
+ "posthog.property_columns",
+ "posthog.week_start_day",
+ "products.event_definitions.backend.property_type",
"posthog.hogql.ast",
"posthog.hogql.base",
"posthog.hogql.visitor",
diff --git a/posthog/hogql/transforms/clickhouse_property_resolution.py b/posthog/hogql/transforms/clickhouse_property_resolution.py
index de43ee96bf47..979531258398 100644
--- a/posthog/hogql/transforms/clickhouse_property_resolution.py
+++ b/posthog/hogql/transforms/clickhouse_property_resolution.py
@@ -43,14 +43,14 @@ from posthog.hogql.type_system import (
from posthog.hogql.utils import ilike_matches, like_matches
from posthog.hogql.visitor import CloningVisitor, clone_expr
-from posthog.clickhouse.materialized_columns import TablesWithMaterializedColumns, get_materialized_column_for_property
-from posthog.clickhouse.property_groups import property_groups
-from posthog.models.event.sql import (
+from posthog.clickhouse.events_json import (
DISTRIBUTED_EVENTS_JSON_TABLE,
EVENTS_PROPERTIES_JSON_SUBCOLUMNS,
PERSON_PROPERTIES_JSON_SUBCOLUMNS,
)
-from posthog.models.property import PropertyName, TableColumn
+from posthog.clickhouse.materialized_columns import TablesWithMaterializedColumns, get_materialized_column_for_property
+from posthog.clickhouse.property_groups import property_groups
+from posthog.property_columns import PropertyName, TableColumn
from posthog.schema_enums import MaterializationMode, PropertyGroupsMode
# In non-nullable materialized columns these stored strings are treated as NULL.
diff --git a/posthog/hogql/transforms/property_types.py b/posthog/hogql/transforms/property_types.py
index c6c7431c0820..92012fe41b87 100644
--- a/posthog/hogql/transforms/property_types.py
+++ b/posthog/hogql/transforms/property_types.py
@@ -25,6 +25,7 @@ from posthog.hogql.restricted_properties import restricted_property_keys_for_tab
from posthog.hogql.type_system import normalized_runtime_type, parse_sql_runtime_type
from posthog.hogql.visitor import CloningVisitor, TraversingVisitor
+from posthog.clickhouse.events_json import EVENTS_PROPERTIES_JSON_SUBCOLUMNS, PERSON_PROPERTIES_JSON_SUBCOLUMNS
from posthog.clickhouse.materialized_columns import (
DMAT_STRING_COLUMN_NAME_PREFIX,
MATERIALIZATION_VALID_TABLES,
@@ -33,8 +34,7 @@ from posthog.clickhouse.materialized_columns import (
get_materialized_column_for_property,
)
from posthog.models import Team
-from posthog.models.event.sql import EVENTS_PROPERTIES_JSON_SUBCOLUMNS, PERSON_PROPERTIES_JSON_SUBCOLUMNS
-from posthog.models.property import PropertyName, TableColumn
+from posthog.property_columns import PropertyName, TableColumn
_JSON_EXTRACT_SCALAR_CASTS: dict[str, tuple[str, object]] = {
"JSONExtractString": ("String", ""),
diff --git a/posthog/models/event/sql.py b/posthog/models/event/sql.py
index 8b0825d90953..33b6dcbe2c7d 100644
--- a/posthog/models/event/sql.py
+++ b/posthog/models/event/sql.py
@@ -1,12 +1,24 @@
-import re
-from functools import cache
-
from django.conf import settings
from posthog.hogql.escape_sql import escape_clickhouse_identifier, escape_clickhouse_string
from posthog.clickhouse.base_sql import COPY_ROWS_BETWEEN_TEAMS_BASE_SQL
from posthog.clickhouse.cluster import ON_CLUSTER_CLAUSE
+
+# The events-JSON schema declarations (table names, JSON subcolumn types, index DDL) live in the
+# Django-free posthog.clickhouse.events_json module so the HogQL engine can use them without
+# booting Django; re-exported here for existing callers.
+from posthog.clickhouse.events_json import (
+ DISTRIBUTED_EVENTS_JSON_TABLE,
+ EVENTS_JSON_DATA_TABLE,
+ EVENTS_JSON_DATA_TABLE_INDEXES,
+ EVENTS_JSON_INDEXED_PROPERTY_NAMES, # noqa: F401
+ EVENTS_PROPERTIES_JSON_SUBCOLUMNS,
+ KAFKA_EVENTS_NATIVE_JSON_TABLE,
+ PERSON_PROPERTIES_JSON_SUBCOLUMNS,
+ WRITABLE_EVENTS_JSON_TABLE,
+ _json_subcolumn_type_supports_nullable,
+)
from posthog.clickhouse.indexes import index_by_kafka_timestamp
from posthog.clickhouse.kafka_engine import (
CONSUMER_GROUP_EVENTS_JSON,
@@ -30,147 +42,6 @@ def WRITABLE_EVENTS_DATA_TABLE():
return "writable_events"
-EVENTS_JSON_DATA_TABLE = "sharded_events_json"
-WRITABLE_EVENTS_JSON_TABLE = "writable_events_json"
-DISTRIBUTED_EVENTS_JSON_TABLE = "events_json"
-KAFKA_EVENTS_NATIVE_JSON_TABLE = "kafka_events_json_native_json"
-
-
-def _json_subcolumn_type_supports_nullable(column_type: str) -> bool:
- return not column_type.startswith(("Array(", "Map("))
-
-
-def _nullable_json_subcolumn_types(subcolumns: dict[str, str]) -> dict[str, str]:
- return {
- path: column_type
- if column_type.startswith("Nullable(") or not _json_subcolumn_type_supports_nullable(column_type)
- else f"Nullable({column_type})"
- for path, column_type in subcolumns.items()
- }
-
-
-# Scalar JSON subcolumns are wrapped as Nullable so missing/JSON-null values stay distinct from scalar defaults such as
-# an empty string. Arrays and maps stay non-nullable because ClickHouse does not support Nullable(Array(...)) or
-# Nullable(Map(...)).
-EVENTS_PROPERTIES_JSON_SUBCOLUMN_DECLARED_TYPES: dict[str, str] = {
- "$active_feature_flags": "Array(String)",
- "$ai_experiment_id": "Nullable(String)",
- "$ai_http_status": "Nullable(String)",
- "$ai_is_error": "Nullable(String)",
- "$ai_model": "Nullable(String)",
- "$ai_parent_id": "Nullable(String)",
- "$ai_prompt_name": "Nullable(String)",
- "$ai_provider": "Nullable(String)",
- "$ai_session_id": "Nullable(String)",
- "$ai_span_id": "Nullable(String)",
- "$ai_total_cost_usd": "Nullable(String)",
- "$ai_trace_id": "Nullable(String)",
- "$anon_distinct_id": "Nullable(String)",
- "$app_build": "String",
- "$app_namespace": "String",
- "$app_version": "String",
- "$browser": "String",
- "$browser_version": "String",
- "$current_url": "String",
- "$device": "String",
- "$device_id": "String",
- "$device_model": "String",
- "$device_type": "String",
- "$el_text": "String",
- "$event_type": "String",
- "$exception_fingerprint": "Nullable(String)",
- "$exception_functions": "Array(String)",
- "$exception_issue_id": "Nullable(String)",
- "$exception_sources": "Array(String)",
- "$exception_types": "Array(String)",
- "$exception_values": "Array(String)",
- "$feature_flag": "String",
- "$feature_flag_payloads": "String",
- "$feature_flag_response": "String",
- "$geoip_city_name": "String",
- "$geoip_country_code": "String",
- "$geoip_country_name": "String",
- "$geoip_subdivision_1_code": "String",
- "$group_0": "String",
- "$group_1": "String",
- "$group_2": "String",
- "$group_3": "String",
- "$group_4": "String",
- "$groups": "String",
- "$host": "String",
- "$initial_pathname": "String",
- "$initial_referrer": "String",
- "$initial_referring_domain": "String",
- "$ip": "String",
- "$is_identified": "Nullable(String)",
- "$lib": "String",
- "$lib_custom_api_host": "String",
- "$lib_version": "String",
- "$lib_version__minor": "String",
- "$os": "String",
- "$os_name": "String",
- "$os_version": "String",
- "$pathname": "String",
- "$prev_pageview_max_content_percentage": "String",
- "$prev_pageview_max_scroll_percentage": "String",
- "$prev_pageview_pathname": "String",
- "$process_person_profile": "Nullable(String)",
- "$referrer": "String",
- "$referring_domain": "String",
- "$screen_height": "String",
- "$screen_name": "String",
- "$screen_width": "String",
- "$sent_at": "String",
- "$session_id": "String",
- "$survey_id": "String",
- "$survey_response": "String",
- "$survey_response_1": "String",
- "$time": "String",
- "$user_id": "String",
- "$viewport_height": "Nullable(String)",
- "$viewport_width": "Nullable(String)",
- "$web_vitals_CLS_value": "Nullable(String)",
- "$web_vitals_FCP_value": "Nullable(String)",
- "$web_vitals_INP_value": "Nullable(String)",
- "$web_vitals_LCP_value": "Nullable(String)",
- "$window_id": "String",
-}
-
-EVENTS_PROPERTIES_JSON_SUBCOLUMNS: dict[str, str] = _nullable_json_subcolumn_types(
- EVENTS_PROPERTIES_JSON_SUBCOLUMN_DECLARED_TYPES
-)
-
-
-PERSON_PROPERTIES_JSON_SUBCOLUMN_DECLARED_TYPES: dict[str, str] = {
- "$app_version": "String",
- "$browser": "String",
- "$current_url": "String",
- "$geoip_continent_name": "String",
- "$geoip_country_code": "String",
- "$geoip_country_name": "String",
- "$initial_current_url": "String",
- "$initial_fbclid": "String",
- "$initial_gad_source": "String",
- "$initial_gbraid": "String",
- "$initial_gclid": "String",
- "$initial_msclkid": "String",
- "$initial_pathname": "String",
- "$initial_referring_domain": "String",
- "$initial_utm_campaign": "String",
- "$initial_utm_content": "String",
- "$initial_utm_medium": "String",
- "$initial_utm_source": "String",
- "$initial_utm_term": "String",
- "$initial_wbraid": "String",
- "$os_name": "String",
- "$referring_domain": "String",
-}
-
-PERSON_PROPERTIES_JSON_SUBCOLUMNS: dict[str, str] = _nullable_json_subcolumn_types(
- PERSON_PROPERTIES_JSON_SUBCOLUMN_DECLARED_TYPES
-)
-
-
def _json_column_type(max_dynamic_types: int, max_dynamic_paths: int, subcolumns: dict[str, str]) -> str:
explicit_paths = ", ".join(
f"{escape_clickhouse_identifier(name)} {column_type}" for name, column_type in subcolumns.items()
@@ -416,121 +287,6 @@ CREATE TABLE IF NOT EXISTS {table_name} {on_cluster_clause}
"""
-def EVENTS_JSON_DATA_TABLE_INDEXES() -> str:
- indexes = [
- "INDEX kafka_timestamp_minmax_sharded_events _timestamp TYPE minmax GRANULARITY 3",
- "INDEX `minmax_$group_0` properties.`$group_0` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_$group_1` properties.`$group_1` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_$group_2` properties.`$group_2` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_$group_3` properties.`$group_3` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_$group_4` properties.`$group_4` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_$window_id` properties.`$window_id` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_$session_id` properties.`$session_id` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$sent_at` properties.`$sent_at` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$initial_pathname` properties.`$initial_pathname` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$lib_version` properties.`$lib_version` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_pp_$initial_utm_campaign` person_properties.`$initial_utm_campaign` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_pp_$initial_utm_medium` person_properties.`$initial_utm_medium` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_pp_$initial_gclid` person_properties.`$initial_gclid` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_pp_$initial_gad_source` person_properties.`$initial_gad_source` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_pp_$initial_utm_source` person_properties.`$initial_utm_source` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_pp_$initial_referring_domain` person_properties.`$initial_referring_domain` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_pp_$initial_utm_term` person_properties.`$initial_utm_term` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_pp_$initial_utm_content` person_properties.`$initial_utm_content` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_pp_$initial_gbraid` person_properties.`$initial_gbraid` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_pp_$initial_wbraid` person_properties.`$initial_wbraid` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_pp_$initial_msclkid` person_properties.`$initial_msclkid` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_pp_$initial_fbclid` person_properties.`$initial_fbclid` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$geoip_subdivision_1_code` properties.`$geoip_subdivision_1_code` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$prev_pageview_max_scroll_percentage` properties.`$prev_pageview_max_scroll_percentage` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$prev_pageview_max_content_percentage` properties.`$prev_pageview_max_content_percentage` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$prev_pageview_pathname` properties.`$prev_pageview_pathname` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_pp_$initial_pathname` person_properties.`$initial_pathname` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_pp_$geoip_country_code` person_properties.`$geoip_country_code` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$browser_version` properties.`$browser_version` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_pp_$initial_current_url` person_properties.`$initial_current_url` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_pp_$current_url` person_properties.`$current_url` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$app_namespace` properties.`$app_namespace` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$os_name` properties.`$os_name` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_pp_$os_name` person_properties.`$os_name` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_pp_$app_version` person_properties.`$app_version` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$screen_height` properties.`$screen_height` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$screen_width` properties.`$screen_width` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$app_build` properties.`$app_build` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$geoip_country_code` properties.`$geoip_country_code` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$survey_id` properties.`$survey_id` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$survey_response_1` properties.`$survey_response_1` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$survey_response` properties.`$survey_response` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$el_text` properties.`$el_text` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$os_version` properties.`$os_version` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$feature_flag_payloads` properties.`$feature_flag_payloads` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$groups` properties.`$groups` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$feature_flag` properties.`$feature_flag` TYPE minmax GRANULARITY 1",
- "INDEX bf_active_feature_flags properties.`$active_feature_flags` TYPE bloom_filter(0.01) GRANULARITY 1",
- "INDEX `minmax_mat_$device_id` properties.`$device_id` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_pp_$geoip_continent_name` person_properties.`$geoip_continent_name` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$feature_flag_response` properties.`$feature_flag_response` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_pp_$referring_domain` person_properties.`$referring_domain` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$lib_version__minor` properties.`$lib_version__minor` TYPE minmax GRANULARITY 1",
- "INDEX minmax_inserted_at coalesce(inserted_at, _timestamp) TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$lib_custom_api_host` properties.`$lib_custom_api_host` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_pp_$geoip_country_name` person_properties.`$geoip_country_name` TYPE minmax GRANULARITY 1",
- "INDEX is_deleted_idx is_deleted TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$device` properties.`$device` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$exception_issue_id` properties.`$exception_issue_id` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$exception_fingerprint` properties.`$exception_fingerprint` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$web_vitals_LCP_value` properties.`$web_vitals_LCP_value` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$web_vitals_FCP_value` properties.`$web_vitals_FCP_value` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$web_vitals_CLS_value` properties.`$web_vitals_CLS_value` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$web_vitals_INP_value` properties.`$web_vitals_INP_value` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$viewport_width` properties.`$viewport_width` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$viewport_height` properties.`$viewport_height` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$anon_distinct_id` properties.`$anon_distinct_id` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$ai_trace_id` properties.`$ai_trace_id` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$ai_model` properties.`$ai_model` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$ai_provider` properties.`$ai_provider` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$ai_parent_id` properties.`$ai_parent_id` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$ai_span_id` properties.`$ai_span_id` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$ai_http_status` properties.`$ai_http_status` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$process_person_profile` properties.`$process_person_profile` TYPE minmax GRANULARITY 1",
- "INDEX `minmax_mat_$app_version` properties.`$app_version` TYPE minmax GRANULARITY 1",
- "INDEX `bloom_mat_$is_identified` properties.`$is_identified` TYPE bloom_filter GRANULARITY 1",
- "INDEX `minmax_$session_id_uuid` toUInt128(toUUIDOrNull(properties.`$session_id`)) TYPE minmax GRANULARITY 1",
- "INDEX `bloom_filter_$ai_trace_id` properties.`$ai_trace_id` TYPE bloom_filter(0.001) GRANULARITY 2",
- "INDEX `bloom_filter_$ai_session_id` properties.`$ai_session_id` TYPE bloom_filter GRANULARITY 1",
- "INDEX `minmax_$ai_session_id` properties.`$ai_session_id` TYPE minmax GRANULARITY 1",
- "INDEX `set_$ai_is_error` properties.`$ai_is_error` TYPE set(7) GRANULARITY 1",
- "INDEX `minmax_mat_$ai_total_cost_usd` properties.`$ai_total_cost_usd` TYPE minmax GRANULARITY 1",
- "INDEX bloom_filter_distinct_id distinct_id TYPE bloom_filter GRANULARITY 1",
- "INDEX minmax_sharded_events_timestamp timestamp TYPE minmax GRANULARITY 1",
- "INDEX minmax_historical_migration historical_migration TYPE minmax GRANULARITY 1",
- "INDEX `bloom_mat_$feature_flag` properties.`$feature_flag` TYPE bloom_filter GRANULARITY 1",
- "INDEX `bloom_filter_$ai_prompt_name` properties.`$ai_prompt_name` TYPE bloom_filter GRANULARITY 1",
- "INDEX `minmax_$ai_prompt_name` properties.`$ai_prompt_name` TYPE minmax GRANULARITY 1",
- "INDEX `bloom_filter_$ai_experiment_id` properties.`$ai_experiment_id` TYPE bloom_filter GRANULARITY 1",
- "INDEX `minmax_$ai_experiment_id` properties.`$ai_experiment_id` TYPE minmax GRANULARITY 1",
- ]
- return " , " + "\n , ".join(indexes)
-
-
-@cache
-def EVENTS_JSON_INDEXED_PROPERTY_NAMES(field_name: str, index_type: str) -> frozenset[str]:
- indexed_property_names: set[str] = set()
- column_pattern = re.compile(
- rf"\b{re.escape(field_name)}\.(?:`(?P<quoted>[^`]+)`|(?P<identifier>[A-Za-z_][A-Za-z0-9_]*))"
- )
-
- for index_definition in EVENTS_JSON_DATA_TABLE_INDEXES().splitlines():
- type_match = re.search(r"\bTYPE\s+(?P<type>[A-Za-z_][A-Za-z0-9_]*)", index_definition)
- if type_match is None or type_match.group("type") != index_type:
- continue
-
- for column_match in column_pattern.finditer(index_definition):
- indexed_property_names.add(column_match.group("quoted") or column_match.group("identifier"))
-
- return frozenset(indexed_property_names)
-
-
def EVENTS_JSON_TABLE_SQL(on_cluster: bool = False) -> str:
return (
EVENTS_JSON_TABLE_BASE_SQL
diff --git a/posthog/models/property/property.py b/posthog/models/property/property.py
index 7e50b0a61b28..0bb01fa81ace 100644
--- a/posthog/models/property/property.py
+++ b/posthog/models/property/property.py
@@ -6,6 +6,10 @@ from typing import Any, Literal, Optional, Union, cast
from posthog.constants import PropertyOperatorType
from posthog.models.filters.mixins.utils import cached_property
from posthog.models.filters.utils import GroupTypeIndex, validate_group_type_index
+
+# Relocated to the Django-free posthog.property_columns module so the HogQL engine can use them
+# without booting Django; re-exported here for existing callers.
+from posthog.property_columns import PropertyName, TableColumn, TableWithProperties # noqa: F401
from posthog.utils import str_to_bool
@@ -52,14 +56,6 @@ PropertyType = Literal[
"workflow_variable",
]
-PropertyName = str
-TableWithProperties = Literal["events", "person", "groups"]
-TableColumn = Literal[
- "properties", # for events & persons table
- "group_properties", # for groups table
- # all below are for person&groups on events table
- "person_properties",
-]
OperatorType = Literal[
"exact",
"is_not",
diff --git a/posthog/models/team/team.py b/posthog/models/team/team.py
index 5271acdf2faf..b61fe531c07d 100644
--- a/posthog/models/team/team.py
+++ b/posthog/models/team/team.py
@@ -37,6 +37,10 @@ from posthog.rbac.decorators import field_access_control
from posthog.session_recordings.models.session_recording_playlist import SessionRecordingPlaylist
from posthog.settings.utils import get_list
+# Relocated to the Django-free posthog.week_start_day module so the HogQL engine can use it
+# without booting Django; re-exported here for existing callers.
+from posthog.week_start_day import WeekStartDay # noqa: F401
+
from products.customer_analytics.backend.facade.constants import DEFAULT_ACTIVITY_EVENT
from ...hogql.modifiers import set_default_modifier_values
@@ -240,15 +244,6 @@ def get_default_data_attributes() -> list[str]:
return ["data-attr"]
-class WeekStartDay(models.IntegerChoices):
- SUNDAY = 0, "Sunday"
- MONDAY = 1, "Monday"
-
- @property
- def clickhouse_mode(self) -> str:
- return "3" if self == WeekStartDay.MONDAY else "0"
-
-
class CookielessServerHashMode(models.IntegerChoices):
DISABLED = 0, "Disabled"
STATELESS = 1, "Stateless"
diff --git a/posthog/property_columns.py b/posthog/property_columns.py
new file mode 100644
index 000000000000..7895a4b1e937
--- /dev/null
+++ b/posthog/property_columns.py
@@ -0,0 +1,16 @@
+"""Type aliases naming the tables and columns that store event/person/group properties.
+
+Django-free so the HogQL engine and the materialized-columns layer can import them without
+booting Django; posthog.models.property re-exports them for existing callers.
+"""
+
+from typing import Literal
+
+PropertyName = str
+TableWithProperties = Literal["events", "person", "groups"]
+TableColumn = Literal[
+ "properties", # for events & persons table
+ "group_properties", # for groups table
+ # all below are for person&groups on events table
+ "person_properties",
+]
diff --git a/posthog/week_start_day.py b/posthog/week_start_day.py
new file mode 100644
index 000000000000..e2976f7cb1d8
--- /dev/null
+++ b/posthog/week_start_day.py
@@ -0,0 +1,17 @@
+"""Week-start-day enum the HogQL engine can import without booting Django (no settings or app registry).
+
+`django.db.models.IntegerChoices` is plain enum machinery — defining a subclass touches neither
+settings nor the app registry, so this module imports cleanly in a bare interpreter.
+posthog.models.team.team re-exports `WeekStartDay` for existing callers.
+"""
+
+from django.db import models
+
+
+class WeekStartDay(models.IntegerChoices):
+ SUNDAY = 0, "Sunday"
+ MONDAY = 1, "Monday"
+
+ @property
+ def clickhouse_mode(self) -> str:
+ return "3" if self == WeekStartDay.MONDAY else "0"
diff --git a/products/event_definitions/backend/models/property_definition.py b/products/event_definitions/backend/models/property_definition.py
index b856cdded75a..0e5861b4c058 100644
--- a/products/event_definitions/backend/models/property_definition.py
+++ b/products/event_definitions/backend/models/property_definition.py
@@ -10,15 +10,11 @@ from posthog.models.utils import UniqueConstraintByExpression, UUIDTModel
from posthog.settings.data_stores import CLICKHOUSE_DATABASE
from posthog.utils import invalidate_has_person_email_cache
-PERSON_EMAIL_PROPERTY_NAME = "email"
-
+# Relocated to the Django-free products.event_definitions.backend.property_type module so the
+# HogQL engine can use it without booting Django; re-exported here for existing callers.
+from products.event_definitions.backend.property_type import PropertyType
-class PropertyType(models.TextChoices):
- Datetime = "DateTime", "DateTime"
- String = "String", "String"
- Numeric = "Numeric", "Numeric"
- Boolean = "Boolean", "Boolean"
- Duration = "Duration", "Duration"
+PERSON_EMAIL_PROPERTY_NAME = "email"
class PropertyFormat(models.TextChoices):
diff --git a/products/event_definitions/backend/property_type.py b/products/event_definitions/backend/property_type.py
new file mode 100644
index 000000000000..f19356be38b5
--- /dev/null
+++ b/products/event_definitions/backend/property_type.py
@@ -0,0 +1,17 @@
+"""Property-type enum the HogQL engine can import without booting Django (no settings or app registry).
+
+`django.db.models.TextChoices` is plain enum machinery — defining a subclass touches neither
+settings nor the app registry, so this module imports cleanly in a bare interpreter.
+products.event_definitions.backend.models.property_definition re-exports `PropertyType` for
+existing callers.
+"""
+
+from django.db import models
+
+
+class PropertyType(models.TextChoices):
+ Datetime = "DateTime", "DateTime"
+ String = "String", "String"
+ Numeric = "Numeric", "Numeric"
+ Boolean = "Boolean", "Boolean"
+ Duration = "Duration", "Duration"