: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
chore(tests): split pure HogQL checks from ClickHouse queries (#111323)
Paul D'Ambra5b62ac0163e8
diff --git a/posthog/hogql/transforms/test/test_property_types.py b/posthog/hogql/transforms/test/test_property_types.pyindex 2c9dc6006c60..c7774911fab3 100644--- a/posthog/hogql/transforms/test/test_property_types.py+++ b/posthog/hogql/transforms/test/test_property_types.py@@ -84,9 +84,7 @@ def _normalize_snapshot_sql(sql: str) -> str:return "\n".join(line.rstrip() for line in sql.splitlines())-@override_settings(CLICKHOUSE_HOGQL_USE_NEW_EVENTS_SCHEMA=True)[email protected]("clickhouse_database")-class TestNewEventsSchemaArraySubcolumns(SimpleTestCase):+class _NewEventsSchemaArraySubcolumnsHelpers:def _context(self, use_new_events_schema: bool | None = None) -> HogQLContext:team = Team(id=1, project_id=1)context = HogQLContext(@@ -130,6 +128,9 @@ class TestNewEventsSchemaArraySubcolumns(SimpleTestCase):assert plan is not Nonereturn plan++@override_settings(CLICKHOUSE_HOGQL_USE_NEW_EVENTS_SCHEMA=True)+class TestNewEventsSchemaArraySubcolumns(_NewEventsSchemaArraySubcolumnsHelpers, SimpleTestCase):@parameterized.expand([("$active_feature_flags",), ("$exception_types",)])def test_property_comparison_planner_does_not_depend_on_json_storage_type(self, property_name: str) -> None:plan = self._plan_where_comparison(f"select count() from events where properties.{property_name} = 'TypeError'")@@ -142,98 +143,6 @@ class TestNewEventsSchemaArraySubcolumns(SimpleTestCase):assert plan.access.source.physical_type == dynamic_plan.access.source.physical_typeassert plan.access.source.has_bloom_filter_index is False- @parameterized.expand([("declared", EVENTS_PROPERTIES_JSON_TYPE()), ("dynamic", "JSON")])- def test_string_paths_read_empty_as_null(self, _name: str, json_type: str) -> None:- context = self._context()- with patch("posthog.hogql.printer.utils.build_property_swapper"):- printed, _ = prepare_and_print_ast(- parse_select(- "SELECT properties.$browser, properties.$browser IS NULL, "- "properties.$browser IS NOT NULL FROM events"- ),- context,- "clickhouse",- )- rows = sync_execute(- "WITH events_json AS (SELECT 1 AS team_id, CAST(arrayJoin(%(documents)s), %(json_type)s) AS properties) "- + printed,- {- **context.values,- "json_type": json_type,- "documents": [- "{}",- '{"$browser":""}',- '{"$browser":"null"}',- '{"$browser":"Chrome"}',- '{"$browser":"[]"}',- '{"$browser":"{}"}',- ],- },- )- assert rows == [(None, 1, 0), (None, 1, 0), ("null", 0, 1), ("Chrome", 0, 1), ("[]", 0, 1), ("{}", 0, 1)]-- @parameterized.expand(- [- (- name,- json_type,- "toFloat(properties.$screen_height), toFloat(properties.custom), toFloat(properties.nested.score)",- 3,- True,- )- for name, json_type in (("declared", EVENTS_PROPERTIES_JSON_TYPE()), ("dynamic", "JSON"))- ]- + [- (- f"{name}_indexed",- json_type,- "toFloat(properties.numbers[1]), toFloat(properties.objects[1].score)",- 2,- False,- )- for name, json_type in (("declared", EVENTS_PROPERTIES_JSON_TYPE()), ("dynamic", "JSON"))- ]- )- def test_numeric_casts_preserve_mixed_native_types(- self, _name: str, json_type: str, select: str, width: int, reads_subcolumn_directly: bool- ) -> None:- context = self._context()- with patch("posthog.hogql.printer.utils.build_property_swapper"):- printed, _ = prepare_and_print_ast(parse_select(f"SELECT {select} FROM events"), context, "clickhouse")- if reads_subcolumn_directly:- for unwanted in ("dynamicElement", "JSONExtract", "replaceRegexpAll", "toJSONString"):- assert unwanted not in printed, printed- values = [42, 2**63, 2.5, "3.75", "invalid", None, ""]- rows = sync_execute(- "WITH events_json AS (SELECT 1 AS team_id, CAST(arrayJoin(%(documents)s), %(json_type)s) AS properties) "- + printed,- {- **context.values,- "json_type": json_type,- "documents": [- json.dumps(- {- "$screen_height": value,- "custom": value,- "nested": {"score": value},- "numbers": [value],- "objects": [{"score": value}],- }- )- for value in values- ],- },- )- assert rows == [- (42.0,) * width,- (float(2**63),) * width,- (2.5,) * width,- (3.75,) * width,- (None,) * width,- (None,) * width,- (None,) * width,- ]-def test_negative_multi_icontains_array_property_stays_optimized(self) -> None:where = property_to_expr({@@ -442,35 +351,6 @@ class TestNewEventsSchemaArraySubcolumns(SimpleTestCase):assert f"toJSONString(events.properties.{escaped_property_name})" in printed, printedassert "JSONExtractKeysAndValuesRaw" not in printed, printed- @parameterized.expand(- [- ("typed_array", "$exception_types"),- ("dynamic_array", "custom_array"),- ]- )- def test_raw_property_helper_reads_only_one_json_subcolumn(self, _name: str, property_name: str) -> None:- expression, _ = get_property_string_expr(- "events",- property_name,- "%(property_key)s",- "events.properties",- use_new_events_schema=True,- )- escaped_property_name = f"`{property_name}`" if property_name.startswith("$") else property_name-- assert f"events.properties.{escaped_property_name}" in expression- assert "toJSONString(events.properties)" not in expression-- rows = sync_execute(- f"SELECT {expression} FROM (SELECT CAST(arrayJoin(%(documents)s), %(json_type)s) AS properties) AS events",- {- "property_key": property_name,- "json_type": EVENTS_PROPERTIES_JSON_TYPE(),- "documents": [json.dumps({property_name: value}) for value in [["a", '"b"'], [], None]],- },- )- assert rows == [('["a","\\"b\\""]',), ("",), ("",)]-def test_jsonextract_string_arrays_read_array_subcolumn(self) -> None:printed = self._print_select("select JSONExtract(ifNull(properties.$exception_types, ''), 'Array(String)') from events"@@ -521,6 +401,131 @@ class TestNewEventsSchemaArraySubcolumns(SimpleTestCase):assert "JSONDropKeys" not in unrestricted, unrestrictedassert "events.properties" in unrestricted or "events.person_properties" in unrestricted, unrestricted++@override_settings(CLICKHOUSE_HOGQL_USE_NEW_EVENTS_SCHEMA=True)[email protected]("clickhouse_database")+class TestNewEventsSchemaArraySubcolumnsClickhouse(_NewEventsSchemaArraySubcolumnsHelpers, SimpleTestCase):+ @parameterized.expand([("declared", EVENTS_PROPERTIES_JSON_TYPE()), ("dynamic", "JSON")])+ def test_string_paths_read_empty_as_null(self, _name: str, json_type: str) -> None:+ context = self._context()+ with patch("posthog.hogql.printer.utils.build_property_swapper"):+ printed, _ = prepare_and_print_ast(+ parse_select(+ "SELECT properties.$browser, properties.$browser IS NULL, "+ "properties.$browser IS NOT NULL FROM events"+ ),+ context,+ "clickhouse",+ )+ rows = sync_execute(+ "WITH events_json AS (SELECT 1 AS team_id, CAST(arrayJoin(%(documents)s), %(json_type)s) AS properties) "+ + printed,+ {+ **context.values,+ "json_type": json_type,+ "documents": [+ "{}",+ '{"$browser":""}',+ '{"$browser":"null"}',+ '{"$browser":"Chrome"}',+ '{"$browser":"[]"}',+ '{"$browser":"{}"}',+ ],+ },+ )+ assert rows == [(None, 1, 0), (None, 1, 0), ("null", 0, 1), ("Chrome", 0, 1), ("[]", 0, 1), ("{}", 0, 1)]++ @parameterized.expand(+ [+ (+ name,+ json_type,+ "toFloat(properties.$screen_height), toFloat(properties.custom), toFloat(properties.nested.score)",+ 3,+ True,+ )+ for name, json_type in (("declared", EVENTS_PROPERTIES_JSON_TYPE()), ("dynamic", "JSON"))+ ]+ + [+ (+ f"{name}_indexed",+ json_type,+ "toFloat(properties.numbers[1]), toFloat(properties.objects[1].score)",+ 2,+ False,+ )+ for name, json_type in (("declared", EVENTS_PROPERTIES_JSON_TYPE()), ("dynamic", "JSON"))+ ]+ )+ def test_numeric_casts_preserve_mixed_native_types(+ self, _name: str, json_type: str, select: str, width: int, reads_subcolumn_directly: bool+ ) -> None:+ context = self._context()+ with patch("posthog.hogql.printer.utils.build_property_swapper"):+ printed, _ = prepare_and_print_ast(parse_select(f"SELECT {select} FROM events"), context, "clickhouse")+ if reads_subcolumn_directly:+ for unwanted in ("dynamicElement", "JSONExtract", "replaceRegexpAll", "toJSONString"):+ assert unwanted not in printed, printed+ values = [42, 2**63, 2.5, "3.75", "invalid", None, ""]+ rows = sync_execute(+ "WITH events_json AS (SELECT 1 AS team_id, CAST(arrayJoin(%(documents)s), %(json_type)s) AS properties) "+ + printed,+ {+ **context.values,+ "json_type": json_type,+ "documents": [+ json.dumps(+ {+ "$screen_height": value,+ "custom": value,+ "nested": {"score": value},+ "numbers": [value],+ "objects": [{"score": value}],+ }+ )+ for value in values+ ],+ },+ )+ assert rows == [+ (42.0,) * width,+ (float(2**63),) * width,+ (2.5,) * width,+ (3.75,) * width,+ (None,) * width,+ (None,) * width,+ (None,) * width,+ ]++ @parameterized.expand(+ [+ ("typed_array", "$exception_types"),+ ("dynamic_array", "custom_array"),+ ]+ )+ def test_raw_property_helper_reads_only_one_json_subcolumn(self, _name: str, property_name: str) -> None:+ expression, _ = get_property_string_expr(+ "events",+ property_name,+ "%(property_key)s",+ "events.properties",+ use_new_events_schema=True,+ )+ escaped_property_name = f"`{property_name}`" if property_name.startswith("$") else property_name++ assert f"events.properties.{escaped_property_name}" in expression+ assert "toJSONString(events.properties)" not in expression++ rows = sync_execute(+ f"SELECT {expression} FROM (SELECT CAST(arrayJoin(%(documents)s), %(json_type)s) AS properties) AS events",+ {+ "property_key": property_name,+ "json_type": EVENTS_PROPERTIES_JSON_TYPE(),+ "documents": [json.dumps({property_name: value}) for value in [["a", '"b"'], [], None]],+ },+ )+ assert rows == [('["a","\\"b\\""]',), ("",), ("",)]+@parameterized.expand([("event", "properties", PropertyDefinition.Type.EVENT),