: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
feat(hogql): flag compatibility for native events and a $false sentinel (#91506)
Rory Shanksf1c19abc8912
diff --git a/.depot/workflows/ci-backend.yml b/.depot/workflows/ci-backend.ymlindex f8523a28ea8f..36502261873a 100644--- a/.depot/workflows/ci-backend.yml+++ b/.depot/workflows/ci-backend.yml@@ -1060,17 +1060,10 @@ jobs:schema_modes='[false]'echo "Skipping new-events-schema variants (RUN_NEW_EVENTS_SCHEMA is off)"fi- # Experiments reads flags from $feature/<key> properties. The native-JSON table keeps- # them in the $feature_flags map, which HogQL does not read yet, so it runs legacy-only.include=$(jq -cn --argjson matrix "$PRODUCT_MATRIX_JSON" --argjson schema_modes "$schema_modes" '[$matrix[] as $entry| $schema_modes[] as $new_events_schema- | select(- ($new_events_schema | not)- or ([($entry.legs // [{"filters": ($entry.filters // "")}])[].filters]- | any(contains("products-experiments")) | not)- )| $entry + {"new-events-schema": $new_events_schema}]')diff --git a/.github/workflows/ci-backend.yml b/.github/workflows/ci-backend.ymlindex a4fa1007e09d..b33964393de5 100644--- a/.github/workflows/ci-backend.yml+++ b/.github/workflows/ci-backend.yml@@ -872,17 +872,10 @@ jobs:schema_modes='[false]'echo "Skipping new-events-schema variants (RUN_NEW_EVENTS_SCHEMA is off)"fi- # Experiments reads flags from $feature/<key> properties. The native-JSON table keeps- # them in the $feature_flags map, which HogQL does not read yet, so it runs legacy-only.include=$(jq -cn --argjson matrix "$PRODUCT_MATRIX_JSON" --argjson schema_modes "$schema_modes" '[$matrix[] as $entry| $schema_modes[] as $new_events_schema- | select(- ($new_events_schema | not)- or ([($entry.legs // [{"filters": ($entry.filters // "")}])[].filters]- | any(contains("products-experiments")) | not)- )| $entry + {"new-events-schema": $new_events_schema}]')diff --git a/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main.go b/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main.goindex 8f2745314fd3..7c146f453e77 100644--- a/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main.go+++ b/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main.go@@ -132,6 +132,11 @@ func makeEventPropertyRules() *pathRule {return root}+// The typed map stores every flag value as a string, so a variant named "false" would read the same as a flag that+// was evaluated and switched off (JSON false). The variant is stored under this sentinel instead. The query layer+// maps it back to "false" and the flag API refuses it as a variant key.+const falseVariantSentinel = "$false"+type valueKind byteconst (@@ -346,6 +351,12 @@ func (p *processor) cleanEventProperties(v *value) (*value, error) {}for _, property := range cleaned.entries {if property.key == "$feature_flags" && property.value.kind == kindObject {+ for _, flag := range property.value.entries {+ if flag.value.kind == kindString && flag.value.s == "false" {+ flag.value.s = falseVariantSentinel+ p.mutated = true+ }+ }compare := func(a, b entry) int { return strings.Compare(a.key, b.key) }if !slices.IsSortedFunc(property.value.entries, compare) {slices.SortFunc(property.value.entries, compare)diff --git a/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main_test.go b/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main_test.goindex 55aa8af8a23b..0146a4b8fd26 100644--- a/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main_test.go+++ b/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main_test.go@@ -67,6 +67,9 @@ func TestProcessLineGroupsFeaturePropertiesAndPreservesExistingFlagValues(t *tes`{"$feature/zebra":false,"$feature/alpha":"control","other":1}`: `{"other":1,"$feature_flags":{"alpha":"control","zebra":false}}`,`{"$feature_flags":{"zebra":false,"alpha":"control"}}`: `{"$feature_flags":{"alpha":"control","zebra":false}}`,`{"$feature/a.b":1,"$feature/a":{"b":2},"$feature/Z":true}`: `{"$feature_flags":{"Z":true,"a":{"b":1}}}`,+ `{"$feature/named-false":"false","$feature/off":false}`: `{"$feature_flags":{"named-false":"$false","off":false}}`,+ `{"$feature_flags":{"off":false,"named-false":"false"}}`: `{"$feature_flags":{"named-false":"$false","off":false}}`,+ `{"$feature_flags.named-false":"false"}`: `{"$feature_flags":{"named-false":"$false"}}`,}for input, want := range tests {diff --git a/docs/internal/clickhouse-utility-udfs.md b/docs/internal/clickhouse-utility-udfs.mdindex 03001ec6e930..b3d77d5df8c1 100644--- a/docs/internal/clickhouse-utility-udfs.md+++ b/docs/internal/clickhouse-utility-udfs.md@@ -3,6 +3,8 @@`JSONCleanPostHogEventProperties` groups `$feature/<key>` event properties into `$feature_flags`.Before emitting JSON for insertion, it sorts the keys in `$feature_flags` alphabetically using case-sensitive string order.This also applies to existing `$feature_flags` objects, after cleanup resolves duplicates and expands dotted keys.+A flag value that is the JSON string `"false"` (a variant named false) is stored as `$false`, so it stays distinct from a flag that was evaluated and switched off (JSON `false`, which the typed map stores as `false`).+`$false` is a reserved variant key; the flag API rejects it.Flag values and person-property ordering follow the existing cleanup rules.Invalid scalar and array `$feature_flags` values are replaced with an empty map and retained in@@ -73,6 +75,15 @@ Documents exceeding the shared depth limit produce `{}` in the temporary output;Native events retain `temporary_properties` for 60 days after insertion, including historical events; TTL merges clear the column asynchronously.Fresh installations use the updated schema definitions. Existing tables require a manual schema rollout and feature-flag query compatibility before native reads are enabled.+Native-event queries derive `$active_feature_flags` from the `$feature_flags` map, excluding empty and `false` values and restricted flags. `$false` counts as active, and reads of `$feature/<key>`, `$feature_flags.<key>` and the `$feature_flags` map return it as `false`. Whole-document reads of `properties`, and batch exports of the whole `$feature_flags` map, return it as stored; single-flag export fields and filters map it back. Array order follows the stored map rather than the original SDK evaluation order. No separate active-flags column is required.++Feature-flag scalar reads still use JSON string encoding when requested: a `control` variant+becomes `"control"` through `toJSONString`, and `JSONExtractString` returns `control`.++On native events, HogQL `JSONExtract*` calls with `$feature_flags` as their first property key read the same restricted-property-aware map as dotted `$feature_flags` access.+The original extractor still determines the return type and missing-value default.+The legacy table stores flags as sent, so HogQL reads every flag property there as stored, `$feature_flags` included.+### Benchmarking the cleaner`BenchmarkProcessFixture` measures cleaning with a reused processor and output buffer.diff --git a/posthog/hogql/constants.py b/posthog/hogql/constants.pyindex 51e999523110..7b6ce924761f 100644--- a/posthog/hogql/constants.py+++ b/posthog/hogql/constants.py@@ -20,6 +20,11 @@ KEYWORDS = ["true", "false", "null"]# Keywords you can't alias toRESERVED_KEYWORDS = [*KEYWORDS, "team_id"]+# The ingest cleaner stores a feature flag variant named "false" under this sentinel in the `$feature_flags` map, because+# the map holds strings and a flag that was evaluated and switched off is already stored as 'false'. Reads map the+# sentinel back to "false", and the flag API refuses it as a variant key.+FEATURE_FLAG_FALSE_VARIANT_SENTINEL = "$false"+# Limit applied to SELECT statements without LIMIT clause when queried via the APIDEFAULT_RETURNED_ROWS = 100# Max limit for all SELECT queries, and the default for CSV exportsdiff --git a/posthog/hogql/functions/mapping.py b/posthog/hogql/functions/mapping.pyindex 382afa31595e..4dde2fd714bc 100644--- a/posthog/hogql/functions/mapping.py+++ b/posthog/hogql/functions/mapping.py@@ -77,6 +77,7 @@ HOGQL_CLICKHOUSE_FUNCTIONS: dict[str, HogQLFunctionMeta] = {"mapExtractKeyLike": HogQLFunctionMeta("mapExtractKeyLike", 2, 2),"mapApply": HogQLFunctionMeta("mapApply", 2, 2),"mapFilter": HogQLFunctionMeta("mapFilter", 2, 2),+ "mapExists": HogQLFunctionMeta("mapExists", 2, 2),"mapUpdate": HogQLFunctionMeta("mapUpdate", 2, 2),# bit"bitAnd": HogQLFunctionMeta("bitAnd", 2, 2),diff --git a/posthog/hogql/printer/clickhouse.py b/posthog/hogql/printer/clickhouse.pyindex a1f3b5460ecb..711b15f976ab 100644--- a/posthog/hogql/printer/clickhouse.py+++ b/posthog/hogql/printer/clickhouse.py@@ -636,8 +636,33 @@ class ClickHousePrinter(BasePrinter):if not keys_to_drop:return field_sql+ restricted_feature_flags = sorted(+ key.removeprefix("$feature/") for key in keys_to_drop if key.startswith("$feature/")+ )+ filter_native_feature_flags = (+ self.context.uses_new_events_schema()+ and resolved_field.name == "properties"+ and isinstance(type.table_type, ast.BaseTableType)+ and isinstance(type.table_type.resolve_database_table(self.context), EVENTS_TABLE_TYPES)+ and bool(restricted_feature_flags)+ and "$feature_flags" not in keys_to_drop+ )+ if filter_native_feature_flags:+ keys_to_drop = {key for key in keys_to_drop if not key.startswith("$feature/")} | {"$feature_flags"}+keys_placeholder = self.context.add_sensitive_value(sorted(keys_to_drop))- return f"{JSON_DROP_KEYS_CLICKHOUSE_NAME}({keys_placeholder})({field_sql})"+ stripped = f"{JSON_DROP_KEYS_CLICKHOUSE_NAME}({keys_placeholder})({field_sql})"+ if not filter_native_feature_flags:+ return stripped++ feature_keys_placeholder = self.context.add_sensitive_value(restricted_feature_flags)+ physical_field = super().visit_field_type(type)+ feature_flags = f"{physical_field}.{escape_clickhouse_identifier('$feature_flags')}"+ filtered_feature_flags = (+ f"mapFilter((key, value) -> not(has({feature_keys_placeholder}, key)), {feature_flags})"+ )+ feature_flags_patch = f"concat('{{\"$feature_flags\":', toJSONString({filtered_feature_flags}), '}}')"+ return f"if(empty({filtered_feature_flags}), {stripped}, JSONMergePatch({stripped}, {feature_flags_patch}))"def _get_optimized_session_id_compare_operation(self, node: ast.CompareOperation) -> str | None:"""Rewrite $session_id comparisons against UUID constants to use the $session_id_uuid column."""diff --git a/posthog/hogql/printer/test/__snapshots__/test_trino_printer.ambr b/posthog/hogql/printer/test/__snapshots__/test_trino_printer.ambrindex 9d6a2ff4c792..2951f56cee01 100644--- a/posthog/hogql/printer/test/__snapshots__/test_trino_printer.ambr+++ b/posthog/hogql/printer/test/__snapshots__/test_trino_printer.ambr@@ -434,6 +434,7 @@'lgamma','make_interval','mapAdd',+ 'mapExists','mapSubtract','multiFuzzyMatchAllIndices','multiFuzzyMatchAny',diff --git a/posthog/hogql/printer/test/test_printer.py b/posthog/hogql/printer/test/test_printer.pyindex e46754ff6e21..cfe4ac7fbe4f 100644--- a/posthog/hogql/printer/test/test_printer.py+++ b/posthog/hogql/printer/test/test_printer.py@@ -56,6 +56,7 @@ from posthog.hogql.modifiers import create_default_modifiers_for_teamfrom posthog.hogql.parser import parse_expr, parse_selectfrom posthog.hogql.printer import prepare_and_print_ast, prepare_ast_for_printing, print_prepared_ast, to_printed_hogqlfrom posthog.hogql.property import property_to_expr+from posthog.hogql.property_access_types import RestrictedPropertyfrom posthog.hogql.query import execute_hogql_queryfrom posthog.hogql.test.utils import json_dynamic_read_sql, json_dynamic_read_sql_from_parts@@ -1460,6 +1461,55 @@ class TestPrinter(BaseTest):self.assertEqual(result[1:4], (has_groups, has_organization, int(expected_groups is None)))self.assertEqual(result[4], (expected_groups or {}).get("organization"))+ @parameterized.expand(+ [+ (None, True),+ ("$feature/flag.with.dot", True),+ ("$feature_flags", True),+ (None, False),+ ]+ )+ def test_feature_flag_json_extracts_match_map_document(self, restricted: str | None, populated: bool) -> None:+ flags = {"flag.with.dot": "control", "numeric": "42", "enabled": "true"} if populated else {}+ context = HogQLContext(team_id=self.team.pk, enable_select_queries=True, use_new_events_schema=True)+ context.restricted_properties = (+ {RestrictedProperty(name=restricted, property_type=PropertyDefinition.Type.EVENT)} if restricted else set()+ )+ extracts = [+ "JSONExtractString(properties, '$feature_flags', 'flag.with.dot')",+ "JSONExtractString(properties, '$feature_flags', concat('flag.', 'with.dot'))",+ "JSONExtractString(properties, '$feature_flags', 'missing')",+ "JSONExtractRaw(properties, '$feature_flags')",+ "JSONExtractRaw(properties, '$feature_flags', 'flag.with.dot')",+ "JSONExtract(properties, '$feature_flags', 'Map(String, String)')",+ "JSONExtractKeys(properties, '$feature_flags')",+ "JSONExtractKeysAndValues(properties, '$feature_flags', 'String')",+ "JSONExtractKeysAndValuesRaw(properties, '$feature_flags')",+ "JSONExtractArrayRaw(properties, '$feature_flags')",+ "JSONExtractInt(properties, '$feature_flags', 'numeric')",+ "JSONExtractUInt(properties, '$feature_flags', 'numeric')",+ "JSONExtractFloat(properties, '$feature_flags', 'numeric')",+ "JSONExtractBool(properties, '$feature_flags', 'enabled')",+ ]+ expression = "tuple(" + ", ".join(extracts) + ")"+ printed = self._expr(expression, context)+ actual = sync_execute(+ f"SELECT {printed} FROM (SELECT CAST(%(raw)s, %(json_type)s) AS properties) AS events",+ {+ **context.values,+ "raw": json.dumps({"$feature_flags": flags}),+ "json_type": EVENTS_PROPERTIES_JSON_TYPE(),+ },+ )+ visible_flags = {+ key: value for key, value in flags.items() if restricted not in ("$feature_flags", f"$feature/{key}")+ }+ expected = sync_execute(+ f"SELECT {expression} FROM (SELECT %(raw)s AS properties)",+ {"raw": json.dumps({"$feature_flags": visible_flags} if visible_flags else {})},+ )+ self.assertEqual(actual, expected)+def test_instance_setting_enables_new_events_schema(self) -> None:# The production rollout lever is the instance setting, not the env var — a fresh context# must pick up a runtime flip.diff --git a/posthog/hogql/transforms/clickhouse_property_resolution.py b/posthog/hogql/transforms/clickhouse_property_resolution.pyindex 867151d8fa59..c974ddb78f4e 100644--- a/posthog/hogql/transforms/clickhouse_property_resolution.py+++ b/posthog/hogql/transforms/clickhouse_property_resolution.py@@ -27,7 +27,7 @@ from typing import Literal, castfrom posthog.hogql import astfrom posthog.hogql.base import _T_AST-from posthog.hogql.constants import EXCEPTION_STRING_ARRAY_PROPERTIES+from posthog.hogql.constants import EXCEPTION_STRING_ARRAY_PROPERTIES, FEATURE_FLAG_FALSE_VARIANT_SENTINELfrom posthog.hogql.context import HogQLContextfrom posthog.hogql.database.models import DatabaseField, MapStringDatabaseFieldfrom posthog.hogql.errors import QueryError@@ -556,6 +556,218 @@ def _mirrored_source_property(field_type: ast.FieldType, context: HogQLContext)return mirrored_property_for_column(field_type.table_type, resolved_field.name, context)+def _false_variant_read(value: ast.Expr) -> ast.Expr:+ """`value` with the `$false` sentinel read back as the variant name "false".++ The cleaner stores a variant named "false" as `$false` so it stays apart from a flag that was evaluated and switched+ off, which the typed map holds as 'false'.+ """+ return _call(+ "if",+ [+ _call("equals", [clone_expr(value), _const(FEATURE_FLAG_FALSE_VARIANT_SENTINEL)]),+ _sentinel("false"),+ value,+ ],+ )+++def _feature_flag_value_read(feature_flags: ast.Expr, key: str) -> ast.Expr:+ """`has(map, key) ? map[key] : null`, with the `$false` sentinel mapped back to "false"."""+ return ast.Call(+ name="if",+ args=[+ ast.Call(name="has", args=[clone_expr(feature_flags), ast.Constant(value=key)]),+ _false_variant_read(ast.ArrayAccess(array=clone_expr(feature_flags), property=ast.Constant(value=key))),+ ast.Constant(value=None),+ ],+ )+++def _is_events_properties(field_type: ast.FieldType, context: HogQLContext) -> bool:+ table_type = _unwrap_to_table_type(field_type)+ field = field_type.resolve_database_field(context)+ return (+ table_type is not None+ and table_type.table.to_printed_clickhouse(context) in ("events", DISTRIBUTED_EVENTS_JSON_TABLE)+ and isinstance(field, DatabaseField)+ and field.name == "properties"+ )+++FEATURE_FLAG_PROPERTY_PREFIX = "$feature/"+++def _is_virtual_feature_flag_key(key: str) -> bool:+ """Whether a native events property is rebuilt from the `$feature_flags` map instead of read under its own name."""+ return key in ("$active_feature_flags", "$feature_flags") or key.startswith(FEATURE_FLAG_PROPERTY_PREFIX)+++def _feature_flags_map(field_type: ast.FieldType, context: HogQLContext) -> ast.Expr | None:+ source = resolve_json_subcolumn_source(+ field_type, DISTRIBUTED_EVENTS_JSON_TABLE, "properties", "$feature_flags", context+ )+ return (+ _json_subcolumn_access(field_type, ["$feature_flags"], source=source, is_nullable=False)+ if source is not None+ else None+ )+++def _restricted_feature_flag_keys(field_type: ast.FieldType, context: HogQLContext) -> list[str]:+ keys = (+ key+ for key in restricted_property_keys_for_table_type(field_type.table_type, context)+ if key.startswith(FEATURE_FLAG_PROPERTY_PREFIX)+ )+ return sorted(key.removeprefix(FEATURE_FLAG_PROPERTY_PREFIX) for key in keys)+++def _not_in_lambda_values(name: str, values: list[str], *, is_sensitive: bool = False) -> ast.Call:+ """`name NOT IN (values)`; restricted flag names are sensitive so display SQL redacts them."""+ return _call(+ "notIn",+ [+ _lambda_string_arg(name),+ ast.Tuple(exprs=[ast.Constant(value=value, is_sensitive=is_sensitive or None) for value in values]),+ ],+ )+++def _filter_feature_flags(feature_flags: ast.Expr, restricted_keys: list[str]) -> ast.Expr:+ if not restricted_keys:+ return feature_flags+ return _call(+ "mapFilter",+ [+ ast.Lambda(args=["key", "value"], expr=_not_in_lambda_values("key", restricted_keys, is_sensitive=True)),+ feature_flags,+ ],+ )+++def _compact_feature_flags_map(+ feature_flags: ast.Expr, restricted_keys: list[str], *, map_values: bool = True+) -> ast.Expr:+ """The visible flags map, with `$false` read back as "false".++ Presence checks pass `map_values=False`: the mapping cannot change the key set, so they skip the per-row `mapApply`.+ """+ filtered = _filter_feature_flags(feature_flags, restricted_keys)+ if not map_values:+ return filtered+ return ast.Call(+ name="mapApply",+ args=[+ ast.Lambda(+ args=["key", "value"],+ expr=ast.Tuple(exprs=[_lambda_string_arg("key"), _false_variant_read(_lambda_string_arg("value"))]),+ ),+ filtered,+ ],+ )+++def _nonempty_container_json(value: ast.Expr, empty_json: str) -> ast.Expr:+ return ast.Call(+ name="nullIf",+ args=[+ ast.Call(name="toJSONString", args=[value]),+ ast.Constant(value=empty_json),+ ],+ type=ast.StringType(nullable=True),+ )+++def _active_flag_lambda(restricted_keys: list[str] | None, key_predicate: ast.Expr | None = None) -> ast.Lambda:+ """`(key, value) -> value is active, key is not restricted, and `key_predicate` holds.++ A variant named "false" is stored as `$false`, so it counts as active here.+ """+ predicates: list[ast.Expr] = [_not_in_lambda_values("value", ["", "false"])]+ if restricted_keys:+ predicates.append(_not_in_lambda_values("key", restricted_keys, is_sensitive=True))+ if key_predicate is not None:+ predicates.append(key_predicate)+ predicate = predicates[0] if len(predicates) == 1 else _call("and", predicates)+ return ast.Lambda(args=["key", "value"], expr=predicate)+++def _active_feature_flag_keys(feature_flags: ast.Expr, restricted_keys: list[str] | None = None) -> ast.Expr:+ # ClickHouse map storage order cannot reproduce the SDK's feature flag evaluation order.+ return _call("mapKeys", [_call("mapFilter", [_active_flag_lambda(restricted_keys), feature_flags])])+++def _active_feature_flag_exists(+ feature_flags: ast.Expr, restricted_keys: list[str], key_predicate: ast.Expr+) -> ast.Expr:+ """Whether an active, visible flag satisfies `key_predicate`, in one pass over the map.++ Materializing the filtered key array first (`mapKeys(mapFilter(...))`) costs 1.5x to 3.5x more per row than testing+ the predicate inside the scan, because the map and the array are both copied for every event.+ """+ return _call("mapExists", [_active_flag_lambda(restricted_keys, key_predicate), feature_flags])+++def _active_feature_flags_json(feature_flags: ast.Expr, restricted_keys: list[str]) -> ast.Expr:+ active_flags = _active_feature_flag_keys(feature_flags, restricted_keys)+ empty_json = _call(+ "if",+ [_call("empty", [_filter_feature_flags(feature_flags, restricted_keys)]), _const("[]"), _const("")],+ )+ return ast.Call(+ name="nullIf",+ args=[_call("toJSONString", [active_flags]), empty_json],+ type=ast.StringType(nullable=True),+ )+++def _active_feature_flags_present(feature_flags: ast.Expr, restricted_keys: list[str]) -> ast.Expr:+ return _call("notEmpty", [_filter_feature_flags(feature_flags, restricted_keys)])+++def _feature_flag_compatibility_read(+ node: ast.PropertyAccess, field_type: ast.FieldType, context: HogQLContext+) -> ast.Expr | None:+ """The read of a flag property the native table rebuilds from `$feature_flags`, or None to read it as stored.++ The legacy table stores every flag property as sent, so it always reads them as stored.+ """+ first_key = str(node.keys[0])+ deeper_keys = list(node.keys[1:])+ if (+ not context.uses_new_events_schema()+ or not _is_virtual_feature_flag_key(first_key)+ or not _is_events_properties(field_type, context)+ ):+ return None++ restricted_properties = restricted_property_keys_for_table_type(field_type.table_type, context)+ if "$feature_flags" in restricted_properties:+ return ast.Constant(value=None, type=ast.StringType(nullable=True))++ feature_flags = _feature_flags_map(field_type, context)+ if feature_flags is None:+ return None+ restricted_keys = _restricted_feature_flag_keys(field_type, context)++ if first_key.startswith(FEATURE_FLAG_PROPERTY_PREFIX):+ value = _feature_flag_value_read(feature_flags, first_key.removeprefix(FEATURE_FLAG_PROPERTY_PREFIX))+ return ast.PropertyAccess(expr=value, keys=deeper_keys) if deeper_keys else value++ if first_key == "$active_feature_flags":+ value = _active_feature_flags_json(feature_flags, restricted_keys)+ return ast.PropertyAccess(expr=value, keys=deeper_keys) if deeper_keys else value++ if deeper_keys:+ map_key = str(deeper_keys[0])+ if map_key in restricted_keys:+ return ast.Constant(value=None, type=ast.StringType(nullable=True))+ value = _feature_flag_value_read(feature_flags, map_key)+ return ast.PropertyAccess(expr=value, keys=deeper_keys[1:]) if len(deeper_keys) > 1 else value+ return _nonempty_container_json(_compact_feature_flags_map(feature_flags, restricted_keys), "{}")++def _substitute_value_read(node: ast.PropertyAccess, context: HogQLContext) -> ast.Expr | None:"""The backing-column read for a `PropertyAccess`, or None to leave it as the JSON extract.@@ -580,6 +792,12 @@ def _substitute_value_read(node: ast.PropertyAccess, context: HogQLContext) -> a_record_property_usage(context, None)return ast.Constant(value=None, type=ast.StringType(nullable=True))+ feature_flag_read = _feature_flag_compatibility_read(node, field_type, context)+ if feature_flag_read is not None:+ denied = isinstance(feature_flag_read, ast.Constant) and feature_flag_read.value is None+ _record_property_usage(context, None if denied else "json_subcolumn")+ return feature_flag_read+source = resolve_materialized_property_source(field_type, first_key, context)if source is None:# A physical Map column (logs/spans/metrics attributes) reads an un-grouped key via map subscript — the JSON@@ -868,7 +1086,7 @@ class ClickHousePropertyResolver(CloningVisitor):return exprreturn None- def _single_key_property(self, expr: ast.Expr) -> tuple[ast.FieldType, str] | None:+ def _property_operand(self, expr: ast.Expr) -> tuple[ast.FieldType, str] | None:"""The (blob `FieldType`, property name) of a single-key property operand, or None.Only a single-key access (`properties.x`, no deeper `.a.b`) maps to one backing column. A multi-key access reads@@ -899,10 +1117,24 @@ class ClickHousePropertyResolver(CloningVisitor):and self._property_table_in_scope(prop_type.field_type)):return prop_type.field_type, str(prop_type.chain[0])+ return None++ def _single_key_property(self, expr: ast.Expr) -> tuple[ast.FieldType, str] | None:+ """`_property_operand` for a property with its own backing column; virtual feature-flag properties have none."""+ single = self._property_operand(expr)+ if single is not None and not self._is_virtual_feature_flag_property(*single):+ return singleif isinstance(expr, ast.Call) and expr.name.lower() == "tobool" and len(expr.args) == 1:return self._single_key_property_from_boolean_conversion(expr.args[0])return None+ def _is_virtual_feature_flag_property(self, field_type: ast.FieldType, property_name: str) -> bool:+ return (+ self.context.uses_new_events_schema()+ and _is_virtual_feature_flag_key(property_name)+ and _is_events_properties(field_type, self.context)+ )+def _single_key_property_from_boolean_conversion(self, expr: ast.Expr) -> tuple[ast.FieldType, str] | None:expr = expr.expr if isinstance(expr, ast.Alias) else exprif not isinstance(expr, ast.Call) or len(expr.args) == 0:@@ -981,11 +1213,17 @@ class ClickHousePropertyResolver(CloningVisitor):if json_string_on_events_json is not None:return json_string_on_events_json+ feature_flag_extract = self._rewrite_feature_flag_json_extract(node)+ if feature_flag_extract is not None:+ return feature_flag_extract+json_extract_on_events_json = self._rewrite_json_extract_on_events_json_subcolumn(node)if json_extract_on_events_json is not None:return json_extract_on_events_json- optimized_json_has = self._optimize_json_has_on_events_json(node)+ optimized_json_has = self._rewrite_feature_flag_json_has(node)+ if optimized_json_has is None:+ optimized_json_has = self._optimize_json_has_on_events_json(node)if optimized_json_has is not None:return optimized_json_has@@ -1014,6 +1252,121 @@ class ClickHousePropertyResolver(CloningVisitor):return super().visit_call(node)+ def _rewrite_feature_flag_json_extract(self, node: ast.Call) -> ast.Expr | None:+ if (+ not node.name.startswith("JSONExtract")+ or len(node.args) < 2+ or not isinstance(node.args[1], ast.Constant)+ or node.args[1].value != "$feature_flags"+ ):+ return None+ field_type = resolve_field_type(node.args[0])+ if not isinstance(field_type, ast.FieldType):+ return None+ value = _feature_flag_compatibility_read(+ ast.PropertyAccess(expr=node.args[0], keys=["$feature_flags"]), field_type, self.context+ )+ if value is None:+ return None+ # Keep the extractor to preserve its return type and missing-value defaults.+ return ast.Call(+ start=node.start,+ end=node.end,+ name=node.name,+ type=node.type,+ args=[ast.Call(name="ifNull", args=[value, _sentinel("")]), *[self.visit(arg) for arg in node.args[2:]]],+ )++ def _rewrite_feature_flag_json_has(self, node: ast.Call) -> ast.Expr | None:+ if (+ not self.context.uses_new_events_schema()+ or node.name != "JSONHas"+ or len(node.args) < 2+ or not isinstance(node.args[1], ast.Constant)+ ):+ return None+ field_type = resolve_field_type(node.args[0])+ if not isinstance(field_type, ast.FieldType) or not _is_events_properties(field_type, self.context):+ return None+ first_key = node.args[1].value+ if not isinstance(first_key, str):+ return None+ if first_key in restricted_property_keys_for_table_type(field_type.table_type, self.context):+ return ast.Constant(value=False, type=ast.BooleanType(nullable=False))+ if not _is_virtual_feature_flag_key(first_key):+ return None++ restricted_properties = restricted_property_keys_for_table_type(field_type.table_type, self.context)+ if "$feature_flags" in restricted_properties:+ return ast.Constant(value=False, type=ast.BooleanType(nullable=False))++ feature_flags = _feature_flags_map(field_type, self.context)+ if feature_flags is None:+ return None+ restricted_keys = _restricted_feature_flag_keys(field_type, self.context)+ if first_key.startswith(FEATURE_FLAG_PROPERTY_PREFIX):+ value_key = first_key.removeprefix(FEATURE_FLAG_PROPERTY_PREFIX)+ if len(node.args) > 2:+ return ast.Call(+ name="JSONHas",+ args=[+ ast.Call(name="ifNull", args=[_map_value_read(feature_flags, value_key), _sentinel("")]),+ *[self.visit(arg) for arg in node.args[2:]],+ ],+ )+ elif first_key == "$feature_flags" and len(node.args) > 2:+ map_key = node.args[2]+ if not isinstance(map_key, ast.Constant) or not isinstance(map_key.value, str):+ compact_map = _nonempty_container_json(+ _compact_feature_flags_map(feature_flags, restricted_keys, map_values=False), "{}"+ )+ return ast.Call(+ name="JSONHas",+ args=[+ ast.Call(name="ifNull", args=[compact_map, _sentinel("")]),+ *[self.visit(arg) for arg in node.args[2:]],+ ],+ )+ value_key = map_key.value+ if len(node.args) > 3:+ if value_key in restricted_keys:+ return ast.Constant(value=False, type=ast.BooleanType(nullable=False))+ return ast.Call(+ name="JSONHas",+ args=[+ ast.Call(name="ifNull", args=[_map_value_read(feature_flags, value_key), _sentinel("")]),+ *[self.visit(arg) for arg in node.args[3:]],+ ],+ )+ else:+ value_key = None+ if value_key is not None:+ if value_key in restricted_keys:+ return ast.Constant(value=False, type=ast.BooleanType(nullable=False))+ return ast.Call(name="has", args=[feature_flags, ast.Constant(value=value_key)])+ if first_key == "$active_feature_flags":+ if len(node.args) > 2:+ return ast.Call(+ name="JSONHas",+ args=[+ ast.Call(+ name="ifNull",+ args=[+ _active_feature_flags_json(feature_flags, restricted_keys),+ _sentinel(""),+ ],+ ),+ *[self.visit(arg) for arg in node.args[2:]],+ ],+ )+ return _active_feature_flags_present(feature_flags, restricted_keys)+ if first_key == "$feature_flags":+ return ast.Call(+ name="notEmpty",+ args=[_compact_feature_flags_map(feature_flags, restricted_keys, map_values=False)],+ )+ return None+def _rewrite_to_json_string_on_events_json_subcolumn(self, node: ast.Call) -> ast.Expr | None:if not self.context.uses_new_events_schema() or node.name != "toJSONString" or len(node.args) != 1:return None@@ -1027,6 +1380,10 @@ class ClickHousePropertyResolver(CloningVisitor):return Noneproperty_name = str(property_access.keys[0])+ if self._is_virtual_feature_flag_property(field_type, property_name):+ if property_name in ("$feature_flags", "$active_feature_flags"):+ return self.visit_property_access(property_access)+ return Nonesource = resolve_materialized_property_source(field_type, property_name, self.context)if source is None or source.kind != "json_subcolumn":return None@@ -1064,6 +1421,8 @@ class ClickHousePropertyResolver(CloningVisitor):return Noneproperty_name = str(property_access.keys[0])+ if self._is_virtual_feature_flag_property(field_type, property_name):+ return Nonesource = resolve_materialized_property_source(field_type, property_name, self.context)if source is None or source.kind != "json_subcolumn":return None@@ -1389,6 +1748,42 @@ class ClickHousePropertyResolver(CloningVisitor):source=replace(source, column_type="Array(String)"),)+ def _string_array_expr(self, expr: ast.Expr, *, allow_to_string: bool = False) -> ast.Expr | None:+ prop = self._materialized_string_array_property(expr, allow_to_string=allow_to_string)+ return prop.bare_column() if prop is not None else None++ def _active_feature_flags_operand(+ self, expr: ast.Expr, *, allow_to_string: bool = False+ ) -> tuple[ast.Expr, list[str]] | None:+ unwrapped = expr.expr if isinstance(expr, ast.Alias) else expr+ if (+ allow_to_string+ and isinstance(unwrapped, ast.Call)+ and unwrapped.name == "toString"+ and len(unwrapped.args) == 1+ ):+ unwrapped = unwrapped.args[0]++ single = self._property_operand(unwrapped)+ if single is None:+ return None++ field_type, property_name = single+ if (+ property_name != "$active_feature_flags"+ or not self.context.uses_new_events_schema()+ or not _is_events_properties(field_type, self.context)+ ):+ return None+ restricted_properties = restricted_property_keys_for_table_type(field_type.table_type, self.context)+ if "$active_feature_flags" in restricted_properties or "$feature_flags" in restricted_properties:+ return None+ feature_flags = _feature_flags_map(field_type, self.context)+ if feature_flags is None:+ return None+ restricted_keys = _restricted_feature_flag_keys(field_type, self.context)+ return feature_flags, restricted_keys+def _property_group_property(self, expr: ast.Expr) -> _OptimizableProperty | None:"""A single-key property backed by a property group, only under OPTIMIZED mode."""if self.context.modifiers.propertyGroupsMode != PropertyGroupsMode.OPTIMIZED:@@ -1532,47 +1927,90 @@ class ClickHousePropertyResolver(CloningVisitor):def _optimize_materialized_array_compare(self, node: ast.CompareOperation) -> ast.Expr | None:if node.op in (ast.CompareOperationOp.Eq, ast.CompareOperationOp.NotEq):- prop: _OptimizableProperty | None = None+ array_expr: ast.Expr | None = Noneconstant_expr: ast.Constant | None = None- if (p := self._materialized_string_array_property(node.left)) and isinstance(node.right, ast.Constant):- prop, constant_expr = p, node.right- elif (p := self._materialized_string_array_property(node.right)) and isinstance(node.left, ast.Constant):- prop, constant_expr = p, node.left- if prop is None or constant_expr is None:+ active_feature_flags: tuple[ast.Expr, list[str]] | None = None+ if (value := self._active_feature_flags_operand(node.left)) is not None and isinstance(+ node.right, ast.Constant+ ):+ active_feature_flags, constant_expr = value, node.right+ elif (value := self._active_feature_flags_operand(node.right)) is not None and isinstance(+ node.left, ast.Constant+ ):+ active_feature_flags, constant_expr = value, node.left+ elif (array_value := self._string_array_expr(node.left)) is not None and isinstance(+ node.right, ast.Constant+ ):+ array_expr, constant_expr = array_value, node.right+ elif (array_value := self._string_array_expr(node.right)) is not None and isinstance(+ node.left, ast.Constant+ ):+ array_expr, constant_expr = array_value, node.left+ if constant_expr is None or (array_expr is None and active_feature_flags is None):return Noneif constant_expr.value is None:- is_set = _call("notEmpty", [prop.bare_column()])- return is_set if node.op == ast.CompareOperationOp.NotEq else _call("empty", [prop.bare_column()])+ if active_feature_flags is not None:+ feature_flags, restricted_keys = active_feature_flags+ is_set = _active_feature_flags_present(feature_flags, restricted_keys)+ return is_set if node.op == ast.CompareOperationOp.NotEq else _call("not", [is_set])+ assert array_expr is not None+ is_set = _call("notEmpty", [clone_expr(array_expr)])+ return is_set if node.op == ast.CompareOperationOp.NotEq else _call("empty", [array_expr])if not isinstance(constant_expr.value, str):return None- contains = _call("has", [prop.bare_column(), _const(constant_expr.value)])+ if active_feature_flags is not None:+ feature_flags, restricted_keys = active_feature_flags+ map_key = constant_expr.value+ if map_key in restricted_keys:+ return _const(node.op == ast.CompareOperationOp.NotEq)+ map_value = ast.ArrayAccess(+ array=feature_flags,+ property=_const(map_key),+ type=ast.StringType(nullable=False),+ )+ contains = _call("notIn", [map_value, ast.Tuple(exprs=[_const(""), _const("false")])])+ return contains if node.op == ast.CompareOperationOp.Eq else _call("not", [contains])+ assert array_expr is not None+ contains = _call("has", [array_expr, _const(constant_expr.value)])return contains if node.op == ast.CompareOperationOp.Eq else _call("not", [contains])if node.op not in (ast.CompareOperationOp.In, ast.CompareOperationOp.NotIn):return None- prop = self._materialized_string_array_property(node.left)- if prop is None:- return Nonevalues = self._extract_string_constants(node.right)if values is None:return None- contains_any = _call("hasAny", [prop.bare_column(), ast.Array(exprs=[_const(v) for v in values])])+ if (active := self._active_feature_flags_operand(node.left)) is not None:+ feature_flags, restricted_keys = active+ key_in_values = _call("in", [_lambda_string_arg("key"), ast.Tuple(exprs=[_const(v) for v in values])])+ contains_any = _active_feature_flag_exists(feature_flags, restricted_keys, key_in_values)+ return contains_any if node.op == ast.CompareOperationOp.In else _call("not", [contains_any])+ array_expr = self._string_array_expr(node.left)+ if array_expr is None:+ return None+ contains_any = _call("hasAny", [array_expr, ast.Array(exprs=[_const(v) for v in values])])return contains_any if node.op == ast.CompareOperationOp.In else _call("not", [contains_any])def _optimize_materialized_array_ilike(self, node: ast.CompareOperation) -> ast.Expr | None:if node.op not in (ast.CompareOperationOp.ILike, ast.CompareOperationOp.NotILike):return None- prop = self._materialized_string_array_property(node.left, allow_to_string=True)pattern = _string_pattern_constant(node.right)- if prop is None or pattern is None:+ if pattern is None:+ return None+ if (active := self._active_feature_flags_operand(node.left, allow_to_string=True)) is not None:+ feature_flags, restricted_keys = active+ key_matches = _call("ilike", [_lambda_string_arg("key"), _const(pattern.value)])+ contains = _active_feature_flag_exists(feature_flags, restricted_keys, key_matches)+ return contains if node.op == ast.CompareOperationOp.ILike else _call("not", [contains])+ array_expr = self._string_array_expr(node.left, allow_to_string=True)+ if array_expr is None:return Nonecontains = _call("arrayExists",[ast.Lambda(args=["v"], expr=_call("ilike", [_lambda_string_arg("v"), _const(pattern.value)])),- prop.bare_column(),+ array_expr,],)return contains if node.op == ast.CompareOperationOp.ILike else _call("not", [contains])@@ -1587,9 +2025,21 @@ class ClickHousePropertyResolver(CloningVisitor):if len(node.left.args) != 2:return None- prop = self._materialized_string_array_property(node.left.args[0], allow_to_string=True)values = self._extract_string_constants(node.left.args[1])- if prop is None or values is None:+ if values is None:+ return None+ if (active := self._active_feature_flags_operand(node.left.args[0], allow_to_string=True)) is not None:+ feature_flags, restricted_keys = active+ key_search = _call(+ "multiSearchAnyCaseInsensitive",+ [_lambda_string_arg("key"), ast.Array(exprs=[_const(v) for v in values])],+ )+ contains = _active_feature_flag_exists(+ feature_flags, restricted_keys, _call("greater", [key_search, _const(0)])+ )+ return contains if node.op == ast.CompareOperationOp.Gt else _call("not", [contains])+ array_expr = self._string_array_expr(node.left.args[0], allow_to_string=True)+ if array_expr is None:return Nonesearch = _call(@@ -1600,7 +2050,7 @@ class ClickHousePropertyResolver(CloningVisitor):"arrayExists",[ast.Lambda(args=["v"], expr=_call("greater", [search, _const(0)])),- prop.bare_column(),+ array_expr,],)return contains if node.op == ast.CompareOperationOp.Gt else _call("not", [contains])diff --git a/posthog/hogql/transforms/property_types.py b/posthog/hogql/transforms/property_types.pyindex 72dbff78a621..d7fd91f605a4 100644--- a/posthog/hogql/transforms/property_types.py+++ b/posthog/hogql/transforms/property_types.py@@ -349,6 +349,17 @@ class PropertySwapper(CloningVisitor):if table_name not in MATERIALIZATION_VALID_TABLES:return None+ # On native events, property resolution rebuilds this virtual map for every JSONExtract* function.+ if (+ self.context.uses_new_events_schema()+ and table_name == "events"+ and database_field.name == "properties"+ and len(node.args) > 1+ and isinstance(node.args[1], ast.Constant)+ and node.args[1].value == "$feature_flags"+ ):+ return None+if (self.context.uses_new_events_schema()and table_name == "events"diff --git a/posthog/hogql/transforms/test/test_property_types.py b/posthog/hogql/transforms/test/test_property_types.pyindex 691b6f853f96..44b580423bc0 100644--- a/posthog/hogql/transforms/test/test_property_types.py+++ b/posthog/hogql/transforms/test/test_property_types.py@@ -34,6 +34,7 @@ from posthog.schema import HogQLQueryModifiers, MaterializationModefrom posthog.hogql import astfrom posthog.hogql.context import HogQLContextfrom posthog.hogql.database.database import Database+from posthog.hogql.functions.udfs import JSON_DROP_KEYS_CLICKHOUSE_NAMEfrom posthog.hogql.parser import parse_selectfrom posthog.hogql.printer import prepare_and_print_astfrom posthog.hogql.property import property_to_expr@@ -56,6 +57,7 @@ from posthog.models import PropertyDefinition, Teamfrom posthog.models.event.sql import EVENTS_PROPERTIES_JSON_TYPE, PERSON_PROPERTIES_JSON_TYPEfrom posthog.models.group.util import create_groupfrom posthog.models.property.util import get_property_string_expr+from posthog.schema_enums import PropertyGroupsModefrom posthog.test.test_utils import create_group_type_mapping_without_created_atfrom products.data_tools.backend.models.join import DataWarehouseJoin@@ -79,17 +81,32 @@ def _normalize_snapshot_sql(sql: str) -> str:@override_settings(CLICKHOUSE_HOGQL_USE_NEW_EVENTS_SCHEMA=True)class TestNewEventsSchemaArraySubcolumns(SimpleTestCase):- def _context(self) -> HogQLContext:+ def _context(self, use_new_events_schema: bool | None = None) -> HogQLContext:team = Team(id=1, project_id=1)- context = HogQLContext(team_id=team.id, team=team, enable_select_queries=True)+ context = HogQLContext(+ team_id=team.id,+ team=team,+ enable_select_queries=True,+ use_new_events_schema=use_new_events_schema,+ )context.database = Database()context.restricted_properties = set()context.property_swapper = PropertySwapper("UTC", {}, {}, {}, context, True)return context- def _print_select(self, select: str) -> str:+ def _print_select(+ self,+ select: str,+ use_new_events_schema: bool | None = None,+ restricted_properties: set[RestrictedProperty] | None = None,+ property_groups_mode: PropertyGroupsMode | None = None,+ ) -> str:expr = parse_select(select)- context = self._context()+ context = self._context(use_new_events_schema)+ if restricted_properties is not None:+ context.restricted_properties = restricted_properties+ if property_groups_mode is not None:+ context.modifiers.propertyGroupsMode = property_groups_modewith patch("posthog.hogql.printer.utils.build_property_swapper"):query, _ = prepare_and_print_ast(expr,@@ -107,8 +124,9 @@ class TestNewEventsSchemaArraySubcolumns(SimpleTestCase):assert plan is not Nonereturn plan- def test_property_comparison_planner_does_not_depend_on_json_storage_type(self) -> None:- plan = self._plan_where_comparison("select count() from events where properties.$exception_types = 'TypeError'")+ @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'")assert plan.access.source.kind == PropertySourceKind.JSONdynamic_plan = self._plan_where_comparison(@@ -221,6 +239,138 @@ class TestNewEventsSchemaArraySubcolumns(SimpleTestCase):# or every row pays to serialize the array just to null-check it.assert "toJSONString" not in printed, printed+ @parameterized.expand(+ [+ (+ "legacy_reads_flags_as_stored",+ "SELECT properties.$feature_flags, properties.$feature_flags.checkout, "+ "JSONHas(properties, '$feature_flags', 'checkout'), JSONHas(properties, '$feature/secret') FROM events",+ False,+ {RestrictedProperty(name="$feature/secret", property_type=PropertyDefinition.Type.EVENT)},+ PropertyGroupsMode.OPTIMIZED,+ (f"JSONExtractRaw({JSON_DROP_KEYS_CLICKHOUSE_NAME}(", f"JSONHas({JSON_DROP_KEYS_CLICKHOUSE_NAME}("),+ ("properties_group_feature_flags", "mapFilter("),+ ),+ (+ "native",+ "SELECT properties.`$feature/checkout`, properties.$active_feature_flags, "+ "JSONHas(properties, '$feature/checkout') FROM events",+ True,+ None,+ None,+ ("events.properties.`$feature_flags`", "mapFilter("),+ ("events.properties.`$feature/checkout`",),+ ),+ (+ "raw_active_flags",+ "SELECT JSONExtractRaw(properties, '$active_feature_flags') FROM events",+ True,+ None,+ None,+ ("events.properties.`$feature_flags`", "mapFilter("),+ ("events.properties.`$active_feature_flags`", "events._active_feature_flags"),+ ),+ (+ "native_restricted",+ "SELECT properties.$feature_flags, properties.$active_feature_flags, "+ "JSONHas(properties, '$feature_flags', 'secret') FROM events",+ True,+ {RestrictedProperty(name="$feature/secret", property_type=PropertyDefinition.Type.EVENT)},+ None,+ ("mapFilter(", "and(", "_sensitive)s"),+ ("has(events.properties.`$feature_flags`",),+ ),+ (+ "restricted_active",+ "SELECT count() FROM events WHERE properties.$active_feature_flags = 'checkout'",+ True,+ {RestrictedProperty(name="$active_feature_flags", property_type=PropertyDefinition.Type.EVENT)},+ None,+ ("ifNull(equals(NULL, %(hogql_val_0)s), 0)",),+ ("events.properties.`$feature_flags`",),+ ),+ (+ "restricted_map",+ "SELECT properties.$active_feature_flags, properties.`$feature/checkout`, "+ "toJSONString(properties.$feature_flags), JSONHas(properties, '$active_feature_flags') FROM events",+ True,+ {RestrictedProperty(name="$feature_flags", property_type=PropertyDefinition.Type.EVENT)},+ None,+ ("SELECT NULL AS `$active_feature_flags`, NULL AS `$feature/checkout`, NULL, 0 ",),+ ("events.properties.`$feature_flags`",),+ ),+ (+ "dynamic_restricted",+ "SELECT properties, JSONHas(properties, '$feature_flags', concat('sec', 'ret')) FROM events",+ True,+ {RestrictedProperty(name="$feature/secret", property_type=PropertyDefinition.Type.EVENT)},+ None,+ ("JSONMergePatch(", "mapFilter("),+ (),+ ),+ ]+ )+ def test_feature_flag_property_compatibility_uses_the_schema_backed_map(+ self,+ _name: str,+ select: str,+ use_new_events_schema: bool,+ restricted_properties: set[RestrictedProperty] | None,+ property_groups_mode: PropertyGroupsMode | None,+ expected_fragments: tuple[str, ...],+ unexpected_fragments: tuple[str, ...],+ ) -> None:+ printed = self._print_select(+ select,+ use_new_events_schema=use_new_events_schema,+ restricted_properties=restricted_properties,+ property_groups_mode=property_groups_mode,+ )++ for fragment in expected_fragments:+ assert fragment in printed, printed+ for fragment in unexpected_fragments:+ assert fragment not in printed, printed++ @parameterized.expand(+ [+ ("direct", "SELECT count() FROM events WHERE properties.$active_feature_flags = 'checkout'"),+ (+ "alias",+ "SELECT properties.$active_feature_flags AS flags, count() FROM events WHERE flags = 'checkout'",+ ),+ ]+ )+ def test_active_feature_flag_comparison_uses_direct_map_lookup(self, _name: str, select: str) -> None:+ printed = self._print_select(select, use_new_events_schema=True)+ where = printed.split("WHERE", 1)[1]++ assert "notIn(events.properties.`$feature_flags`[" in where, printed+ assert "mapFilter(" not in where, printed++ @parameterized.expand(+ [+ ("in", "properties.$active_feature_flags IN ('checkout', 'variant')", "in(key, tuple("),+ ("not_in", "properties.$active_feature_flags NOT IN ('checkout')", "not(mapExists("),+ ("ilike", "properties.$active_feature_flags ILIKE 'check%'", "ilike(key, %(hogql_val_"),+ (+ "multi_search",+ "multiSearchAnyCaseInsensitive(properties.$active_feature_flags, ['check']) > 0",+ "multiSearchAnyCaseInsensitive(key, [%(hogql_val_",+ ),+ ]+ )+ def test_active_feature_flag_operator_families_scan_the_map_once(+ self, _name: str, where_clause: str, expected_fragment: str+ ) -> None:+ printed = self._print_select(f"SELECT count() FROM events WHERE {where_clause}", use_new_events_schema=True)+ where = printed.split("WHERE", 1)[1]++ assert "mapExists((key, value) -> and(" in where, printed+ assert expected_fragment in where, printed+ assert "mapKeys(" not in where, printed+ assert "events.properties.`$active_feature_flags`" not in where, printed+def test_exception_types_use_array_subcolumn(self) -> None:printed = self._print_select("select count() from events where properties.$exception_types = 'TypeError'")@@ -1379,6 +1529,133 @@ class TestEventsSchemaPropertyParity(ClickhouseTestMixin, HypothesisDjangoTestCaelse:assert native[2] == legacy[2]+ def test_feature_flag_interfaces_work_across_event_schemas(self) -> None:+ native_uuid = _create_event(+ team=self.team,+ distinct_id="native-flags",+ event="schema-parity",+ properties={+ "$active_feature_flags": ["false-variant", "only-in-array", "secret"],+ "$feature_flags": {+ "checkout": "true",+ "disabled": False,+ "false-variant": "false",+ "only-in-map": "true",+ "secret": "false",+ "variant": "control",+ },+ },+ )+ empty_uuid = _create_event(+ team=self.team,+ distinct_id="native-empty-flags",+ event="schema-parity",+ properties={"$active_feature_flags": [], "$feature_flags": {"checkout": "true"}},+ )+ inactive_uuid = _create_event(+ team=self.team,+ distinct_id="native-inactive-flags",+ event="schema-parity",+ properties={"$feature_flags": {"disabled": False}},+ )+ restricted_only_uuid = _create_event(+ team=self.team,+ distinct_id="native-restricted-flags",+ event="schema-parity",+ properties={"$active_feature_flags": ["secret"], "$feature_flags": {"secret": "false"}},+ )+ flush_persons_and_events()++ native = execute_hogql_query(+ "SELECT properties.`$feature/checkout`, properties.`$feature/variant`, "+ "properties.$feature_flags.checkout, JSONHas(properties, '$feature_flags', 'checkout'), "+ "properties.$active_feature_flags, properties.$active_feature_flags.1, "+ "JSONHas(properties, '$active_feature_flags', 99), "+ "toJSONString(properties.`$feature/variant`), JSONExtractString(properties, '$feature/variant'), "+ "properties.`$feature/false-variant`, properties.$feature_flags.`false-variant`, "+ "properties.`$feature/disabled`, properties.$feature_flags "+ f"FROM events WHERE uuid = '{native_uuid}'",+ team=self.team,+ context=HogQLContext(team_id=self.team.pk, enable_select_queries=True, use_new_events_schema=True),+ )++ assert native.results is not None+ assert native.results[0][:4] == ("true", "control", "true", 1)+ assert json.loads(native.results[0][4]) == ["checkout", "false-variant", "only-in-map", "secret", "variant"]+ assert native.results[0][5] == "checkout"+ assert native.results[0][6] == 0+ assert native.results[0][7:12] == ('"control"', "control", "false", "false", "false")+ assert json.loads(native.results[0][12]) == {+ "checkout": "true",+ "disabled": "false",+ "false-variant": "false",+ "only-in-map": "true",+ "secret": "false",+ "variant": "control",+ }++ operators = execute_hogql_query(+ "SELECT properties.$active_feature_flags IN ('only-in-map', 'missing'), "+ "properties.$active_feature_flags NOT IN ('checkout'), "+ "properties.$active_feature_flags ILIKE 'vari%', "+ "multiSearchAnyCaseInsensitive(properties.$active_feature_flags, ['ONLY-IN']) > 0, "+ "properties.$active_feature_flags = 'false-variant', properties.$active_feature_flags = 'disabled', "+ "properties.`$feature/false-variant` = 'false', properties.`$feature/disabled` = 'false' "+ f"FROM events WHERE uuid = '{native_uuid}'",+ team=self.team,+ context=HogQLContext(team_id=self.team.pk, enable_select_queries=True, use_new_events_schema=True),+ )+ assert operators.results == [(1, 0, 1, 1, 1, 0, 1, 1)]++ restricted_context = HogQLContext(team_id=self.team.pk, enable_select_queries=True, use_new_events_schema=True)+ restricted_context.restricted_properties = {+ RestrictedProperty(name="$feature/secret", property_type=PropertyDefinition.Type.EVENT)+ }+ restricted = execute_hogql_query(+ "SELECT properties, JSONHas(properties, '$feature_flags', concat('sec', 'ret')), "+ "properties.$active_feature_flags "+ f"FROM events WHERE uuid = '{native_uuid}'",+ team=self.team,+ context=restricted_context,+ )+ assert restricted.results is not None+ assert json.loads(restricted.results[0][0])["$feature_flags"] == {+ "checkout": "true",+ "disabled": "false",+ "false-variant": "$false",+ "only-in-map": "true",+ "variant": "control",+ }+ assert restricted.results[0][1] == 0+ assert json.loads(restricted.results[0][2]) == ["checkout", "false-variant", "only-in-map", "variant"]++ restricted_only = execute_hogql_query(+ "SELECT properties.$active_feature_flags, properties.$active_feature_flags != null, "+ "JSONHas(properties, '$active_feature_flags') "+ f"FROM events WHERE uuid = '{restricted_only_uuid}'",+ team=self.team,+ context=restricted_context,+ )+ assert restricted_only.results == [(None, 0, 0)]++ empty = execute_hogql_query(+ "SELECT properties.$active_feature_flags, properties.$active_feature_flags != null, "+ "JSONHas(properties, '$active_feature_flags') "+ f"FROM events WHERE uuid = '{empty_uuid}'",+ team=self.team,+ context=HogQLContext(team_id=self.team.pk, enable_select_queries=True, use_new_events_schema=True),+ )+ assert empty.results == [('["checkout"]', 1, 1)]++ inactive = execute_hogql_query(+ "SELECT properties.$active_feature_flags, properties.$active_feature_flags != null, "+ "JSONHas(properties, '$active_feature_flags') "+ f"FROM events WHERE uuid = '{inactive_uuid}'",+ team=self.team,+ context=HogQLContext(team_id=self.team.pk, enable_select_queries=True, use_new_events_schema=True),+ )+ assert inactive.results == [("[]", 1, 1)]+# ── Timezone index pruning tests ──────────────────────────────────────────────#diff --git a/posthog/hogql/visitor.py b/posthog/hogql/visitor.pyindex bf6e9dd4abed..88631e699a62 100644--- a/posthog/hogql/visitor.py+++ b/posthog/hogql/visitor.py@@ -785,6 +785,7 @@ class CloningVisitor(Visitor[Any]):end=None if self.clear_locations else node.end,type=None if self.clear_types else node.type,value=node.value,+ is_sensitive=node.is_sensitive,inline_sentinel=node.inline_sentinel,)diff --git a/posthog/session_recordings/queries/test/listing_recordings/__snapshots__/test_session_recording_list_by_top_level_event_property.new_events_schema.ambr b/posthog/session_recordings/queries/test/listing_recordings/__snapshots__/test_session_recording_list_by_top_level_event_property.new_events_schema.ambrindex ed9b61eafbea..9baf2460aa70 100644--- a/posthog/session_recordings/queries/test/listing_recordings/__snapshots__/test_session_recording_list_by_top_level_event_property.new_events_schema.ambr+++ b/posthog/session_recordings/queries/test/listing_recordings/__snapshots__/test_session_recording_list_by_top_level_event_property.new_events_schema.ambr@@ -120,12 +120,11 @@coalesce(max(s.surfacing_score), 0.36) AS surfacing_scoreFROM session_replay_events AS sWHERE and(equals(s.team_id, 99999), greaterOrEquals(s.min_first_timestamp, toDateTime64('2021-01-18 00:00:00.000000', 6, 'UTC')), lessOrEquals(s.min_first_timestamp, toDateTime64('today', 6, 'UTC')), globalIn(s.session_id,- (SELECT events.properties.`$session_id` AS session_id+ (SELECT if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))) AS session_idFROM events_json AS events- WHERE and(equals(events.team_id, 99999), notEmpty(events.properties.`$session_id`), lessOrEquals(events.timestamp, now64(6, 'UTC')), and(in(events.event, ['$pageview', 'bar', 'baz', 'foo']), ifNull(equals(if(notEquals(toJSONString(events.properties.^`$feature/target-flag`), '{}'), toJSONString(events.properties.^`$feature/target-flag`), if(isNull(events.properties.`$feature/target-flag`), NULL, if(startsWith(dynamicType(events.properties.`$feature/target-flag`), 'DateTime'), replaceOne(toString(events.properties.`$feature/target-flag`), ' ', 'T'), if(or(startsWith(dynamicType(events.properties.`$feature/target-flag`), 'Array'), startsWith(dynamicType(events.properties.`$feature/target-flag`), 'Map'), startsWith(dynamicType(events.properties.`$feature/target-flag`), 'Tuple')), toJSONString(events.properties.`$feature/target-flag`), toString(events.properties.`$feature/target-flag`))))), 'true'), 0)), greaterOrEquals(events.timestamp, toDateTime64('2021-01-17 00:00:00.000000', 6, 'UTC')))- GROUP BY events.properties.`$session_id`+ WHERE and(equals(events.team_id, 99999), notEmpty(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`)))))), lessOrEquals(events.timestamp, now64(6, 'UTC')), and(in(events.event, ['$pageview', 'bar', 'baz', 'foo']), ifNull(equals(if(has(events.properties.`$feature_flags`, 'target-flag'), if(ifNull(equals(events.properties.`$feature_flags`['target-flag'], '$false'), 0), 'false', events.properties.`$feature_flags`['target-flag']), NULL), 'true'), 0)), greaterOrEquals(events.timestamp, toDateTime64('2021-01-17 00:00:00.000000', 6, 'UTC')))+ GROUP BY if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`)))))HAVING 1- ORDER BY min(toTimeZone(events.timestamp, 'UTC')) DESCLIMIT 1000000)))GROUP BY s.session_idHAVING and(greaterOrEquals(expiry_time, toDateTime64('today', 6, 'UTC')), equals(max(s.is_deleted), 0))@@ -170,12 +169,11 @@coalesce(max(s.surfacing_score), 0.36) AS surfacing_scoreFROM session_replay_events AS sWHERE and(equals(s.team_id, 99999), greaterOrEquals(s.min_first_timestamp, toDateTime64('2021-01-18 00:00:00.000000', 6, 'UTC')), lessOrEquals(s.min_first_timestamp, toDateTime64('today', 6, 'UTC')), globalIn(s.session_id,- (SELECT events.properties.`$session_id` AS session_id+ (SELECT if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))) AS session_idFROM events_json AS events- WHERE and(equals(events.team_id, 99999), notEmpty(events.properties.`$session_id`), lessOrEquals(events.timestamp, now64(6, 'UTC')), and(in(events.event, ['$pageview', 'bar', 'baz', 'foo']), ifNull(equals(if(notEquals(toJSONString(events.properties.^`$feature/target-flag`), '{}'), toJSONString(events.properties.^`$feature/target-flag`), if(isNull(events.properties.`$feature/target-flag`), NULL, if(startsWith(dynamicType(events.properties.`$feature/target-flag`), 'DateTime'), replaceOne(toString(events.properties.`$feature/target-flag`), ' ', 'T'), if(or(startsWith(dynamicType(events.properties.`$feature/target-flag`), 'Array'), startsWith(dynamicType(events.properties.`$feature/target-flag`), 'Map'), startsWith(dynamicType(events.properties.`$feature/target-flag`), 'Tuple')), toJSONString(events.properties.`$feature/target-flag`), toString(events.properties.`$feature/target-flag`))))), 'false'), 0)), greaterOrEquals(events.timestamp, toDateTime64('2021-01-17 00:00:00.000000', 6, 'UTC')))- GROUP BY events.properties.`$session_id`+ WHERE and(equals(events.team_id, 99999), notEmpty(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`)))))), lessOrEquals(events.timestamp, now64(6, 'UTC')), and(in(events.event, ['$pageview', 'bar', 'baz', 'foo']), ifNull(equals(if(has(events.properties.`$feature_flags`, 'target-flag'), if(ifNull(equals(events.properties.`$feature_flags`['target-flag'], '$false'), 0), 'false', events.properties.`$feature_flags`['target-flag']), NULL), 'false'), 0)), greaterOrEquals(events.timestamp, toDateTime64('2021-01-17 00:00:00.000000', 6, 'UTC')))+ GROUP BY if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`)))))HAVING 1- ORDER BY min(toTimeZone(events.timestamp, 'UTC')) DESCLIMIT 1000000)))GROUP BY s.session_idHAVING and(greaterOrEquals(expiry_time, toDateTime64('today', 6, 'UTC')), equals(max(s.is_deleted), 0))@@ -220,12 +218,11 @@coalesce(max(s.surfacing_score), 0.36) AS surfacing_scoreFROM session_replay_events AS sWHERE and(equals(s.team_id, 99999), greaterOrEquals(s.min_first_timestamp, toDateTime64('2021-01-18 00:00:00.000000', 6, 'UTC')), lessOrEquals(s.min_first_timestamp, toDateTime64('today', 6, 'UTC')), globalIn(s.session_id,- (SELECT events.properties.`$session_id` AS session_id+ (SELECT if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))) AS session_idFROM events_json AS events- WHERE and(equals(events.team_id, 99999), notEmpty(events.properties.`$session_id`), lessOrEquals(events.timestamp, now64(6, 'UTC')), and(in(events.event, ['$pageview', 'bar', 'baz', 'foo']), isNotNull(if(notEquals(toJSONString(events.properties.^`$feature/target-flag`), '{}'), toJSONString(events.properties.^`$feature/target-flag`), if(isNull(events.properties.`$feature/target-flag`), NULL, if(startsWith(dynamicType(events.properties.`$feature/target-flag`), 'DateTime'), replaceOne(toString(events.properties.`$feature/target-flag`), ' ', 'T'), if(or(startsWith(dynamicType(events.properties.`$feature/target-flag`), 'Array'), startsWith(dynamicType(events.properties.`$feature/target-flag`), 'Map'), startsWith(dynamicType(events.properties.`$feature/target-flag`), 'Tuple')), toJSONString(events.properties.`$feature/target-flag`), toString(events.properties.`$feature/target-flag`))))))), greaterOrEquals(events.timestamp, toDateTime64('2021-01-17 00:00:00.000000', 6, 'UTC')))- GROUP BY events.properties.`$session_id`+ WHERE and(equals(events.team_id, 99999), notEmpty(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`)))))), lessOrEquals(events.timestamp, now64(6, 'UTC')), and(in(events.event, ['$pageview', 'bar', 'baz', 'foo']), isNotNull(if(has(events.properties.`$feature_flags`, 'target-flag'), if(ifNull(equals(events.properties.`$feature_flags`['target-flag'], '$false'), 0), 'false', events.properties.`$feature_flags`['target-flag']), NULL))), greaterOrEquals(events.timestamp, toDateTime64('2021-01-17 00:00:00.000000', 6, 'UTC')))+ GROUP BY if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`)))))HAVING 1- ORDER BY min(toTimeZone(events.timestamp, 'UTC')) DESCLIMIT 1000000)))GROUP BY s.session_idHAVING and(greaterOrEquals(expiry_time, toDateTime64('today', 6, 'UTC')), equals(max(s.is_deleted), 0))@@ -270,10 +267,10 @@coalesce(max(s.surfacing_score), 0.36) AS surfacing_scoreFROM session_replay_events AS sWHERE and(equals(s.team_id, 99999), greaterOrEquals(s.min_first_timestamp, toDateTime64('2021-01-18 00:00:00.000000', 6, 'UTC')), lessOrEquals(s.min_first_timestamp, toDateTime64('today', 6, 'UTC')), globalNotIn(s.session_id,- (SELECT events.properties.`$session_id` AS session_id+ (SELECT if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))) AS session_idFROM events_json AS events- WHERE and(equals(events.team_id, 99999), notEmpty(events.properties.`$session_id`), lessOrEquals(events.timestamp, now64(6, 'UTC')), isNotNull(if(notEquals(toJSONString(events.properties.^`$feature/target-flag`), '{}'), toJSONString(events.properties.^`$feature/target-flag`), if(isNull(events.properties.`$feature/target-flag`), NULL, if(startsWith(dynamicType(events.properties.`$feature/target-flag`), 'DateTime'), replaceOne(toString(events.properties.`$feature/target-flag`), ' ', 'T'), if(or(startsWith(dynamicType(events.properties.`$feature/target-flag`), 'Array'), startsWith(dynamicType(events.properties.`$feature/target-flag`), 'Map'), startsWith(dynamicType(events.properties.`$feature/target-flag`), 'Tuple')), toJSONString(events.properties.`$feature/target-flag`), toString(events.properties.`$feature/target-flag`)))))), greaterOrEquals(events.timestamp, toDateTime64('2021-01-17 00:00:00.000000', 6, 'UTC')))- GROUP BY events.properties.`$session_id`+ WHERE and(equals(events.team_id, 99999), notEmpty(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`)))))), lessOrEquals(events.timestamp, now64(6, 'UTC')), isNotNull(if(has(events.properties.`$feature_flags`, 'target-flag'), if(ifNull(equals(events.properties.`$feature_flags`['target-flag'], '$false'), 0), 'false', events.properties.`$feature_flags`['target-flag']), NULL)), greaterOrEquals(events.timestamp, toDateTime64('2021-01-17 00:00:00.000000', 6, 'UTC')))+ GROUP BY if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`)))))LIMIT 1000000)))GROUP BY s.session_idHAVING and(greaterOrEquals(expiry_time, toDateTime64('today', 6, 'UTC')), equals(max(s.is_deleted), 0))@@ -462,12 +459,11 @@coalesce(max(s.surfacing_score), 0.36) AS surfacing_scoreFROM session_replay_events AS sWHERE and(equals(s.team_id, 99999), greaterOrEquals(s.min_first_timestamp, toDateTime64('2021-01-18 00:00:00.000000', 6, 'UTC')), lessOrEquals(s.min_first_timestamp, toDateTime64('today', 6, 'UTC')), globalIn(s.session_id,- (SELECT events.properties.`$session_id` AS session_id+ (SELECT if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))) AS session_idFROM events_json AS events- WHERE and(equals(events.team_id, 99999), notEmpty(events.properties.`$session_id`), lessOrEquals(events.timestamp, now64(6, 'UTC')), ifNull(equals(if(notEquals(toJSONString(events.properties.^`$feature/target-flag`), '{}'), toJSONString(events.properties.^`$feature/target-flag`), if(isNull(events.properties.`$feature/target-flag`), NULL, if(startsWith(dynamicType(events.properties.`$feature/target-flag`), 'DateTime'), replaceOne(toString(events.properties.`$feature/target-flag`), ' ', 'T'), if(or(startsWith(dynamicType(events.properties.`$feature/target-flag`), 'Array'), startsWith(dynamicType(events.properties.`$feature/target-flag`), 'Map'), startsWith(dynamicType(events.properties.`$feature/target-flag`), 'Tuple')), toJSONString(events.properties.`$feature/target-flag`), toString(events.properties.`$feature/target-flag`))))), 'true'), 0), greaterOrEquals(events.timestamp, toDateTime64('2021-01-17 00:00:00.000000', 6, 'UTC')))- GROUP BY events.properties.`$session_id`+ WHERE and(equals(events.team_id, 99999), notEmpty(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`)))))), lessOrEquals(events.timestamp, now64(6, 'UTC')), ifNull(equals(if(has(events.properties.`$feature_flags`, 'target-flag'), if(ifNull(equals(events.properties.`$feature_flags`['target-flag'], '$false'), 0), 'false', events.properties.`$feature_flags`['target-flag']), NULL), 'true'), 0), greaterOrEquals(events.timestamp, toDateTime64('2021-01-17 00:00:00.000000', 6, 'UTC')))+ GROUP BY if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`)))))HAVING 1- ORDER BY min(toTimeZone(events.timestamp, 'UTC')) DESCLIMIT 1000000)))GROUP BY s.session_idHAVING and(greaterOrEquals(expiry_time, toDateTime64('today', 6, 'UTC')), equals(max(s.is_deleted), 0))@@ -512,12 +508,11 @@coalesce(max(s.surfacing_score), 0.36) AS surfacing_scoreFROM session_replay_events AS sWHERE and(equals(s.team_id, 99999), greaterOrEquals(s.min_first_timestamp, toDateTime64('2021-01-18 00:00:00.000000', 6, 'UTC')), lessOrEquals(s.min_first_timestamp, toDateTime64('today', 6, 'UTC')), globalIn(s.session_id,- (SELECT events.properties.`$session_id` AS session_id+ (SELECT if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))) AS session_idFROM events_json AS events- WHERE and(equals(events.team_id, 99999), notEmpty(events.properties.`$session_id`), lessOrEquals(events.timestamp, now64(6, 'UTC')), ifNull(equals(if(notEquals(toJSONString(events.properties.^`$feature/target-flag`), '{}'), toJSONString(events.properties.^`$feature/target-flag`), if(isNull(events.properties.`$feature/target-flag`), NULL, if(startsWith(dynamicType(events.properties.`$feature/target-flag`), 'DateTime'), replaceOne(toString(events.properties.`$feature/target-flag`), ' ', 'T'), if(or(startsWith(dynamicType(events.properties.`$feature/target-flag`), 'Array'), startsWith(dynamicType(events.properties.`$feature/target-flag`), 'Map'), startsWith(dynamicType(events.properties.`$feature/target-flag`), 'Tuple')), toJSONString(events.properties.`$feature/target-flag`), toString(events.properties.`$feature/target-flag`))))), 'false'), 0), greaterOrEquals(events.timestamp, toDateTime64('2021-01-17 00:00:00.000000', 6, 'UTC')))- GROUP BY events.properties.`$session_id`+ WHERE and(equals(events.team_id, 99999), notEmpty(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`)))))), lessOrEquals(events.timestamp, now64(6, 'UTC')), ifNull(equals(if(has(events.properties.`$feature_flags`, 'target-flag'), if(ifNull(equals(events.properties.`$feature_flags`['target-flag'], '$false'), 0), 'false', events.properties.`$feature_flags`['target-flag']), NULL), 'false'), 0), greaterOrEquals(events.timestamp, toDateTime64('2021-01-17 00:00:00.000000', 6, 'UTC')))+ GROUP BY if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`)))))HAVING 1- ORDER BY min(toTimeZone(events.timestamp, 'UTC')) DESCLIMIT 1000000)))GROUP BY s.session_idHAVING and(greaterOrEquals(expiry_time, toDateTime64('today', 6, 'UTC')), equals(max(s.is_deleted), 0))@@ -562,12 +557,11 @@coalesce(max(s.surfacing_score), 0.36) AS surfacing_scoreFROM session_replay_events AS sWHERE and(equals(s.team_id, 99999), greaterOrEquals(s.min_first_timestamp, toDateTime64('2021-01-18 00:00:00.000000', 6, 'UTC')), lessOrEquals(s.min_first_timestamp, toDateTime64('today', 6, 'UTC')), globalIn(s.session_id,- (SELECT events.properties.`$session_id` AS session_id+ (SELECT if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))) AS session_idFROM events_json AS events- WHERE and(equals(events.team_id, 99999), notEmpty(events.properties.`$session_id`), lessOrEquals(events.timestamp, now64(6, 'UTC')), isNotNull(if(notEquals(toJSONString(events.properties.^`$feature/target-flag`), '{}'), toJSONString(events.properties.^`$feature/target-flag`), if(isNull(events.properties.`$feature/target-flag`), NULL, if(startsWith(dynamicType(events.properties.`$feature/target-flag`), 'DateTime'), replaceOne(toString(events.properties.`$feature/target-flag`), ' ', 'T'), if(or(startsWith(dynamicType(events.properties.`$feature/target-flag`), 'Array'), startsWith(dynamicType(events.properties.`$feature/target-flag`), 'Map'), startsWith(dynamicType(events.properties.`$feature/target-flag`), 'Tuple')), toJSONString(events.properties.`$feature/target-flag`), toString(events.properties.`$feature/target-flag`)))))), greaterOrEquals(events.timestamp, toDateTime64('2021-01-17 00:00:00.000000', 6, 'UTC')))- GROUP BY events.properties.`$session_id`+ WHERE and(equals(events.team_id, 99999), notEmpty(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`)))))), lessOrEquals(events.timestamp, now64(6, 'UTC')), isNotNull(if(has(events.properties.`$feature_flags`, 'target-flag'), if(ifNull(equals(events.properties.`$feature_flags`['target-flag'], '$false'), 0), 'false', events.properties.`$feature_flags`['target-flag']), NULL)), greaterOrEquals(events.timestamp, toDateTime64('2021-01-17 00:00:00.000000', 6, 'UTC')))+ GROUP BY if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`)))))HAVING 1- ORDER BY min(toTimeZone(events.timestamp, 'UTC')) DESCLIMIT 1000000)))GROUP BY s.session_idHAVING and(greaterOrEquals(expiry_time, toDateTime64('today', 6, 'UTC')), equals(max(s.is_deleted), 0))@@ -612,10 +606,10 @@coalesce(max(s.surfacing_score), 0.36) AS surfacing_scoreFROM session_replay_events AS sWHERE and(equals(s.team_id, 99999), greaterOrEquals(s.min_first_timestamp, toDateTime64('2021-01-18 00:00:00.000000', 6, 'UTC')), lessOrEquals(s.min_first_timestamp, toDateTime64('today', 6, 'UTC')), globalNotIn(s.session_id,- (SELECT events.properties.`$session_id` AS session_id+ (SELECT if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))) AS session_idFROM events_json AS events- WHERE and(equals(events.team_id, 99999), notEmpty(events.properties.`$session_id`), lessOrEquals(events.timestamp, now64(6, 'UTC')), isNotNull(if(notEquals(toJSONString(events.properties.^`$feature/target-flag`), '{}'), toJSONString(events.properties.^`$feature/target-flag`), if(isNull(events.properties.`$feature/target-flag`), NULL, if(startsWith(dynamicType(events.properties.`$feature/target-flag`), 'DateTime'), replaceOne(toString(events.properties.`$feature/target-flag`), ' ', 'T'), if(or(startsWith(dynamicType(events.properties.`$feature/target-flag`), 'Array'), startsWith(dynamicType(events.properties.`$feature/target-flag`), 'Map'), startsWith(dynamicType(events.properties.`$feature/target-flag`), 'Tuple')), toJSONString(events.properties.`$feature/target-flag`), toString(events.properties.`$feature/target-flag`)))))), greaterOrEquals(events.timestamp, toDateTime64('2021-01-17 00:00:00.000000', 6, 'UTC')))- GROUP BY events.properties.`$session_id`+ WHERE and(equals(events.team_id, 99999), notEmpty(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`)))))), lessOrEquals(events.timestamp, now64(6, 'UTC')), isNotNull(if(has(events.properties.`$feature_flags`, 'target-flag'), if(ifNull(equals(events.properties.`$feature_flags`['target-flag'], '$false'), 0), 'false', events.properties.`$feature_flags`['target-flag']), NULL)), greaterOrEquals(events.timestamp, toDateTime64('2021-01-17 00:00:00.000000', 6, 'UTC')))+ GROUP BY if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`)))))LIMIT 1000000)))GROUP BY s.session_idHAVING and(greaterOrEquals(expiry_time, toDateTime64('today', 6, 'UTC')), equals(max(s.is_deleted), 0))diff --git a/posthog/session_recordings/queries/test/listing_recordings/test_session_recording_list_by_top_level_event_property.py b/posthog/session_recordings/queries/test/listing_recordings/test_session_recording_list_by_top_level_event_property.pyindex 838e552e96f7..4fc60f4d564a 100644--- a/posthog/session_recordings/queries/test/listing_recordings/test_session_recording_list_by_top_level_event_property.py+++ b/posthog/session_recordings/queries/test/listing_recordings/test_session_recording_list_by_top_level_event_property.py@@ -2,9 +2,7 @@ from datetime import timedeltaimport time_machinefrom posthog.test.base import APIBaseTest, ClickhouseTestMixin, snapshot_clickhouse_queries-from unittest import skipIf-from django.conf import settingsfrom django.utils.timezone import nowfrom dateutil.relativedelta import relativedelta@@ -161,10 +159,6 @@ class TestSessionRecordingsListByTopLevelEventProperty(ClickhouseTestMixin, APIB)@time_machine.travel("2021-01-21T20:00:00.000Z", tick=False)@snapshot_clickhouse_queries- @skipIf(- settings.CLICKHOUSE_HOGQL_USE_NEW_EVENTS_SCHEMA,- "the native-JSON events table keeps flags in the $feature_flags map, which HogQL does not read yet",- )def test_can_filter_for_flags(self, _name: str, properties: dict, expected: list[str]) -> None:create_person(team=self.team, distinct_ids=["user"], properties={"email": "bla"})diff --git a/posthog/session_recordings/queries/test/listing_recordings/test_session_recordings_list_by_experiment_exposure.py b/posthog/session_recordings/queries/test/listing_recordings/test_session_recordings_list_by_experiment_exposure.pyindex 6120bff33039..167680893df7 100644--- a/posthog/session_recordings/queries/test/listing_recordings/test_session_recordings_list_by_experiment_exposure.py+++ b/posthog/session_recordings/queries/test/listing_recordings/test_session_recordings_list_by_experiment_exposure.py@@ -4,11 +4,8 @@ from typing import castimport time_machinefrom posthog.test.base import APIBaseTest, ClickhouseTestMixin, _create_event, flush_persons_and_events-from unittest import skipIffrom unittest.mock import patch-from django.conf import settings-from parameterized import parameterizedfrom rest_framework.exceptions import PermissionDenied, ValidationError@@ -414,10 +411,6 @@ class TestSessionRecordingsListByExperimentExposure(ClickhouseTestMixin, APIBase["session-after-activation"],)- @skipIf(- settings.CLICKHOUSE_HOGQL_USE_NEW_EVENTS_SCHEMA,- "the native-JSON events table keeps flags in the $feature_flags map, which HogQL does not read yet",- )def test_custom_exposure_event_defines_the_exposure_moment(self) -> None:experiment = self._create_experiment(exposure_criteria={@@ -528,10 +521,6 @@ class TestSessionRecordingsListByExperimentExposure(ClickhouseTestMixin, APIBase["session-test-evidence"],)- @skipIf(- settings.CLICKHOUSE_HOGQL_USE_NEW_EVENTS_SCHEMA,- "the native-JSON events table keeps flags in the $feature_flags map, which HogQL does not read yet",- )def test_in_session_reads_the_stamped_flag_property_when_the_exposure_event_was_never_session_linked(self) -> None:experiment = self._create_experiment()create_person(team=self.team, distinct_ids=["backend-exposed-user"])diff --git a/posthog/user_scripts/json_clean_posthog_event_properties_udf_aarch64 b/posthog/user_scripts/json_clean_posthog_event_properties_udf_aarch64index 77104d5ee885..96a2ee7dbb94 100755Binary files a/posthog/user_scripts/json_clean_posthog_event_properties_udf_aarch64 and b/posthog/user_scripts/json_clean_posthog_event_properties_udf_aarch64 differdiff --git a/posthog/user_scripts/json_clean_posthog_event_properties_udf_x86_64 b/posthog/user_scripts/json_clean_posthog_event_properties_udf_x86_64index 676940073097..bab120ad189a 100755Binary files a/posthog/user_scripts/json_clean_posthog_event_properties_udf_x86_64 and b/posthog/user_scripts/json_clean_posthog_event_properties_udf_x86_64 differdiff --git a/products/batch_exports/backend/debug/debugger.py b/products/batch_exports/backend/debug/debugger.pyindex 7ba089ba3103..7b2cde13e505 100644--- a/products/batch_exports/backend/debug/debugger.py+++ b/products/batch_exports/backend/debug/debugger.py@@ -429,13 +429,6 @@ class BatchExportsDebugger:extra_query_parameters: dict[str, str] = {}filters = batch_export_run.parent.filters- if filters is not None and len(filters) > 0:- filters_str, extra_query_parameters = compose_filters_clause(- filters, team_id=team_id, values=extra_query_parameters- )- else:- filters_str, extra_query_parameters = "", extra_query_parameters-is_backfill = batch_export_run.backfill is not Noneif batch_export_run.parent.model == BatchExport.Model.PERSONS:@@ -488,7 +481,15 @@ class BatchExportsDebugger:query_fields = ",".join(f"{field['expression']} AS {field['alias']}" for field in fields + control_fields)- if query_template is SELECT_FROM_EVENTS_VIEW_BACKFILL and use_new_events_schema(team_id):+ native_source = query_template is SELECT_FROM_EVENTS_VIEW_BACKFILL and use_new_events_schema(team_id)++ filters_str = ""+ if filters is not None and len(filters) > 0:+ filters_str, extra_query_parameters = compose_filters_clause(+ filters, team_id=team_id, values=extra_query_parameters, native_events_source=native_source+ )++ if native_source:query = native_events_export_query(query_fields, filters_str)else:if filters_str:diff --git a/products/batch_exports/backend/hogql_source.py b/products/batch_exports/backend/hogql_source.pyindex b7885340a361..48aae9e9fe2e 100644--- a/products/batch_exports/backend/hogql_source.py+++ b/products/batch_exports/backend/hogql_source.py@@ -8,6 +8,7 @@ import typingimport datetime as dtfrom posthog.hogql import ast+from posthog.hogql.constants import FEATURE_FLAG_FALSE_VARIANT_SENTINELfrom posthog.hogql.context import HogQLContextfrom posthog.hogql.database.database import Databasefrom posthog.hogql.errors import ExposedHogQLError, QueryError@@ -16,7 +17,7 @@ from posthog.hogql.modifiers import create_default_modifiers_for_teamfrom posthog.hogql.parser import parse_selectfrom posthog.hogql.placeholders import find_placeholders, replace_placeholdersfrom posthog.hogql.printer import prepare_ast_for_printing, print_prepared_ast-from posthog.hogql.visitor import CloningVisitor+from posthog.hogql.visitor import CloningVisitor, clone_exprfrom posthog.clickhouse.events_json import UNPARSEABLE_PROPERTIES_KEY@@ -205,6 +206,37 @@ def validate_hogql_query_for_batch_export(hogql_query: str, team: "Team", *, useraise UnsupportedHogQLQueryError(f"Invalid HogQL query: {e}") from e+def native_event_property_chain(property_chain: list[str | int]) -> list[str | int]:+ """The path an event property has in the native source's JSON.++ The native table keeps `$feature/<key>` flags in the `$feature_flags` map, so a `$feature/<key>` read must+ become `$feature_flags.<key>` there. Every other property keeps its path.+ """+ key = property_chain[0] if property_chain else None+ if isinstance(key, str) and key.startswith("$feature/"):+ return ["$feature_flags", key.removeprefix("$feature/"), *property_chain[1:]]+ return list(property_chain)+++def native_feature_flag_read(field: ast.Field, property_chain: list[str | int]) -> ast.Expr:+ """A native `$feature_flags.<key>` read with the `$false` sentinel mapped back to the variant name "false".++ The hidden alias carries the column name the resolver would give the bare field, so an un-aliased select column+ still serializes to a stable name instead of the parameterized expression.+ """+ if len(property_chain) != 2 or property_chain[0] != "$feature_flags":+ return field+ mapped = ast.Call(+ name="if",+ args=[+ ast.Call(name="equals", args=[clone_expr(field), ast.Constant(value=FEATURE_FLAG_FALSE_VARIANT_SENTINEL)]),+ ast.Constant(value="false"),+ field,+ ],+ )+ return ast.Alias(alias="__".join(str(part) for part in property_chain), expr=mapped, hidden=True)++class SerializedExportProperties(CloningVisitor):"""Rebind event fields while preserving property access restrictions."""@@ -216,6 +248,7 @@ class SerializedExportProperties(CloningVisitor):split_restricted_property_names,)+ self.use_native_schema = context.uses_new_events_schema()restrictions = context.restricted_properties or set()restricted_names = split_restricted_property_names(restrictions)self.event_restrictions = set(restricted_names.event)@@ -234,21 +267,36 @@ class SerializedExportProperties(CloningVisitor):if node.chain[index : index + 2] == ["person", "properties"]:# `poe.properties` is the events table's own copy of the person properties.node.chain[index : index + 2] = ["poe", "properties"]+ is_event_property = Falseif node.chain[index : index + 2] == ["poe", "properties"]:restrictions, property_chain = self.person_restrictions, node.chain[index + 2 :]elif str(node.chain[index]) == "properties":restrictions, property_chain = self.event_restrictions, node.chain[index + 1 :]+ is_event_property = Trueelse:return nodeif restrictions:property_path = ".".join(str(part) for part in property_chain)if not property_path:raise QueryError("Batch export queries cannot select a restricted properties object")+ checked_paths = [property_path]+ if self.use_native_schema and is_event_property and property_chain[0] == "$feature_flags":+ if len(property_chain) == 1:+ # The serialized map cannot drop single entries, so any restricted flag hides the whole map.+ if any(key.startswith("$feature/") for key in restrictions):+ return ast.Constant(value=None)+ else:+ # A restriction names the flag as `$feature/<key>`, the spelling the native map entry replaces.+ checked_paths.append("$feature/" + ".".join(str(part) for part in property_chain[1:]))if any(- property_path == key or property_path.startswith(key + ".") or key.startswith(property_path + ".")+ path == key or path.startswith(key + ".") or key.startswith(path + ".")+ for path in checked_pathsfor key in restrictions):return ast.Constant(value=None)+ if self.use_native_schema and is_event_property:+ node.chain[index + 1 :] = native_event_property_chain(property_chain)+ return native_feature_flag_read(node, node.chain[index + 1 :])return nodediff --git a/products/batch_exports/backend/temporal/destinations/http_batch_export.py b/products/batch_exports/backend/temporal/destinations/http_batch_export.pyindex d831a40cca49..25aac8d21e7f 100644--- a/products/batch_exports/backend/temporal/destinations/http_batch_export.py+++ b/products/batch_exports/backend/temporal/destinations/http_batch_export.py@@ -220,22 +220,21 @@ async def insert_into_http_activity(inputs: HttpInsertInputs) -> BatchExportResu# Only the native source projects the mutation columns; a legacy table carries the same keys# in its `properties`.- fields = http_default_fields(- reads_native_events_source(- use_new_events_schema=use_native_schema,- team_id=inputs.team_id,- interval_start=interval_start,- interval_end=inputs.data_interval_end,- is_backfill=is_backfill,- backfill_details=inputs.backfill_details,- )+ native_source = reads_native_events_source(+ use_new_events_schema=use_native_schema,+ team_id=inputs.team_id,+ interval_start=interval_start,+ interval_end=inputs.data_interval_end,+ is_backfill=is_backfill,+ backfill_details=inputs.backfill_details,)+ fields = http_default_fields(native_source)columns = [field["alias"] for field in fields]filters = inputs.batch_export_model.filters if inputs.batch_export_model is not None else Noneif filters is not None and len(filters) > 0:filters_str, extra_query_parameters = await database_sync_to_async(compose_filters_clause)(- filters, team_id=inputs.team_id, values=None+ filters, team_id=inputs.team_id, values=None, native_events_source=native_source)else:filters_str = ""diff --git a/products/batch_exports/backend/temporal/filters.py b/products/batch_exports/backend/temporal/filters.pyindex 6659f8576a41..ad2886bcd5ae 100644--- a/products/batch_exports/backend/temporal/filters.py+++ b/products/batch_exports/backend/temporal/filters.py@@ -16,10 +16,11 @@ from posthog.hogql.hogql import astfrom posthog.hogql.parser import parse_exprfrom posthog.hogql.printer import prepare_ast_for_printing, print_prepared_astfrom posthog.hogql.property import property_to_expr-from posthog.hogql.visitor import TraversingVisitor+from posthog.hogql.visitor import CloningVisitor, TraversingVisitorfrom posthog.models import Team+from products.batch_exports.backend.hogql_source import native_event_property_chain, native_feature_flag_readfrom products.batch_exports.backend.service import SUPPORTED_FILTER_TYPES@@ -31,6 +32,18 @@ class UpdatePropertiesToPersonProperties(TraversingVisitor):node.chain = ["events", "poe", "properties", *node.chain[1:]]+class UpdatePropertiesToNativePaths(CloningVisitor):+ """Move event property paths to where the native events source's JSON keeps them."""++ def visit_field(self, node: ast.Field) -> ast.Expr:+ node = super().visit_field(node)+ index = 1 if node.chain and node.chain[0] == "events" else 0+ if node.chain[index : index + 1] != ["properties"]:+ return node+ node.chain[index + 1 :] = native_event_property_chain(node.chain[index + 1 :])+ return native_feature_flag_read(node, node.chain[index + 1 :])++class InvalidFilterError(Exception):"""Error raised when an invalid filter is used."""@@ -48,6 +61,8 @@ def compose_filters_clause(filters: list[dict[str, str | list[str] | None]],team_id: int,values: dict[str, str] | None = None,+ *,+ native_events_source: bool = False,) -> tuple[str, dict[str, str]]:"""Compose a clause of matching filters for a batch exports query.@@ -58,6 +73,8 @@ def compose_filters_clause(filters: A list of serialized HogQL filters.team_id: Team we are running for.values: HogQL placeholder values already in use.+ native_events_source: Whether the clause filters the native events source, whose+ `properties` JSON keeps `$feature/<key>` flags under `$feature_flags`.Returns:A printed string with the ClickHouse SQL clause, and a dictionary@@ -115,7 +132,9 @@ def compose_filters_clause(# Reachable only if SUPPORTED_FILTER_TYPES gains a type without a handler here.raise TypeError(f"Unhandled filter type: '{filter_type}'")- and_expr = ast.And(exprs=exprs)+ and_expr: ast.Expr = ast.And(exprs=exprs)+ if native_events_source:+ and_expr = UpdatePropertiesToNativePaths().visit(and_expr)# This query only supports events at the moment.# TODO: Extend for other models that also wish to implement property filtering.select_query = ast.SelectQuery(diff --git a/products/batch_exports/backend/temporal/pipeline/internal_stage.py b/products/batch_exports/backend/temporal/pipeline/internal_stage.pyindex 6c36219c002c..44cfb03e37f8 100644--- a/products/batch_exports/backend/temporal/pipeline/internal_stage.py+++ b/products/batch_exports/backend/temporal/pipeline/internal_stage.py@@ -554,13 +554,6 @@ async def _get_query(extra_query_parameters = parameters.pop("extra_query_parameters", {}) or {}- if filters is not None and len(filters) > 0:- filters_str, extra_query_parameters = await database_sync_to_async(compose_filters_clause)(- filters, team_id=team_id, values=extra_query_parameters- )- else:- filters_str, extra_query_parameters = "", extra_query_parameters-is_backfill = backfill_details is not None# The number of partitions controls how many files ClickHouse writes to concurrently.num_partitions = num_partitions or settings.BATCH_EXPORT_CLICKHOUSE_S3_PARTITIONS@@ -642,9 +635,17 @@ async def _get_query(lookback_days = settings.OVERRIDE_TIMESTAMP_TEAM_IDS.get(team_id, settings.DEFAULT_TIMESTAMP_LOOKBACK_DAYS)parameters["lookback_days"] = lookback_days- if query_template is EXPORT_TO_S3_FROM_EVENTS_BACKFILL and await database_sync_to_async(use_new_events_schema)(- team_id- ):+ native_source = query_template is EXPORT_TO_S3_FROM_EVENTS_BACKFILL and await database_sync_to_async(+ use_new_events_schema+ )(team_id)++ filters_str = ""+ if filters is not None and len(filters) > 0:+ filters_str, extra_query_parameters = await database_sync_to_async(compose_filters_clause)(+ filters, team_id=team_id, values=extra_query_parameters, native_events_source=native_source+ )++ if native_source:query = native_events_export_query(query_fields, filters_str, s3_function=s3_function)else:if filters_str:diff --git a/products/batch_exports/backend/tests/api/test_serializers.py b/products/batch_exports/backend/tests/api/test_serializers.pyindex 52b6a5e7f25b..feef7ed11e14 100644--- a/products/batch_exports/backend/tests/api/test_serializers.py+++ b/products/batch_exports/backend/tests/api/test_serializers.py@@ -209,6 +209,48 @@ class TestSerializeHogQLQueryToBatchExportSchema(BaseTest):prepare_query("SELECT properties FROM events", self.team.pk))+ @parameterized.expand(+ [+ ("legacy_name", "SELECT properties.`$feature/secret` AS flag FROM events", "NULL"),+ ("map_entry", "SELECT properties.$feature_flags.secret AS flag FROM events", "NULL"),+ ("whole_map", "SELECT properties.$feature_flags AS flags FROM events", "NULL"),+ ("visible_entry", "SELECT properties.$feature_flags.checkout AS flag FROM events", "JSONExtractRaw("),+ ]+ )+ @override_settings(CLICKHOUSE_HOGQL_USE_NEW_EVENTS_SCHEMA=True)+ @patch("products.batch_exports.backend.api.batch_export.get_restricted_properties_with_group_type_index_for_team")+ @patch("posthog.models.event.new_events_schema.use_new_events_schema", return_value=True)+ def test_native_export_hides_a_restricted_flag_under_every_spelling(+ self, _name: str, query: str, expected: str, _use_new_events_schema, get_restricted_properties+ ):+ get_restricted_properties.return_value = {+ RestrictedProperty(name="$feature/secret", property_type=PropertyDefinition.Type.EVENT)+ }+ serializer = BatchExportSerializer(+ context={"team_id": self.team.pk, "request": SimpleNamespace(user=self.user)}+ )++ schema = serializer.serialize_hogql_query_to_batch_export_schema(prepare_query(query, self.team.pk))++ expression = schema["fields"][0]["expression"]+ assert expression == expected if expected == "NULL" else expected in expression, expression++ @override_settings(CLICKHOUSE_HOGQL_USE_NEW_EVENTS_SCHEMA=True)+ @patch("posthog.models.event.new_events_schema.use_new_events_schema", return_value=True)+ def test_native_export_names_a_bare_flag_column(self, _use_new_events_schema):+ serializer = BatchExportSerializer(+ context={"team_id": self.team.pk, "request": SimpleNamespace(user=self.user)}+ )++ schema = serializer.serialize_hogql_query_to_batch_export_schema(+ prepare_query("SELECT properties.`$feature/checkout` FROM events", self.team.pk)+ )++ field = schema["fields"][0]+ assert field["alias"] == "`$feature_flags__checkout`", field+ assert "%(" not in field["alias"]+ assert "JSONExtractRaw(" in field["expression"], field+class TestBatchExportDestinationSerializerTeamScoping(BaseTest):def _make_integration(self, team: Team) -> Integration:diff --git a/products/batch_exports/backend/tests/temporal/test_filters.py b/products/batch_exports/backend/tests/temporal/test_filters.pyindex d3fb7d454120..1674840995ea 100644--- a/products/batch_exports/backend/tests/temporal/test_filters.py+++ b/products/batch_exports/backend/tests/temporal/test_filters.py@@ -125,6 +125,41 @@ def test_compose_filters_clause_uses_legacy_events_schema(settings, ateam):assert result_values == {"hogql_val_0": "$browser", "hogql_val_1": "Chrome"}+def test_compose_filters_clause_reads_flags_from_the_native_map(ateam):+ result_clause, result_values = compose_filters_clause(+ [+ {"key": "$feature/some-feature", "type": "event", "operator": "exact", "value": ["true"]},+ {"key": "properties.`$feature/other-feature` = 'control'", "type": "hogql"},+ ],+ team_id=ateam.id,+ native_events_source=True,+ )++ def map_read(flag_key: int, flag_name: int) -> str:+ return f"""replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_{flag_key})s, %(hogql_val_{flag_name})s), ''), 'null'), '^"|"$', '')"""++ assert result_clause == (+ f"""and(ifNull(equals(if(ifNull(equals({map_read(0, 1)}, %(hogql_val_2)s), 0), %(hogql_val_3)s, {map_read(4, 5)}), %(hogql_val_6)s), 0), """+ f"""ifNull(equals(if(ifNull(equals({map_read(7, 8)}, %(hogql_val_9)s), 0), %(hogql_val_10)s, {map_read(11, 12)}), %(hogql_val_13)s), 0))"""+ )+ assert result_values == {+ "hogql_val_0": "$feature_flags",+ "hogql_val_1": "some-feature",+ "hogql_val_2": "$false",+ "hogql_val_3": "false",+ "hogql_val_4": "$feature_flags",+ "hogql_val_5": "some-feature",+ "hogql_val_6": "true",+ "hogql_val_7": "$feature_flags",+ "hogql_val_8": "other-feature",+ "hogql_val_9": "$false",+ "hogql_val_10": "false",+ "hogql_val_11": "$feature_flags",+ "hogql_val_12": "other-feature",+ "hogql_val_13": "control",+ }++@pytest.mark.parametrize("property_type,document,predicate",[diff --git a/products/batch_exports/backend/tests/temporal/test_record_batch_model.py b/products/batch_exports/backend/tests/temporal/test_record_batch_model.pyindex a0277d137e80..f6a7483756e9 100644--- a/products/batch_exports/backend/tests/temporal/test_record_batch_model.py+++ b/products/batch_exports/backend/tests/temporal/test_record_batch_model.py@@ -707,7 +707,12 @@ async def test_custom_export_backfill_runs_without_legacy_events_tables("inserted_at": "2024-02-01 12:00:00","created_at": "2024-02-01 12:00:00","elements_chain": "",- "properties": {"$browser": "Firefox", "amount": 2.5, "person": {"properties": "event value"}},+ "properties": {+ "$browser": "Firefox",+ "amount": 2.5,+ "person": {"properties": "event value"},+ "$feature_flags": {"named-false": "$false", "some-feature": "true"},+ },"person_properties": {"email": "[email protected]"},"temporary_properties": {"$set": {"email": "[email protected]"}},}@@ -716,10 +721,10 @@ async def test_custom_export_backfill_runs_without_legacy_events_tables(+ "\n".join(json.dumps(value) for value in [row, row, {**row, "team_id": ateam.pk + 1}]))schema: BatchExportSchema = {- "hogql_query": "SELECT e.properties.$browser AS browser, e.properties.amount AS amount, e.person.properties.email AS email, e.properties.person.properties AS nested FROM events AS e",+ "hogql_query": "SELECT e.properties.$browser AS browser, e.properties.amount AS amount, e.person.properties.email AS email, e.properties.person.properties AS nested, e.properties.`$feature/some-feature` AS flag, e.properties.`$feature/named-false` AS named_false FROM events AS e","fields": [{"expression": "events.mat_removed_column", "alias": alias}- for alias in ("browser", "amount", "email", "nested")+ for alias in ("browser", "amount", "email", "nested", "flag", "named_false")],"values": {"unused_old_parameter": "stale"},}@@ -729,9 +734,10 @@ async def test_custom_export_backfill_runs_without_legacy_events_tables(assert fields is not Noneassert "unused_old_parameter" not in valuespredicate, values = await database_sync_to_async(compose_filters_clause)(- [{"key": "$browser", "type": "event", "operator": "exact", "value": ["Firefox"]}],+ [{"key": "$feature/some-feature", "type": "event", "operator": "exact", "value": ["true"]}],team_id=ateam.pk,values=values,+ native_events_source=True,)batches: list[pa.RecordBatch] = await database_sync_to_async(lambda: list(@@ -756,6 +762,8 @@ async def test_custom_export_backfill_runs_without_legacy_events_tables(assert rows[0]["amount"] == "2.5"assert rows[0]["email"] == "[email protected]"assert rows[0]["nested"] == "event value"+ assert rows[0]["flag"] == "true"+ assert rows[0]["named_false"] == "false"assert json.loads(rows[0]["properties"])["$browser"] == "Firefox"assert json.loads(rows[0]["person_properties"])["email"] == "[email protected]"assert json.loads(rows[0]["set"]) == {"email": "[email protected]"}diff --git a/products/data_warehouse/backend/test/__snapshots__/test_hogql_fixer_ai.ambr b/products/data_warehouse/backend/test/__snapshots__/test_hogql_fixer_ai.ambrindex 44d88249b1e7..49de4e9b84ac 100644--- a/products/data_warehouse/backend/test/__snapshots__/test_hogql_fixer_ai.ambr+++ b/products/data_warehouse/backend/test/__snapshots__/test_hogql_fixer_ai.ambr@@ -64,7 +64,7 @@HogQL defines what functions are available with most (but not all) having a 1:1 mapping to ClickHouse functions.These are the non-aggregated HogQL functions:```- ['plus', 'minus', 'multiply', 'divide', 'intDiv', 'intDivOrZero', 'modulo', 'moduloOrZero', 'positiveModulo', 'negate', 'abs', 'gcd', 'lcm', 'max2', 'min2', 'multiplyDecimal', 'divideDecimal', 'empty', 'notEmpty', 'length', 'reverse', 'array', 'range', 'arrayConcat', 'arrayElement', 'has', 'hasAll', 'hasAny', 'hasSubstr', 'indexOf', 'arrayCount', 'countEqual', 'arrayEnumerate', 'arrayEnumerateUniq', 'arrayPopBack', 'arrayPopFront', 'arrayPushBack', 'arrayPushFront', 'arrayResize', 'arraySlice', 'arraySort', 'arrayReverseSort', 'arrayUniq', 'arrayJoin', 'arrayDifference', 'arrayDistinct', 'arrayEnumerateDense', 'arrayIntersect', 'arrayReduce', 'arrayReverse', 'arrayFilter', 'arrayFlatten', 'arrayCompact', 'arrayZip', 'arrayAUC', 'arrayMap', 'arrayFill', 'arrayFold', 'arrayWithConstant', 'arraySplit', 'arrayReverseFill', 'arrayReverseSplit', 'arrayRotateLeft', 'arrayRotateRight', 'arrayExists', 'arrayAll', 'arrayFirst', 'arrayLast', 'arrayFirstIndex', 'arrayLastIndex', 'arrayMin', 'arrayMax', 'arraySum', 'arrayAvg', 'arrayCumSum', 'arrayCumSumNonNegative', 'arrayProduct', 'arrayStringConcat', 'generateSeries', 'hex', 'unhex', 'reinterpretAsUInt8', 'reinterpretAsUInt16', 'reinterpretAsUInt32', 'reinterpretAsUInt64', 'reinterpretAsUInt128', 'reinterpretAsUInt256', 'reinterpretAsInt8', 'reinterpretAsInt16', 'reinterpretAsInt32', 'reinterpretAsInt64', 'reinterpretAsInt128', 'reinterpretAsInt256', 'reinterpretAsFloat32', 'reinterpretAsFloat64', 'reinterpretAsUUID', 'accurateCast', 'accurateCastOrNull', 'toInt', 'toIntOrZero', 'toIntOrDefault', '_toInt8', '_toInt16', '_toInt32', '_toInt64', '_toUInt8', '_toUInt64', '_toUInt128', 'toFloat', 'toFloatOrNull', 'toFloat64OrNull', 'toFloatOrZero', 'toFloatOrDefault', 'toDecimal', '_toDate', 'toUUID', 'toUUIDOrDefault', 'toString', 'toNullableString', 'toBool', 'toJSONString', 'parseDateTime', 'parseDateTimeBestEffort', 'dynamicType', 'toTypeName', 'defaultValueOfTypeName', 'cityHash64', 'MD5', 'UUIDv7ToDateTime', 'toDate', 'to_date', 'toDateTime', 'toDateTime64', 'toDateTimeUS', 'isnull', 'isNotNull', 'coalesce', 'ifnull', 'nullif', 'assumeNotNull', 'toNullable', 'timeZoneOf', 'timeZoneOffset', 'toYear', 'toQuarter', 'toMonth', 'toDayOfYear', 'toDayOfMonth', 'toDayOfWeek', 'toHour', 'toMinute', 'toSecond', 'toUnixTimestamp', 'toUnixTimestamp64Milli', 'fromUnixTimestamp64Milli', 'toStartOfInterval', 'toStartOfYear', 'toStartOfISOYear', 'toStartOfQuarter', 'toStartOfMonth', 'toLastDayOfMonth', 'toMonday', 'toStartOfWeek', 'toStartOfDay', 'toLastDayOfWeek', 'toStartOfHour', 'toStartOfMinute', 'toStartOfSecond', 'toStartOfFiveMinutes', 'toStartOfTenMinutes', 'toStartOfFifteenMinutes', 'toTime', 'toISOYear', 'toISOWeek', 'toWeek', 'toYearWeek', 'age', 'dateAdd', 'dateSub', 'date_bin', 'date_add', 'date_subtract', 'date_diff', 'dateDiff', 'timeStampAdd', 'timeStampSub', 'nowInBlock', 'rowNumberInBlock', 'rowNumberInAllBlocks', 'timeSlot', 'toYYYYMM', 'toYYYYMMDD', 'toYYYYMMDDhhmmss', 'addYears', 'addMonths', 'addWeeks', 'addDays', 'addHours', 'addMinutes', 'addSeconds', 'addQuarters', 'subtractYears', 'subtractMonths', 'subtractWeeks', 'subtractDays', 'subtractHours', 'subtractMinutes', 'subtractSeconds', 'subtractQuarters', 'timeSlots', 'formatDateTime', 'dateName', 'monthName', 'fromUnixTimestamp', 'toModifiedJulianDay', 'fromModifiedJulianDay', 'toIntervalSecond', 'toIntervalMinute', 'toIntervalHour', 'toIntervalDay', 'toIntervalWeek', 'toIntervalMonth', 'toIntervalQuarter', 'toIntervalYear', 'now', 'yesterday', 'current_timestamp', 'today', 'current_date', 'date_part', 'date_trunc', 'dateTrunc', 'to_timestamp', 'to_char', 'make_timestamp', 'make_date', 'make_interval', 'make_timestamptz', 'timezone', 'toTimeZone', 'greatCircleDistance', 'geoDistance', 'greatCircleAngle', 'pointInEllipses', 'pointInPolygon', 'geohashEncode', 'geohashDecode', 'geohashesInBox', 'h3IsValid', 'h3GetResolution', 'h3GetBaseCell', 'h3EdgeAngle', 'h3EdgeLengthM', 'h3EdgeLengthKm', 'geoToH3', 'h3ToGeo', 'h3ToGeoBoundary', 'h3kRing', 'h3HexAreaM2', 'h3HexAreaKm2', 'h3IndexesAreNeighbors', 'h3ToChildren', 'h3ToParent', 'h3ToString', 'stringToH3', 'h3IsResClassIII', 'h3IsPentagon', 'h3GetFaces', 'h3CellAreaM2', 'h3CellAreaRads2', 'h3ToCenterChild', 'h3ExactEdgeLengthM', 'h3ExactEdgeLengthKm', 'h3ExactEdgeLengthRads', 'h3NumHexagons', 'h3PointDistM', 'h3PointDistKm', 'h3PointDistRads', 'h3GetRes0Indexes', 'h3GetPentagonIndexes', 'h3Line', 'h3Distance', 'h3HexRing', 'h3GetUnidirectionalEdge', 'h3UnidirectionalEdgeIsValid', 'h3GetOriginIndexFromUnidirectionalEdge', 'h3GetDestinationIndexFromUnidirectionalEdge', 'h3GetIndexesFromUnidirectionalEdge', 'h3GetUnidirectionalEdgesFromHexagon', 'h3GetUnidirectionalEdgeBoundary', 'isValidJSON', 'JSONHas', 'JSONLength', 'JSONArrayLength', 'JSONType', 'JSONExtract', 'JSONExtractUInt', 'JSONExtractInt', 'JSONExtractFloat', 'JSONExtractBool', 'JSONExtractString', 'JSONExtractKeys', 'JSONExtractRaw', 'JSONExtractArrayRaw', 'JSONExtractKeysAndValues', 'JSONExtractKeysAndValuesRaw', 'JSON_VALUE', 'e', 'pi', 'exp', 'log', 'ln', 'exp2', 'log2', 'exp10', 'log10', 'sqrt', 'cbrt', 'erf', 'erfc', 'lgamma', 'tgamma', 'sin', 'cos', 'tan', 'asin', 'acos', 'atan', 'pow', 'power', 'intExp2', 'intExp10', 'cosh', 'acosh', 'sinh', 'asinh', 'atanh', 'atan2', 'hypot', 'log1p', 'sign', 'degrees', 'radians', 'factorial', 'width_bucket', 'floor', 'ceil', 'trunc', 'round', 'roundBankers', 'roundToExp2', 'roundDuration', 'roundAge', 'roundDown', 'rand', 'left', 'right', 'lengthUTF8', 'leftPad', 'rightPad', 'leftPadUTF8', 'rightPadUTF8', 'lower', 'upper', 'lowerUTF8', 'upperUTF8', 'isValidUTF8', 'toValidUTF8', 'format', 'reverseUTF8', 'concat', 'substring', 'substringUTF8', 'appendTrailingCharIfAbsent', 'convertCharset', 'base58Encode', 'base58Decode', 'tryBase58Decode', 'base64Encode', 'base64Decode', 'tryBase64Decode', 'endsWith', 'startsWith', 'encodeXMLComponent', 'decodeXMLComponent', 'extractTextFromHTML', 'ascii', 'concatWithSeparator', 'position', 'positionCaseInsensitive', 'positionUTF8', 'positionCaseInsensitiveUTF8', 'multiSearchAllPositions', 'multiSearchAllPositionsUTF8', 'multiSearchFirstPosition', 'multiSearchFirstIndex', 'multiSearchAny', 'multiSearchAllPositionsCaseInsensitive', 'multiSearchAllPositionsCaseInsensitiveUTF8', 'multiSearchAnyUTF8', 'multiSearchAnyCaseInsensitive', 'multiSearchAnyCaseInsensitiveUTF8', 'multiSearchFirstIndexUTF8', 'multiSearchFirstIndexCaseInsensitive', 'multiSearchFirstIndexCaseInsensitiveUTF8', 'multiSearchFirstPositionUTF8', 'multiSearchFirstPositionCaseInsensitive', 'multiSearchFirstPositionCaseInsensitiveUTF8', 'match', 'multiMatchAny', 'multiMatchAnyIndex', 'multiMatchAllIndices', 'multiFuzzyMatchAny', 'multiFuzzyMatchAnyIndex', 'multiFuzzyMatchAllIndices', 'extract', 'extractAll', 'extractAllGroupsHorizontal', 'extractAllGroupsVertical', 'extractGroups', 'like', 'ilike', 'notLike', 'notILike', 'locate', 'ngramDistance', 'ngramDistanceCaseInsensitive', 'ngramDistanceUTF8', 'ngramDistanceCaseInsensitiveUTF8', 'ngramSearch', 'ngramSearchCaseInsensitive', 'ngramSearchUTF8', 'ngramSearchCaseInsensitiveUTF8', 'countSubstrings', 'countSubstringsCaseInsensitive', 'countSubstringsCaseInsensitiveUTF8', 'countMatches', 'countMatchesCaseInsensitive', 'hasSubsequence', 'hasSubsequenceCaseInsensitive', 'hasSubsequenceUTF8', 'hasSubsequenceCaseInsensitiveUTF8', 'hasToken', 'hasTokenCaseInsensitive', 'hasTokenOrNull', 'hasTokenCaseInsensitiveOrNull', 'hasAllTokens', 'hasAnyTokens', 'regexpExtract', 'replace', 'replaceAll', 'replaceOne', 'replaceRegexpAll', 'replaceRegexpOne', 'regexpQuoteMeta', 'translate', 'translateUTF8', 'splitByChar', 'splitByString', 'splitByRegexp', 'splitByWhitespace', 'splitByNonAlpha', 'alphaTokens', 'extractAllGroups', 'ngrams', 'tokens', 'repeat', 'initcap', 'lpad', 'rpad', 'split_part', 'ltrim', 'trimLeft', 'rtrim', 'trimRight', 'btrim', 'trim', 'equals', 'notEquals', 'less', 'greater', 'lessOrEquals', 'greaterOrEquals', 'in', 'notIn', 'and', 'or', 'xor', 'not', '_caseWithExpression', 'if', 'multiIf', 'throwIf', 'map', 'mapFromArrays', 'mapAdd', 'mapSubtract', 'mapPopulateSeries', 'mapContains', 'mapKeys', 'mapValues', 'mapContainsKeyLike', 'mapExtractKeyLike', 'mapApply', 'mapFilter', 'mapUpdate', 'bitAnd', 'bitOr', 'bitXor', 'bitNot', 'bitShiftLeft', 'bitShiftRight', 'bitRotateLeft', 'bitRotateRight', 'bitSlice', 'bitTest', 'bitTestAll', 'bitTestAny', 'bitCount', 'bitHammingDistance', 'bitmapBuild', 'bitmapToArray', 'bitmapSubsetInRange', 'bitmapSubsetLimit', 'subBitmap', 'bitmapContains', 'bitmapHasAny', 'bitmapHasAll', 'bitmapCardinality', 'bitmapMin', 'bitmapMax', 'bitmapTransform', 'bitmapAnd', 'bitmapOr', 'bitmapXor', 'bitmapAndnot', 'bitmapAndCardinality', 'bitmapOrCardinality', 'bitmapXorCardinality', 'bitmapAndnotCardinality', 'protocol', 'domain', 'domainWithoutWWW', 'topLevelDomain', 'firstSignificantSubdomain', 'cutToFirstSignificantSubdomain', 'cutToFirstSignificantSubdomainWithWWW', 'port', 'path', 'pathFull', 'queryString', 'fragment', 'queryStringAndFragment', 'extractURLParameter', 'extractURLParameters', 'extractURLParameterNames', 'URLHierarchy', 'URLPathHierarchy', 'encodeURLComponent', 'decodeURLComponent', 'encodeURLFormComponent', 'decodeURLFormComponent', 'netloc', 'cutWWW', 'cutQueryString', 'cutFragment', 'cutQueryStringAndFragment', 'cutURLParameter', 'IPv4NumToString', 'IPv4StringToNum', 'IPv4StringToNumOrDefault', 'IPv4StringToNumOrNull', 'IPv6NumToString', 'IPv6StringToNum', 'IPv6StringToNumOrDefault', 'IPv6StringToNumOrNull', 'IPv4ToIPv6', 'toIPv4', 'toIPv4OrDefault', 'toIPv4OrNull', 'toIPv4OrZero', 'toIPv6', 'toIPv6OrDefault', 'toIPv6OrNull', 'toIPv6OrZero', 'isIPv4String', 'isIPv6String', 'isIPAddressInRange', 'IPv4CIDRToRange', 'IPv6CIDRToRange', 'cutIPv6', 'tuple', 'tupleElement', 'untuple', 'tupleHammingDistance', 'tupleToNameValuePairs', 'tuplePlus', 'tupleMinus', 'tupleMultiply', 'tupleDivide', 'tupleNegate', 'tupleMultiplyByNumber', 'tupleDivideByNumber', 'dotProduct', 'isFinite', 'isInfinite', 'ifNotFinite', 'isNaN', 'bar', 'transform', 'formatReadableDecimalSize', 'formatReadableSize', 'formatReadableQuantity', 'formatReadableTimeDelta', 'least', 'greatest', 'indexHint', 'extractIPv4Substrings', 'tumble', 'hop', 'tumbleStart', 'tumbleEnd', 'hopStart', 'hopEnd', 'L1Norm', 'L2Norm', 'LinfNorm', 'LpNorm', 'L1Distance', 'L2Distance', 'LinfDistance', 'LpDistance', 'L1Normalize', 'L2Normalize', 'LinfNormalize', 'LpNormalize', 'cosineDistance', 'rank', 'dense_rank', 'row_number', 'first_value', 'last_value', 'nth_value', 'lagInFrame', 'leadInFrame', 'lag', 'lead', 'languageCodeToName', '_finalizeAggregation', 'aggregate_funnel', 'aggregate_funnel_array', 'aggregate_funnel_cohort', 'aggregate_funnel_trends', 'aggregate_funnel_array_trends', 'aggregate_funnel_cohort_trends', 'aggregate_funnel_json', 'aggregate_funnel_array_json', 'aggregate_funnel_cohort_json', 'aggregate_funnel_trends_json', 'aggregate_funnel_array_trends_json', 'aggregate_funnel_cohort_trends_json', 'aggregate_funnel_test', 'aggregate_funnel_array_trends_test']+ ['plus', 'minus', 'multiply', 'divide', 'intDiv', 'intDivOrZero', 'modulo', 'moduloOrZero', 'positiveModulo', 'negate', 'abs', 'gcd', 'lcm', 'max2', 'min2', 'multiplyDecimal', 'divideDecimal', 'empty', 'notEmpty', 'length', 'reverse', 'array', 'range', 'arrayConcat', 'arrayElement', 'has', 'hasAll', 'hasAny', 'hasSubstr', 'indexOf', 'arrayCount', 'countEqual', 'arrayEnumerate', 'arrayEnumerateUniq', 'arrayPopBack', 'arrayPopFront', 'arrayPushBack', 'arrayPushFront', 'arrayResize', 'arraySlice', 'arraySort', 'arrayReverseSort', 'arrayUniq', 'arrayJoin', 'arrayDifference', 'arrayDistinct', 'arrayEnumerateDense', 'arrayIntersect', 'arrayReduce', 'arrayReverse', 'arrayFilter', 'arrayFlatten', 'arrayCompact', 'arrayZip', 'arrayAUC', 'arrayMap', 'arrayFill', 'arrayFold', 'arrayWithConstant', 'arraySplit', 'arrayReverseFill', 'arrayReverseSplit', 'arrayRotateLeft', 'arrayRotateRight', 'arrayExists', 'arrayAll', 'arrayFirst', 'arrayLast', 'arrayFirstIndex', 'arrayLastIndex', 'arrayMin', 'arrayMax', 'arraySum', 'arrayAvg', 'arrayCumSum', 'arrayCumSumNonNegative', 'arrayProduct', 'arrayStringConcat', 'generateSeries', 'hex', 'unhex', 'reinterpretAsUInt8', 'reinterpretAsUInt16', 'reinterpretAsUInt32', 'reinterpretAsUInt64', 'reinterpretAsUInt128', 'reinterpretAsUInt256', 'reinterpretAsInt8', 'reinterpretAsInt16', 'reinterpretAsInt32', 'reinterpretAsInt64', 'reinterpretAsInt128', 'reinterpretAsInt256', 'reinterpretAsFloat32', 'reinterpretAsFloat64', 'reinterpretAsUUID', 'accurateCast', 'accurateCastOrNull', 'toInt', 'toIntOrZero', 'toIntOrDefault', '_toInt8', '_toInt16', '_toInt32', '_toInt64', '_toUInt8', '_toUInt64', '_toUInt128', 'toFloat', 'toFloatOrNull', 'toFloat64OrNull', 'toFloatOrZero', 'toFloatOrDefault', 'toDecimal', '_toDate', 'toUUID', 'toUUIDOrDefault', 'toString', 'toNullableString', 'toBool', 'toJSONString', 'parseDateTime', 'parseDateTimeBestEffort', 'dynamicType', 'toTypeName', 'defaultValueOfTypeName', 'cityHash64', 'MD5', 'UUIDv7ToDateTime', 'toDate', 'to_date', 'toDateTime', 'toDateTime64', 'toDateTimeUS', 'isnull', 'isNotNull', 'coalesce', 'ifnull', 'nullif', 'assumeNotNull', 'toNullable', 'timeZoneOf', 'timeZoneOffset', 'toYear', 'toQuarter', 'toMonth', 'toDayOfYear', 'toDayOfMonth', 'toDayOfWeek', 'toHour', 'toMinute', 'toSecond', 'toUnixTimestamp', 'toUnixTimestamp64Milli', 'fromUnixTimestamp64Milli', 'toStartOfInterval', 'toStartOfYear', 'toStartOfISOYear', 'toStartOfQuarter', 'toStartOfMonth', 'toLastDayOfMonth', 'toMonday', 'toStartOfWeek', 'toStartOfDay', 'toLastDayOfWeek', 'toStartOfHour', 'toStartOfMinute', 'toStartOfSecond', 'toStartOfFiveMinutes', 'toStartOfTenMinutes', 'toStartOfFifteenMinutes', 'toTime', 'toISOYear', 'toISOWeek', 'toWeek', 'toYearWeek', 'age', 'dateAdd', 'dateSub', 'date_bin', 'date_add', 'date_subtract', 'date_diff', 'dateDiff', 'timeStampAdd', 'timeStampSub', 'nowInBlock', 'rowNumberInBlock', 'rowNumberInAllBlocks', 'timeSlot', 'toYYYYMM', 'toYYYYMMDD', 'toYYYYMMDDhhmmss', 'addYears', 'addMonths', 'addWeeks', 'addDays', 'addHours', 'addMinutes', 'addSeconds', 'addQuarters', 'subtractYears', 'subtractMonths', 'subtractWeeks', 'subtractDays', 'subtractHours', 'subtractMinutes', 'subtractSeconds', 'subtractQuarters', 'timeSlots', 'formatDateTime', 'dateName', 'monthName', 'fromUnixTimestamp', 'toModifiedJulianDay', 'fromModifiedJulianDay', 'toIntervalSecond', 'toIntervalMinute', 'toIntervalHour', 'toIntervalDay', 'toIntervalWeek', 'toIntervalMonth', 'toIntervalQuarter', 'toIntervalYear', 'now', 'yesterday', 'current_timestamp', 'today', 'current_date', 'date_part', 'date_trunc', 'dateTrunc', 'to_timestamp', 'to_char', 'make_timestamp', 'make_date', 'make_interval', 'make_timestamptz', 'timezone', 'toTimeZone', 'greatCircleDistance', 'geoDistance', 'greatCircleAngle', 'pointInEllipses', 'pointInPolygon', 'geohashEncode', 'geohashDecode', 'geohashesInBox', 'h3IsValid', 'h3GetResolution', 'h3GetBaseCell', 'h3EdgeAngle', 'h3EdgeLengthM', 'h3EdgeLengthKm', 'geoToH3', 'h3ToGeo', 'h3ToGeoBoundary', 'h3kRing', 'h3HexAreaM2', 'h3HexAreaKm2', 'h3IndexesAreNeighbors', 'h3ToChildren', 'h3ToParent', 'h3ToString', 'stringToH3', 'h3IsResClassIII', 'h3IsPentagon', 'h3GetFaces', 'h3CellAreaM2', 'h3CellAreaRads2', 'h3ToCenterChild', 'h3ExactEdgeLengthM', 'h3ExactEdgeLengthKm', 'h3ExactEdgeLengthRads', 'h3NumHexagons', 'h3PointDistM', 'h3PointDistKm', 'h3PointDistRads', 'h3GetRes0Indexes', 'h3GetPentagonIndexes', 'h3Line', 'h3Distance', 'h3HexRing', 'h3GetUnidirectionalEdge', 'h3UnidirectionalEdgeIsValid', 'h3GetOriginIndexFromUnidirectionalEdge', 'h3GetDestinationIndexFromUnidirectionalEdge', 'h3GetIndexesFromUnidirectionalEdge', 'h3GetUnidirectionalEdgesFromHexagon', 'h3GetUnidirectionalEdgeBoundary', 'isValidJSON', 'JSONHas', 'JSONLength', 'JSONArrayLength', 'JSONType', 'JSONExtract', 'JSONExtractUInt', 'JSONExtractInt', 'JSONExtractFloat', 'JSONExtractBool', 'JSONExtractString', 'JSONExtractKeys', 'JSONExtractRaw', 'JSONExtractArrayRaw', 'JSONExtractKeysAndValues', 'JSONExtractKeysAndValuesRaw', 'JSON_VALUE', 'e', 'pi', 'exp', 'log', 'ln', 'exp2', 'log2', 'exp10', 'log10', 'sqrt', 'cbrt', 'erf', 'erfc', 'lgamma', 'tgamma', 'sin', 'cos', 'tan', 'asin', 'acos', 'atan', 'pow', 'power', 'intExp2', 'intExp10', 'cosh', 'acosh', 'sinh', 'asinh', 'atanh', 'atan2', 'hypot', 'log1p', 'sign', 'degrees', 'radians', 'factorial', 'width_bucket', 'floor', 'ceil', 'trunc', 'round', 'roundBankers', 'roundToExp2', 'roundDuration', 'roundAge', 'roundDown', 'rand', 'left', 'right', 'lengthUTF8', 'leftPad', 'rightPad', 'leftPadUTF8', 'rightPadUTF8', 'lower', 'upper', 'lowerUTF8', 'upperUTF8', 'isValidUTF8', 'toValidUTF8', 'format', 'reverseUTF8', 'concat', 'substring', 'substringUTF8', 'appendTrailingCharIfAbsent', 'convertCharset', 'base58Encode', 'base58Decode', 'tryBase58Decode', 'base64Encode', 'base64Decode', 'tryBase64Decode', 'endsWith', 'startsWith', 'encodeXMLComponent', 'decodeXMLComponent', 'extractTextFromHTML', 'ascii', 'concatWithSeparator', 'position', 'positionCaseInsensitive', 'positionUTF8', 'positionCaseInsensitiveUTF8', 'multiSearchAllPositions', 'multiSearchAllPositionsUTF8', 'multiSearchFirstPosition', 'multiSearchFirstIndex', 'multiSearchAny', 'multiSearchAllPositionsCaseInsensitive', 'multiSearchAllPositionsCaseInsensitiveUTF8', 'multiSearchAnyUTF8', 'multiSearchAnyCaseInsensitive', 'multiSearchAnyCaseInsensitiveUTF8', 'multiSearchFirstIndexUTF8', 'multiSearchFirstIndexCaseInsensitive', 'multiSearchFirstIndexCaseInsensitiveUTF8', 'multiSearchFirstPositionUTF8', 'multiSearchFirstPositionCaseInsensitive', 'multiSearchFirstPositionCaseInsensitiveUTF8', 'match', 'multiMatchAny', 'multiMatchAnyIndex', 'multiMatchAllIndices', 'multiFuzzyMatchAny', 'multiFuzzyMatchAnyIndex', 'multiFuzzyMatchAllIndices', 'extract', 'extractAll', 'extractAllGroupsHorizontal', 'extractAllGroupsVertical', 'extractGroups', 'like', 'ilike', 'notLike', 'notILike', 'locate', 'ngramDistance', 'ngramDistanceCaseInsensitive', 'ngramDistanceUTF8', 'ngramDistanceCaseInsensitiveUTF8', 'ngramSearch', 'ngramSearchCaseInsensitive', 'ngramSearchUTF8', 'ngramSearchCaseInsensitiveUTF8', 'countSubstrings', 'countSubstringsCaseInsensitive', 'countSubstringsCaseInsensitiveUTF8', 'countMatches', 'countMatchesCaseInsensitive', 'hasSubsequence', 'hasSubsequenceCaseInsensitive', 'hasSubsequenceUTF8', 'hasSubsequenceCaseInsensitiveUTF8', 'hasToken', 'hasTokenCaseInsensitive', 'hasTokenOrNull', 'hasTokenCaseInsensitiveOrNull', 'hasAllTokens', 'hasAnyTokens', 'regexpExtract', 'replace', 'replaceAll', 'replaceOne', 'replaceRegexpAll', 'replaceRegexpOne', 'regexpQuoteMeta', 'translate', 'translateUTF8', 'splitByChar', 'splitByString', 'splitByRegexp', 'splitByWhitespace', 'splitByNonAlpha', 'alphaTokens', 'extractAllGroups', 'ngrams', 'tokens', 'repeat', 'initcap', 'lpad', 'rpad', 'split_part', 'ltrim', 'trimLeft', 'rtrim', 'trimRight', 'btrim', 'trim', 'equals', 'notEquals', 'less', 'greater', 'lessOrEquals', 'greaterOrEquals', 'in', 'notIn', 'and', 'or', 'xor', 'not', '_caseWithExpression', 'if', 'multiIf', 'throwIf', 'map', 'mapFromArrays', 'mapAdd', 'mapSubtract', 'mapPopulateSeries', 'mapContains', 'mapKeys', 'mapValues', 'mapContainsKeyLike', 'mapExtractKeyLike', 'mapApply', 'mapFilter', 'mapExists', 'mapUpdate', 'bitAnd', 'bitOr', 'bitXor', 'bitNot', 'bitShiftLeft', 'bitShiftRight', 'bitRotateLeft', 'bitRotateRight', 'bitSlice', 'bitTest', 'bitTestAll', 'bitTestAny', 'bitCount', 'bitHammingDistance', 'bitmapBuild', 'bitmapToArray', 'bitmapSubsetInRange', 'bitmapSubsetLimit', 'subBitmap', 'bitmapContains', 'bitmapHasAny', 'bitmapHasAll', 'bitmapCardinality', 'bitmapMin', 'bitmapMax', 'bitmapTransform', 'bitmapAnd', 'bitmapOr', 'bitmapXor', 'bitmapAndnot', 'bitmapAndCardinality', 'bitmapOrCardinality', 'bitmapXorCardinality', 'bitmapAndnotCardinality', 'protocol', 'domain', 'domainWithoutWWW', 'topLevelDomain', 'firstSignificantSubdomain', 'cutToFirstSignificantSubdomain', 'cutToFirstSignificantSubdomainWithWWW', 'port', 'path', 'pathFull', 'queryString', 'fragment', 'queryStringAndFragment', 'extractURLParameter', 'extractURLParameters', 'extractURLParameterNames', 'URLHierarchy', 'URLPathHierarchy', 'encodeURLComponent', 'decodeURLComponent', 'encodeURLFormComponent', 'decodeURLFormComponent', 'netloc', 'cutWWW', 'cutQueryString', 'cutFragment', 'cutQueryStringAndFragment', 'cutURLParameter', 'IPv4NumToString', 'IPv4StringToNum', 'IPv4StringToNumOrDefault', 'IPv4StringToNumOrNull', 'IPv6NumToString', 'IPv6StringToNum', 'IPv6StringToNumOrDefault', 'IPv6StringToNumOrNull', 'IPv4ToIPv6', 'toIPv4', 'toIPv4OrDefault', 'toIPv4OrNull', 'toIPv4OrZero', 'toIPv6', 'toIPv6OrDefault', 'toIPv6OrNull', 'toIPv6OrZero', 'isIPv4String', 'isIPv6String', 'isIPAddressInRange', 'IPv4CIDRToRange', 'IPv6CIDRToRange', 'cutIPv6', 'tuple', 'tupleElement', 'untuple', 'tupleHammingDistance', 'tupleToNameValuePairs', 'tuplePlus', 'tupleMinus', 'tupleMultiply', 'tupleDivide', 'tupleNegate', 'tupleMultiplyByNumber', 'tupleDivideByNumber', 'dotProduct', 'isFinite', 'isInfinite', 'ifNotFinite', 'isNaN', 'bar', 'transform', 'formatReadableDecimalSize', 'formatReadableSize', 'formatReadableQuantity', 'formatReadableTimeDelta', 'least', 'greatest', 'indexHint', 'extractIPv4Substrings', 'tumble', 'hop', 'tumbleStart', 'tumbleEnd', 'hopStart', 'hopEnd', 'L1Norm', 'L2Norm', 'LinfNorm', 'LpNorm', 'L1Distance', 'L2Distance', 'LinfDistance', 'LpDistance', 'L1Normalize', 'L2Normalize', 'LinfNormalize', 'LpNormalize', 'cosineDistance', 'rank', 'dense_rank', 'row_number', 'first_value', 'last_value', 'nth_value', 'lagInFrame', 'leadInFrame', 'lag', 'lead', 'languageCodeToName', '_finalizeAggregation', 'aggregate_funnel', 'aggregate_funnel_array', 'aggregate_funnel_cohort', 'aggregate_funnel_trends', 'aggregate_funnel_array_trends', 'aggregate_funnel_cohort_trends', 'aggregate_funnel_json', 'aggregate_funnel_array_json', 'aggregate_funnel_cohort_json', 'aggregate_funnel_trends_json', 'aggregate_funnel_array_trends_json', 'aggregate_funnel_cohort_trends_json', 'aggregate_funnel_test', 'aggregate_funnel_array_trends_test']```These are the aggregated HogQL functions:diff --git a/products/experiments/backend/hogql_queries/test/__snapshots__/test_experiment_actors_query.new_events_schema.ambr b/products/experiments/backend/hogql_queries/test/__snapshots__/test_experiment_actors_query.new_events_schema.ambrnew file mode 100644index 000000000000..655beb3821df--- /dev/null+++ b/products/experiments/backend/hogql_queries/test/__snapshots__/test_experiment_actors_query.new_events_schema.ambr@@ -0,0 +1,937 @@+# serializer version: 1+# name: TestExperimentActorsQuery.test_experiment_exposure_actors_with_recordings[new_events_schema]+ '''+ SELECT source.id,+ source.id AS id,+ source.matching_events AS matching_events+ FROM+ (WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id) SELECT exposures.entity_id AS actor_id,+ exposures.variant AS variant,+ array(tuple(toTimeZone(exposures.first_exposure_time, 'UTC'), exposures.exposure_event_uuid, exposures.exposure_session_id, '')) AS matching_events,+ actor_id AS id+ FROM exposures+ WHERE ifNull(equals(variant, 'control'), 0)+ ORDER BY actor_id ASC) AS source+ ORDER BY source.id ASC+ LIMIT 101+ OFFSET 0 SETTINGS optimize_aggregation_in_order=1,+ join_algorithm='auto',+ readonly=2,+ max_execution_time=60,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=0,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentActorsQuery.test_experiment_funnel_actors_0_exposure_control[new_events_schema]+ '''+ SELECT source.id,+ source.id AS id+ FROM+ (WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id) SELECT exposures.entity_id AS actor_id,+ exposures.variant AS variant,+ actor_id AS id+ FROM exposures+ WHERE ifNull(equals(variant, 'control'), 0)+ ORDER BY actor_id ASC) AS source+ ORDER BY source.id ASC+ LIMIT 101+ OFFSET 0 SETTINGS optimize_aggregation_in_order=1,+ join_algorithm='auto',+ readonly=2,+ max_execution_time=60,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=0,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentActorsQuery.test_experiment_funnel_actors_1_exposure_test[new_events_schema]+ '''+ SELECT source.id,+ source.id AS id+ FROM+ (WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id) SELECT exposures.entity_id AS actor_id,+ exposures.variant AS variant,+ actor_id AS id+ FROM exposures+ WHERE ifNull(equals(variant, 'test'), 0)+ ORDER BY actor_id ASC) AS source+ ORDER BY source.id ASC+ LIMIT 101+ OFFSET 0 SETTINGS optimize_aggregation_in_order=1,+ join_algorithm='auto',+ readonly=2,+ max_execution_time=60,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=0,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentActorsQuery.test_experiment_funnel_actors_2_conversions_step1_control[new_events_schema]+ '''+ SELECT source.id,+ source.id AS id+ FROM+ (WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))) AS session_id,+ multiply(1, and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0))) AS step_0,+ if(equals(events.event, 'signup'), 1, 0) AS step_1,+ if(equals(events.event, 'purchase'), 1, 0) AS step_2+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), or(and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), or(equals(events.event, 'signup'), equals(events.event, 'purchase'))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ exposures.exposure_event_uuid AS exposure_event_uuid,+ (arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(3, 94608000, 'first_touch', 'ordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(metric_events.timestamp, 'UTC'), 'Float64'), metric_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, metric_events.step_0), multiply(2, metric_events.step_1), multiply(3, metric_events.step_2)]))))))))[1]).1 AS step_reached+ FROM exposures+ LEFT JOIN metric_events ON equals(exposures.entity_id, metric_events.entity_id)+ GROUP BY exposures.entity_id,+ exposures.variant,+ exposures.exposure_event_uuid) SELECT entity_metrics.entity_id AS actor_id,+ entity_metrics.variant AS variant,+ actor_id AS id+ FROM entity_metrics+ WHERE and(ifNull(greaterOrEquals(entity_metrics.step_reached, 1), 0), ifNull(equals(variant, 'control'), 0))+ ORDER BY entity_metrics.entity_id ASC) AS source+ ORDER BY source.id ASC+ LIMIT 101+ OFFSET 0 SETTINGS optimize_aggregation_in_order=1,+ join_algorithm='auto',+ readonly=2,+ max_execution_time=60,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=0,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentActorsQuery.test_experiment_funnel_actors_3_conversions_step2_control[new_events_schema]+ '''+ SELECT source.id,+ source.id AS id+ FROM+ (WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))) AS session_id,+ multiply(1, and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0))) AS step_0,+ if(equals(events.event, 'signup'), 1, 0) AS step_1,+ if(equals(events.event, 'purchase'), 1, 0) AS step_2+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), or(and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), or(equals(events.event, 'signup'), equals(events.event, 'purchase'))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ exposures.exposure_event_uuid AS exposure_event_uuid,+ (arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(3, 94608000, 'first_touch', 'ordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(metric_events.timestamp, 'UTC'), 'Float64'), metric_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, metric_events.step_0), multiply(2, metric_events.step_1), multiply(3, metric_events.step_2)]))))))))[1]).1 AS step_reached+ FROM exposures+ LEFT JOIN metric_events ON equals(exposures.entity_id, metric_events.entity_id)+ GROUP BY exposures.entity_id,+ exposures.variant,+ exposures.exposure_event_uuid) SELECT entity_metrics.entity_id AS actor_id,+ entity_metrics.variant AS variant,+ actor_id AS id+ FROM entity_metrics+ WHERE and(ifNull(greaterOrEquals(entity_metrics.step_reached, 2), 0), ifNull(equals(variant, 'control'), 0))+ ORDER BY entity_metrics.entity_id ASC) AS source+ ORDER BY source.id ASC+ LIMIT 101+ OFFSET 0 SETTINGS optimize_aggregation_in_order=1,+ join_algorithm='auto',+ readonly=2,+ max_execution_time=60,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=0,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentActorsQuery.test_experiment_funnel_actors_4_dropoffs_step1_control[new_events_schema]+ '''+ SELECT source.id,+ source.id AS id+ FROM+ (WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))) AS session_id,+ multiply(1, and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0))) AS step_0,+ if(equals(events.event, 'signup'), 1, 0) AS step_1,+ if(equals(events.event, 'purchase'), 1, 0) AS step_2+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), or(and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), or(equals(events.event, 'signup'), equals(events.event, 'purchase'))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ exposures.exposure_event_uuid AS exposure_event_uuid,+ (arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(3, 94608000, 'first_touch', 'ordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(metric_events.timestamp, 'UTC'), 'Float64'), metric_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, metric_events.step_0), multiply(2, metric_events.step_1), multiply(3, metric_events.step_2)]))))))))[1]).1 AS step_reached+ FROM exposures+ LEFT JOIN metric_events ON equals(exposures.entity_id, metric_events.entity_id)+ GROUP BY exposures.entity_id,+ exposures.variant,+ exposures.exposure_event_uuid) SELECT entity_metrics.entity_id AS actor_id,+ entity_metrics.variant AS variant,+ actor_id AS id+ FROM entity_metrics+ WHERE and(ifNull(greaterOrEquals(entity_metrics.step_reached, 0), 0), ifNull(less(entity_metrics.step_reached, 1), 0), ifNull(equals(variant, 'control'), 0))+ ORDER BY entity_metrics.entity_id ASC) AS source+ ORDER BY source.id ASC+ LIMIT 101+ OFFSET 0 SETTINGS optimize_aggregation_in_order=1,+ join_algorithm='auto',+ readonly=2,+ max_execution_time=60,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=0,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentActorsQuery.test_experiment_funnel_actors_5_dropoffs_control[new_events_schema]+ '''+ SELECT source.id,+ source.id AS id+ FROM+ (WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))) AS session_id,+ multiply(1, and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0))) AS step_0,+ if(equals(events.event, 'signup'), 1, 0) AS step_1,+ if(equals(events.event, 'purchase'), 1, 0) AS step_2+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), or(and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), or(equals(events.event, 'signup'), equals(events.event, 'purchase'))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ exposures.exposure_event_uuid AS exposure_event_uuid,+ (arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(3, 94608000, 'first_touch', 'ordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(metric_events.timestamp, 'UTC'), 'Float64'), metric_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, metric_events.step_0), multiply(2, metric_events.step_1), multiply(3, metric_events.step_2)]))))))))[1]).1 AS step_reached+ FROM exposures+ LEFT JOIN metric_events ON equals(exposures.entity_id, metric_events.entity_id)+ GROUP BY exposures.entity_id,+ exposures.variant,+ exposures.exposure_event_uuid) SELECT entity_metrics.entity_id AS actor_id,+ entity_metrics.variant AS variant,+ actor_id AS id+ FROM entity_metrics+ WHERE and(ifNull(greaterOrEquals(entity_metrics.step_reached, 1), 0), ifNull(less(entity_metrics.step_reached, 2), 0), ifNull(equals(variant, 'control'), 0))+ ORDER BY entity_metrics.entity_id ASC) AS source+ ORDER BY source.id ASC+ LIMIT 101+ OFFSET 0 SETTINGS optimize_aggregation_in_order=1,+ join_algorithm='auto',+ readonly=2,+ max_execution_time=60,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=0,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentActorsQuery.test_experiment_funnel_actors_6_conversions_step1_test[new_events_schema]+ '''+ SELECT source.id,+ source.id AS id+ FROM+ (WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))) AS session_id,+ multiply(1, and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0))) AS step_0,+ if(equals(events.event, 'signup'), 1, 0) AS step_1,+ if(equals(events.event, 'purchase'), 1, 0) AS step_2+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), or(and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), or(equals(events.event, 'signup'), equals(events.event, 'purchase'))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ exposures.exposure_event_uuid AS exposure_event_uuid,+ (arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(3, 94608000, 'first_touch', 'ordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(metric_events.timestamp, 'UTC'), 'Float64'), metric_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, metric_events.step_0), multiply(2, metric_events.step_1), multiply(3, metric_events.step_2)]))))))))[1]).1 AS step_reached+ FROM exposures+ LEFT JOIN metric_events ON equals(exposures.entity_id, metric_events.entity_id)+ GROUP BY exposures.entity_id,+ exposures.variant,+ exposures.exposure_event_uuid) SELECT entity_metrics.entity_id AS actor_id,+ entity_metrics.variant AS variant,+ actor_id AS id+ FROM entity_metrics+ WHERE and(ifNull(greaterOrEquals(entity_metrics.step_reached, 1), 0), ifNull(equals(variant, 'test'), 0))+ ORDER BY entity_metrics.entity_id ASC) AS source+ ORDER BY source.id ASC+ LIMIT 101+ OFFSET 0 SETTINGS optimize_aggregation_in_order=1,+ join_algorithm='auto',+ readonly=2,+ max_execution_time=60,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=0,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentActorsQuery.test_experiment_funnel_actors_7_conversions_step2_test[new_events_schema]+ '''+ SELECT source.id,+ source.id AS id+ FROM+ (WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))) AS session_id,+ multiply(1, and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0))) AS step_0,+ if(equals(events.event, 'signup'), 1, 0) AS step_1,+ if(equals(events.event, 'purchase'), 1, 0) AS step_2+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), or(and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), or(equals(events.event, 'signup'), equals(events.event, 'purchase'))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ exposures.exposure_event_uuid AS exposure_event_uuid,+ (arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(3, 94608000, 'first_touch', 'ordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(metric_events.timestamp, 'UTC'), 'Float64'), metric_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, metric_events.step_0), multiply(2, metric_events.step_1), multiply(3, metric_events.step_2)]))))))))[1]).1 AS step_reached+ FROM exposures+ LEFT JOIN metric_events ON equals(exposures.entity_id, metric_events.entity_id)+ GROUP BY exposures.entity_id,+ exposures.variant,+ exposures.exposure_event_uuid) SELECT entity_metrics.entity_id AS actor_id,+ entity_metrics.variant AS variant,+ actor_id AS id+ FROM entity_metrics+ WHERE and(ifNull(greaterOrEquals(entity_metrics.step_reached, 2), 0), ifNull(equals(variant, 'test'), 0))+ ORDER BY entity_metrics.entity_id ASC) AS source+ ORDER BY source.id ASC+ LIMIT 101+ OFFSET 0 SETTINGS optimize_aggregation_in_order=1,+ join_algorithm='auto',+ readonly=2,+ max_execution_time=60,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=0,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentActorsQuery.test_experiment_funnel_actors_8_dropoffs_step1_test[new_events_schema]+ '''+ SELECT source.id,+ source.id AS id+ FROM+ (WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))) AS session_id,+ multiply(1, and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0))) AS step_0,+ if(equals(events.event, 'signup'), 1, 0) AS step_1,+ if(equals(events.event, 'purchase'), 1, 0) AS step_2+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), or(and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), or(equals(events.event, 'signup'), equals(events.event, 'purchase'))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ exposures.exposure_event_uuid AS exposure_event_uuid,+ (arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(3, 94608000, 'first_touch', 'ordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(metric_events.timestamp, 'UTC'), 'Float64'), metric_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, metric_events.step_0), multiply(2, metric_events.step_1), multiply(3, metric_events.step_2)]))))))))[1]).1 AS step_reached+ FROM exposures+ LEFT JOIN metric_events ON equals(exposures.entity_id, metric_events.entity_id)+ GROUP BY exposures.entity_id,+ exposures.variant,+ exposures.exposure_event_uuid) SELECT entity_metrics.entity_id AS actor_id,+ entity_metrics.variant AS variant,+ actor_id AS id+ FROM entity_metrics+ WHERE and(ifNull(greaterOrEquals(entity_metrics.step_reached, 0), 0), ifNull(less(entity_metrics.step_reached, 1), 0), ifNull(equals(variant, 'test'), 0))+ ORDER BY entity_metrics.entity_id ASC) AS source+ ORDER BY source.id ASC+ LIMIT 101+ OFFSET 0 SETTINGS optimize_aggregation_in_order=1,+ join_algorithm='auto',+ readonly=2,+ max_execution_time=60,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=0,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentActorsQuery.test_experiment_funnel_actors_9_dropoffs_test[new_events_schema]+ '''+ SELECT source.id,+ source.id AS id+ FROM+ (WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))) AS session_id,+ multiply(1, and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0))) AS step_0,+ if(equals(events.event, 'signup'), 1, 0) AS step_1,+ if(equals(events.event, 'purchase'), 1, 0) AS step_2+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), or(and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), or(equals(events.event, 'signup'), equals(events.event, 'purchase'))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ exposures.exposure_event_uuid AS exposure_event_uuid,+ (arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(3, 94608000, 'first_touch', 'ordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(metric_events.timestamp, 'UTC'), 'Float64'), metric_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, metric_events.step_0), multiply(2, metric_events.step_1), multiply(3, metric_events.step_2)]))))))))[1]).1 AS step_reached+ FROM exposures+ LEFT JOIN metric_events ON equals(exposures.entity_id, metric_events.entity_id)+ GROUP BY exposures.entity_id,+ exposures.variant,+ exposures.exposure_event_uuid) SELECT entity_metrics.entity_id AS actor_id,+ entity_metrics.variant AS variant,+ actor_id AS id+ FROM entity_metrics+ WHERE and(ifNull(greaterOrEquals(entity_metrics.step_reached, 1), 0), ifNull(less(entity_metrics.step_reached, 2), 0), ifNull(equals(variant, 'test'), 0))+ ORDER BY entity_metrics.entity_id ASC) AS source+ ORDER BY source.id ASC+ LIMIT 101+ OFFSET 0 SETTINGS optimize_aggregation_in_order=1,+ join_algorithm='auto',+ readonly=2,+ max_execution_time=60,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=0,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentActorsQuery.test_experiment_funnel_actors_excludes_events_before_exposure[new_events_schema]+ '''+ SELECT source.id,+ source.id AS id+ FROM+ (WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))) AS session_id,+ multiply(1, and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0))) AS step_0,+ if(equals(events.event, 'signup'), 1, 0) AS step_1,+ if(equals(events.event, 'purchase'), 1, 0) AS step_2+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), or(and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), or(equals(events.event, 'signup'), equals(events.event, 'purchase'))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ exposures.exposure_event_uuid AS exposure_event_uuid,+ (arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(3, 94608000, 'first_touch', 'ordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(metric_events.timestamp, 'UTC'), 'Float64'), metric_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, metric_events.step_0), multiply(2, metric_events.step_1), multiply(3, metric_events.step_2)]))))))))[1]).1 AS step_reached+ FROM exposures+ LEFT JOIN metric_events ON equals(exposures.entity_id, metric_events.entity_id)+ GROUP BY exposures.entity_id,+ exposures.variant,+ exposures.exposure_event_uuid) SELECT entity_metrics.entity_id AS actor_id,+ entity_metrics.variant AS variant,+ actor_id AS id+ FROM entity_metrics+ WHERE and(ifNull(greaterOrEquals(entity_metrics.step_reached, 1), 0), ifNull(less(entity_metrics.step_reached, 2), 0), ifNull(equals(variant, 'control'), 0))+ ORDER BY entity_metrics.entity_id ASC) AS source+ ORDER BY source.id ASC+ LIMIT 101+ OFFSET 0 SETTINGS optimize_aggregation_in_order=1,+ join_algorithm='auto',+ readonly=2,+ max_execution_time=60,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=0,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentActorsQuery.test_experiment_funnel_actors_step1_dropoff_counts_pre_exposure_signup[new_events_schema]+ '''+ SELECT source.id,+ source.id AS id+ FROM+ (WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))) AS session_id,+ multiply(1, and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0))) AS step_0,+ if(equals(events.event, 'signup'), 1, 0) AS step_1,+ if(equals(events.event, 'purchase'), 1, 0) AS step_2+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), or(and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), or(equals(events.event, 'signup'), equals(events.event, 'purchase'))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ exposures.exposure_event_uuid AS exposure_event_uuid,+ (arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(3, 94608000, 'first_touch', 'ordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(metric_events.timestamp, 'UTC'), 'Float64'), metric_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, metric_events.step_0), multiply(2, metric_events.step_1), multiply(3, metric_events.step_2)]))))))))[1]).1 AS step_reached+ FROM exposures+ LEFT JOIN metric_events ON equals(exposures.entity_id, metric_events.entity_id)+ GROUP BY exposures.entity_id,+ exposures.variant,+ exposures.exposure_event_uuid) SELECT entity_metrics.entity_id AS actor_id,+ entity_metrics.variant AS variant,+ actor_id AS id+ FROM entity_metrics+ WHERE and(ifNull(greaterOrEquals(entity_metrics.step_reached, 0), 0), ifNull(less(entity_metrics.step_reached, 1), 0), ifNull(equals(variant, 'control'), 0))+ ORDER BY entity_metrics.entity_id ASC) AS source+ ORDER BY source.id ASC+ LIMIT 101+ OFFSET 0 SETTINGS optimize_aggregation_in_order=1,+ join_algorithm='auto',+ readonly=2,+ max_execution_time=60,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=0,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentActorsQuery.test_experiment_funnel_actors_with_recordings[new_events_schema]+ '''+ SELECT source.id,+ source.id AS id,+ source.matching_events AS matching_events+ FROM+ (WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))) AS session_id,+ multiply(1, and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0))) AS step_0,+ if(equals(events.event, 'signup'), 1, 0) AS step_1,+ if(equals(events.event, 'purchase'), 1, 0) AS step_2+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), or(and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), or(equals(events.event, 'signup'), equals(events.event, 'purchase'))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ exposures.exposure_event_uuid AS exposure_event_uuid,+ groupArray(tuple(toTimeZone(metric_events.timestamp, 'UTC'), metric_events.uuid, metric_events.session_id, '')) AS user_events,+ mapFromArrays(arrayMap(x -> x.2, user_events), user_events) AS user_events_map,+ arraySort(x -> minus(0, x.1), aggregate_funnel_array(3, 94608000, 'first_touch', 'ordered', array(array('')), [], arraySort(t -> t.1, groupArray(tuple(accurateCastOrNull(toTimeZone(metric_events.timestamp, 'UTC'), 'Float64'), metric_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, metric_events.step_0), multiply(2, metric_events.step_1), multiply(3, metric_events.step_2)]))))))[1] AS af_tuple,+ af_tuple.1 AS step_reached,+ af_tuple.4 AS matched_event_uuids_array_array,+ arrayMap(matched_event_uuids_array -> arrayMap(event_uuid -> user_events_map[event_uuid], arrayDistinct(matched_event_uuids_array)), matched_event_uuids_array_array) AS matched_events_array+ FROM exposures+ LEFT JOIN metric_events ON equals(exposures.entity_id, metric_events.entity_id)+ GROUP BY exposures.entity_id,+ exposures.variant,+ exposures.exposure_event_uuid) SELECT entity_metrics.entity_id AS actor_id,+ entity_metrics.variant AS variant,+ entity_metrics.matched_events_array[plus(entity_metrics.step_reached, 1)] AS matching_events,+ actor_id AS id+ FROM entity_metrics+ WHERE and(ifNull(greaterOrEquals(entity_metrics.step_reached, 2), 0), ifNull(equals(variant, 'control'), 0))+ ORDER BY entity_metrics.entity_id ASC) AS source+ ORDER BY source.id ASC+ LIMIT 101+ OFFSET 0 SETTINGS optimize_aggregation_in_order=1,+ join_algorithm='auto',+ readonly=2,+ max_execution_time=60,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=0,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---diff --git a/products/experiments/backend/hogql_queries/test/__snapshots__/test_experiment_funnels_query_runner.new_events_schema.ambr b/products/experiments/backend/hogql_queries/test/__snapshots__/test_experiment_funnels_query_runner.new_events_schema.ambrnew file mode 100644index 000000000000..770478618d0e--- /dev/null+++ b/products/experiments/backend/hogql_queries/test/__snapshots__/test_experiment_funnels_query_runner.new_events_schema.ambr@@ -0,0 +1,622 @@+# serializer version: 1+# name: TestExperimentFunnelsQueryRunner.test_query_runner_with_persons_on_events_mode_0_person_id_override_properties_on_events_no_filter[new_events_schema]+ '''+ SELECT sum(step_1) AS step_1,+ sum(step_2) AS step_2,+ arrayMap(x -> if(isNaN(x), NULL, x), [avgArrayOrNull(step_1_conversion_times)])[1] AS step_1_average_conversion_time,+ arrayMap(x -> if(isNaN(x), NULL, x), [medianArrayOrNull(step_1_conversion_times)])[1] AS step_1_median_conversion_time,+ arrayMap(x -> if(isNaN(x), NULL, x), [arrayReduce('median', arrayFlatten(groupArray(arrayFlatten(groupArray(total_conversion_times))) OVER ()))])[1] AS total_median_conversion_time,+ groupArray(row_number) AS row_number,+ final_prop AS final_prop+ FROM+ (SELECT countIf(ifNull(notEquals(bitAnd(steps_bitfield, 1), 0), 1)) AS step_1,+ countIf(ifNull(notEquals(bitAnd(steps_bitfield, 2), 0), 1)) AS step_2,+ groupArrayIf(timings[1], ifNull(greater(timings[1], 0), 0)) AS step_1_conversion_times,+ groupArrayIf(arraySum(timings), ifNull(greaterOrEquals(step_reached, 1), 0)) AS total_conversion_times,+ rowNumberInAllBlocks() AS row_number,+ if(less(row_number, 25), breakdown, ['Other']) AS final_prop+ FROM+ (SELECT arraySort(t -> t.1, groupArray(tuple(accurateCastOrNull(timestamp, 'Float64'), uuid, arrayMap(x -> ifNull(x, ''), prop_basic), arrayFilter(x -> ifNull(notEquals(x, 0), 1), [multiply(1, step_0), multiply(2, step_1)])))) AS events_array,+ argMinIf(prop_basic, timestamp, notEmpty(arrayFilter(x -> notEmpty(x), prop_basic))) AS prop,+ arrayJoin(aggregate_funnel_array(2, 1209600, 'first_touch', 'ordered', [if(empty(prop), [''], prop)], [], arrayFilter((x, x_before, x_after) -> not(and(lessOrEquals(length(x.4), 1), equals(x.4, x_before.4), equals(x.4, x_after.4), equals(x.3, x_before.3), equals(x.3, x_after.3), ifNull(greater(x.1, x_before.1), 0), ifNull(less(x.1, x_after.1), 0))), events_array, arrayRotateRight(events_array, 1), arrayRotateLeft(events_array, 1)))) AS af_tuple,+ af_tuple.1 AS step_reached,+ plus(af_tuple.1, 1) AS steps,+ af_tuple.2 AS breakdown,+ af_tuple.3 AS timings,+ af_tuple.5 AS steps_bitfield,+ aggregation_target AS aggregation_target+ FROM+ (SELECT toTimeZone(e.timestamp, 'UTC') AS timestamp,+ if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id) AS aggregation_target,+ e.uuid AS uuid,+ e.properties.`$session_id` AS `$session_id`,+ e.properties.`$window_id` AS `$window_id`,+ if(equals(e.event, '$pageview'), 1, 0) AS step_0,+ if(equals(e.event, 'purchase'), 1, 0) AS step_1,+ [ifNull(toString(if(has(e.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(e.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', e.properties.`$feature_flags`['test-experiment']), NULL)), '')] AS prop_basic,+ prop_basic AS prop+ FROM events_json AS e+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS e__override ON equals(e.distinct_id, e__override.distinct_id)+ WHERE and(equals(e.team_id, 99999), and(and(greaterOrEquals(e.timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC')), lessOrEquals(e.timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC'))), in(e.event, tuple('$pageview', 'purchase')), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(e.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(e.person_properties.^email)), if(isNull(nullIf(toString(e.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(e.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(e.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(e.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(e.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(e.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(e.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(e.person_properties.email), '[]'), '{}'), toString(e.person_properties.email)))))), '%@posthog.com%'), 1)), or(equals(step_0, 1), equals(step_1, 1))))+ GROUP BY aggregation_target+ HAVING ifNull(greaterOrEquals(step_reached, 0), 0))+ GROUP BY breakdown+ ORDER BY step_2 DESC,+ step_1 DESC)+ GROUP BY final_prop+ ORDER BY step_2 DESC,+ step_1 DESC+ LIMIT 26 SETTINGS join_algorithm='auto',+ readonly=2,+ max_execution_time=60,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=23622320128,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentFunnelsQueryRunner.test_query_runner_with_persons_on_events_mode_1_person_id_override_properties_on_events_filter_earlierevent[new_events_schema]+ '''+ SELECT sum(step_1) AS step_1,+ sum(step_2) AS step_2,+ arrayMap(x -> if(isNaN(x), NULL, x), [avgArrayOrNull(step_1_conversion_times)])[1] AS step_1_average_conversion_time,+ arrayMap(x -> if(isNaN(x), NULL, x), [medianArrayOrNull(step_1_conversion_times)])[1] AS step_1_median_conversion_time,+ arrayMap(x -> if(isNaN(x), NULL, x), [arrayReduce('median', arrayFlatten(groupArray(arrayFlatten(groupArray(total_conversion_times))) OVER ()))])[1] AS total_median_conversion_time,+ groupArray(row_number) AS row_number,+ final_prop AS final_prop+ FROM+ (SELECT countIf(ifNull(notEquals(bitAnd(steps_bitfield, 1), 0), 1)) AS step_1,+ countIf(ifNull(notEquals(bitAnd(steps_bitfield, 2), 0), 1)) AS step_2,+ groupArrayIf(timings[1], ifNull(greater(timings[1], 0), 0)) AS step_1_conversion_times,+ groupArrayIf(arraySum(timings), ifNull(greaterOrEquals(step_reached, 1), 0)) AS total_conversion_times,+ rowNumberInAllBlocks() AS row_number,+ if(less(row_number, 25), breakdown, ['Other']) AS final_prop+ FROM+ (SELECT arraySort(t -> t.1, groupArray(tuple(accurateCastOrNull(timestamp, 'Float64'), uuid, arrayMap(x -> ifNull(x, ''), prop_basic), arrayFilter(x -> ifNull(notEquals(x, 0), 1), [multiply(1, step_0), multiply(2, step_1)])))) AS events_array,+ argMinIf(prop_basic, timestamp, notEmpty(arrayFilter(x -> notEmpty(x), prop_basic))) AS prop,+ arrayJoin(aggregate_funnel_array(2, 1209600, 'first_touch', 'ordered', [if(empty(prop), [''], prop)], [], arrayFilter((x, x_before, x_after) -> not(and(lessOrEquals(length(x.4), 1), equals(x.4, x_before.4), equals(x.4, x_after.4), equals(x.3, x_before.3), equals(x.3, x_after.3), ifNull(greater(x.1, x_before.1), 0), ifNull(less(x.1, x_after.1), 0))), events_array, arrayRotateRight(events_array, 1), arrayRotateLeft(events_array, 1)))) AS af_tuple,+ af_tuple.1 AS step_reached,+ plus(af_tuple.1, 1) AS steps,+ af_tuple.2 AS breakdown,+ af_tuple.3 AS timings,+ af_tuple.5 AS steps_bitfield,+ aggregation_target AS aggregation_target+ FROM+ (SELECT toTimeZone(e.timestamp, 'UTC') AS timestamp,+ if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id) AS aggregation_target,+ e.uuid AS uuid,+ e.properties.`$session_id` AS `$session_id`,+ e.properties.`$window_id` AS `$window_id`,+ if(equals(e.event, '$pageview'), 1, 0) AS step_0,+ if(equals(e.event, 'purchase'), 1, 0) AS step_1,+ [ifNull(toString(if(has(e.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(e.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', e.properties.`$feature_flags`['test-experiment']), NULL)), '')] AS prop_basic,+ prop_basic AS prop+ FROM events_json AS e+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS e__override ON equals(e.distinct_id, e__override.distinct_id)+ WHERE and(equals(e.team_id, 99999), and(and(greaterOrEquals(e.timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC')), lessOrEquals(e.timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC'))), in(e.event, tuple('$pageview', 'purchase')), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(e.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(e.person_properties.^email)), if(isNull(nullIf(toString(e.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(e.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(e.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(e.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(e.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(e.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(e.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(e.person_properties.email), '[]'), '{}'), toString(e.person_properties.email)))))), '%@earlierevent.com%'), 1)), or(equals(step_0, 1), equals(step_1, 1))))+ GROUP BY aggregation_target+ HAVING ifNull(greaterOrEquals(step_reached, 0), 0))+ GROUP BY breakdown+ ORDER BY step_2 DESC,+ step_1 DESC)+ GROUP BY final_prop+ ORDER BY step_2 DESC,+ step_1 DESC+ LIMIT 26 SETTINGS join_algorithm='auto',+ readonly=2,+ max_execution_time=60,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=23622320128,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentFunnelsQueryRunner.test_query_runner_with_persons_on_events_mode_2_person_id_override_properties_on_events_filter_laterevent[new_events_schema]+ '''+ SELECT sum(step_1) AS step_1,+ sum(step_2) AS step_2,+ arrayMap(x -> if(isNaN(x), NULL, x), [avgArrayOrNull(step_1_conversion_times)])[1] AS step_1_average_conversion_time,+ arrayMap(x -> if(isNaN(x), NULL, x), [medianArrayOrNull(step_1_conversion_times)])[1] AS step_1_median_conversion_time,+ arrayMap(x -> if(isNaN(x), NULL, x), [arrayReduce('median', arrayFlatten(groupArray(arrayFlatten(groupArray(total_conversion_times))) OVER ()))])[1] AS total_median_conversion_time,+ groupArray(row_number) AS row_number,+ final_prop AS final_prop+ FROM+ (SELECT countIf(ifNull(notEquals(bitAnd(steps_bitfield, 1), 0), 1)) AS step_1,+ countIf(ifNull(notEquals(bitAnd(steps_bitfield, 2), 0), 1)) AS step_2,+ groupArrayIf(timings[1], ifNull(greater(timings[1], 0), 0)) AS step_1_conversion_times,+ groupArrayIf(arraySum(timings), ifNull(greaterOrEquals(step_reached, 1), 0)) AS total_conversion_times,+ rowNumberInAllBlocks() AS row_number,+ if(less(row_number, 25), breakdown, ['Other']) AS final_prop+ FROM+ (SELECT arraySort(t -> t.1, groupArray(tuple(accurateCastOrNull(timestamp, 'Float64'), uuid, arrayMap(x -> ifNull(x, ''), prop_basic), arrayFilter(x -> ifNull(notEquals(x, 0), 1), [multiply(1, step_0), multiply(2, step_1)])))) AS events_array,+ argMinIf(prop_basic, timestamp, notEmpty(arrayFilter(x -> notEmpty(x), prop_basic))) AS prop,+ arrayJoin(aggregate_funnel_array(2, 1209600, 'first_touch', 'ordered', [if(empty(prop), [''], prop)], [], arrayFilter((x, x_before, x_after) -> not(and(lessOrEquals(length(x.4), 1), equals(x.4, x_before.4), equals(x.4, x_after.4), equals(x.3, x_before.3), equals(x.3, x_after.3), ifNull(greater(x.1, x_before.1), 0), ifNull(less(x.1, x_after.1), 0))), events_array, arrayRotateRight(events_array, 1), arrayRotateLeft(events_array, 1)))) AS af_tuple,+ af_tuple.1 AS step_reached,+ plus(af_tuple.1, 1) AS steps,+ af_tuple.2 AS breakdown,+ af_tuple.3 AS timings,+ af_tuple.5 AS steps_bitfield,+ aggregation_target AS aggregation_target+ FROM+ (SELECT toTimeZone(e.timestamp, 'UTC') AS timestamp,+ if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id) AS aggregation_target,+ e.uuid AS uuid,+ e.properties.`$session_id` AS `$session_id`,+ e.properties.`$window_id` AS `$window_id`,+ if(equals(e.event, '$pageview'), 1, 0) AS step_0,+ if(equals(e.event, 'purchase'), 1, 0) AS step_1,+ [ifNull(toString(if(has(e.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(e.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', e.properties.`$feature_flags`['test-experiment']), NULL)), '')] AS prop_basic,+ prop_basic AS prop+ FROM events_json AS e+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS e__override ON equals(e.distinct_id, e__override.distinct_id)+ WHERE and(equals(e.team_id, 99999), and(and(greaterOrEquals(e.timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC')), lessOrEquals(e.timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC'))), in(e.event, tuple('$pageview', 'purchase')), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(e.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(e.person_properties.^email)), if(isNull(nullIf(toString(e.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(e.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(e.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(e.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(e.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(e.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(e.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(e.person_properties.email), '[]'), '{}'), toString(e.person_properties.email)))))), '%@laterevent.com%'), 1)), or(equals(step_0, 1), equals(step_1, 1))))+ GROUP BY aggregation_target+ HAVING ifNull(greaterOrEquals(step_reached, 0), 0))+ GROUP BY breakdown+ ORDER BY step_2 DESC,+ step_1 DESC)+ GROUP BY final_prop+ ORDER BY step_2 DESC,+ step_1 DESC+ LIMIT 26 SETTINGS join_algorithm='auto',+ readonly=2,+ max_execution_time=60,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=23622320128,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentFunnelsQueryRunner.test_query_runner_with_persons_on_events_mode_3_person_id_override_properties_joined_no_filter[new_events_schema]+ '''+ SELECT sum(step_1) AS step_1,+ sum(step_2) AS step_2,+ arrayMap(x -> if(isNaN(x), NULL, x), [avgArrayOrNull(step_1_conversion_times)])[1] AS step_1_average_conversion_time,+ arrayMap(x -> if(isNaN(x), NULL, x), [medianArrayOrNull(step_1_conversion_times)])[1] AS step_1_median_conversion_time,+ arrayMap(x -> if(isNaN(x), NULL, x), [arrayReduce('median', arrayFlatten(groupArray(arrayFlatten(groupArray(total_conversion_times))) OVER ()))])[1] AS total_median_conversion_time,+ groupArray(row_number) AS row_number,+ final_prop AS final_prop+ FROM+ (SELECT countIf(ifNull(notEquals(bitAnd(steps_bitfield, 1), 0), 1)) AS step_1,+ countIf(ifNull(notEquals(bitAnd(steps_bitfield, 2), 0), 1)) AS step_2,+ groupArrayIf(timings[1], ifNull(greater(timings[1], 0), 0)) AS step_1_conversion_times,+ groupArrayIf(arraySum(timings), ifNull(greaterOrEquals(step_reached, 1), 0)) AS total_conversion_times,+ rowNumberInAllBlocks() AS row_number,+ if(less(row_number, 25), breakdown, ['Other']) AS final_prop+ FROM+ (SELECT arraySort(t -> t.1, groupArray(tuple(accurateCastOrNull(timestamp, 'Float64'), uuid, arrayMap(x -> ifNull(x, ''), prop_basic), arrayFilter(x -> ifNull(notEquals(x, 0), 1), [multiply(1, step_0), multiply(2, step_1)])))) AS events_array,+ argMinIf(prop_basic, timestamp, notEmpty(arrayFilter(x -> notEmpty(x), prop_basic))) AS prop,+ arrayJoin(aggregate_funnel_array(2, 1209600, 'first_touch', 'ordered', [if(empty(prop), [''], prop)], [], arrayFilter((x, x_before, x_after) -> not(and(lessOrEquals(length(x.4), 1), equals(x.4, x_before.4), equals(x.4, x_after.4), equals(x.3, x_before.3), equals(x.3, x_after.3), ifNull(greater(x.1, x_before.1), 0), ifNull(less(x.1, x_after.1), 0))), events_array, arrayRotateRight(events_array, 1), arrayRotateLeft(events_array, 1)))) AS af_tuple,+ af_tuple.1 AS step_reached,+ plus(af_tuple.1, 1) AS steps,+ af_tuple.2 AS breakdown,+ af_tuple.3 AS timings,+ af_tuple.5 AS steps_bitfield,+ aggregation_target AS aggregation_target+ FROM+ (SELECT toTimeZone(e.timestamp, 'UTC') AS timestamp,+ if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id) AS aggregation_target,+ e.uuid AS uuid,+ e.properties.`$session_id` AS `$session_id`,+ e.properties.`$window_id` AS `$window_id`,+ if(equals(e.event, '$pageview'), 1, 0) AS step_0,+ if(equals(e.event, 'purchase'), 1, 0) AS step_1,+ [ifNull(toString(if(has(e.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(e.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', e.properties.`$feature_flags`['test-experiment']), NULL)), '')] AS prop_basic,+ prop_basic AS prop+ FROM events_json AS e+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS e__override ON equals(e.distinct_id, e__override.distinct_id)+ LEFT JOIN+ (SELECT person.id AS id,+ replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'email'), ''), 'null'), '^"|"$', '') AS properties___email+ FROM person+ WHERE and(equals(person.team_id, 99999), in(tuple(person.id, person.version),+ (SELECT person.id AS id, max(person.version) AS version+ FROM person+ WHERE equals(person.team_id, 99999)+ GROUP BY person.id+ HAVING and(equals(argMax(person.is_deleted, person.version), 0), less(argMax(toTimeZone(person.created_at, 'UTC'), person.version), plus(now64(6, 'UTC'), toIntervalDay(1))))))) SETTINGS optimize_aggregation_in_order=1) AS e__person ON equals(if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id), e__person.id)+ WHERE and(equals(e.team_id, 99999), and(and(greaterOrEquals(e.timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC')), lessOrEquals(e.timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC'))), in(e.event, tuple('$pageview', 'purchase')), ifNull(notILike(toString(e__person.properties___email), '%@posthog.com%'), 1)), or(equals(step_0, 1), equals(step_1, 1))))+ GROUP BY aggregation_target+ HAVING ifNull(greaterOrEquals(step_reached, 0), 0))+ GROUP BY breakdown+ ORDER BY step_2 DESC,+ step_1 DESC)+ GROUP BY final_prop+ ORDER BY step_2 DESC,+ step_1 DESC+ LIMIT 26 SETTINGS join_algorithm='auto',+ readonly=2,+ max_execution_time=60,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=23622320128,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentFunnelsQueryRunner.test_query_runner_with_persons_on_events_mode_4_person_id_override_properties_joined_filter_earlierevent[new_events_schema]+ '''+ SELECT sum(step_1) AS step_1,+ sum(step_2) AS step_2,+ arrayMap(x -> if(isNaN(x), NULL, x), [avgArrayOrNull(step_1_conversion_times)])[1] AS step_1_average_conversion_time,+ arrayMap(x -> if(isNaN(x), NULL, x), [medianArrayOrNull(step_1_conversion_times)])[1] AS step_1_median_conversion_time,+ arrayMap(x -> if(isNaN(x), NULL, x), [arrayReduce('median', arrayFlatten(groupArray(arrayFlatten(groupArray(total_conversion_times))) OVER ()))])[1] AS total_median_conversion_time,+ groupArray(row_number) AS row_number,+ final_prop AS final_prop+ FROM+ (SELECT countIf(ifNull(notEquals(bitAnd(steps_bitfield, 1), 0), 1)) AS step_1,+ countIf(ifNull(notEquals(bitAnd(steps_bitfield, 2), 0), 1)) AS step_2,+ groupArrayIf(timings[1], ifNull(greater(timings[1], 0), 0)) AS step_1_conversion_times,+ groupArrayIf(arraySum(timings), ifNull(greaterOrEquals(step_reached, 1), 0)) AS total_conversion_times,+ rowNumberInAllBlocks() AS row_number,+ if(less(row_number, 25), breakdown, ['Other']) AS final_prop+ FROM+ (SELECT arraySort(t -> t.1, groupArray(tuple(accurateCastOrNull(timestamp, 'Float64'), uuid, arrayMap(x -> ifNull(x, ''), prop_basic), arrayFilter(x -> ifNull(notEquals(x, 0), 1), [multiply(1, step_0), multiply(2, step_1)])))) AS events_array,+ argMinIf(prop_basic, timestamp, notEmpty(arrayFilter(x -> notEmpty(x), prop_basic))) AS prop,+ arrayJoin(aggregate_funnel_array(2, 1209600, 'first_touch', 'ordered', [if(empty(prop), [''], prop)], [], arrayFilter((x, x_before, x_after) -> not(and(lessOrEquals(length(x.4), 1), equals(x.4, x_before.4), equals(x.4, x_after.4), equals(x.3, x_before.3), equals(x.3, x_after.3), ifNull(greater(x.1, x_before.1), 0), ifNull(less(x.1, x_after.1), 0))), events_array, arrayRotateRight(events_array, 1), arrayRotateLeft(events_array, 1)))) AS af_tuple,+ af_tuple.1 AS step_reached,+ plus(af_tuple.1, 1) AS steps,+ af_tuple.2 AS breakdown,+ af_tuple.3 AS timings,+ af_tuple.5 AS steps_bitfield,+ aggregation_target AS aggregation_target+ FROM+ (SELECT toTimeZone(e.timestamp, 'UTC') AS timestamp,+ if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id) AS aggregation_target,+ e.uuid AS uuid,+ e.properties.`$session_id` AS `$session_id`,+ e.properties.`$window_id` AS `$window_id`,+ if(equals(e.event, '$pageview'), 1, 0) AS step_0,+ if(equals(e.event, 'purchase'), 1, 0) AS step_1,+ [ifNull(toString(if(has(e.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(e.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', e.properties.`$feature_flags`['test-experiment']), NULL)), '')] AS prop_basic,+ prop_basic AS prop+ FROM events_json AS e+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS e__override ON equals(e.distinct_id, e__override.distinct_id)+ LEFT JOIN+ (SELECT person.id AS id,+ replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'email'), ''), 'null'), '^"|"$', '') AS properties___email+ FROM person+ WHERE and(equals(person.team_id, 99999), in(tuple(person.id, person.version),+ (SELECT person.id AS id, max(person.version) AS version+ FROM person+ WHERE equals(person.team_id, 99999)+ GROUP BY person.id+ HAVING and(equals(argMax(person.is_deleted, person.version), 0), less(argMax(toTimeZone(person.created_at, 'UTC'), person.version), plus(now64(6, 'UTC'), toIntervalDay(1))))))) SETTINGS optimize_aggregation_in_order=1) AS e__person ON equals(if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id), e__person.id)+ WHERE and(equals(e.team_id, 99999), and(and(greaterOrEquals(e.timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC')), lessOrEquals(e.timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC'))), in(e.event, tuple('$pageview', 'purchase')), ifNull(notILike(toString(e__person.properties___email), '%@earlierevent.com%'), 1)), or(equals(step_0, 1), equals(step_1, 1))))+ GROUP BY aggregation_target+ HAVING ifNull(greaterOrEquals(step_reached, 0), 0))+ GROUP BY breakdown+ ORDER BY step_2 DESC,+ step_1 DESC)+ GROUP BY final_prop+ ORDER BY step_2 DESC,+ step_1 DESC+ LIMIT 26 SETTINGS join_algorithm='auto',+ readonly=2,+ max_execution_time=60,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=23622320128,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentFunnelsQueryRunner.test_query_runner_with_persons_on_events_mode_5_person_id_override_properties_joined_filter_laterevent[new_events_schema]+ '''+ SELECT sum(step_1) AS step_1,+ sum(step_2) AS step_2,+ arrayMap(x -> if(isNaN(x), NULL, x), [avgArrayOrNull(step_1_conversion_times)])[1] AS step_1_average_conversion_time,+ arrayMap(x -> if(isNaN(x), NULL, x), [medianArrayOrNull(step_1_conversion_times)])[1] AS step_1_median_conversion_time,+ arrayMap(x -> if(isNaN(x), NULL, x), [arrayReduce('median', arrayFlatten(groupArray(arrayFlatten(groupArray(total_conversion_times))) OVER ()))])[1] AS total_median_conversion_time,+ groupArray(row_number) AS row_number,+ final_prop AS final_prop+ FROM+ (SELECT countIf(ifNull(notEquals(bitAnd(steps_bitfield, 1), 0), 1)) AS step_1,+ countIf(ifNull(notEquals(bitAnd(steps_bitfield, 2), 0), 1)) AS step_2,+ groupArrayIf(timings[1], ifNull(greater(timings[1], 0), 0)) AS step_1_conversion_times,+ groupArrayIf(arraySum(timings), ifNull(greaterOrEquals(step_reached, 1), 0)) AS total_conversion_times,+ rowNumberInAllBlocks() AS row_number,+ if(less(row_number, 25), breakdown, ['Other']) AS final_prop+ FROM+ (SELECT arraySort(t -> t.1, groupArray(tuple(accurateCastOrNull(timestamp, 'Float64'), uuid, arrayMap(x -> ifNull(x, ''), prop_basic), arrayFilter(x -> ifNull(notEquals(x, 0), 1), [multiply(1, step_0), multiply(2, step_1)])))) AS events_array,+ argMinIf(prop_basic, timestamp, notEmpty(arrayFilter(x -> notEmpty(x), prop_basic))) AS prop,+ arrayJoin(aggregate_funnel_array(2, 1209600, 'first_touch', 'ordered', [if(empty(prop), [''], prop)], [], arrayFilter((x, x_before, x_after) -> not(and(lessOrEquals(length(x.4), 1), equals(x.4, x_before.4), equals(x.4, x_after.4), equals(x.3, x_before.3), equals(x.3, x_after.3), ifNull(greater(x.1, x_before.1), 0), ifNull(less(x.1, x_after.1), 0))), events_array, arrayRotateRight(events_array, 1), arrayRotateLeft(events_array, 1)))) AS af_tuple,+ af_tuple.1 AS step_reached,+ plus(af_tuple.1, 1) AS steps,+ af_tuple.2 AS breakdown,+ af_tuple.3 AS timings,+ af_tuple.5 AS steps_bitfield,+ aggregation_target AS aggregation_target+ FROM+ (SELECT toTimeZone(e.timestamp, 'UTC') AS timestamp,+ if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id) AS aggregation_target,+ e.uuid AS uuid,+ e.properties.`$session_id` AS `$session_id`,+ e.properties.`$window_id` AS `$window_id`,+ if(equals(e.event, '$pageview'), 1, 0) AS step_0,+ if(equals(e.event, 'purchase'), 1, 0) AS step_1,+ [ifNull(toString(if(has(e.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(e.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', e.properties.`$feature_flags`['test-experiment']), NULL)), '')] AS prop_basic,+ prop_basic AS prop+ FROM events_json AS e+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS e__override ON equals(e.distinct_id, e__override.distinct_id)+ LEFT JOIN+ (SELECT person.id AS id,+ replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'email'), ''), 'null'), '^"|"$', '') AS properties___email+ FROM person+ WHERE and(equals(person.team_id, 99999), in(tuple(person.id, person.version),+ (SELECT person.id AS id, max(person.version) AS version+ FROM person+ WHERE equals(person.team_id, 99999)+ GROUP BY person.id+ HAVING and(equals(argMax(person.is_deleted, person.version), 0), less(argMax(toTimeZone(person.created_at, 'UTC'), person.version), plus(now64(6, 'UTC'), toIntervalDay(1))))))) SETTINGS optimize_aggregation_in_order=1) AS e__person ON equals(if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id), e__person.id)+ WHERE and(equals(e.team_id, 99999), and(and(greaterOrEquals(e.timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC')), lessOrEquals(e.timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC'))), in(e.event, tuple('$pageview', 'purchase')), ifNull(notILike(toString(e__person.properties___email), '%@laterevent.com%'), 1)), or(equals(step_0, 1), equals(step_1, 1))))+ GROUP BY aggregation_target+ HAVING ifNull(greaterOrEquals(step_reached, 0), 0))+ GROUP BY breakdown+ ORDER BY step_2 DESC,+ step_1 DESC)+ GROUP BY final_prop+ ORDER BY step_2 DESC,+ step_1 DESC+ LIMIT 26 SETTINGS join_algorithm='auto',+ readonly=2,+ max_execution_time=60,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=23622320128,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentFunnelsQueryRunner.test_query_runner_with_persons_on_events_mode_6_person_id_no_override_properties_on_events_no_filter[new_events_schema]+ '''+ SELECT sum(step_1) AS step_1,+ sum(step_2) AS step_2,+ arrayMap(x -> if(isNaN(x), NULL, x), [avgArrayOrNull(step_1_conversion_times)])[1] AS step_1_average_conversion_time,+ arrayMap(x -> if(isNaN(x), NULL, x), [medianArrayOrNull(step_1_conversion_times)])[1] AS step_1_median_conversion_time,+ arrayMap(x -> if(isNaN(x), NULL, x), [arrayReduce('median', arrayFlatten(groupArray(arrayFlatten(groupArray(total_conversion_times))) OVER ()))])[1] AS total_median_conversion_time,+ groupArray(row_number) AS row_number,+ final_prop AS final_prop+ FROM+ (SELECT countIf(ifNull(notEquals(bitAnd(steps_bitfield, 1), 0), 1)) AS step_1,+ countIf(ifNull(notEquals(bitAnd(steps_bitfield, 2), 0), 1)) AS step_2,+ groupArrayIf(timings[1], ifNull(greater(timings[1], 0), 0)) AS step_1_conversion_times,+ groupArrayIf(arraySum(timings), ifNull(greaterOrEquals(step_reached, 1), 0)) AS total_conversion_times,+ rowNumberInAllBlocks() AS row_number,+ if(less(row_number, 25), breakdown, ['Other']) AS final_prop+ FROM+ (SELECT arraySort(t -> t.1, groupArray(tuple(accurateCastOrNull(timestamp, 'Float64'), uuid, arrayMap(x -> ifNull(x, ''), prop_basic), arrayFilter(x -> ifNull(notEquals(x, 0), 1), [multiply(1, step_0), multiply(2, step_1)])))) AS events_array,+ argMinIf(prop_basic, timestamp, notEmpty(arrayFilter(x -> notEmpty(x), prop_basic))) AS prop,+ arrayJoin(aggregate_funnel_array(2, 1209600, 'first_touch', 'ordered', [if(empty(prop), [''], prop)], [], arrayFilter((x, x_before, x_after) -> not(and(lessOrEquals(length(x.4), 1), equals(x.4, x_before.4), equals(x.4, x_after.4), equals(x.3, x_before.3), equals(x.3, x_after.3), ifNull(greater(x.1, x_before.1), 0), ifNull(less(x.1, x_after.1), 0))), events_array, arrayRotateRight(events_array, 1), arrayRotateLeft(events_array, 1)))) AS af_tuple,+ af_tuple.1 AS step_reached,+ plus(af_tuple.1, 1) AS steps,+ af_tuple.2 AS breakdown,+ af_tuple.3 AS timings,+ af_tuple.5 AS steps_bitfield,+ aggregation_target AS aggregation_target+ FROM+ (SELECT toTimeZone(e.timestamp, 'UTC') AS timestamp,+ e.person_id AS aggregation_target,+ e.uuid AS uuid,+ e.properties.`$session_id` AS `$session_id`,+ e.properties.`$window_id` AS `$window_id`,+ if(equals(e.event, '$pageview'), 1, 0) AS step_0,+ if(equals(e.event, 'purchase'), 1, 0) AS step_1,+ [ifNull(toString(if(has(e.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(e.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', e.properties.`$feature_flags`['test-experiment']), NULL)), '')] AS prop_basic,+ prop_basic AS prop+ FROM events_json AS e+ WHERE and(equals(e.team_id, 99999), and(and(greaterOrEquals(e.timestamp, toDateTime64('2020-01-01 00:00:00.000000', 6, 'UTC')), lessOrEquals(e.timestamp, toDateTime64('2020-01-31 00:00:00.000000', 6, 'UTC'))), in(e.event, tuple('$pageview', 'purchase')), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(e.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(e.person_properties.^email)), if(isNull(nullIf(toString(e.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(e.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(e.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(e.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(e.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(e.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(e.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(e.person_properties.email), '[]'), '{}'), toString(e.person_properties.email)))))), '%@posthog.com%'), 1)), or(equals(step_0, 1), equals(step_1, 1))))+ GROUP BY aggregation_target+ HAVING ifNull(greaterOrEquals(step_reached, 0), 0))+ GROUP BY breakdown+ ORDER BY step_2 DESC,+ step_1 DESC)+ GROUP BY final_prop+ ORDER BY step_2 DESC,+ step_1 DESC+ LIMIT 26 SETTINGS join_algorithm='auto',+ readonly=2,+ max_execution_time=60,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=23622320128,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentFunnelsQueryRunner.test_query_runner_with_persons_on_events_mode_7_person_id_no_override_properties_on_events_filter_earlierevent[new_events_schema]+ '''+ SELECT sum(step_1) AS step_1,+ sum(step_2) AS step_2,+ arrayMap(x -> if(isNaN(x), NULL, x), [avgArrayOrNull(step_1_conversion_times)])[1] AS step_1_average_conversion_time,+ arrayMap(x -> if(isNaN(x), NULL, x), [medianArrayOrNull(step_1_conversion_times)])[1] AS step_1_median_conversion_time,+ arrayMap(x -> if(isNaN(x), NULL, x), [arrayReduce('median', arrayFlatten(groupArray(arrayFlatten(groupArray(total_conversion_times))) OVER ()))])[1] AS total_median_conversion_time,+ groupArray(row_number) AS row_number,+ final_prop AS final_prop+ FROM+ (SELECT countIf(ifNull(notEquals(bitAnd(steps_bitfield, 1), 0), 1)) AS step_1,+ countIf(ifNull(notEquals(bitAnd(steps_bitfield, 2), 0), 1)) AS step_2,+ groupArrayIf(timings[1], ifNull(greater(timings[1], 0), 0)) AS step_1_conversion_times,+ groupArrayIf(arraySum(timings), ifNull(greaterOrEquals(step_reached, 1), 0)) AS total_conversion_times,+ rowNumberInAllBlocks() AS row_number,+ if(less(row_number, 25), breakdown, ['Other']) AS final_prop+ FROM+ (SELECT arraySort(t -> t.1, groupArray(tuple(accurateCastOrNull(timestamp, 'Float64'), uuid, arrayMap(x -> ifNull(x, ''), prop_basic), arrayFilter(x -> ifNull(notEquals(x, 0), 1), [multiply(1, step_0), multiply(2, step_1)])))) AS events_array,+ argMinIf(prop_basic, timestamp, notEmpty(arrayFilter(x -> notEmpty(x), prop_basic))) AS prop,+ arrayJoin(aggregate_funnel_array(2, 1209600, 'first_touch', 'ordered', [if(empty(prop), [''], prop)], [], arrayFilter((x, x_before, x_after) -> not(and(lessOrEquals(length(x.4), 1), equals(x.4, x_before.4), equals(x.4, x_after.4), equals(x.3, x_before.3), equals(x.3, x_after.3), ifNull(greater(x.1, x_before.1), 0), ifNull(less(x.1, x_after.1), 0))), events_array, arrayRotateRight(events_array, 1), arrayRotateLeft(events_array, 1)))) AS af_tuple,+ af_tuple.1 AS step_reached,+ plus(af_tuple.1, 1) AS steps,+ af_tuple.2 AS breakdown,+ af_tuple.3 AS timings,+ af_tuple.5 AS steps_bitfield,+ aggregation_target AS aggregation_target+ FROM+ (SELECT toTimeZone(e.timestamp, 'UTC') AS timestamp,+ e.person_id AS aggregation_target,+ e.uuid AS uuid,+ e.properties.`$session_id` AS `$session_id`,+ e.properties.`$window_id` AS `$window_id`,+ if(equals(e.event, '$pageview'), 1, 0) AS step_0,+ if(equals(e.event, 'purchase'), 1, 0) AS step_1,+ [ifNull(toString(if(has(e.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(e.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', e.properties.`$feature_flags`['test-experiment']), NULL)), '')] AS prop_basic,+ prop_basic AS prop+ FROM events_json AS e+ WHERE and(equals(e.team_id, 99999), and(and(greaterOrEquals(e.timestamp, toDateTime64('2020-01-01 00:00:00.000000', 6, 'UTC')), lessOrEquals(e.timestamp, toDateTime64('2020-01-31 00:00:00.000000', 6, 'UTC'))), in(e.event, tuple('$pageview', 'purchase')), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(e.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(e.person_properties.^email)), if(isNull(nullIf(toString(e.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(e.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(e.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(e.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(e.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(e.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(e.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(e.person_properties.email), '[]'), '{}'), toString(e.person_properties.email)))))), '%@earlierevent.com%'), 1)), or(equals(step_0, 1), equals(step_1, 1))))+ GROUP BY aggregation_target+ HAVING ifNull(greaterOrEquals(step_reached, 0), 0))+ GROUP BY breakdown+ ORDER BY step_2 DESC,+ step_1 DESC)+ GROUP BY final_prop+ ORDER BY step_2 DESC,+ step_1 DESC+ LIMIT 26 SETTINGS join_algorithm='auto',+ readonly=2,+ max_execution_time=60,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=23622320128,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentFunnelsQueryRunner.test_query_runner_with_persons_on_events_mode_8_person_id_no_override_properties_on_events_filter_laterevent[new_events_schema]+ '''+ SELECT sum(step_1) AS step_1,+ sum(step_2) AS step_2,+ arrayMap(x -> if(isNaN(x), NULL, x), [avgArrayOrNull(step_1_conversion_times)])[1] AS step_1_average_conversion_time,+ arrayMap(x -> if(isNaN(x), NULL, x), [medianArrayOrNull(step_1_conversion_times)])[1] AS step_1_median_conversion_time,+ arrayMap(x -> if(isNaN(x), NULL, x), [arrayReduce('median', arrayFlatten(groupArray(arrayFlatten(groupArray(total_conversion_times))) OVER ()))])[1] AS total_median_conversion_time,+ groupArray(row_number) AS row_number,+ final_prop AS final_prop+ FROM+ (SELECT countIf(ifNull(notEquals(bitAnd(steps_bitfield, 1), 0), 1)) AS step_1,+ countIf(ifNull(notEquals(bitAnd(steps_bitfield, 2), 0), 1)) AS step_2,+ groupArrayIf(timings[1], ifNull(greater(timings[1], 0), 0)) AS step_1_conversion_times,+ groupArrayIf(arraySum(timings), ifNull(greaterOrEquals(step_reached, 1), 0)) AS total_conversion_times,+ rowNumberInAllBlocks() AS row_number,+ if(less(row_number, 25), breakdown, ['Other']) AS final_prop+ FROM+ (SELECT arraySort(t -> t.1, groupArray(tuple(accurateCastOrNull(timestamp, 'Float64'), uuid, arrayMap(x -> ifNull(x, ''), prop_basic), arrayFilter(x -> ifNull(notEquals(x, 0), 1), [multiply(1, step_0), multiply(2, step_1)])))) AS events_array,+ argMinIf(prop_basic, timestamp, notEmpty(arrayFilter(x -> notEmpty(x), prop_basic))) AS prop,+ arrayJoin(aggregate_funnel_array(2, 1209600, 'first_touch', 'ordered', [if(empty(prop), [''], prop)], [], arrayFilter((x, x_before, x_after) -> not(and(lessOrEquals(length(x.4), 1), equals(x.4, x_before.4), equals(x.4, x_after.4), equals(x.3, x_before.3), equals(x.3, x_after.3), ifNull(greater(x.1, x_before.1), 0), ifNull(less(x.1, x_after.1), 0))), events_array, arrayRotateRight(events_array, 1), arrayRotateLeft(events_array, 1)))) AS af_tuple,+ af_tuple.1 AS step_reached,+ plus(af_tuple.1, 1) AS steps,+ af_tuple.2 AS breakdown,+ af_tuple.3 AS timings,+ af_tuple.5 AS steps_bitfield,+ aggregation_target AS aggregation_target+ FROM+ (SELECT toTimeZone(e.timestamp, 'UTC') AS timestamp,+ e.person_id AS aggregation_target,+ e.uuid AS uuid,+ e.properties.`$session_id` AS `$session_id`,+ e.properties.`$window_id` AS `$window_id`,+ if(equals(e.event, '$pageview'), 1, 0) AS step_0,+ if(equals(e.event, 'purchase'), 1, 0) AS step_1,+ [ifNull(toString(if(has(e.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(e.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', e.properties.`$feature_flags`['test-experiment']), NULL)), '')] AS prop_basic,+ prop_basic AS prop+ FROM events_json AS e+ WHERE and(equals(e.team_id, 99999), and(and(greaterOrEquals(e.timestamp, toDateTime64('2020-01-01 00:00:00.000000', 6, 'UTC')), lessOrEquals(e.timestamp, toDateTime64('2020-01-31 00:00:00.000000', 6, 'UTC'))), in(e.event, tuple('$pageview', 'purchase')), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(e.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(e.person_properties.^email)), if(isNull(nullIf(toString(e.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(e.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(e.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(e.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(e.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(e.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(e.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(e.person_properties.email), '[]'), '{}'), toString(e.person_properties.email)))))), '%@laterevent.com%'), 1)), or(equals(step_0, 1), equals(step_1, 1))))+ GROUP BY aggregation_target+ HAVING ifNull(greaterOrEquals(step_reached, 0), 0))+ GROUP BY breakdown+ ORDER BY step_2 DESC,+ step_1 DESC)+ GROUP BY final_prop+ ORDER BY step_2 DESC,+ step_1 DESC+ LIMIT 26 SETTINGS join_algorithm='auto',+ readonly=2,+ max_execution_time=60,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=23622320128,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---diff --git a/products/experiments/backend/hogql_queries/test/__snapshots__/test_experiment_trends_query_runner.new_events_schema.ambr b/products/experiments/backend/hogql_queries/test/__snapshots__/test_experiment_trends_query_runner.new_events_schema.ambrnew file mode 100644index 000000000000..b2429f00d11f--- /dev/null+++ b/products/experiments/backend/hogql_queries/test/__snapshots__/test_experiment_trends_query_runner.new_events_schema.ambr@@ -0,0 +1,112 @@+# serializer version: 1+# name: TestExperimentTrendsQueryRunner.test_query_runner_with_action[new_events_schema.1]+ '''+ SELECT groupArray(1)(date)[1] AS date,+ arrayFold((acc, x) -> arrayMap(i -> plus(acc[i], x[i]), range(1, plus(length(date), 1))), groupArray(ifNull(total, 0)), arrayWithConstant(length(date), reinterpretAsFloat64(0))) AS total,+ if(ifNull(greaterOrEquals(row_number, 25), 0), '$$_posthog_breakdown_other_$$', breakdown_value) AS breakdown_value+ FROM+ (SELECT arrayMap(number -> plus(toStartOfInterval(assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC')), toIntervalDay(1)), toIntervalDay(number)), range(0, plus(coalesce(dateDiff('day', toStartOfInterval(assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC')), toIntervalDay(1)), toStartOfInterval(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalDay(1)))), 1))) AS date,+ arrayFill(x -> ifNull(greater(x, 0), 0), arrayMap(_match_date -> arraySum(arraySlice(groupArray(ifNull(count, 0)), indexOf(groupArray(day_start) AS _days_for_count, _match_date) AS _index, plus(minus(arrayLastIndex(x -> ifNull(equals(x, _match_date), isNull(x)+ and isNull(_match_date)), _days_for_count), _index), 1))), date)) AS total,+ breakdown_value AS breakdown_value,+ rowNumberInAllBlocks() AS row_number+ FROM+ (SELECT day_start AS day_start,+ sum(count) OVER (PARTITION BY breakdown_value+ ORDER BY day_start ASC) AS count,+ breakdown_value AS breakdown_value+ FROM+ (SELECT sum(total) AS count,+ day_start AS day_start,+ breakdown_value AS breakdown_value+ FROM+ (SELECT count(DISTINCT if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id)) AS total,+ min(toStartOfDay(toTimeZone(e.timestamp, 'UTC'))) AS day_start,+ ifNull(nullIf(leftUTF8(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(e.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(e.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(e.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(e.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(e.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(e.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(e.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(e.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(e.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(e.properties.`$feature_flag_response`), '[]'), '{}'), toString(e.properties.`$feature_flag_response`)))))), 400), ''), '$$_posthog_breakdown_null_$$') AS breakdown_value+ FROM events_json AS e+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS e__override ON equals(e.distinct_id, e__override.distinct_id)+ WHERE and(equals(e.team_id, 99999), greaterOrEquals(e.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(e.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), equals(e.event, '$feature_flag_called'), and(in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(e.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(e.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(e.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(e.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(e.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(e.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(e.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(e.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(e.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(e.properties.`$feature_flag_response`), '[]'), '{}'), toString(e.properties.`$feature_flag_response`))))), tuple('control', 'test')), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(e.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(e.properties.^`$feature_flag`)), if(isNull(nullIf(toString(e.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(e.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(e.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(e.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(e.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(e.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(e.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(e.properties.`$feature_flag`), '[]'), '{}'), toString(e.properties.`$feature_flag`))))), 'test-experiment'), 0)))+ GROUP BY if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id),+ breakdown_value)+ GROUP BY day_start,+ breakdown_value+ ORDER BY day_start ASC,+ breakdown_value ASC)+ ORDER BY day_start ASC)+ GROUP BY breakdown_value+ ORDER BY if(ifNull(equals(breakdown_value, '$$_posthog_breakdown_other_$$'), 0), 2, if(ifNull(equals(breakdown_value, '$$_posthog_breakdown_null_$$'), 0), 1, 0)) ASC, arraySum(total) DESC, breakdown_value ASC)+ WHERE isNotNull(breakdown_value)+ GROUP BY breakdown_value+ ORDER BY if(ifNull(equals(breakdown_value, '$$_posthog_breakdown_other_$$'), 0), 2, if(ifNull(equals(breakdown_value, '$$_posthog_breakdown_null_$$'), 0), 1, 0)) ASC, arraySum(total) DESC, breakdown_value ASC+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=60,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=0,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentTrendsQueryRunner.test_query_runner_with_action[new_events_schema]+ '''+ SELECT groupArray(1)(date)[1] AS date,+ arrayFold((acc, x) -> arrayMap(i -> plus(acc[i], x[i]), range(1, plus(length(date), 1))), groupArray(ifNull(total, 0)), arrayWithConstant(length(date), reinterpretAsFloat64(0))) AS total,+ if(ifNull(greaterOrEquals(row_number, 25), 0), '$$_posthog_breakdown_other_$$', breakdown_value) AS breakdown_value+ FROM+ (SELECT arrayMap(number -> plus(toStartOfInterval(assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC')), toIntervalDay(1)), toIntervalDay(number)), range(0, plus(coalesce(dateDiff('day', toStartOfInterval(assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC')), toIntervalDay(1)), toStartOfInterval(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalDay(1)))), 1))) AS date,+ arrayFill(x -> ifNull(greater(x, 0), 0), arrayMap(_match_date -> arraySum(arraySlice(groupArray(ifNull(count, 0)), indexOf(groupArray(day_start) AS _days_for_count, _match_date) AS _index, plus(minus(arrayLastIndex(x -> ifNull(equals(x, _match_date), isNull(x)+ and isNull(_match_date)), _days_for_count), _index), 1))), date)) AS total,+ breakdown_value AS breakdown_value,+ rowNumberInAllBlocks() AS row_number+ FROM+ (SELECT day_start AS day_start,+ sum(count) OVER (PARTITION BY breakdown_value+ ORDER BY day_start ASC) AS count,+ breakdown_value AS breakdown_value+ FROM+ (SELECT sum(total) AS count,+ day_start AS day_start,+ breakdown_value AS breakdown_value+ FROM+ (SELECT count() AS total,+ toStartOfDay(toTimeZone(e.timestamp, 'UTC')) AS day_start,+ ifNull(nullIf(leftUTF8(toString(if(has(e.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(e.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', e.properties.`$feature_flags`['test-experiment']), NULL)), 400), ''), '$$_posthog_breakdown_null_$$') AS breakdown_value+ FROM events_json AS e+ WHERE and(equals(e.team_id, 99999), greaterOrEquals(e.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(e.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), equals(e.event, '$pageview'), in(if(has(e.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(e.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', e.properties.`$feature_flags`['test-experiment']), NULL), tuple('control', 'test')))+ GROUP BY day_start,+ breakdown_value)+ GROUP BY day_start,+ breakdown_value+ ORDER BY day_start ASC,+ breakdown_value ASC)+ ORDER BY day_start ASC)+ GROUP BY breakdown_value+ ORDER BY if(ifNull(equals(breakdown_value, '$$_posthog_breakdown_other_$$'), 0), 2, if(ifNull(equals(breakdown_value, '$$_posthog_breakdown_null_$$'), 0), 1, 0)) ASC, arraySum(total) DESC, breakdown_value ASC)+ WHERE isNotNull(breakdown_value)+ GROUP BY breakdown_value+ ORDER BY if(ifNull(equals(breakdown_value, '$$_posthog_breakdown_other_$$'), 0), 2, if(ifNull(equals(breakdown_value, '$$_posthog_breakdown_null_$$'), 0), 1, 0)) ASC, arraySum(total) DESC, breakdown_value ASC+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=60,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=0,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---diff --git a/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_activation_config.new_events_schema.ambr b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_activation_config.new_events_schema.ambrnew file mode 100644index 000000000000..0a9adcc1bf17--- /dev/null+++ b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_activation_config.new_events_schema.ambr@@ -0,0 +1,208 @@+# serializer version: 1+# name: TestExperimentActivationConfig.test_exposures_timeseries_counts_users_on_activation_day[new_events_schema]+ '''+ WITH first_exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ any(flag_exposures.variant) AS variant,+ toDateOrNull(toString(min(toTimeZone(events.timestamp, 'UTC')))) AS day+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ INNER JOIN+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_flag_exposure_time+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-10 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id) AS flag_exposures ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), flag_exposures.entity_id)+ WHERE and(equals(events.team_id, 99999), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-10 00:00:00', 'UTC'))), equals(events.event, 'activated'), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1)), greaterOrEquals(events.timestamp, flag_exposures.first_flag_exposure_time))+ GROUP BY entity_id)+ SELECT first_exposures.day AS day,+ first_exposures.variant AS variant,+ count(first_exposures.entity_id) AS exposed_count+ FROM first_exposures+ WHERE notEmpty(variant)+ GROUP BY first_exposures.day,+ first_exposures.variant+ ORDER BY first_exposures.day ASC+ LIMIT 5000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=0,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentActivationConfig.test_funnel_metric_counts_conversions_after_activation_only[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ any(flag_exposures.variant) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ INNER JOIN+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_flag_exposure_time+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-10 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id) AS flag_exposures ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), flag_exposures.entity_id)+ WHERE and(equals(events.team_id, 99999), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-10 00:00:00', 'UTC'))), equals(events.event, 'activated'), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1)), greaterOrEquals(events.timestamp, flag_exposures.first_flag_exposure_time))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))) AS variant,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ and(greaterOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2020-01-10 00:00:00', 'UTC'))), equals(events.event, 'activated'), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1)) AS step_0,+ if(equals(events.event, 'purchase'), 1, 0) AS step_1+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), or(and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-10 00:00:00', 'UTC'))), equals(events.event, 'activated'), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1)), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-10 00:00:00', 'UTC'))), equals(events.event, 'purchase'))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(2, 94608000, 'first_touch', 'ordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(metric_events.timestamp, 'UTC'), 'Float64'), metric_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, metric_events.step_0), multiply(2, metric_events.step_1)]))))))))[1] AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ countIf(ifNull(equals((entity_metrics.value).1, 1), 0)) AS total_sum,+ countIf(ifNull(equals((entity_metrics.value).1, 1), 0)) AS total_sum_of_squares,+ tuple(countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 1), 0))) AS step_counts+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentActivationConfig.test_mean_metric_anchors_exposure_on_activation_event[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ any(flag_exposures.variant) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ INNER JOIN+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_flag_exposure_time+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-10 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id) AS flag_exposures ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), flag_exposures.entity_id)+ WHERE and(equals(events.team_id, 99999), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-10 00:00:00', 'UTC'))), equals(events.event, 'activated'), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1)), greaterOrEquals(events.timestamp, flag_exposures.first_flag_exposure_time))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-10 00:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---diff --git a/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_base.new_events_schema.ambr b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_base.new_events_schema.ambrnew file mode 100644index 000000000000..60549b9722a3--- /dev/null+++ b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_base.new_events_schema.ambr@@ -0,0 +1,2577 @@+# serializer version: 1+# name: TestExperimentQueryRunner.test_mean_metric_with_parametric_aggregation[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-10 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^margin)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^margin)), if(isNull(nullIf(toString(events.properties.margin), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.margin, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.margin, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.margin), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.margin, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.margin, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.margin, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.margin), '[]'), '{}'), toString(events.properties.margin))))) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-10 00:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'checkout-conversion'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ coalesce(quantile(0.9)(accurateCastOrNull(metric_events.value, 'Float64')), 0) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_excludes_multiple_variants[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, '$pageview'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_group_aggregation_mean_metric[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(less(toTimeZone(events.timestamp, 'UTC'), toDateTime64('today', 6, 'UTC')), '', events.properties.`$group_0`) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']), notEmpty(if(less(toTimeZone(events.timestamp, 'UTC'), toDateTime64('today', 6, 'UTC')), '', events.properties.`$group_0`)))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(less(toTimeZone(events.timestamp, 'UTC'), toDateTime64('today', 6, 'UTC')), '', events.properties.`$group_0`) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_group_aggregation_mean_property_sum_metric[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(less(toTimeZone(events.timestamp, 'UTC'), toDateTime64('today', 6, 'UTC')), '', events.properties.`$group_0`) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']), notEmpty(if(less(toTimeZone(events.timestamp, 'UTC'), toDateTime64('today', 6, 'UTC')), '', events.properties.`$group_0`)))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(less(toTimeZone(events.timestamp, 'UTC'), toDateTime64('today', 6, 'UTC')), '', events.properties.`$group_0`) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.amount, 'Float64'), 0) AS value+ FROM events_json AS events+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_includes_date_range[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-02-01 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-02-01 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_includes_event_property_filters[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), and(equals(events.event, 'purchase'), ifNull(notEquals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^plan)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^plan)), if(isNull(nullIf(toString(events.properties.plan), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.plan, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.plan, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.plan), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.plan, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.plan, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.plan, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.plan), '[]'), '{}'), toString(events.properties.plan))))), 'pro'), 1)))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_multiple_variant_handling_options_0_exclude[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, '$pageview'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_multiple_variant_handling_options_1_first_seen[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ argMin(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), toTimeZone(events.timestamp, 'UTC')) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, '$pageview'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_no_control_variant[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_no_exposures[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_no_variant_exposures[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_standard_flow_v2_stats[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, '$pageview'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_using_action[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_action_as_exposure_criteria[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL)), 1), '$multiple', any(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC'))), and(equals(events.event, 'purchase'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^plan)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^plan)), if(isNull(nullIf(toString(events.properties.plan), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.plan, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.plan, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.plan), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.plan, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.plan, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.plan, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.plan), '[]'), '{}'), toString(events.properties.plan))))), 'premium'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'conversion'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_custom_exposure[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL)), 1), '$multiple', any(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$pageview'), ifNull(notEquals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^plan)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^plan)), if(isNull(nullIf(toString(events.properties.plan), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.plan, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.plan, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.plan), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.plan, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.plan, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.plan, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.plan), '[]'), '{}'), toString(events.properties.plan))))), 'free'), 1)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_custom_exposure_on_feature_flag_called_event[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC'))), and(and(equals(events.event, '$feature_flag_called'), ifNull(notEquals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^plan)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^plan)), if(isNull(nullIf(toString(events.properties.plan), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.plan, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.plan, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.plan), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.plan, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.plan, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.plan, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.plan), '[]'), '{}'), toString(events.properties.plan))))), 'free'), 1)), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_custom_exposure_without_properties[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL)), 1), '$multiple', any(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC'))), equals(events.event, '$pageview'), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_hogql_aggregation_end_to_end[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^amount)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^amount)), if(isNull(nullIf(toString(events.properties.amount), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.amount, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.amount, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.amount), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.amount, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.amount, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.amount, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.amount), '[]'), '{}'), toString(events.properties.amount))))) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ coalesce(avg(accurateCastOrNull(metric_events.value, 'Float64')), 0) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_hogql_aggregation_expressions[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ minus(accurateCastOrNull(events.properties.revenue, 'Float64'), accurateCastOrNull(events.properties.cost, 'Float64')) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ coalesce(sum(accurateCastOrNull(metric_events.value, 'Float64')), 0) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_hogql_fallback_to_sum[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^price)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^price)), if(isNull(nullIf(toString(events.properties.price), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.price, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.price, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.price), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.price, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.price, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.price, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.price), '[]'), '{}'), toString(events.properties.price))))) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_internal_filters_0_person_properties[new_events_schema.1]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, '$pageview'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_internal_filters_0_person_properties[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, '$pageview'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_internal_filters_1_event_properties[new_events_schema.1]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, '$pageview'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_internal_filters_1_event_properties[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(not(match(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$host`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$host`)), if(isNull(nullIf(toString(events.properties.`$host`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$host`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$host`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$host`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$host`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$host`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$host`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$host`), '[]'), '{}'), toString(events.properties.`$host`)))))), '^(localhost|127\\.0\\.0\\.1)($|:)')), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, '$pageview'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_internal_filters_2_feature_flags[new_events_schema.1]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, '$pageview'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_internal_filters_2_feature_flags[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(has(events.properties.`$feature_flags`, 'flag_doesnt_exist'), if(ifNull(equals(events.properties.`$feature_flags`['flag_doesnt_exist'], '$false'), 0), 'false', events.properties.`$feature_flags`['flag_doesnt_exist']), NULL), tuple('test', 'control')), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, '$pageview'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_internal_filters_3_cohort_static[new_events_schema.1]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, '$pageview'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_internal_filters_3_cohort_static[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id),+ (SELECT person_static_cohort.person_id AS person_id+ FROM person_static_cohort+ WHERE and(equals(person_static_cohort.team_id, 99999), equals(person_static_cohort.cohort_id, 99999)))), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, '$pageview'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_internal_filters_4_cohort_dynamic[new_events_schema.1]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, '$pageview'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_internal_filters_4_cohort_dynamic[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id),+ (SELECT cohortpeople.person_id AS person_id+ FROM cohortpeople+ WHERE and(equals(cohortpeople.team_id, 99999), equals(cohortpeople.cohort_id, 99999), equals(cohortpeople.version, 0)))), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, '$pageview'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_internal_filters_5_group[new_events_schema.1]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, '$pageview'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_internal_filters_5_group[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ LEFT JOIN+ (SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(groups.group_properties, 'name'), ''), 'null'), '^"|"$', '')), toTimeZone(groups._timestamp, 'UTC')), 1) AS properties___name,+ groups.group_type_index AS index,+ groups.group_key AS key+ FROM groups+ WHERE and(equals(groups.team_id, 99999), equals(index, 0))+ GROUP BY groups.group_type_index,+ groups.group_key) AS events__group_0 ON equals(events.properties.`$group_0`, events__group_0.key)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(equals(events__group_0.properties___name, 'Test Group'), 0), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, '$pageview'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_internal_filters_6_element[new_events_schema.1]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, '$pageview'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_internal_filters_6_element[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), match(events.elements_chain, '(^|;)button(\\.|$|;|:)'), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-31 00:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, '$pageview'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_none_event_filters_all_events[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^test_property)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^test_property)), if(isNull(nullIf(toString(events.properties.test_property), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.test_property, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.test_property, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.test_property), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.test_property, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.test_property, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.test_property, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.test_property), '[]'), '{}'), toString(events.properties.test_property))))), 'test_value'), 0))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_time_window_0_experiment_duration[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-05 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-05 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_time_window_1_24_hour_window[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-05 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-05 12:00:00', 'UTC')), toIntervalSecond(86400))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), and(greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')), less(toTimeZone(metric_events.timestamp, 'UTC'), plus(toTimeZone(exposures.first_exposure_time, 'UTC'), toIntervalSecond(86400)))))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_time_window_2_48_hour_window[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-05 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-05 12:00:00', 'UTC')), toIntervalSecond(172800))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), and(greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')), less(toTimeZone(metric_events.timestamp, 'UTC'), plus(toTimeZone(exposures.first_exposure_time, 'UTC'), toIntervalSecond(172800)))))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_time_window_3_72_hour_window[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-05 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-05 12:00:00', 'UTC')), toIntervalSecond(259200))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), and(greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')), less(toTimeZone(metric_events.timestamp, 'UTC'), plus(toTimeZone(exposures.first_exposure_time, 'UTC'), toIntervalSecond(259200)))))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_unique_group_metric[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(less(toTimeZone(events.timestamp, 'UTC'), toDateTime64('today', 6, 'UTC')), '', events.properties.`$group_0`) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-10 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']), notEmpty(if(less(toTimeZone(events.timestamp, 'UTC'), toDateTime64('today', 6, 'UTC')), '', events.properties.`$group_0`)))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(less(toTimeZone(events.timestamp, 'UTC'), toDateTime64('today', 6, 'UTC')), '', events.properties.`$group_0`) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ 1 AS value+ FROM events_json AS events+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-10 00:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ accurateCastOrNull(count(DISTINCT multiIf(isNull(metric_events.value), NULL, and(in(toTypeName(metric_events.value), tuple('UUID', 'Nullable(UUID)')), equals(reinterpretAsUInt128(assumeNotNull(metric_events.value)), 0)), NULL, equals(toString(metric_events.value), ''), NULL, metric_events.value)), 'Float64') AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_unique_users_metric_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-10 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-10 00:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ accurateCastOrNull(count(DISTINCT multiIf(isNull(metric_events.value), NULL, and(in(toTypeName(metric_events.value), tuple('UUID', 'Nullable(UUID)')), equals(reinterpretAsUInt128(assumeNotNull(metric_events.value)), 0)), NULL, equals(toString(metric_events.value), ''), NULL, metric_events.value)), 'Float64') AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_without_feature_flag_property[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-02-01 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-02-01 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---diff --git a/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_breakdown.new_events_schema.ambr b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_breakdown.new_events_schema.ambrnew file mode 100644index 000000000000..66de13b0c1e5--- /dev/null+++ b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_breakdown.new_events_schema.ambr@@ -0,0 +1,1426 @@+# serializer version: 1+# name: TestExperimentBreakdown.test_funnel_metric_with_breakdown[new_events_schema]+ '''+ WITH base_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))) AS variant_value,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), if(isNull(nullIf(toString(events.properties.`$browser`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$browser`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$browser`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$browser`), '[]'), '{}'), toString(events.properties.`$browser`)))))), '$$_posthog_breakdown_null_$$') AS breakdown_value_1,+ and(greaterOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])) AS step_0,+ if(equals(events.event, 'purchase'), 1, 0) AS step_1+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), or(and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), equals(events.event, 'purchase'))))),+ entity_metrics AS+ (SELECT base_events.entity_id AS entity_id,+ if(greater(uniqExactIf(base_events.variant_value, equals(base_events.step_0, 1)), 1), '$multiple', anyIf(base_events.variant_value, equals(base_events.step_0, 1))) AS variant,+ arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(2, 94608000, 'first_touch', 'ordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(base_events.timestamp, 'UTC'), 'Float64'), base_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, base_events.step_0), multiply(2, base_events.step_1)]))))))))[1] AS value,+ argMinIf(base_events.breakdown_value_1, toTimeZone(base_events.timestamp, 'UTC'), equals(base_events.step_0, 1)) AS breakdown_value_1+ FROM base_events+ GROUP BY base_events.entity_id+ HAVING greater(countIf(equals(base_events.step_0, 1)), 0))+ SELECT entity_metrics.variant AS variant,+ entity_metrics.breakdown_value_1 AS breakdown_value_1,+ count(entity_metrics.entity_id) AS num_users,+ countIf(ifNull(equals((entity_metrics.value).1, 1), 0)) AS total_sum,+ countIf(ifNull(equals((entity_metrics.value).1, 1), 0)) AS total_sum_of_squares,+ tuple(countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 1), 0))) AS step_counts+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant,+ entity_metrics.breakdown_value_1+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentBreakdown.test_funnel_metric_with_person_property_breakdown[new_events_schema]+ '''+ WITH base_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))) AS variant_value,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^country)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^country)), if(isNull(nullIf(toString(events.person_properties.country), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.country, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.country), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.country), '[]'), '{}'), toString(events.person_properties.country)))))), '$$_posthog_breakdown_null_$$') AS breakdown_value_1,+ and(greaterOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])) AS step_0,+ if(equals(events.event, '$pageview'), 1, 0) AS step_1,+ if(equals(events.event, 'purchase'), 1, 0) AS step_2+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), or(and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(1209600))), or(equals(events.event, '$pageview'), equals(events.event, 'purchase')))))),+ entity_metrics AS+ (SELECT base_events.entity_id AS entity_id,+ if(greater(uniqExactIf(base_events.variant_value, equals(base_events.step_0, 1)), 1), '$multiple', anyIf(base_events.variant_value, equals(base_events.step_0, 1))) AS variant,+ arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(3, 1209600, 'first_touch', 'ordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(base_events.timestamp, 'UTC'), 'Float64'), base_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, base_events.step_0), multiply(2, base_events.step_1), multiply(3, base_events.step_2)]))))))))[1] AS value,+ argMinIf(base_events.breakdown_value_1, toTimeZone(base_events.timestamp, 'UTC'), equals(base_events.step_0, 1)) AS breakdown_value_1+ FROM base_events+ GROUP BY base_events.entity_id+ HAVING greater(countIf(equals(base_events.step_0, 1)), 0))+ SELECT entity_metrics.variant AS variant,+ entity_metrics.breakdown_value_1 AS breakdown_value_1,+ count(entity_metrics.entity_id) AS num_users,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum_of_squares,+ tuple(countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 1), 0)), countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 2), 0))) AS step_counts+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant,+ entity_metrics.breakdown_value_1+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentBreakdown.test_funnel_metric_with_two_breakdowns[new_events_schema]+ '''+ WITH base_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))) AS variant_value,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), if(isNull(nullIf(toString(events.properties.`$browser`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$browser`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$browser`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$browser`), '[]'), '{}'), toString(events.properties.`$browser`)))))), '$$_posthog_breakdown_null_$$') AS breakdown_value_1,+ coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$os`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$os`)), if(isNull(nullIf(toString(events.properties.`$os`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$os`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$os`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$os`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$os`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$os`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$os`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$os`), '[]'), '{}'), toString(events.properties.`$os`)))))), '$$_posthog_breakdown_null_$$') AS breakdown_value_2,+ and(greaterOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])) AS step_0,+ if(equals(events.event, 'purchase'), 1, 0) AS step_1+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), or(and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), equals(events.event, 'purchase'))))),+ entity_metrics AS+ (SELECT base_events.entity_id AS entity_id,+ if(greater(uniqExactIf(base_events.variant_value, equals(base_events.step_0, 1)), 1), '$multiple', anyIf(base_events.variant_value, equals(base_events.step_0, 1))) AS variant,+ arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(2, 94608000, 'first_touch', 'ordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(base_events.timestamp, 'UTC'), 'Float64'), base_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, base_events.step_0), multiply(2, base_events.step_1)]))))))))[1] AS value,+ argMinIf(base_events.breakdown_value_1, toTimeZone(base_events.timestamp, 'UTC'), equals(base_events.step_0, 1)) AS breakdown_value_1,+ argMinIf(base_events.breakdown_value_2, toTimeZone(base_events.timestamp, 'UTC'), equals(base_events.step_0, 1)) AS breakdown_value_2+ FROM base_events+ GROUP BY base_events.entity_id+ HAVING greater(countIf(equals(base_events.step_0, 1)), 0))+ SELECT entity_metrics.variant AS variant,+ entity_metrics.breakdown_value_1 AS breakdown_value_1,+ entity_metrics.breakdown_value_2 AS breakdown_value_2,+ count(entity_metrics.entity_id) AS num_users,+ countIf(ifNull(equals((entity_metrics.value).1, 1), 0)) AS total_sum,+ countIf(ifNull(equals((entity_metrics.value).1, 1), 0)) AS total_sum_of_squares,+ tuple(countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 1), 0))) AS step_counts+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant,+ entity_metrics.breakdown_value_1,+ entity_metrics.breakdown_value_2+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentBreakdown.test_mean_metric_breakdown_with_changing_property_values[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), if(isNull(nullIf(toString(events.properties.`$browser`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$browser`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$browser`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$browser`), '[]'), '{}'), toString(events.properties.`$browser`)))))), '$$_posthog_breakdown_null_$$'), toTimeZone(events.timestamp, 'UTC')) AS breakdown_value_1+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.amount, 'Float64'), 0) AS value,+ coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), if(isNull(nullIf(toString(events.properties.`$browser`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$browser`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$browser`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$browser`), '[]'), '{}'), toString(events.properties.`$browser`)))))), '$$_posthog_breakdown_null_$$') AS breakdown_value_1+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value,+ exposures.breakdown_value_1 AS breakdown_value_1+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant,+ exposures.breakdown_value_1)+ SELECT entity_metrics.variant AS variant,+ entity_metrics.breakdown_value_1 AS breakdown_value_1,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant,+ entity_metrics.breakdown_value_1+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentBreakdown.test_mean_metric_with_breakdown[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), if(isNull(nullIf(toString(events.properties.`$browser`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$browser`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$browser`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$browser`), '[]'), '{}'), toString(events.properties.`$browser`)))))), '$$_posthog_breakdown_null_$$'), toTimeZone(events.timestamp, 'UTC')) AS breakdown_value_1+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.amount, 'Float64'), 0) AS value,+ coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), if(isNull(nullIf(toString(events.properties.`$browser`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$browser`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$browser`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$browser`), '[]'), '{}'), toString(events.properties.`$browser`)))))), '$$_posthog_breakdown_null_$$') AS breakdown_value_1+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value,+ exposures.breakdown_value_1 AS breakdown_value_1+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant,+ exposures.breakdown_value_1)+ SELECT entity_metrics.variant AS variant,+ entity_metrics.breakdown_value_1 AS breakdown_value_1,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant,+ entity_metrics.breakdown_value_1+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentBreakdown.test_mean_metric_with_null_breakdown_values[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), if(isNull(nullIf(toString(events.properties.`$browser`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$browser`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$browser`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$browser`), '[]'), '{}'), toString(events.properties.`$browser`)))))), '$$_posthog_breakdown_null_$$'), toTimeZone(events.timestamp, 'UTC')) AS breakdown_value_1+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value,+ coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), if(isNull(nullIf(toString(events.properties.`$browser`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$browser`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$browser`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$browser`), '[]'), '{}'), toString(events.properties.`$browser`)))))), '$$_posthog_breakdown_null_$$') AS breakdown_value_1+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value,+ exposures.breakdown_value_1 AS breakdown_value_1+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant,+ exposures.breakdown_value_1)+ SELECT entity_metrics.variant AS variant,+ entity_metrics.breakdown_value_1 AS breakdown_value_1,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant,+ entity_metrics.breakdown_value_1+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentBreakdown.test_mean_metric_with_null_person_property_breakdown[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^country)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^country)), if(isNull(nullIf(toString(events.person_properties.country), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.country, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.country), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.country), '[]'), '{}'), toString(events.person_properties.country)))))), '$$_posthog_breakdown_null_$$'), toTimeZone(events.timestamp, 'UTC')) AS breakdown_value_1+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.amount, 'Float64'), 0) AS value,+ coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^country)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^country)), if(isNull(nullIf(toString(events.person_properties.country), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.country, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.country), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.country), '[]'), '{}'), toString(events.person_properties.country)))))), '$$_posthog_breakdown_null_$$') AS breakdown_value_1+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value,+ exposures.breakdown_value_1 AS breakdown_value_1+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant,+ exposures.breakdown_value_1)+ SELECT entity_metrics.variant AS variant,+ entity_metrics.breakdown_value_1 AS breakdown_value_1,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant,+ entity_metrics.breakdown_value_1+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentBreakdown.test_mean_metric_with_null_type_defaults_to_event_breakdown[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), if(isNull(nullIf(toString(events.properties.`$browser`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$browser`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$browser`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$browser`), '[]'), '{}'), toString(events.properties.`$browser`)))))), '$$_posthog_breakdown_null_$$'), toTimeZone(events.timestamp, 'UTC')) AS breakdown_value_1,+ argMin(coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$current_url`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$current_url`)), if(isNull(nullIf(toString(events.properties.`$current_url`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$current_url`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$current_url`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$current_url`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$current_url`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$current_url`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$current_url`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$current_url`), '[]'), '{}'), toString(events.properties.`$current_url`)))))), '$$_posthog_breakdown_null_$$'), toTimeZone(events.timestamp, 'UTC')) AS breakdown_value_2+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.amount, 'Float64'), 0) AS value,+ coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), if(isNull(nullIf(toString(events.properties.`$browser`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$browser`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$browser`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$browser`), '[]'), '{}'), toString(events.properties.`$browser`)))))), '$$_posthog_breakdown_null_$$') AS breakdown_value_1,+ coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$current_url`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$current_url`)), if(isNull(nullIf(toString(events.properties.`$current_url`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$current_url`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$current_url`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$current_url`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$current_url`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$current_url`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$current_url`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$current_url`), '[]'), '{}'), toString(events.properties.`$current_url`)))))), '$$_posthog_breakdown_null_$$') AS breakdown_value_2+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value,+ exposures.breakdown_value_1 AS breakdown_value_1,+ exposures.breakdown_value_2 AS breakdown_value_2+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant,+ exposures.breakdown_value_1,+ exposures.breakdown_value_2)+ SELECT entity_metrics.variant AS variant,+ entity_metrics.breakdown_value_1 AS breakdown_value_1,+ entity_metrics.breakdown_value_2 AS breakdown_value_2,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant,+ entity_metrics.breakdown_value_1,+ entity_metrics.breakdown_value_2+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentBreakdown.test_mean_metric_with_person_property_breakdown[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^country)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^country)), if(isNull(nullIf(toString(events.person_properties.country), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.country, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.country), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.country), '[]'), '{}'), toString(events.person_properties.country)))))), '$$_posthog_breakdown_null_$$'), toTimeZone(events.timestamp, 'UTC')) AS breakdown_value_1+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.amount, 'Float64'), 0) AS value,+ coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^country)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^country)), if(isNull(nullIf(toString(events.person_properties.country), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.country, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.country), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.country), '[]'), '{}'), toString(events.person_properties.country)))))), '$$_posthog_breakdown_null_$$') AS breakdown_value_1+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value,+ exposures.breakdown_value_1 AS breakdown_value_1+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant,+ exposures.breakdown_value_1)+ SELECT entity_metrics.variant AS variant,+ entity_metrics.breakdown_value_1 AS breakdown_value_1,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant,+ entity_metrics.breakdown_value_1+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentBreakdown.test_mean_metric_with_two_breakdowns[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), if(isNull(nullIf(toString(events.properties.`$browser`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$browser`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$browser`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$browser`), '[]'), '{}'), toString(events.properties.`$browser`)))))), '$$_posthog_breakdown_null_$$'), toTimeZone(events.timestamp, 'UTC')) AS breakdown_value_1,+ argMin(coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$os`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$os`)), if(isNull(nullIf(toString(events.properties.`$os`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$os`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$os`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$os`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$os`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$os`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$os`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$os`), '[]'), '{}'), toString(events.properties.`$os`)))))), '$$_posthog_breakdown_null_$$'), toTimeZone(events.timestamp, 'UTC')) AS breakdown_value_2+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.amount, 'Float64'), 0) AS value,+ coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), if(isNull(nullIf(toString(events.properties.`$browser`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$browser`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$browser`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$browser`), '[]'), '{}'), toString(events.properties.`$browser`)))))), '$$_posthog_breakdown_null_$$') AS breakdown_value_1,+ coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$os`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$os`)), if(isNull(nullIf(toString(events.properties.`$os`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$os`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$os`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$os`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$os`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$os`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$os`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$os`), '[]'), '{}'), toString(events.properties.`$os`)))))), '$$_posthog_breakdown_null_$$') AS breakdown_value_2+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value,+ exposures.breakdown_value_1 AS breakdown_value_1,+ exposures.breakdown_value_2 AS breakdown_value_2+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant,+ exposures.breakdown_value_1,+ exposures.breakdown_value_2)+ SELECT entity_metrics.variant AS variant,+ entity_metrics.breakdown_value_1 AS breakdown_value_1,+ entity_metrics.breakdown_value_2 AS breakdown_value_2,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant,+ entity_metrics.breakdown_value_1,+ entity_metrics.breakdown_value_2+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentBreakdown.test_mean_metric_with_winsorization_and_breakdown[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), if(isNull(nullIf(toString(events.properties.`$browser`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$browser`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$browser`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$browser`), '[]'), '{}'), toString(events.properties.`$browser`)))))), '$$_posthog_breakdown_null_$$'), toTimeZone(events.timestamp, 'UTC')) AS breakdown_value_1+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.amount, 'Float64'), 0) AS value,+ coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), if(isNull(nullIf(toString(events.properties.`$browser`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$browser`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$browser`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$browser`), '[]'), '{}'), toString(events.properties.`$browser`)))))), '$$_posthog_breakdown_null_$$') AS breakdown_value_1+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value,+ exposures.breakdown_value_1 AS breakdown_value_1+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant,+ exposures.breakdown_value_1),+ percentiles AS+ (SELECT quantileExact(0.05)(entity_metrics.value) AS lower_bound,+ quantileExact(0.95)(entity_metrics.value) AS upper_bound,+ entity_metrics.breakdown_value_1 AS breakdown_value_1+ FROM entity_metrics+ GROUP BY entity_metrics.breakdown_value_1),+ winsorized_entity_metrics AS+ (SELECT entity_metrics.entity_id AS entity_id,+ entity_metrics.variant AS variant,+ least(greatest(percentiles.lower_bound, entity_metrics.value), percentiles.upper_bound) AS value,+ entity_metrics.breakdown_value_1 AS breakdown_value_1+ FROM entity_metrics+ JOIN percentiles ON equals(percentiles.breakdown_value_1, entity_metrics.breakdown_value_1))+ SELECT winsorized_entity_metrics.variant AS variant,+ winsorized_entity_metrics.breakdown_value_1 AS breakdown_value_1,+ count(winsorized_entity_metrics.entity_id) AS num_users,+ sum(winsorized_entity_metrics.value) AS total_sum,+ sum(power(winsorized_entity_metrics.value, 2)) AS total_sum_of_squares+ FROM winsorized_entity_metrics+ GROUP BY winsorized_entity_metrics.variant,+ winsorized_entity_metrics.breakdown_value_1+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentBreakdown.test_missing_variants_across_breakdown_combinations[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), if(isNull(nullIf(toString(events.properties.`$browser`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$browser`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$browser`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$browser`), '[]'), '{}'), toString(events.properties.`$browser`)))))), '$$_posthog_breakdown_null_$$'), toTimeZone(events.timestamp, 'UTC')) AS breakdown_value_1,+ argMin(coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$os`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$os`)), if(isNull(nullIf(toString(events.properties.`$os`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$os`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$os`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$os`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$os`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$os`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$os`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$os`), '[]'), '{}'), toString(events.properties.`$os`)))))), '$$_posthog_breakdown_null_$$'), toTimeZone(events.timestamp, 'UTC')) AS breakdown_value_2+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.amount, 'Float64'), 0) AS value,+ coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), if(isNull(nullIf(toString(events.properties.`$browser`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$browser`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$browser`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$browser`), '[]'), '{}'), toString(events.properties.`$browser`)))))), '$$_posthog_breakdown_null_$$') AS breakdown_value_1,+ coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$os`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$os`)), if(isNull(nullIf(toString(events.properties.`$os`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$os`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$os`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$os`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$os`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$os`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$os`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$os`), '[]'), '{}'), toString(events.properties.`$os`)))))), '$$_posthog_breakdown_null_$$') AS breakdown_value_2+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value,+ exposures.breakdown_value_1 AS breakdown_value_1,+ exposures.breakdown_value_2 AS breakdown_value_2+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant,+ exposures.breakdown_value_1,+ exposures.breakdown_value_2)+ SELECT entity_metrics.variant AS variant,+ entity_metrics.breakdown_value_1 AS breakdown_value_1,+ entity_metrics.breakdown_value_2 AS breakdown_value_2,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant,+ entity_metrics.breakdown_value_1,+ entity_metrics.breakdown_value_2+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentBreakdown.test_person_property_breakdown_attribution_at_exposure[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^country)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^country)), if(isNull(nullIf(toString(events.person_properties.country), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.country, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.country), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.country), '[]'), '{}'), toString(events.person_properties.country)))))), '$$_posthog_breakdown_null_$$'), toTimeZone(events.timestamp, 'UTC')) AS breakdown_value_1+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.amount, 'Float64'), 0) AS value,+ coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^country)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^country)), if(isNull(nullIf(toString(events.person_properties.country), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.country, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.country), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.country), '[]'), '{}'), toString(events.person_properties.country)))))), '$$_posthog_breakdown_null_$$') AS breakdown_value_1+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value,+ exposures.breakdown_value_1 AS breakdown_value_1+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant,+ exposures.breakdown_value_1)+ SELECT entity_metrics.variant AS variant,+ entity_metrics.breakdown_value_1 AS breakdown_value_1,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant,+ entity_metrics.breakdown_value_1+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentBreakdown.test_ratio_metric_with_breakdown[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), if(isNull(nullIf(toString(events.properties.`$browser`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$browser`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$browser`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$browser`), '[]'), '{}'), toString(events.properties.`$browser`)))))), '$$_posthog_breakdown_null_$$'), toTimeZone(events.timestamp, 'UTC')) AS breakdown_value_1+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ numerator_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.amount, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ denominator_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'view_item'))),+ numerator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ sum(coalesce(accurateCastOrNull(numerator_events.value, 'Float64'), 0)) AS value+ FROM numerator_events+ JOIN exposures ON equals(exposures.entity_id, numerator_events.entity_id)+ WHERE greaterOrEquals(numerator_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC'))+ GROUP BY exposures.entity_id),+ denominator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ sum(coalesce(accurateCastOrNull(denominator_events.value, 'Float64'), 0)) AS value+ FROM denominator_events+ JOIN exposures ON equals(exposures.entity_id, denominator_events.entity_id)+ WHERE greaterOrEquals(denominator_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC'))+ GROUP BY exposures.entity_id),+ entity_metrics AS+ (SELECT exposures.variant AS variant,+ exposures.entity_id AS entity_id,+ any(coalesce(numerator_agg.value, 0)) AS numerator_value,+ any(coalesce(denominator_agg.value, 0)) AS denominator_value,+ exposures.breakdown_value_1 AS breakdown_value_1+ FROM exposures+ LEFT JOIN numerator_agg ON equals(exposures.entity_id, numerator_agg.entity_id)+ LEFT JOIN denominator_agg ON equals(exposures.entity_id, denominator_agg.entity_id)+ GROUP BY exposures.variant,+ exposures.entity_id,+ exposures.breakdown_value_1)+ SELECT entity_metrics.variant AS variant,+ entity_metrics.breakdown_value_1 AS breakdown_value_1,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.numerator_value) AS total_sum,+ sum(power(entity_metrics.numerator_value, 2)) AS total_sum_of_squares,+ sum(entity_metrics.denominator_value) AS denominator_sum,+ sum(power(entity_metrics.denominator_value, 2)) AS denominator_sum_squares,+ sum(multiply(entity_metrics.numerator_value, entity_metrics.denominator_value)) AS numerator_denominator_sum_product+ FROM entity_metrics+ GROUP BY entity_metrics.variant,+ entity_metrics.breakdown_value_1+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentBreakdown.test_ratio_metric_with_person_property_breakdown[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^country)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^country)), if(isNull(nullIf(toString(events.person_properties.country), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.country, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.country), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.country), '[]'), '{}'), toString(events.person_properties.country)))))), '$$_posthog_breakdown_null_$$'), toTimeZone(events.timestamp, 'UTC')) AS breakdown_value_1+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ numerator_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ denominator_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, '$pageview'))),+ numerator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ sum(coalesce(accurateCastOrNull(numerator_events.value, 'Float64'), 0)) AS value+ FROM numerator_events+ JOIN exposures ON equals(exposures.entity_id, numerator_events.entity_id)+ WHERE greaterOrEquals(numerator_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC'))+ GROUP BY exposures.entity_id),+ denominator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ sum(coalesce(accurateCastOrNull(denominator_events.value, 'Float64'), 0)) AS value+ FROM denominator_events+ JOIN exposures ON equals(exposures.entity_id, denominator_events.entity_id)+ WHERE greaterOrEquals(denominator_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC'))+ GROUP BY exposures.entity_id),+ entity_metrics AS+ (SELECT exposures.variant AS variant,+ exposures.entity_id AS entity_id,+ any(coalesce(numerator_agg.value, 0)) AS numerator_value,+ any(coalesce(denominator_agg.value, 0)) AS denominator_value,+ exposures.breakdown_value_1 AS breakdown_value_1+ FROM exposures+ LEFT JOIN numerator_agg ON equals(exposures.entity_id, numerator_agg.entity_id)+ LEFT JOIN denominator_agg ON equals(exposures.entity_id, denominator_agg.entity_id)+ GROUP BY exposures.variant,+ exposures.entity_id,+ exposures.breakdown_value_1)+ SELECT entity_metrics.variant AS variant,+ entity_metrics.breakdown_value_1 AS breakdown_value_1,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.numerator_value) AS total_sum,+ sum(power(entity_metrics.numerator_value, 2)) AS total_sum_of_squares,+ sum(entity_metrics.denominator_value) AS denominator_sum,+ sum(power(entity_metrics.denominator_value, 2)) AS denominator_sum_squares,+ sum(multiply(entity_metrics.numerator_value, entity_metrics.denominator_value)) AS numerator_denominator_sum_product+ FROM entity_metrics+ GROUP BY entity_metrics.variant,+ entity_metrics.breakdown_value_1+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentBreakdown.test_ratio_metric_with_two_breakdowns[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), if(isNull(nullIf(toString(events.properties.`$browser`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$browser`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$browser`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$browser`), '[]'), '{}'), toString(events.properties.`$browser`)))))), '$$_posthog_breakdown_null_$$'), toTimeZone(events.timestamp, 'UTC')) AS breakdown_value_1,+ argMin(coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$os`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$os`)), if(isNull(nullIf(toString(events.properties.`$os`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$os`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$os`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$os`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$os`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$os`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$os`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$os`), '[]'), '{}'), toString(events.properties.`$os`)))))), '$$_posthog_breakdown_null_$$'), toTimeZone(events.timestamp, 'UTC')) AS breakdown_value_2+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ numerator_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.amount, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ denominator_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'view_item'))),+ numerator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ sum(coalesce(accurateCastOrNull(numerator_events.value, 'Float64'), 0)) AS value+ FROM numerator_events+ JOIN exposures ON equals(exposures.entity_id, numerator_events.entity_id)+ WHERE greaterOrEquals(numerator_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC'))+ GROUP BY exposures.entity_id),+ denominator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ sum(coalesce(accurateCastOrNull(denominator_events.value, 'Float64'), 0)) AS value+ FROM denominator_events+ JOIN exposures ON equals(exposures.entity_id, denominator_events.entity_id)+ WHERE greaterOrEquals(denominator_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC'))+ GROUP BY exposures.entity_id),+ entity_metrics AS+ (SELECT exposures.variant AS variant,+ exposures.entity_id AS entity_id,+ any(coalesce(numerator_agg.value, 0)) AS numerator_value,+ any(coalesce(denominator_agg.value, 0)) AS denominator_value,+ exposures.breakdown_value_1 AS breakdown_value_1,+ exposures.breakdown_value_2 AS breakdown_value_2+ FROM exposures+ LEFT JOIN numerator_agg ON equals(exposures.entity_id, numerator_agg.entity_id)+ LEFT JOIN denominator_agg ON equals(exposures.entity_id, denominator_agg.entity_id)+ GROUP BY exposures.variant,+ exposures.entity_id,+ exposures.breakdown_value_1,+ exposures.breakdown_value_2)+ SELECT entity_metrics.variant AS variant,+ entity_metrics.breakdown_value_1 AS breakdown_value_1,+ entity_metrics.breakdown_value_2 AS breakdown_value_2,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.numerator_value) AS total_sum,+ sum(power(entity_metrics.numerator_value, 2)) AS total_sum_of_squares,+ sum(entity_metrics.denominator_value) AS denominator_sum,+ sum(power(entity_metrics.denominator_value, 2)) AS denominator_sum_squares,+ sum(multiply(entity_metrics.numerator_value, entity_metrics.denominator_value)) AS numerator_denominator_sum_product+ FROM entity_metrics+ GROUP BY entity_metrics.variant,+ entity_metrics.breakdown_value_1,+ entity_metrics.breakdown_value_2+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentBreakdown.test_retention_metric_with_breakdown[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), if(isNull(nullIf(toString(events.properties.`$browser`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$browser`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$browser`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$browser`), '[]'), '{}'), toString(events.properties.`$browser`)))))), '$$_posthog_breakdown_null_$$'), toTimeZone(events.timestamp, 'UTC')) AS breakdown_value_1+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ start_events AS+ (SELECT exposures.entity_id AS entity_id,+ min(toTimeZone(events.timestamp, 'UTC')) AS start_timestamp,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS start_uuid+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ INNER JOIN exposures ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), exposures.entity_id)+ WHERE and(equals(events.team_id, 99999), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'signup')), greaterOrEquals(events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id),+ completion_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ events.uuid AS completion_uuid,+ toTimeZone(events.timestamp, 'UTC') AS completion_timestamp+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(604800))), equals(events.event, 'login'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ exposures.breakdown_value_1 AS breakdown_value_1,+ max(if(and(isNotNull(toTimeZone(completion_events.completion_timestamp, 'UTC')), greaterOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(1))), lessOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(7)))), 1, 0)) AS value+ FROM exposures+ INNER JOIN start_events ON equals(exposures.entity_id, start_events.entity_id)+ LEFT JOIN completion_events ON and(equals(exposures.entity_id, completion_events.entity_id), and(greaterOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), toTimeZone(start_events.start_timestamp, 'UTC')), lessOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), plus(toTimeZone(start_events.start_timestamp, 'UTC'), toIntervalSecond(691200)))), notEquals(completion_events.completion_uuid, start_events.start_uuid))+ GROUP BY exposures.entity_id,+ exposures.variant,+ exposures.breakdown_value_1)+ SELECT entity_metrics.variant AS variant,+ entity_metrics.breakdown_value_1 AS breakdown_value_1,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares,+ count(entity_metrics.entity_id) AS denominator_sum,+ count(entity_metrics.entity_id) AS denominator_sum_squares,+ sum(entity_metrics.value) AS numerator_denominator_sum_product+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant,+ entity_metrics.breakdown_value_1+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentBreakdown.test_retention_metric_with_person_property_breakdown[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^country)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^country)), if(isNull(nullIf(toString(events.person_properties.country), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.country, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.country), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.country, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.country), '[]'), '{}'), toString(events.person_properties.country)))))), '$$_posthog_breakdown_null_$$'), toTimeZone(events.timestamp, 'UTC')) AS breakdown_value_1+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ start_events AS+ (SELECT exposures.entity_id AS entity_id,+ min(toTimeZone(events.timestamp, 'UTC')) AS start_timestamp,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS start_uuid+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ INNER JOIN exposures ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), exposures.entity_id)+ WHERE and(equals(events.team_id, 99999), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'signup')), greaterOrEquals(events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id),+ completion_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ events.uuid AS completion_uuid,+ toTimeZone(events.timestamp, 'UTC') AS completion_timestamp+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(604800))), equals(events.event, 'login'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ exposures.breakdown_value_1 AS breakdown_value_1,+ max(if(and(isNotNull(toTimeZone(completion_events.completion_timestamp, 'UTC')), greaterOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(1))), lessOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(7)))), 1, 0)) AS value+ FROM exposures+ INNER JOIN start_events ON equals(exposures.entity_id, start_events.entity_id)+ LEFT JOIN completion_events ON and(equals(exposures.entity_id, completion_events.entity_id), and(greaterOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), toTimeZone(start_events.start_timestamp, 'UTC')), lessOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), plus(toTimeZone(start_events.start_timestamp, 'UTC'), toIntervalSecond(691200)))), notEquals(completion_events.completion_uuid, start_events.start_uuid))+ GROUP BY exposures.entity_id,+ exposures.variant,+ exposures.breakdown_value_1)+ SELECT entity_metrics.variant AS variant,+ entity_metrics.breakdown_value_1 AS breakdown_value_1,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares,+ count(entity_metrics.entity_id) AS denominator_sum,+ count(entity_metrics.entity_id) AS denominator_sum_squares,+ sum(entity_metrics.value) AS numerator_denominator_sum_product+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant,+ entity_metrics.breakdown_value_1+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentBreakdown.test_retention_metric_with_two_breakdowns[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), if(isNull(nullIf(toString(events.properties.`$browser`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$browser`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$browser`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$browser`), '[]'), '{}'), toString(events.properties.`$browser`)))))), '$$_posthog_breakdown_null_$$'), toTimeZone(events.timestamp, 'UTC')) AS breakdown_value_1,+ argMin(coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$os`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$os`)), if(isNull(nullIf(toString(events.properties.`$os`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$os`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$os`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$os`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$os`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$os`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$os`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$os`), '[]'), '{}'), toString(events.properties.`$os`)))))), '$$_posthog_breakdown_null_$$'), toTimeZone(events.timestamp, 'UTC')) AS breakdown_value_2+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ start_events AS+ (SELECT exposures.entity_id AS entity_id,+ min(toTimeZone(events.timestamp, 'UTC')) AS start_timestamp,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS start_uuid+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ INNER JOIN exposures ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), exposures.entity_id)+ WHERE and(equals(events.team_id, 99999), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'signup')), greaterOrEquals(events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id),+ completion_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ events.uuid AS completion_uuid,+ toTimeZone(events.timestamp, 'UTC') AS completion_timestamp+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(604800))), equals(events.event, 'login'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ exposures.breakdown_value_1 AS breakdown_value_1,+ exposures.breakdown_value_2 AS breakdown_value_2,+ max(if(and(isNotNull(toTimeZone(completion_events.completion_timestamp, 'UTC')), greaterOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(1))), lessOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(7)))), 1, 0)) AS value+ FROM exposures+ INNER JOIN start_events ON equals(exposures.entity_id, start_events.entity_id)+ LEFT JOIN completion_events ON and(equals(exposures.entity_id, completion_events.entity_id), and(greaterOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), toTimeZone(start_events.start_timestamp, 'UTC')), lessOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), plus(toTimeZone(start_events.start_timestamp, 'UTC'), toIntervalSecond(691200)))), notEquals(completion_events.completion_uuid, start_events.start_uuid))+ GROUP BY exposures.entity_id,+ exposures.variant,+ exposures.breakdown_value_1,+ exposures.breakdown_value_2)+ SELECT entity_metrics.variant AS variant,+ entity_metrics.breakdown_value_1 AS breakdown_value_1,+ entity_metrics.breakdown_value_2 AS breakdown_value_2,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares,+ count(entity_metrics.entity_id) AS denominator_sum,+ count(entity_metrics.entity_id) AS denominator_sum_squares,+ sum(entity_metrics.value) AS numerator_denominator_sum_product+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant,+ entity_metrics.breakdown_value_1,+ entity_metrics.breakdown_value_2+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---diff --git a/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_data_warehouse_metric.new_events_schema.ambr b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_data_warehouse_metric.new_events_schema.ambrnew file mode 100644index 000000000000..98ae3b0cb10a--- /dev/null+++ b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_data_warehouse_metric.new_events_schema.ambr@@ -0,0 +1,1556 @@+# serializer version: 1+# name: TestExperimentQueryRunner.test_data_warehouse_metric_skips_precomputation_0_mean_metric[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), if(isNull(nullIf(toString(events.properties.`$user_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$user_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$user_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$user_id`), '[]'), '{}'), toString(events.properties.`$user_id`))))), toTimeZone(events.timestamp, 'UTC')) AS exposure_identifier+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT posthog_test_usage.userid AS entity_id,+ posthog_test_usage.ds AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_usage/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`ds` Date, `id` String, `plan` String, `usage` Float64, `region` String, `userid` String') AS posthog_test_usage+ WHERE and(greaterOrEquals(posthog_test_usage.ds, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(posthog_test_usage.ds, plus(assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC')), toIntervalSecond(0))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(toString(exposures.exposure_identifier), toString(metric_events.entity_id)), greaterOrEquals(metric_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ format_csv_allow_double_quotes=1,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_data_warehouse_metric_skips_precomputation_1_ratio_metric[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), if(isNull(nullIf(toString(events.properties.`$user_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$user_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$user_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$user_id`), '[]'), '{}'), toString(events.properties.`$user_id`))))), toTimeZone(events.timestamp, 'UTC')) AS exposure_identifier_num,+ argMin(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email))))), toTimeZone(events.timestamp, 'UTC')) AS exposure_identifier_denom+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-10 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ numerator_events AS+ (SELECT posthog_test_usage.userid AS entity_id,+ posthog_test_usage.ds AS timestamp,+ coalesce(accurateCastOrNull(posthog_test_usage.usage, 'Float64'), 0) AS value+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_usage/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`ds` Date, `id` String, `plan` String, `usage` Float64, `region` String, `userid` String') AS posthog_test_usage+ WHERE and(greaterOrEquals(posthog_test_usage.ds, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(posthog_test_usage.ds, plus(assumeNotNull(toDateTime('2023-01-10 00:00:00', 'UTC')), toIntervalSecond(0))))),+ denominator_events AS+ (SELECT posthog_test_stripe_subscriptions__subscription_customer.customer_email AS entity_id,+ toTimeZone(posthog_test_stripe_subscriptions.subscription_created_at, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM+ (SELECT *+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_stripe_subscriptions/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`subscription_id` String, `subscription_amount` Int64, `subscription_created_at` DateTime, `subscription_customer_id` String')) AS posthog_test_stripe_subscriptions+ LEFT JOIN+ (SELECT posthog_test_stripe_customers.customer_email AS customer_email,+ posthog_test_stripe_customers.customer_id AS posthog_test_stripe_subscriptions__subscription_customer___customer_id+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_stripe_customers/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`customer_id` String, `signup_count` Int32, `customer_name` String, `customer_email` String, `customer_created_at` DateTime') AS posthog_test_stripe_customers) AS posthog_test_stripe_subscriptions__subscription_customer ON equals(posthog_test_stripe_subscriptions.subscription_customer_id, posthog_test_stripe_subscriptions__subscription_customer.posthog_test_stripe_subscriptions__subscription_customer___customer_id)+ WHERE and(greaterOrEquals(posthog_test_stripe_subscriptions.subscription_created_at, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(posthog_test_stripe_subscriptions.subscription_created_at, plus(assumeNotNull(toDateTime('2023-01-10 00:00:00', 'UTC')), toIntervalSecond(0))))),+ numerator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ sum(coalesce(accurateCastOrNull(numerator_events.value, 'Float64'), 0)) AS value+ FROM numerator_events+ JOIN exposures ON equals(toString(exposures.exposure_identifier_num), toString(numerator_events.entity_id))+ WHERE greaterOrEquals(numerator_events.timestamp, exposures.first_exposure_time)+ GROUP BY exposures.entity_id),+ denominator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ sum(coalesce(accurateCastOrNull(denominator_events.value, 'Float64'), 0)) AS value+ FROM denominator_events+ JOIN exposures ON equals(toString(exposures.exposure_identifier_denom), toString(denominator_events.entity_id))+ WHERE greaterOrEquals(denominator_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC'))+ GROUP BY exposures.entity_id),+ entity_metrics AS+ (SELECT exposures.variant AS variant,+ exposures.entity_id AS entity_id,+ any(coalesce(numerator_agg.value, 0)) AS numerator_value,+ any(coalesce(denominator_agg.value, 0)) AS denominator_value+ FROM exposures+ LEFT JOIN numerator_agg ON equals(exposures.entity_id, numerator_agg.entity_id)+ LEFT JOIN denominator_agg ON equals(exposures.entity_id, denominator_agg.entity_id)+ GROUP BY exposures.variant,+ exposures.entity_id)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.numerator_value) AS total_sum,+ sum(power(entity_metrics.numerator_value, 2)) AS total_sum_of_squares,+ sum(entity_metrics.denominator_value) AS denominator_sum,+ sum(power(entity_metrics.denominator_value, 2)) AS denominator_sum_squares,+ sum(multiply(entity_metrics.numerator_value, entity_metrics.denominator_value)) AS numerator_denominator_sum_product+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS format_csv_allow_double_quotes=1,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_data_warehouse_continuous_metric[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), if(isNull(nullIf(toString(events.properties.`$user_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$user_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$user_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$user_id`), '[]'), '{}'), toString(events.properties.`$user_id`))))), toTimeZone(events.timestamp, 'UTC')) AS exposure_identifier+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT posthog_test_usage.userid AS entity_id,+ posthog_test_usage.ds AS timestamp,+ coalesce(accurateCastOrNull(posthog_test_usage.usage, 'Float64'), 0) AS value+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_usage/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`ds` Date, `id` String, `plan` String, `usage` Float64, `region` String, `userid` String') AS posthog_test_usage+ WHERE and(greaterOrEquals(posthog_test_usage.ds, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(posthog_test_usage.ds, plus(assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC')), toIntervalSecond(0))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(toString(exposures.exposure_identifier), toString(metric_events.entity_id)), greaterOrEquals(metric_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ format_csv_allow_double_quotes=1,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_data_warehouse_count_metric[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), if(isNull(nullIf(toString(events.properties.`$user_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$user_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$user_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$user_id`), '[]'), '{}'), toString(events.properties.`$user_id`))))), toTimeZone(events.timestamp, 'UTC')) AS exposure_identifier+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT posthog_test_usage.userid AS entity_id,+ posthog_test_usage.ds AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_usage/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`ds` Date, `id` String, `plan` String, `usage` Float64, `region` String, `userid` String') AS posthog_test_usage+ WHERE and(greaterOrEquals(posthog_test_usage.ds, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(posthog_test_usage.ds, plus(assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC')), toIntervalSecond(0))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(toString(exposures.exposure_identifier), toString(metric_events.entity_id)), greaterOrEquals(metric_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ format_csv_allow_double_quotes=1,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_data_warehouse_metric_with_fixed_properties[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), if(isNull(nullIf(toString(events.properties.`$user_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$user_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$user_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$user_id`), '[]'), '{}'), toString(events.properties.`$user_id`))))), toTimeZone(events.timestamp, 'UTC')) AS exposure_identifier+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT posthog_test_usage.userid AS entity_id,+ posthog_test_usage.ds AS timestamp,+ coalesce(accurateCastOrNull(posthog_test_usage.usage, 'Float64'), 0) AS value+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_usage/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`ds` Date, `id` String, `plan` String, `usage` Float64, `region` String, `userid` String') AS posthog_test_usage+ WHERE and(greaterOrEquals(posthog_test_usage.ds, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(posthog_test_usage.ds, plus(assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC')), toIntervalSecond(0))), equals(posthog_test_usage.plan, 'premium'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(toString(exposures.exposure_identifier), toString(metric_events.entity_id)), greaterOrEquals(metric_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ format_csv_allow_double_quotes=1,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_data_warehouse_metric_with_property_filters_0_single_property_filter[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), if(isNull(nullIf(toString(events.properties.`$user_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$user_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$user_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$user_id`), '[]'), '{}'), toString(events.properties.`$user_id`))))), toTimeZone(events.timestamp, 'UTC')) AS exposure_identifier+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT posthog_test_usage.userid AS entity_id,+ posthog_test_usage.ds AS timestamp,+ coalesce(accurateCastOrNull(posthog_test_usage.usage, 'Float64'), 0) AS value+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_usage/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`ds` Date, `id` String, `plan` String, `usage` Float64, `region` String, `userid` String') AS posthog_test_usage+ WHERE and(greaterOrEquals(posthog_test_usage.ds, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(posthog_test_usage.ds, plus(assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC')), toIntervalSecond(0))), equals(posthog_test_usage.plan, 'premium'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(toString(exposures.exposure_identifier), toString(metric_events.entity_id)), greaterOrEquals(metric_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ format_csv_allow_double_quotes=1,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_data_warehouse_metric_with_property_filters_1_multiple_property_filters[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), if(isNull(nullIf(toString(events.properties.`$user_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$user_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$user_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$user_id`), '[]'), '{}'), toString(events.properties.`$user_id`))))), toTimeZone(events.timestamp, 'UTC')) AS exposure_identifier+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT posthog_test_usage.userid AS entity_id,+ posthog_test_usage.ds AS timestamp,+ coalesce(accurateCastOrNull(posthog_test_usage.usage, 'Float64'), 0) AS value+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_usage/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`ds` Date, `id` String, `plan` String, `usage` Float64, `region` String, `userid` String') AS posthog_test_usage+ WHERE and(greaterOrEquals(posthog_test_usage.ds, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(posthog_test_usage.ds, plus(assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC')), toIntervalSecond(0))), and(equals(posthog_test_usage.plan, 'premium'), equals(posthog_test_usage.region, 'us-west')))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(toString(exposures.exposure_identifier), toString(metric_events.entity_id)), greaterOrEquals(metric_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ format_csv_allow_double_quotes=1,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_data_warehouse_metric_with_property_filters_2_numeric_property_filter[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), if(isNull(nullIf(toString(events.properties.`$user_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$user_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$user_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$user_id`), '[]'), '{}'), toString(events.properties.`$user_id`))))), toTimeZone(events.timestamp, 'UTC')) AS exposure_identifier+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT posthog_test_usage.userid AS entity_id,+ posthog_test_usage.ds AS timestamp,+ coalesce(accurateCastOrNull(posthog_test_usage.usage, 'Float64'), 0) AS value+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_usage/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`ds` Date, `id` String, `plan` String, `usage` Float64, `region` String, `userid` String') AS posthog_test_usage+ WHERE and(greaterOrEquals(posthog_test_usage.ds, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(posthog_test_usage.ds, plus(assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC')), toIntervalSecond(0))), greater(posthog_test_usage.usage, 100.0))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(toString(exposures.exposure_identifier), toString(metric_events.entity_id)), greaterOrEquals(metric_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ format_csv_allow_double_quotes=1,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_data_warehouse_metric_with_property_filters_3_mixed_property_filters[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), if(isNull(nullIf(toString(events.properties.`$user_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$user_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$user_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$user_id`), '[]'), '{}'), toString(events.properties.`$user_id`))))), toTimeZone(events.timestamp, 'UTC')) AS exposure_identifier+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT posthog_test_usage.userid AS entity_id,+ posthog_test_usage.ds AS timestamp,+ coalesce(accurateCastOrNull(posthog_test_usage.usage, 'Float64'), 0) AS value+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_usage/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`ds` Date, `id` String, `plan` String, `usage` Float64, `region` String, `userid` String') AS posthog_test_usage+ WHERE and(greaterOrEquals(posthog_test_usage.ds, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(posthog_test_usage.ds, plus(assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC')), toIntervalSecond(0))), and(equals(posthog_test_usage.plan, 'premium'), greater(posthog_test_usage.usage, 50.0)))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(toString(exposures.exposure_identifier), toString(metric_events.entity_id)), greaterOrEquals(metric_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ format_csv_allow_double_quotes=1,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_group_aggregation_data_warehouse_mean_metric[new_events_schema]+ '''+ WITH exposures AS+ (SELECT events.properties.`$group_0` AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(events.distinct_id, toTimeZone(events.timestamp, 'UTC')) AS exposure_identifier+ FROM events_json AS events+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']), notEmpty(events.properties.`$group_0`))+ GROUP BY entity_id),+ metric_events AS+ (SELECT posthog_test_usage.userid AS entity_id,+ posthog_test_usage.ds AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_usage/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`ds` Date, `id` String, `plan` String, `usage` Float64, `region` String, `userid` String') AS posthog_test_usage+ WHERE and(greaterOrEquals(posthog_test_usage.ds, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(posthog_test_usage.ds, plus(assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC')), toIntervalSecond(0))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(toString(exposures.exposure_identifier), toString(metric_events.entity_id)), greaterOrEquals(metric_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ format_csv_allow_double_quotes=1,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_data_warehouse_internal_filters_0_person_properties[new_events_schema.1]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), if(isNull(nullIf(toString(events.properties.`$user_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$user_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$user_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$user_id`), '[]'), '{}'), toString(events.properties.`$user_id`))))), toTimeZone(events.timestamp, 'UTC')) AS exposure_identifier+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT posthog_test_usage.userid AS entity_id,+ posthog_test_usage.ds AS timestamp,+ coalesce(accurateCastOrNull(posthog_test_usage.usage, 'Float64'), 0) AS value+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_usage/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`ds` Date, `id` String, `plan` String, `usage` Float64, `region` String, `userid` String') AS posthog_test_usage+ WHERE and(greaterOrEquals(posthog_test_usage.ds, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(posthog_test_usage.ds, plus(assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC')), toIntervalSecond(0))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(toString(exposures.exposure_identifier), toString(metric_events.entity_id)), greaterOrEquals(metric_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ format_csv_allow_double_quotes=1,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_data_warehouse_internal_filters_0_person_properties[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), if(isNull(nullIf(toString(events.properties.`$user_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$user_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$user_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$user_id`), '[]'), '{}'), toString(events.properties.`$user_id`))))), toTimeZone(events.timestamp, 'UTC')) AS exposure_identifier+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT posthog_test_usage.userid AS entity_id,+ posthog_test_usage.ds AS timestamp,+ coalesce(accurateCastOrNull(posthog_test_usage.usage, 'Float64'), 0) AS value+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_usage/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`ds` Date, `id` String, `plan` String, `usage` Float64, `region` String, `userid` String') AS posthog_test_usage+ WHERE and(greaterOrEquals(posthog_test_usage.ds, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(posthog_test_usage.ds, plus(assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC')), toIntervalSecond(0))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(toString(exposures.exposure_identifier), toString(metric_events.entity_id)), greaterOrEquals(metric_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ format_csv_allow_double_quotes=1,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_data_warehouse_internal_filters_1_event_properties[new_events_schema.1]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), if(isNull(nullIf(toString(events.properties.`$user_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$user_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$user_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$user_id`), '[]'), '{}'), toString(events.properties.`$user_id`))))), toTimeZone(events.timestamp, 'UTC')) AS exposure_identifier+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT posthog_test_usage.userid AS entity_id,+ posthog_test_usage.ds AS timestamp,+ coalesce(accurateCastOrNull(posthog_test_usage.usage, 'Float64'), 0) AS value+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_usage/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`ds` Date, `id` String, `plan` String, `usage` Float64, `region` String, `userid` String') AS posthog_test_usage+ WHERE and(greaterOrEquals(posthog_test_usage.ds, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(posthog_test_usage.ds, plus(assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC')), toIntervalSecond(0))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(toString(exposures.exposure_identifier), toString(metric_events.entity_id)), greaterOrEquals(metric_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ format_csv_allow_double_quotes=1,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_data_warehouse_internal_filters_1_event_properties[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), if(isNull(nullIf(toString(events.properties.`$user_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$user_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$user_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$user_id`), '[]'), '{}'), toString(events.properties.`$user_id`))))), toTimeZone(events.timestamp, 'UTC')) AS exposure_identifier+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(not(match(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$host`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$host`)), if(isNull(nullIf(toString(events.properties.`$host`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$host`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$host`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$host`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$host`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$host`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$host`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$host`), '[]'), '{}'), toString(events.properties.`$host`)))))), '^(localhost|127\\.0\\.0\\.1)($|:)')), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT posthog_test_usage.userid AS entity_id,+ posthog_test_usage.ds AS timestamp,+ coalesce(accurateCastOrNull(posthog_test_usage.usage, 'Float64'), 0) AS value+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_usage/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`ds` Date, `id` String, `plan` String, `usage` Float64, `region` String, `userid` String') AS posthog_test_usage+ WHERE and(greaterOrEquals(posthog_test_usage.ds, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(posthog_test_usage.ds, plus(assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC')), toIntervalSecond(0))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(toString(exposures.exposure_identifier), toString(metric_events.entity_id)), greaterOrEquals(metric_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ format_csv_allow_double_quotes=1,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_data_warehouse_internal_filters_2_feature_flags[new_events_schema.1]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), if(isNull(nullIf(toString(events.properties.`$user_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$user_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$user_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$user_id`), '[]'), '{}'), toString(events.properties.`$user_id`))))), toTimeZone(events.timestamp, 'UTC')) AS exposure_identifier+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT posthog_test_usage.userid AS entity_id,+ posthog_test_usage.ds AS timestamp,+ coalesce(accurateCastOrNull(posthog_test_usage.usage, 'Float64'), 0) AS value+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_usage/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`ds` Date, `id` String, `plan` String, `usage` Float64, `region` String, `userid` String') AS posthog_test_usage+ WHERE and(greaterOrEquals(posthog_test_usage.ds, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(posthog_test_usage.ds, plus(assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC')), toIntervalSecond(0))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(toString(exposures.exposure_identifier), toString(metric_events.entity_id)), greaterOrEquals(metric_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ format_csv_allow_double_quotes=1,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_data_warehouse_internal_filters_2_feature_flags[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), if(isNull(nullIf(toString(events.properties.`$user_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$user_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$user_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$user_id`), '[]'), '{}'), toString(events.properties.`$user_id`))))), toTimeZone(events.timestamp, 'UTC')) AS exposure_identifier+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(has(events.properties.`$feature_flags`, 'flag_doesnt_exist'), if(ifNull(equals(events.properties.`$feature_flags`['flag_doesnt_exist'], '$false'), 0), 'false', events.properties.`$feature_flags`['flag_doesnt_exist']), NULL), tuple('test', 'control')), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT posthog_test_usage.userid AS entity_id,+ posthog_test_usage.ds AS timestamp,+ coalesce(accurateCastOrNull(posthog_test_usage.usage, 'Float64'), 0) AS value+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_usage/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`ds` Date, `id` String, `plan` String, `usage` Float64, `region` String, `userid` String') AS posthog_test_usage+ WHERE and(greaterOrEquals(posthog_test_usage.ds, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(posthog_test_usage.ds, plus(assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC')), toIntervalSecond(0))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(toString(exposures.exposure_identifier), toString(metric_events.entity_id)), greaterOrEquals(metric_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ format_csv_allow_double_quotes=1,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_data_warehouse_internal_filters_3_cohort_static[new_events_schema.1]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), if(isNull(nullIf(toString(events.properties.`$user_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$user_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$user_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$user_id`), '[]'), '{}'), toString(events.properties.`$user_id`))))), toTimeZone(events.timestamp, 'UTC')) AS exposure_identifier+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT posthog_test_usage.userid AS entity_id,+ posthog_test_usage.ds AS timestamp,+ coalesce(accurateCastOrNull(posthog_test_usage.usage, 'Float64'), 0) AS value+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_usage/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`ds` Date, `id` String, `plan` String, `usage` Float64, `region` String, `userid` String') AS posthog_test_usage+ WHERE and(greaterOrEquals(posthog_test_usage.ds, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(posthog_test_usage.ds, plus(assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC')), toIntervalSecond(0))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(toString(exposures.exposure_identifier), toString(metric_events.entity_id)), greaterOrEquals(metric_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ format_csv_allow_double_quotes=1,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_data_warehouse_internal_filters_3_cohort_static[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), if(isNull(nullIf(toString(events.properties.`$user_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$user_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$user_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$user_id`), '[]'), '{}'), toString(events.properties.`$user_id`))))), toTimeZone(events.timestamp, 'UTC')) AS exposure_identifier+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id),+ (SELECT person_static_cohort.person_id AS person_id+ FROM person_static_cohort+ WHERE and(equals(person_static_cohort.team_id, 99999), equals(person_static_cohort.cohort_id, 99999)))), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT posthog_test_usage.userid AS entity_id,+ posthog_test_usage.ds AS timestamp,+ coalesce(accurateCastOrNull(posthog_test_usage.usage, 'Float64'), 0) AS value+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_usage/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`ds` Date, `id` String, `plan` String, `usage` Float64, `region` String, `userid` String') AS posthog_test_usage+ WHERE and(greaterOrEquals(posthog_test_usage.ds, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(posthog_test_usage.ds, plus(assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC')), toIntervalSecond(0))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(toString(exposures.exposure_identifier), toString(metric_events.entity_id)), greaterOrEquals(metric_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ format_csv_allow_double_quotes=1,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_data_warehouse_internal_filters_4_cohort_dynamic[new_events_schema.1]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), if(isNull(nullIf(toString(events.properties.`$user_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$user_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$user_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$user_id`), '[]'), '{}'), toString(events.properties.`$user_id`))))), toTimeZone(events.timestamp, 'UTC')) AS exposure_identifier+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT posthog_test_usage.userid AS entity_id,+ posthog_test_usage.ds AS timestamp,+ coalesce(accurateCastOrNull(posthog_test_usage.usage, 'Float64'), 0) AS value+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_usage/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`ds` Date, `id` String, `plan` String, `usage` Float64, `region` String, `userid` String') AS posthog_test_usage+ WHERE and(greaterOrEquals(posthog_test_usage.ds, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(posthog_test_usage.ds, plus(assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC')), toIntervalSecond(0))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(toString(exposures.exposure_identifier), toString(metric_events.entity_id)), greaterOrEquals(metric_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ format_csv_allow_double_quotes=1,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_data_warehouse_internal_filters_4_cohort_dynamic[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), if(isNull(nullIf(toString(events.properties.`$user_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$user_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$user_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$user_id`), '[]'), '{}'), toString(events.properties.`$user_id`))))), toTimeZone(events.timestamp, 'UTC')) AS exposure_identifier+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id),+ (SELECT cohortpeople.person_id AS person_id+ FROM cohortpeople+ WHERE and(equals(cohortpeople.team_id, 99999), equals(cohortpeople.cohort_id, 99999), equals(cohortpeople.version, 0)))), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT posthog_test_usage.userid AS entity_id,+ posthog_test_usage.ds AS timestamp,+ coalesce(accurateCastOrNull(posthog_test_usage.usage, 'Float64'), 0) AS value+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_usage/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`ds` Date, `id` String, `plan` String, `usage` Float64, `region` String, `userid` String') AS posthog_test_usage+ WHERE and(greaterOrEquals(posthog_test_usage.ds, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(posthog_test_usage.ds, plus(assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC')), toIntervalSecond(0))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(toString(exposures.exposure_identifier), toString(metric_events.entity_id)), greaterOrEquals(metric_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ format_csv_allow_double_quotes=1,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_data_warehouse_internal_filters_5_group[new_events_schema.1]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), if(isNull(nullIf(toString(events.properties.`$user_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$user_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$user_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$user_id`), '[]'), '{}'), toString(events.properties.`$user_id`))))), toTimeZone(events.timestamp, 'UTC')) AS exposure_identifier+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT posthog_test_usage.userid AS entity_id,+ posthog_test_usage.ds AS timestamp,+ coalesce(accurateCastOrNull(posthog_test_usage.usage, 'Float64'), 0) AS value+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_usage/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`ds` Date, `id` String, `plan` String, `usage` Float64, `region` String, `userid` String') AS posthog_test_usage+ WHERE and(greaterOrEquals(posthog_test_usage.ds, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(posthog_test_usage.ds, plus(assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC')), toIntervalSecond(0))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(toString(exposures.exposure_identifier), toString(metric_events.entity_id)), greaterOrEquals(metric_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ format_csv_allow_double_quotes=1,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_data_warehouse_internal_filters_5_group[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), if(isNull(nullIf(toString(events.properties.`$user_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$user_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$user_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$user_id`), '[]'), '{}'), toString(events.properties.`$user_id`))))), toTimeZone(events.timestamp, 'UTC')) AS exposure_identifier+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ LEFT JOIN+ (SELECT tupleElement(argMax(tuple(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(groups.group_properties, 'name'), ''), 'null'), '^"|"$', '')), toTimeZone(groups._timestamp, 'UTC')), 1) AS properties___name,+ groups.group_type_index AS index,+ groups.group_key AS key+ FROM groups+ WHERE and(equals(groups.team_id, 99999), equals(index, 0))+ GROUP BY groups.group_type_index,+ groups.group_key) AS events__group_0 ON equals(events.properties.`$group_0`, events__group_0.key)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(equals(events__group_0.properties___name, 'Test Group'), 0), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT posthog_test_usage.userid AS entity_id,+ posthog_test_usage.ds AS timestamp,+ coalesce(accurateCastOrNull(posthog_test_usage.usage, 'Float64'), 0) AS value+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_usage/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`ds` Date, `id` String, `plan` String, `usage` Float64, `region` String, `userid` String') AS posthog_test_usage+ WHERE and(greaterOrEquals(posthog_test_usage.ds, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(posthog_test_usage.ds, plus(assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC')), toIntervalSecond(0))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(toString(exposures.exposure_identifier), toString(metric_events.entity_id)), greaterOrEquals(metric_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ format_csv_allow_double_quotes=1,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_data_warehouse_internal_filters_6_element[new_events_schema.1]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), if(isNull(nullIf(toString(events.properties.`$user_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$user_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$user_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$user_id`), '[]'), '{}'), toString(events.properties.`$user_id`))))), toTimeZone(events.timestamp, 'UTC')) AS exposure_identifier+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT posthog_test_usage.userid AS entity_id,+ posthog_test_usage.ds AS timestamp,+ coalesce(accurateCastOrNull(posthog_test_usage.usage, 'Float64'), 0) AS value+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_usage/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`ds` Date, `id` String, `plan` String, `usage` Float64, `region` String, `userid` String') AS posthog_test_usage+ WHERE and(greaterOrEquals(posthog_test_usage.ds, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(posthog_test_usage.ds, plus(assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC')), toIntervalSecond(0))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(toString(exposures.exposure_identifier), toString(metric_events.entity_id)), greaterOrEquals(metric_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ format_csv_allow_double_quotes=1,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_data_warehouse_internal_filters_6_element[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), if(isNull(nullIf(toString(events.properties.`$user_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$user_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$user_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$user_id`), '[]'), '{}'), toString(events.properties.`$user_id`))))), toTimeZone(events.timestamp, 'UTC')) AS exposure_identifier+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), match(events.elements_chain, '(^|;)button(\\.|$|;|:)'), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT posthog_test_usage.userid AS entity_id,+ posthog_test_usage.ds AS timestamp,+ coalesce(accurateCastOrNull(posthog_test_usage.usage, 'Float64'), 0) AS value+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_usage/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`ds` Date, `id` String, `plan` String, `usage` Float64, `region` String, `userid` String') AS posthog_test_usage+ WHERE and(greaterOrEquals(posthog_test_usage.ds, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(posthog_test_usage.ds, plus(assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC')), toIntervalSecond(0))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(toString(exposures.exposure_identifier), toString(metric_events.entity_id)), greaterOrEquals(metric_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ format_csv_allow_double_quotes=1,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_query_runner_with_data_warehouse_subscriptions_table[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email))))), toTimeZone(events.timestamp, 'UTC')) AS exposure_identifier+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-10 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT posthog_test_stripe_subscriptions__subscription_customer.customer_email AS entity_id,+ toTimeZone(posthog_test_stripe_subscriptions.subscription_created_at, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM+ (SELECT *+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_stripe_subscriptions/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`subscription_id` String, `subscription_amount` Int64, `subscription_created_at` DateTime, `subscription_customer_id` String')) AS posthog_test_stripe_subscriptions+ LEFT JOIN+ (SELECT posthog_test_stripe_customers.customer_email AS customer_email,+ posthog_test_stripe_customers.customer_id AS posthog_test_stripe_subscriptions__subscription_customer___customer_id+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_stripe_customers/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`customer_id` String, `signup_count` Int32, `customer_name` String, `customer_email` String, `customer_created_at` DateTime') AS posthog_test_stripe_customers) AS posthog_test_stripe_subscriptions__subscription_customer ON equals(posthog_test_stripe_subscriptions.subscription_customer_id, posthog_test_stripe_subscriptions__subscription_customer.posthog_test_stripe_subscriptions__subscription_customer___customer_id)+ WHERE and(greaterOrEquals(posthog_test_stripe_subscriptions.subscription_created_at, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(posthog_test_stripe_subscriptions.subscription_created_at, plus(assumeNotNull(toDateTime('2023-01-10 00:00:00', 'UTC')), toIntervalSecond(0))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(toString(exposures.exposure_identifier), toString(metric_events.entity_id)), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ format_csv_allow_double_quotes=1,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentQueryRunner.test_ratio_metric_with_different_join_keys[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), if(isNull(nullIf(toString(events.properties.`$user_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$user_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$user_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$user_id`), '[]'), '{}'), toString(events.properties.`$user_id`))))), toTimeZone(events.timestamp, 'UTC')) AS exposure_identifier_num,+ argMin(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email))))), toTimeZone(events.timestamp, 'UTC')) AS exposure_identifier_denom+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-10 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ numerator_events AS+ (SELECT posthog_test_usage.userid AS entity_id,+ posthog_test_usage.ds AS timestamp,+ coalesce(accurateCastOrNull(posthog_test_usage.usage, 'Float64'), 0) AS value+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_usage/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`ds` Date, `id` String, `plan` String, `usage` Float64, `region` String, `userid` String') AS posthog_test_usage+ WHERE and(greaterOrEquals(posthog_test_usage.ds, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(posthog_test_usage.ds, plus(assumeNotNull(toDateTime('2023-01-10 00:00:00', 'UTC')), toIntervalSecond(0))))),+ denominator_events AS+ (SELECT posthog_test_stripe_subscriptions__subscription_customer.customer_email AS entity_id,+ toTimeZone(posthog_test_stripe_subscriptions.subscription_created_at, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM+ (SELECT *+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_stripe_subscriptions/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`subscription_id` String, `subscription_amount` Int64, `subscription_created_at` DateTime, `subscription_customer_id` String')) AS posthog_test_stripe_subscriptions+ LEFT JOIN+ (SELECT posthog_test_stripe_customers.customer_email AS customer_email,+ posthog_test_stripe_customers.customer_id AS posthog_test_stripe_subscriptions__subscription_customer___customer_id+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_stripe_customers/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`customer_id` String, `signup_count` Int32, `customer_name` String, `customer_email` String, `customer_created_at` DateTime') AS posthog_test_stripe_customers) AS posthog_test_stripe_subscriptions__subscription_customer ON equals(posthog_test_stripe_subscriptions.subscription_customer_id, posthog_test_stripe_subscriptions__subscription_customer.posthog_test_stripe_subscriptions__subscription_customer___customer_id)+ WHERE and(greaterOrEquals(posthog_test_stripe_subscriptions.subscription_created_at, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(posthog_test_stripe_subscriptions.subscription_created_at, plus(assumeNotNull(toDateTime('2023-01-10 00:00:00', 'UTC')), toIntervalSecond(0))))),+ numerator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ sum(coalesce(accurateCastOrNull(numerator_events.value, 'Float64'), 0)) AS value+ FROM numerator_events+ JOIN exposures ON equals(toString(exposures.exposure_identifier_num), toString(numerator_events.entity_id))+ WHERE greaterOrEquals(numerator_events.timestamp, exposures.first_exposure_time)+ GROUP BY exposures.entity_id),+ denominator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ sum(coalesce(accurateCastOrNull(denominator_events.value, 'Float64'), 0)) AS value+ FROM denominator_events+ JOIN exposures ON equals(toString(exposures.exposure_identifier_denom), toString(denominator_events.entity_id))+ WHERE greaterOrEquals(denominator_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC'))+ GROUP BY exposures.entity_id),+ entity_metrics AS+ (SELECT exposures.variant AS variant,+ exposures.entity_id AS entity_id,+ any(coalesce(numerator_agg.value, 0)) AS numerator_value,+ any(coalesce(denominator_agg.value, 0)) AS denominator_value+ FROM exposures+ LEFT JOIN numerator_agg ON equals(exposures.entity_id, numerator_agg.entity_id)+ LEFT JOIN denominator_agg ON equals(exposures.entity_id, denominator_agg.entity_id)+ GROUP BY exposures.variant,+ exposures.entity_id)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.numerator_value) AS total_sum,+ sum(power(entity_metrics.numerator_value, 2)) AS total_sum_of_squares,+ sum(entity_metrics.denominator_value) AS denominator_sum,+ sum(power(entity_metrics.denominator_value, 2)) AS denominator_sum_squares,+ sum(multiply(entity_metrics.numerator_value, entity_metrics.denominator_value)) AS numerator_denominator_sum_product+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS format_csv_allow_double_quotes=1,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---diff --git a/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_experiment_exposures_query_runner.new_events_schema.ambr b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_experiment_exposures_query_runner.new_events_schema.ambrnew file mode 100644index 000000000000..2b9ee17ce52a--- /dev/null+++ b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_experiment_exposures_query_runner.new_events_schema.ambr@@ -0,0 +1,412 @@+# serializer version: 1+# name: TestExperimentExposuresQueryRunner.test_exposure_query_counts_users_only_on_first_exposure_0_direct[new_events_schema]+ '''+ WITH first_exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ toDateOrNull(toString(min(toTimeZone(events.timestamp, 'UTC')))) AS day+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-07 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id)+ SELECT first_exposures.day AS day,+ first_exposures.variant AS variant,+ count(first_exposures.entity_id) AS exposed_count+ FROM first_exposures+ WHERE notEmpty(variant)+ GROUP BY first_exposures.day,+ first_exposures.variant+ ORDER BY first_exposures.day ASC+ LIMIT 5000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=0,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentExposuresQueryRunner.test_exposure_query_filters_test_accounts_0_direct[new_events_schema.1]+ '''+ WITH first_exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ toDateOrNull(toString(min(toTimeZone(events.timestamp, 'UTC')))) AS day+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-07 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id)+ SELECT first_exposures.day AS day,+ first_exposures.variant AS variant,+ count(first_exposures.entity_id) AS exposed_count+ FROM first_exposures+ WHERE notEmpty(variant)+ GROUP BY first_exposures.day,+ first_exposures.variant+ ORDER BY first_exposures.day ASC+ LIMIT 5000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=0,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentExposuresQueryRunner.test_exposure_query_filters_test_accounts_0_direct[new_events_schema]+ '''+ WITH first_exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ toDateOrNull(toString(min(toTimeZone(events.timestamp, 'UTC')))) AS day+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-07 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id)+ SELECT first_exposures.day AS day,+ first_exposures.variant AS variant,+ count(first_exposures.entity_id) AS exposed_count+ FROM first_exposures+ WHERE notEmpty(variant)+ GROUP BY first_exposures.day,+ first_exposures.variant+ ORDER BY first_exposures.day ASC+ LIMIT 5000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=0,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentExposuresQueryRunner.test_exposure_query_multiple_variant_handling_first_seen_0_direct[new_events_schema]+ '''+ WITH first_exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ argMin(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), toTimeZone(events.timestamp, 'UTC')) AS variant,+ toDateOrNull(toString(min(toTimeZone(events.timestamp, 'UTC')))) AS day+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-07 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id)+ SELECT first_exposures.day AS day,+ first_exposures.variant AS variant,+ count(first_exposures.entity_id) AS exposed_count+ FROM first_exposures+ WHERE notEmpty(variant)+ GROUP BY first_exposures.day,+ first_exposures.variant+ ORDER BY first_exposures.day ASC+ LIMIT 5000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=0,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentExposuresQueryRunner.test_exposure_query_returns_correct_timeseries_0_direct[new_events_schema]+ '''+ WITH first_exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ toDateOrNull(toString(min(toTimeZone(events.timestamp, 'UTC')))) AS day+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-07 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id)+ SELECT first_exposures.day AS day,+ first_exposures.variant AS variant,+ count(first_exposures.entity_id) AS exposed_count+ FROM first_exposures+ WHERE notEmpty(variant)+ GROUP BY first_exposures.day,+ first_exposures.variant+ ORDER BY first_exposures.day ASC+ LIMIT 5000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=0,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentExposuresQueryRunner.test_exposure_query_using_group_aggregation_0_direct[new_events_schema]+ '''+ WITH first_exposures AS+ (SELECT if(less(toTimeZone(events.timestamp, 'UTC'), toDateTime64('today', 6, 'UTC')), '', events.properties.`$group_0`) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ toDateOrNull(toString(min(toTimeZone(events.timestamp, 'UTC')))) AS day+ FROM events_json AS events+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-28 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']), notEmpty(if(less(toTimeZone(events.timestamp, 'UTC'), toDateTime64('today', 6, 'UTC')), '', events.properties.`$group_0`)))+ GROUP BY entity_id)+ SELECT first_exposures.day AS day,+ first_exposures.variant AS variant,+ count(first_exposures.entity_id) AS exposed_count+ FROM first_exposures+ WHERE notEmpty(variant)+ GROUP BY first_exposures.day,+ first_exposures.variant+ ORDER BY first_exposures.day ASC+ LIMIT 5000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=0,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentExposuresQueryRunner.test_exposure_query_with_action_as_exposure_criteria_0_direct[new_events_schema]+ '''+ WITH first_exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL)), 1), '$multiple', any(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL))) AS variant,+ toDateOrNull(toString(min(toTimeZone(events.timestamp, 'UTC')))) AS day+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-07 00:00:00', 'UTC'))), and(equals(events.event, 'purchase'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^plan)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^plan)), if(isNull(nullIf(toString(events.properties.plan), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.plan, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.plan, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.plan), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.plan, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.plan, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.plan, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.plan), '[]'), '{}'), toString(events.properties.plan))))), 'premium'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL), ['control', 'test']))+ GROUP BY entity_id)+ SELECT first_exposures.day AS day,+ first_exposures.variant AS variant,+ count(first_exposures.entity_id) AS exposed_count+ FROM first_exposures+ WHERE notEmpty(variant)+ GROUP BY first_exposures.day,+ first_exposures.variant+ ORDER BY first_exposures.day ASC+ LIMIT 5000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=0,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentExposuresQueryRunner.test_exposure_query_with_custom_exposure_0_pageview_direct[new_events_schema]+ '''+ WITH first_exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL)), 1), '$multiple', any(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL))) AS variant,+ toDateOrNull(toString(min(toTimeZone(events.timestamp, 'UTC')))) AS day+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-07 00:00:00', 'UTC'))), and(equals(events.event, '$pageview'), ifNull(notEquals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^plan)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^plan)), if(isNull(nullIf(toString(events.properties.plan), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.plan, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.plan, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.plan), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.plan, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.plan, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.plan, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.plan), '[]'), '{}'), toString(events.properties.plan))))), 'free'), 1)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL), ['control', 'test']))+ GROUP BY entity_id)+ SELECT first_exposures.day AS day,+ first_exposures.variant AS variant,+ count(first_exposures.entity_id) AS exposed_count+ FROM first_exposures+ WHERE notEmpty(variant)+ GROUP BY first_exposures.day,+ first_exposures.variant+ ORDER BY first_exposures.day ASC+ LIMIT 5000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=0,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentExposuresQueryRunner.test_exposure_query_with_custom_exposure_2_feature_flag_called_direct[new_events_schema]+ '''+ WITH first_exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ toDateOrNull(toString(min(toTimeZone(events.timestamp, 'UTC')))) AS day+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-07 00:00:00', 'UTC'))), and(and(equals(events.event, '$feature_flag_called'), ifNull(notEquals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^plan)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^plan)), if(isNull(nullIf(toString(events.properties.plan), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.plan, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.plan, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.plan), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.plan, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.plan, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.plan, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.plan), '[]'), '{}'), toString(events.properties.plan))))), 'free'), 1)), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id)+ SELECT first_exposures.day AS day,+ first_exposures.variant AS variant,+ count(first_exposures.entity_id) AS exposed_count+ FROM first_exposures+ WHERE notEmpty(variant)+ GROUP BY first_exposures.day,+ first_exposures.variant+ ORDER BY first_exposures.day ASC+ LIMIT 5000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=0,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentExposuresQueryRunner.test_exposure_query_with_multiple_variant_exposures_0_direct[new_events_schema]+ '''+ WITH first_exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ toDateOrNull(toString(min(toTimeZone(events.timestamp, 'UTC')))) AS day+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-07 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id)+ SELECT first_exposures.day AS day,+ first_exposures.variant AS variant,+ count(first_exposures.entity_id) AS exposed_count+ FROM first_exposures+ WHERE notEmpty(variant)+ GROUP BY first_exposures.day,+ first_exposures.variant+ ORDER BY first_exposures.day ASC+ LIMIT 5000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=0,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentExposuresQueryRunner.test_exposure_query_without_feature_flag_property_0_direct[new_events_schema]+ '''+ WITH first_exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ toDateOrNull(toString(min(toTimeZone(events.timestamp, 'UTC')))) AS day+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-07 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id)+ SELECT first_exposures.day AS day,+ first_exposures.variant AS variant,+ count(first_exposures.entity_id) AS exposed_count+ FROM first_exposures+ WHERE notEmpty(variant)+ GROUP BY first_exposures.day,+ first_exposures.variant+ ORDER BY first_exposures.day ASC+ LIMIT 5000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=0,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---diff --git a/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_frequentist_method.new_events_schema.ambr b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_frequentist_method.new_events_schema.ambrnew file mode 100644index 000000000000..f341a33d6387--- /dev/null+++ b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_frequentist_method.new_events_schema.ambr@@ -0,0 +1,63 @@+# serializer version: 1+# name: TestFrequentistMethod.test_frequentist_property_sum_metric[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.amount, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---diff --git a/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_funnel_metric.new_events_schema.ambr b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_funnel_metric.new_events_schema.ambrnew file mode 100644index 000000000000..ad4c78df371c--- /dev/null+++ b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_funnel_metric.new_events_schema.ambr@@ -0,0 +1,1121 @@+# serializer version: 1+# name: TestExperimentFunnelMetric.test_funnel_metric_events_after_exposure_0_ordered[new_events_schema]+ '''+ WITH base_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))) AS variant_value,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ and(greaterOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])) AS step_0,+ if(equals(events.event, '$pageview'), 1, 0) AS step_1,+ if(equals(events.event, 'purchase'), 1, 0) AS step_2+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), or(and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), or(equals(events.event, '$pageview'), equals(events.event, 'purchase')))))),+ entity_metrics AS+ (SELECT base_events.entity_id AS entity_id,+ if(greater(uniqExactIf(base_events.variant_value, equals(base_events.step_0, 1)), 1), '$multiple', anyIf(base_events.variant_value, equals(base_events.step_0, 1))) AS variant,+ arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(3, 94608000, 'first_touch', 'ordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(base_events.timestamp, 'UTC'), 'Float64'), base_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, base_events.step_0), multiply(2, base_events.step_1), multiply(3, base_events.step_2)]))))))))[1] AS value+ FROM base_events+ GROUP BY base_events.entity_id+ HAVING greater(countIf(equals(base_events.step_0, 1)), 0))+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum_of_squares,+ tuple(countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 1), 0)), countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 2), 0))) AS step_counts+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentFunnelMetric.test_funnel_metric_events_after_exposure_1_unordered[new_events_schema]+ '''+ WITH base_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))) AS variant_value,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ and(greaterOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])) AS step_0,+ if(equals(events.event, '$pageview'), 1, 0) AS step_1,+ if(equals(events.event, 'purchase'), 1, 0) AS step_2+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), or(and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), or(equals(events.event, '$pageview'), equals(events.event, 'purchase')))))),+ first_exposures AS+ (SELECT base_events.entity_id AS entity_id,+ min(toTimeZone(base_events.timestamp, 'UTC')) AS first_exposure_time+ FROM base_events+ WHERE equals(base_events.step_0, 1)+ GROUP BY base_events.entity_id),+ entity_metrics AS+ (SELECT base_events.entity_id AS entity_id,+ if(greater(uniqExactIf(base_events.variant_value, equals(base_events.step_0, 1)), 1), '$multiple', anyIf(base_events.variant_value, equals(base_events.step_0, 1))) AS variant,+ arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(3, 94608000, 'first_touch', 'unordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(base_events.timestamp, 'UTC'), 'Float64'), base_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, base_events.step_0), multiply(2, base_events.step_1), multiply(3, base_events.step_2)]))))))))[1] AS value+ FROM base_events+ INNER JOIN first_exposures ON equals(base_events.entity_id, first_exposures.entity_id)+ WHERE greaterOrEquals(base_events.timestamp, toTimeZone(first_exposures.first_exposure_time, 'UTC'))+ GROUP BY base_events.entity_id)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum_of_squares,+ tuple(countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 1), 0)), countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 2), 0))) AS step_counts+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentFunnelMetric.test_funnel_metric_excludes_different_feature_flags_0_direct[new_events_schema]+ '''+ WITH base_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))) AS variant_value,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ and(greaterOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'experiment-flag'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])) AS step_0,+ if(equals(events.event, '$pageview'), 1, 0) AS step_1,+ if(equals(events.event, 'purchase'), 1, 0) AS step_2+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), or(and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'experiment-flag'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), or(equals(events.event, '$pageview'), equals(events.event, 'purchase')))))),+ entity_metrics AS+ (SELECT base_events.entity_id AS entity_id,+ if(greater(uniqExactIf(base_events.variant_value, equals(base_events.step_0, 1)), 1), '$multiple', anyIf(base_events.variant_value, equals(base_events.step_0, 1))) AS variant,+ arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(3, 94608000, 'first_touch', 'ordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(base_events.timestamp, 'UTC'), 'Float64'), base_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, base_events.step_0), multiply(2, base_events.step_1), multiply(3, base_events.step_2)]))))))))[1] AS value+ FROM base_events+ GROUP BY base_events.entity_id+ HAVING greater(countIf(equals(base_events.step_0, 1)), 0))+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum_of_squares,+ tuple(countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 1), 0)), countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 2), 0))) AS step_counts+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentFunnelMetric.test_funnel_metric_excludes_events_after_experiment_end_date_0_direct[new_events_schema]+ '''+ WITH base_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))) AS variant_value,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ and(greaterOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2024-01-02 00:00:00', 'UTC'))), lessOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2024-01-10 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])) AS step_0,+ if(equals(events.event, 'purchase'), 1, 0) AS step_1+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), or(and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-02 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-10 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-02 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-10 00:00:00', 'UTC'))), equals(events.event, 'purchase'))))),+ entity_metrics AS+ (SELECT base_events.entity_id AS entity_id,+ if(greater(uniqExactIf(base_events.variant_value, equals(base_events.step_0, 1)), 1), '$multiple', anyIf(base_events.variant_value, equals(base_events.step_0, 1))) AS variant,+ arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(2, 94608000, 'first_touch', 'ordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(base_events.timestamp, 'UTC'), 'Float64'), base_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, base_events.step_0), multiply(2, base_events.step_1)]))))))))[1] AS value+ FROM base_events+ GROUP BY base_events.entity_id+ HAVING greater(countIf(equals(base_events.step_0, 1)), 0))+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ countIf(ifNull(equals((entity_metrics.value).1, 1), 0)) AS total_sum,+ countIf(ifNull(equals((entity_metrics.value).1, 1), 0)) AS total_sum_of_squares,+ tuple(countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 1), 0))) AS step_counts+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentFunnelMetric.test_funnel_metric_ordered_vs_unordered_comparison_0_direct[new_events_schema.1]+ '''+ WITH base_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))) AS variant_value,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ and(greaterOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])) AS step_0,+ if(equals(events.event, '$pageview'), 1, 0) AS step_1,+ if(equals(events.event, 'purchase'), 1, 0) AS step_2+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), or(and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), or(equals(events.event, '$pageview'), equals(events.event, 'purchase')))))),+ first_exposures AS+ (SELECT base_events.entity_id AS entity_id,+ min(toTimeZone(base_events.timestamp, 'UTC')) AS first_exposure_time+ FROM base_events+ WHERE equals(base_events.step_0, 1)+ GROUP BY base_events.entity_id),+ entity_metrics AS+ (SELECT base_events.entity_id AS entity_id,+ if(greater(uniqExactIf(base_events.variant_value, equals(base_events.step_0, 1)), 1), '$multiple', anyIf(base_events.variant_value, equals(base_events.step_0, 1))) AS variant,+ arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(3, 94608000, 'first_touch', 'unordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(base_events.timestamp, 'UTC'), 'Float64'), base_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, base_events.step_0), multiply(2, base_events.step_1), multiply(3, base_events.step_2)]))))))))[1] AS value+ FROM base_events+ INNER JOIN first_exposures ON equals(base_events.entity_id, first_exposures.entity_id)+ WHERE greaterOrEquals(base_events.timestamp, toTimeZone(first_exposures.first_exposure_time, 'UTC'))+ GROUP BY base_events.entity_id)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum_of_squares,+ tuple(countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 1), 0)), countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 2), 0))) AS step_counts+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentFunnelMetric.test_funnel_metric_ordered_vs_unordered_comparison_0_direct[new_events_schema]+ '''+ WITH base_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))) AS variant_value,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ and(greaterOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])) AS step_0,+ if(equals(events.event, '$pageview'), 1, 0) AS step_1,+ if(equals(events.event, 'purchase'), 1, 0) AS step_2+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), or(and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), or(equals(events.event, '$pageview'), equals(events.event, 'purchase')))))),+ entity_metrics AS+ (SELECT base_events.entity_id AS entity_id,+ if(greater(uniqExactIf(base_events.variant_value, equals(base_events.step_0, 1)), 1), '$multiple', anyIf(base_events.variant_value, equals(base_events.step_0, 1))) AS variant,+ arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(3, 94608000, 'first_touch', 'ordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(base_events.timestamp, 'UTC'), 'Float64'), base_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, base_events.step_0), multiply(2, base_events.step_1), multiply(3, base_events.step_2)]))))))))[1] AS value+ FROM base_events+ GROUP BY base_events.entity_id+ HAVING greater(countIf(equals(base_events.step_0, 1)), 0))+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum_of_squares,+ tuple(countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 1), 0)), countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 2), 0))) AS step_counts+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentFunnelMetric.test_funnel_metric_with_action_0_direct[new_events_schema]+ '''+ WITH base_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))) AS variant_value,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ and(greaterOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])) AS step_0,+ if(equals(events.event, '$pageview'), 1, 0) AS step_1,+ if(equals(events.event, 'purchase'), 1, 0) AS step_2+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), or(and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), or(equals(events.event, '$pageview'), equals(events.event, 'purchase')))))),+ entity_metrics AS+ (SELECT base_events.entity_id AS entity_id,+ if(greater(uniqExactIf(base_events.variant_value, equals(base_events.step_0, 1)), 1), '$multiple', anyIf(base_events.variant_value, equals(base_events.step_0, 1))) AS variant,+ arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(3, 94608000, 'first_touch', 'ordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(base_events.timestamp, 'UTC'), 'Float64'), base_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, base_events.step_0), multiply(2, base_events.step_1), multiply(3, base_events.step_2)]))))))))[1] AS value+ FROM base_events+ GROUP BY base_events.entity_id+ HAVING greater(countIf(equals(base_events.step_0, 1)), 0))+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum_of_squares,+ tuple(countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 1), 0)), countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 2), 0))) AS step_counts+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentFunnelMetric.test_funnel_metric_with_conversion_window_0_direct[new_events_schema]+ '''+ WITH base_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))) AS variant_value,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ and(greaterOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])) AS step_0,+ if(equals(events.event, '$pageview'), 1, 0) AS step_1,+ if(equals(events.event, 'purchase'), 1, 0) AS step_2+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), or(and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), or(equals(events.event, '$pageview'), equals(events.event, 'purchase')))))),+ entity_metrics AS+ (SELECT base_events.entity_id AS entity_id,+ if(greater(uniqExactIf(base_events.variant_value, equals(base_events.step_0, 1)), 1), '$multiple', anyIf(base_events.variant_value, equals(base_events.step_0, 1))) AS variant,+ arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(3, 94608000, 'first_touch', 'ordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(base_events.timestamp, 'UTC'), 'Float64'), base_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, base_events.step_0), multiply(2, base_events.step_1), multiply(3, base_events.step_2)]))))))))[1] AS value+ FROM base_events+ GROUP BY base_events.entity_id+ HAVING greater(countIf(equals(base_events.step_0, 1)), 0))+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum_of_squares,+ tuple(countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 1), 0)), countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 2), 0))) AS step_counts+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentFunnelMetric.test_funnel_metric_with_custom_conversion_window_0_direct[new_events_schema]+ '''+ WITH base_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))) AS variant_value,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ and(greaterOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])) AS step_0,+ if(equals(events.event, '$pageview'), 1, 0) AS step_1,+ if(equals(events.event, 'purchase'), 1, 0) AS step_2+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), or(and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(86400))), or(equals(events.event, '$pageview'), equals(events.event, 'purchase')))))),+ entity_metrics AS+ (SELECT base_events.entity_id AS entity_id,+ if(greater(uniqExactIf(base_events.variant_value, equals(base_events.step_0, 1)), 1), '$multiple', anyIf(base_events.variant_value, equals(base_events.step_0, 1))) AS variant,+ arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(3, 86400, 'first_touch', 'ordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(base_events.timestamp, 'UTC'), 'Float64'), base_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, base_events.step_0), multiply(2, base_events.step_1), multiply(3, base_events.step_2)]))))))))[1] AS value+ FROM base_events+ GROUP BY base_events.entity_id+ HAVING greater(countIf(equals(base_events.step_0, 1)), 0))+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum_of_squares,+ tuple(countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 1), 0)), countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 2), 0))) AS step_counts+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentFunnelMetric.test_funnel_metric_with_many_steps_0_direct[new_events_schema]+ '''+ WITH base_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))) AS variant_value,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ and(greaterOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])) AS step_0,+ if(equals(events.event, '$pageview'), 1, 0) AS step_1,+ if(equals(events.event, 'add to cart'), 1, 0) AS step_2,+ if(equals(events.event, 'checkout started'), 1, 0) AS step_3,+ if(equals(events.event, 'checkout completed'), 1, 0) AS step_4,+ if(equals(events.event, 'survey submitted'), 1, 0) AS step_5,+ if(equals(events.event, 'referral'), 1, 0) AS step_6+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), or(and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), or(equals(events.event, '$pageview'), equals(events.event, 'add to cart'), equals(events.event, 'checkout started'), equals(events.event, 'checkout completed'), equals(events.event, 'survey submitted'), equals(events.event, 'referral')))))),+ entity_metrics AS+ (SELECT base_events.entity_id AS entity_id,+ if(greater(uniqExactIf(base_events.variant_value, equals(base_events.step_0, 1)), 1), '$multiple', anyIf(base_events.variant_value, equals(base_events.step_0, 1))) AS variant,+ arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(7, 94608000, 'first_touch', 'ordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(base_events.timestamp, 'UTC'), 'Float64'), base_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, base_events.step_0), multiply(2, base_events.step_1), multiply(3, base_events.step_2), multiply(4, base_events.step_3), multiply(5, base_events.step_4), multiply(6, base_events.step_5), multiply(7, base_events.step_6)]))))))))[1] AS value+ FROM base_events+ GROUP BY base_events.entity_id+ HAVING greater(countIf(equals(base_events.step_0, 1)), 0))+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ countIf(ifNull(equals((entity_metrics.value).1, 6), 0)) AS total_sum,+ countIf(ifNull(equals((entity_metrics.value).1, 6), 0)) AS total_sum_of_squares,+ tuple(countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 1), 0)), countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 2), 0)), countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 3), 0)), countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 4), 0)), countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 5), 0)), countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 6), 0))) AS step_counts+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentFunnelMetric.test_funnel_metric_with_multiple_similar_steps_0_direct[new_events_schema]+ '''+ WITH base_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))) AS variant_value,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ and(greaterOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])) AS step_0,+ if(equals(events.event, '$pageview'), 1, 0) AS step_1,+ if(equals(events.event, 'purchase'), 1, 0) AS step_2,+ if(equals(events.event, 'purchase'), 1, 0) AS step_3+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), or(and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), or(equals(events.event, '$pageview'), equals(events.event, 'purchase'), equals(events.event, 'purchase')))))),+ entity_metrics AS+ (SELECT base_events.entity_id AS entity_id,+ if(greater(uniqExactIf(base_events.variant_value, equals(base_events.step_0, 1)), 1), '$multiple', anyIf(base_events.variant_value, equals(base_events.step_0, 1))) AS variant,+ arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(4, 94608000, 'first_touch', 'ordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(base_events.timestamp, 'UTC'), 'Float64'), base_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, base_events.step_0), multiply(2, base_events.step_1), multiply(3, base_events.step_2), multiply(4, base_events.step_3)]))))))))[1] AS value+ FROM base_events+ GROUP BY base_events.entity_id+ HAVING greater(countIf(equals(base_events.step_0, 1)), 0))+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ countIf(ifNull(equals((entity_metrics.value).1, 3), 0)) AS total_sum,+ countIf(ifNull(equals((entity_metrics.value).1, 3), 0)) AS total_sum_of_squares,+ tuple(countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 1), 0)), countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 2), 0)), countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 3), 0))) AS step_counts+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentFunnelMetric.test_funnel_metric_with_step_property_filter_0_direct[new_events_schema]+ '''+ WITH base_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))) AS variant_value,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ and(greaterOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])) AS step_0,+ if(and(equals(events.event, '$pageview'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^wizard_step)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^wizard_step)), if(isNull(nullIf(toString(events.properties.wizard_step), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.wizard_step, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.wizard_step, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.wizard_step), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.wizard_step, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.wizard_step, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.wizard_step, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.wizard_step), '[]'), '{}'), toString(events.properties.wizard_step))))), 'step_1'), 0)), 1, 0) AS step_1,+ if(and(equals(events.event, '$pageview'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^wizard_step)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^wizard_step)), if(isNull(nullIf(toString(events.properties.wizard_step), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.wizard_step, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.wizard_step, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.wizard_step), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.wizard_step, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.wizard_step, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.wizard_step, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.wizard_step), '[]'), '{}'), toString(events.properties.wizard_step))))), 'step_2'), 0)), 1, 0) AS step_2+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), or(and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), or(and(equals(events.event, '$pageview'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^wizard_step)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^wizard_step)), if(isNull(nullIf(toString(events.properties.wizard_step), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.wizard_step, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.wizard_step, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.wizard_step), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.wizard_step, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.wizard_step, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.wizard_step, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.wizard_step), '[]'), '{}'), toString(events.properties.wizard_step))))), 'step_1'), 0)), and(equals(events.event, '$pageview'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^wizard_step)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^wizard_step)), if(isNull(nullIf(toString(events.properties.wizard_step), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.wizard_step, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.wizard_step, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.wizard_step), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.wizard_step, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.wizard_step, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.wizard_step, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.wizard_step), '[]'), '{}'), toString(events.properties.wizard_step))))), 'step_2'), 0))))))),+ entity_metrics AS+ (SELECT base_events.entity_id AS entity_id,+ if(greater(uniqExactIf(base_events.variant_value, equals(base_events.step_0, 1)), 1), '$multiple', anyIf(base_events.variant_value, equals(base_events.step_0, 1))) AS variant,+ arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(3, 94608000, 'first_touch', 'ordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(base_events.timestamp, 'UTC'), 'Float64'), base_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, base_events.step_0), multiply(2, base_events.step_1), multiply(3, base_events.step_2)]))))))))[1] AS value+ FROM base_events+ GROUP BY base_events.entity_id+ HAVING greater(countIf(equals(base_events.step_0, 1)), 0))+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum_of_squares,+ tuple(countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 1), 0)), countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 2), 0))) AS step_counts+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentFunnelMetric.test_funnel_metric_with_unordered_steps_0_direct[new_events_schema]+ '''+ WITH base_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))) AS variant_value,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ and(greaterOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])) AS step_0,+ if(equals(events.event, '$pageview'), 1, 0) AS step_1,+ if(equals(events.event, 'purchase'), 1, 0) AS step_2+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), or(and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), or(equals(events.event, '$pageview'), equals(events.event, 'purchase')))))),+ first_exposures AS+ (SELECT base_events.entity_id AS entity_id,+ min(toTimeZone(base_events.timestamp, 'UTC')) AS first_exposure_time+ FROM base_events+ WHERE equals(base_events.step_0, 1)+ GROUP BY base_events.entity_id),+ entity_metrics AS+ (SELECT base_events.entity_id AS entity_id,+ if(greater(uniqExactIf(base_events.variant_value, equals(base_events.step_0, 1)), 1), '$multiple', anyIf(base_events.variant_value, equals(base_events.step_0, 1))) AS variant,+ arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(3, 94608000, 'first_touch', 'unordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(base_events.timestamp, 'UTC'), 'Float64'), base_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, base_events.step_0), multiply(2, base_events.step_1), multiply(3, base_events.step_2)]))))))))[1] AS value+ FROM base_events+ INNER JOIN first_exposures ON equals(base_events.entity_id, first_exposures.entity_id)+ WHERE greaterOrEquals(base_events.timestamp, toTimeZone(first_exposures.first_exposure_time, 'UTC'))+ GROUP BY base_events.entity_id)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum_of_squares,+ tuple(countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 1), 0)), countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 2), 0))) AS step_counts+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentFunnelMetric.test_maturity_anchored_on_first_exposure_0_direct[new_events_schema]+ '''+ WITH base_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))) AS variant_value,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ and(greaterOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2020-01-15 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])) AS step_0,+ if(equals(events.event, '$pageview'), 1, 0) AS step_1,+ if(equals(events.event, 'purchase'), 1, 0) AS step_2+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), or(and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 00:00:00', 'UTC')), toIntervalSecond(604800))), or(equals(events.event, '$pageview'), equals(events.event, 'purchase')))))),+ entity_metrics AS+ (SELECT base_events.entity_id AS entity_id,+ if(greater(uniqExactIf(base_events.variant_value, equals(base_events.step_0, 1)), 1), '$multiple', anyIf(base_events.variant_value, equals(base_events.step_0, 1))) AS variant,+ arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(3, 604800, 'first_touch', 'ordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(base_events.timestamp, 'UTC'), 'Float64'), base_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, base_events.step_0), multiply(2, base_events.step_1), multiply(3, base_events.step_2)]))))))))[1] AS value+ FROM base_events+ GROUP BY base_events.entity_id+ HAVING and(greater(countIf(equals(base_events.step_0, 1)), 0), ifNull(lessOrEquals(plus(minIf(toTimeZone(base_events.timestamp, 'UTC'), equals(base_events.step_0, 1)), toIntervalSecond(604800)), parseDateTime64BestEffortOrNull('2020-01-15 12:00:00', 6, 'UTC')), 0)))+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum_of_squares,+ tuple(countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 1), 0)), countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 2), 0))) AS step_counts+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentFunnelMetric.test_only_count_matured_users_0_direct[new_events_schema]+ '''+ WITH base_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))) AS variant_value,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ and(greaterOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2020-01-15 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])) AS step_0,+ if(equals(events.event, '$pageview'), 1, 0) AS step_1,+ if(equals(events.event, 'purchase'), 1, 0) AS step_2+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), or(and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 00:00:00', 'UTC')), toIntervalSecond(604800))), or(equals(events.event, '$pageview'), equals(events.event, 'purchase')))))),+ entity_metrics AS+ (SELECT base_events.entity_id AS entity_id,+ if(greater(uniqExactIf(base_events.variant_value, equals(base_events.step_0, 1)), 1), '$multiple', anyIf(base_events.variant_value, equals(base_events.step_0, 1))) AS variant,+ arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(3, 604800, 'first_touch', 'ordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(base_events.timestamp, 'UTC'), 'Float64'), base_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, base_events.step_0), multiply(2, base_events.step_1), multiply(3, base_events.step_2)]))))))))[1] AS value+ FROM base_events+ GROUP BY base_events.entity_id+ HAVING and(greater(countIf(equals(base_events.step_0, 1)), 0), ifNull(lessOrEquals(plus(minIf(toTimeZone(base_events.timestamp, 'UTC'), equals(base_events.step_0, 1)), toIntervalSecond(604800)), parseDateTime64BestEffortOrNull('2020-01-15 12:00:00', 6, 'UTC')), 0)))+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum_of_squares,+ tuple(countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 1), 0)), countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 2), 0))) AS step_counts+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentFunnelMetric.test_query_runner_data_warehouse_funnel_metric_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), if(isNull(nullIf(toString(events.properties.`$user_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$user_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$user_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$user_id`), '[]'), '{}'), toString(events.properties.`$user_id`))))), toTimeZone(events.timestamp, 'UTC')) AS exposure_identifier+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), if(isNull(nullIf(toString(events.properties.`$user_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$user_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$user_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$user_id`), '[]'), '{}'), toString(events.properties.`$user_id`)))))) AS entity_id,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))) AS variant,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))) AS session_id,+ if(and(greaterOrEquals(timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])), 1, 0) AS step_0,+ if(equals(events.event, '$pageview'), 1, 0) AS step_1,+ 0 AS step_2+ FROM events_json AS events+ WHERE and(equals(events.team_id, 99999), and(greaterOrEquals(timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC')))), or(and(greaterOrEquals(timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])), equals(events.event, '$pageview')))+ UNION ALL SELECT toString(posthog_test_usage.userid) AS entity_id,+ '' AS variant,+ posthog_test_usage.ds AS timestamp,+ accurateCastOrNull('00000000-0000-0000-0000-000000000000', 'UUID') AS uuid,+ '' AS session_id,+ 0 AS step_0,+ 0 AS step_1,+ 1 AS step_2+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_usage/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`ds` Date, `id` String, `plan` String, `usage` Float64, `region` String, `userid` String') AS posthog_test_usage+ WHERE and(greaterOrEquals(posthog_test_usage.ds, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(posthog_test_usage.ds, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(3, 94608000, 'first_touch', 'ordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(metric_events.timestamp, 'UTC'), 'Float64'), metric_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, metric_events.step_0), multiply(2, metric_events.step_1), multiply(3, metric_events.step_2)]))))))))[1] AS value+ FROM exposures+ LEFT JOIN metric_events ON equals(toString(exposures.exposure_identifier), metric_events.entity_id)+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum_of_squares,+ tuple(countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 1), 0)), countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 2), 0))) AS step_counts+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS format_csv_allow_double_quotes=1,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentFunnelMetric.test_query_runner_data_warehouse_funnel_no_fanout_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), if(isNull(nullIf(toString(events.properties.`$user_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$user_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$user_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$user_id`), '[]'), '{}'), toString(events.properties.`$user_id`))))), toTimeZone(events.timestamp, 'UTC')) AS exposure_identifier+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), if(isNull(nullIf(toString(events.properties.`$user_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$user_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$user_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$user_id`), '[]'), '{}'), toString(events.properties.`$user_id`)))))) AS entity_id,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))) AS variant,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))) AS session_id,+ if(and(greaterOrEquals(timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])), 1, 0) AS step_0,+ if(equals(events.event, '$pageview'), 1, 0) AS step_1,+ 0 AS step_2+ FROM events_json AS events+ WHERE and(equals(events.team_id, 99999), and(greaterOrEquals(timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC')))), or(and(greaterOrEquals(timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])), equals(events.event, '$pageview')))+ UNION ALL SELECT toString(posthog_test_usage.userid) AS entity_id,+ '' AS variant,+ posthog_test_usage.ds AS timestamp,+ accurateCastOrNull('00000000-0000-0000-0000-000000000000', 'UUID') AS uuid,+ '' AS session_id,+ 0 AS step_0,+ 0 AS step_1,+ 1 AS step_2+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_usage/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`ds` Date, `id` String, `plan` String, `usage` Float64, `region` String, `userid` String') AS posthog_test_usage+ WHERE and(greaterOrEquals(posthog_test_usage.ds, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(posthog_test_usage.ds, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(3, 94608000, 'first_touch', 'ordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(metric_events.timestamp, 'UTC'), 'Float64'), metric_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, metric_events.step_0), multiply(2, metric_events.step_1), multiply(3, metric_events.step_2)]))))))))[1] AS value+ FROM exposures+ LEFT JOIN metric_events ON equals(toString(exposures.exposure_identifier), metric_events.entity_id)+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum_of_squares,+ tuple(countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 1), 0)), countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 2), 0))) AS step_counts+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS format_csv_allow_double_quotes=1,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentFunnelMetric.test_query_runner_data_warehouse_funnel_partial_coverage_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), if(isNull(nullIf(toString(events.properties.`$user_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$user_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$user_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$user_id`), '[]'), '{}'), toString(events.properties.`$user_id`))))), toTimeZone(events.timestamp, 'UTC')) AS exposure_identifier+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), if(isNull(nullIf(toString(events.properties.`$user_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$user_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$user_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$user_id`), '[]'), '{}'), toString(events.properties.`$user_id`)))))) AS entity_id,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))) AS variant,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))) AS session_id,+ if(and(greaterOrEquals(timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])), 1, 0) AS step_0,+ if(equals(events.event, '$pageview'), 1, 0) AS step_1,+ 0 AS step_2+ FROM events_json AS events+ WHERE and(equals(events.team_id, 99999), and(greaterOrEquals(timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC')))), or(and(greaterOrEquals(timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])), equals(events.event, '$pageview')))+ UNION ALL SELECT toString(posthog_test_usage.userid) AS entity_id,+ '' AS variant,+ posthog_test_usage.ds AS timestamp,+ accurateCastOrNull('00000000-0000-0000-0000-000000000000', 'UUID') AS uuid,+ '' AS session_id,+ 0 AS step_0,+ 0 AS step_1,+ 1 AS step_2+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_usage/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`ds` Date, `id` String, `plan` String, `usage` Float64, `region` String, `userid` String') AS posthog_test_usage+ WHERE and(greaterOrEquals(posthog_test_usage.ds, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(posthog_test_usage.ds, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(3, 94608000, 'first_touch', 'ordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(metric_events.timestamp, 'UTC'), 'Float64'), metric_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, metric_events.step_0), multiply(2, metric_events.step_1), multiply(3, metric_events.step_2)]))))))))[1] AS value+ FROM exposures+ LEFT JOIN metric_events ON equals(toString(exposures.exposure_identifier), metric_events.entity_id)+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum_of_squares,+ tuple(countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 1), 0)), countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 2), 0))) AS step_counts+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS format_csv_allow_double_quotes=1,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentFunnelMetric.test_query_runner_data_warehouse_funnel_zero_coverage_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), if(isNull(nullIf(toString(events.properties.`$user_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$user_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$user_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$user_id`), '[]'), '{}'), toString(events.properties.`$user_id`))))), toTimeZone(events.timestamp, 'UTC')) AS exposure_identifier+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$user_id`)), if(isNull(nullIf(toString(events.properties.`$user_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$user_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$user_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$user_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$user_id`), '[]'), '{}'), toString(events.properties.`$user_id`)))))) AS entity_id,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))) AS variant,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$session_id`)), if(isNull(nullIf(toString(events.properties.`$session_id`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$session_id`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$session_id`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$session_id`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$session_id`), '[]'), '{}'), toString(events.properties.`$session_id`))))) AS session_id,+ if(and(greaterOrEquals(timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])), 1, 0) AS step_0,+ if(equals(events.event, '$pageview'), 1, 0) AS step_1,+ 0 AS step_2+ FROM events_json AS events+ WHERE and(equals(events.team_id, 99999), and(greaterOrEquals(timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC')))), or(and(greaterOrEquals(timestamp, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), lessOrEquals(timestamp, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])), equals(events.event, '$pageview')))+ UNION ALL SELECT toString(posthog_test_usage.userid) AS entity_id,+ '' AS variant,+ posthog_test_usage.ds AS timestamp,+ accurateCastOrNull('00000000-0000-0000-0000-000000000000', 'UUID') AS uuid,+ '' AS session_id,+ 0 AS step_0,+ 0 AS step_1,+ 1 AS step_2+ FROM s3('http://host.docker.internal:19000/posthog/test_storage_bucket-posthog.hogql.experiments.queryrunner/posthog_test_usage/*.csv', 'object_storage_root_user', 'object_storage_root_password', 'CSVWithNames', '`ds` Date, `id` String, `plan` String, `usage` Float64, `region` String, `userid` String') AS posthog_test_usage+ WHERE and(greaterOrEquals(posthog_test_usage.ds, assumeNotNull(toDateTime('2023-01-01 00:00:00', 'UTC'))), less(posthog_test_usage.ds, assumeNotNull(toDateTime('2023-01-31 00:00:00', 'UTC'))))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(3, 94608000, 'first_touch', 'ordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(metric_events.timestamp, 'UTC'), 'Float64'), metric_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, metric_events.step_0), multiply(2, metric_events.step_1), multiply(3, metric_events.step_2)]))))))))[1] AS value+ FROM exposures+ LEFT JOIN metric_events ON equals(toString(exposures.exposure_identifier), metric_events.entity_id)+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum,+ countIf(ifNull(equals((entity_metrics.value).1, 2), 0)) AS total_sum_of_squares,+ tuple(countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 1), 0)), countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 2), 0))) AS step_counts+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS format_csv_allow_double_quotes=1,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentFunnelMetric.test_query_runner_funnel_metric_0_direct[new_events_schema]+ '''+ WITH base_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))) AS variant_value,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ and(greaterOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])) AS step_0,+ if(equals(events.event, 'purchase'), 1, 0) AS step_1+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), or(and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test'])), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), equals(events.event, 'purchase'))))),+ entity_metrics AS+ (SELECT base_events.entity_id AS entity_id,+ if(greater(uniqExactIf(base_events.variant_value, equals(base_events.step_0, 1)), 1), '$multiple', anyIf(base_events.variant_value, equals(base_events.step_0, 1))) AS variant,+ arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(2, 94608000, 'first_touch', 'ordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(base_events.timestamp, 'UTC'), 'Float64'), base_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, base_events.step_0), multiply(2, base_events.step_1)]))))))))[1] AS value+ FROM base_events+ GROUP BY base_events.entity_id+ HAVING greater(countIf(equals(base_events.step_0, 1)), 0))+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ countIf(ifNull(equals((entity_metrics.value).1, 1), 0)) AS total_sum,+ countIf(ifNull(equals((entity_metrics.value).1, 1), 0)) AS total_sum_of_squares,+ tuple(countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 1), 0))) AS step_counts+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentFunnelMetric.test_query_runner_group_aggregation_funnel_metric_0_direct[new_events_schema]+ '''+ WITH base_events AS+ (SELECT events.properties.`$group_0` AS entity_id,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))) AS variant_value,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.uuid AS uuid,+ and(greaterOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(toTimeZone(events.timestamp, 'UTC'), assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']), notEmpty(events.properties.`$group_0`)) AS step_0,+ if(equals(events.event, 'purchase'), 1, 0) AS step_1+ FROM events_json AS events+ WHERE and(equals(events.team_id, 99999), or(and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']), notEmpty(events.properties.`$group_0`)), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), equals(events.event, 'purchase'))))),+ entity_metrics AS+ (SELECT base_events.entity_id AS entity_id,+ if(greater(uniqExactIf(base_events.variant_value, equals(base_events.step_0, 1)), 1), '$multiple', anyIf(base_events.variant_value, equals(base_events.step_0, 1))) AS variant,+ arraySort(x -> minus(0, x.1), arrayMap(result -> tuple(result.1, if(and(ifNull(greaterOrEquals(result.1, 0), 0), ifNull(greater(length(result.4), result.1), 0)), if(ifNull(greater(length(arrayElement(result.4, plus(result.1, 1))), 0), 0), toString(arrayElement(result.4, plus(result.1, 1))[1]), ''), '')), aggregate_funnel_array(2, 94608000, 'first_touch', 'ordered', array(array('')), [], arraySort(t -> t.1, arrayFilter(t -> and(isNotNull(t.1), isNotNull(t.2)), groupArray(tuple(accurateCastOrNull(toTimeZone(base_events.timestamp, 'UTC'), 'Float64'), base_events.uuid, array(''), arrayFilter(x -> ifNull(greater(x, 0), 0), [multiply(1, base_events.step_0), multiply(2, base_events.step_1)]))))))))[1] AS value+ FROM base_events+ GROUP BY base_events.entity_id+ HAVING greater(countIf(equals(base_events.step_0, 1)), 0))+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ countIf(ifNull(equals((entity_metrics.value).1, 1), 0)) AS total_sum,+ countIf(ifNull(equals((entity_metrics.value).1, 1), 0)) AS total_sum_of_squares,+ tuple(countIf(ifNull(greaterOrEquals((entity_metrics.value).1, 1), 0))) AS step_counts+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---diff --git a/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_mean_metric.new_events_schema.ambr b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_mean_metric.new_events_schema.ambrnew file mode 100644index 000000000000..0ef19df0ceaa--- /dev/null+++ b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_mean_metric.new_events_schema.ambr@@ -0,0 +1,848 @@+# serializer version: 1+# name: TestExperimentMeanMetric.test_conversion_window_anchored_on_first_exposure_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-10 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.amount, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-10 00:00:00', 'UTC')), toIntervalSecond(86400))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), and(greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')), less(toTimeZone(metric_events.timestamp, 'UTC'), plus(toTimeZone(exposures.first_exposure_time, 'UTC'), toIntervalSecond(86400)))))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentMeanMetric.test_count_unique_property_values_via_hogql_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^category)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^category)), if(isNull(nullIf(toString(events.properties.category), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.category, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.category, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.category), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.category, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.category, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.category, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.category), '[]'), '{}'), toString(events.properties.category))))) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ coalesce(accurateCastOrNull(uniqExact(metric_events.value), 'Float64'), 0) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentMeanMetric.test_hogql_count_metric_with_winsorization_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^category)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^category)), if(isNull(nullIf(toString(events.properties.category), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.category, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.category, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.category), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.category, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.category, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.category, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.category), '[]'), '{}'), toString(events.properties.category))))) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ coalesce(accurateCastOrNull(count(metric_events.value), 'Float64'), 0) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant),+ percentiles AS+ (SELECT quantileExact(0.1)(entity_metrics.value) AS lower_bound,+ quantileExact(0.9)(entity_metrics.value) AS upper_bound+ FROM entity_metrics),+ winsorized_entity_metrics AS+ (SELECT entity_metrics.entity_id AS entity_id,+ entity_metrics.variant AS variant,+ least(greatest(percentiles.lower_bound, entity_metrics.value), percentiles.upper_bound) AS value+ FROM entity_metrics+ CROSS JOIN percentiles)+ SELECT winsorized_entity_metrics.variant AS variant,+ count(winsorized_entity_metrics.entity_id) AS num_users,+ sum(winsorized_entity_metrics.value) AS total_sum,+ sum(power(winsorized_entity_metrics.value, 2)) AS total_sum_of_squares+ FROM winsorized_entity_metrics+ GROUP BY winsorized_entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentMeanMetric.test_only_count_matured_users_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id+ HAVING lessOrEquals(plus(min(toTimeZone(events.timestamp, 'UTC')), toIntervalSecond(604800)), parseDateTime64BestEffortOrNull('2020-01-15 12:00:00', 6, 'UTC'))),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.amount, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 00:00:00', 'UTC')), toIntervalSecond(604800))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), and(greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')), less(toTimeZone(metric_events.timestamp, 'UTC'), plus(toTimeZone(exposures.first_exposure_time, 'UTC'), toIntervalSecond(604800)))))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentMeanMetric.test_outlier_handling_for_count_metric_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant),+ percentiles AS+ (SELECT quantileExact(0.1)(entity_metrics.value) AS lower_bound,+ quantileExact(0.9)(entity_metrics.value) AS upper_bound+ FROM entity_metrics),+ winsorized_entity_metrics AS+ (SELECT entity_metrics.entity_id AS entity_id,+ entity_metrics.variant AS variant,+ least(greatest(percentiles.lower_bound, entity_metrics.value), percentiles.upper_bound) AS value+ FROM entity_metrics+ CROSS JOIN percentiles)+ SELECT winsorized_entity_metrics.variant AS variant,+ count(winsorized_entity_metrics.entity_id) AS num_users,+ sum(winsorized_entity_metrics.value) AS total_sum,+ sum(power(winsorized_entity_metrics.value, 2)) AS total_sum_of_squares+ FROM winsorized_entity_metrics+ GROUP BY winsorized_entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentMeanMetric.test_outlier_handling_for_sum_metric_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.amount, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant),+ percentiles AS+ (SELECT quantileExact(0.1)(entity_metrics.value) AS lower_bound,+ quantileExact(0.9)(entity_metrics.value) AS upper_bound+ FROM entity_metrics),+ winsorized_entity_metrics AS+ (SELECT entity_metrics.entity_id AS entity_id,+ entity_metrics.variant AS variant,+ least(greatest(percentiles.lower_bound, entity_metrics.value), percentiles.upper_bound) AS value+ FROM entity_metrics+ CROSS JOIN percentiles)+ SELECT winsorized_entity_metrics.variant AS variant,+ count(winsorized_entity_metrics.entity_id) AS num_users,+ sum(winsorized_entity_metrics.value) AS total_sum,+ sum(power(winsorized_entity_metrics.value, 2)) AS total_sum_of_squares+ FROM winsorized_entity_metrics+ GROUP BY winsorized_entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentMeanMetric.test_outlier_handling_with_ignore_zeros_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.amount, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant),+ percentiles AS+ (SELECT min(entity_metrics.value) AS lower_bound,+ quantileExact(0.9)(if(ifNull(notEquals(entity_metrics.value, 0), 1), entity_metrics.value, NULL)) AS upper_bound+ FROM entity_metrics),+ winsorized_entity_metrics AS+ (SELECT entity_metrics.entity_id AS entity_id,+ entity_metrics.variant AS variant,+ least(greatest(percentiles.lower_bound, entity_metrics.value), percentiles.upper_bound) AS value+ FROM entity_metrics+ CROSS JOIN percentiles)+ SELECT winsorized_entity_metrics.variant AS variant,+ count(winsorized_entity_metrics.entity_id) AS num_users,+ sum(winsorized_entity_metrics.value) AS total_sum,+ sum(power(winsorized_entity_metrics.value, 2)) AS total_sum_of_squares+ FROM winsorized_entity_metrics+ GROUP BY winsorized_entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentMeanMetric.test_property_aggregation_metric_0_max_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.amount, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ coalesce(max(accurateCastOrNull(metric_events.value, 'Float64')), 0) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentMeanMetric.test_property_aggregation_metric_2_min_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.amount, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ coalesce(min(accurateCastOrNull(metric_events.value, 'Float64')), 0) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentMeanMetric.test_property_aggregation_metric_4_avg_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.amount, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ coalesce(avg(accurateCastOrNull(metric_events.value, 'Float64')), 0) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentMeanMetric.test_property_sum_metric_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.amount, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentMeanMetric.test_threshold_sum_metric_counts_users_crossing_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.amount, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(if(ifNull(greaterOrEquals(entity_metrics.value, 100.0), 0), 1, 0)) AS total_sum,+ sum(if(ifNull(greaterOrEquals(entity_metrics.value, 100.0), 0), 1, 0)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentMeanMetric.test_unique_sessions_math_type_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.properties.`$session_id` AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, '$pageview'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ accurateCastOrNull(count(DISTINCT multiIf(isNull(metric_events.value), NULL, and(in(toTypeName(metric_events.value), tuple('UUID', 'Nullable(UUID)')), equals(reinterpretAsUInt128(assumeNotNull(metric_events.value)), 0)), NULL, equals(toString(metric_events.value), ''), NULL, metric_events.value)), 'Float64') AS value+ FROM exposures+ LEFT JOIN metric_events ON and(equals(exposures.entity_id, metric_events.entity_id), greaterOrEquals(toTimeZone(metric_events.timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---diff --git a/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_ratio_metric.new_events_schema.ambr b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_ratio_metric.new_events_schema.ambrnew file mode 100644index 000000000000..06b016bebd5b--- /dev/null+++ b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_ratio_metric.new_events_schema.ambr@@ -0,0 +1,1147 @@+# serializer version: 1+# name: TestExperimentRatioMetric.test_basic_ratio_metric_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ numerator_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.amount, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ denominator_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ numerator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ sum(coalesce(accurateCastOrNull(numerator_events.value, 'Float64'), 0)) AS value+ FROM numerator_events+ JOIN exposures ON equals(exposures.entity_id, numerator_events.entity_id)+ WHERE greaterOrEquals(numerator_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC'))+ GROUP BY exposures.entity_id),+ denominator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ sum(coalesce(accurateCastOrNull(denominator_events.value, 'Float64'), 0)) AS value+ FROM denominator_events+ JOIN exposures ON equals(exposures.entity_id, denominator_events.entity_id)+ WHERE greaterOrEquals(denominator_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC'))+ GROUP BY exposures.entity_id),+ entity_metrics AS+ (SELECT exposures.variant AS variant,+ exposures.entity_id AS entity_id,+ any(coalesce(numerator_agg.value, 0)) AS numerator_value,+ any(coalesce(denominator_agg.value, 0)) AS denominator_value+ FROM exposures+ LEFT JOIN numerator_agg ON equals(exposures.entity_id, numerator_agg.entity_id)+ LEFT JOIN denominator_agg ON equals(exposures.entity_id, denominator_agg.entity_id)+ GROUP BY exposures.variant,+ exposures.entity_id)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.numerator_value) AS total_sum,+ sum(power(entity_metrics.numerator_value, 2)) AS total_sum_of_squares,+ sum(entity_metrics.denominator_value) AS denominator_sum,+ sum(power(entity_metrics.denominator_value, 2)) AS denominator_sum_squares,+ sum(multiply(entity_metrics.numerator_value, entity_metrics.denominator_value)) AS numerator_denominator_sum_product+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentRatioMetric.test_basic_ratio_metric_1_precomputed[new_events_schema]+ '''+ WITH exposures AS+ (SELECT accurateCastOrNull(t.entity_id, 'UUID') AS entity_id,+ if(greater(uniqExact(t.variant), 1), '$multiple', argMin(t.variant, toTimeZone(t.first_exposure_time, 'UTC'))) AS variant,+ min(toTimeZone(t.first_exposure_time, 'UTC')) AS first_exposure_time,+ max(toTimeZone(t.last_exposure_time, 'UTC')) AS last_exposure_time,+ argMin(t.exposure_event_uuid, toTimeZone(t.first_exposure_time, 'UTC')) AS exposure_event_uuid,+ argMin(t.exposure_session_id, toTimeZone(t.first_exposure_time, 'UTC')) AS exposure_session_id+ FROM experiment_exposures_preaggregated AS t+ WHERE and(equals(t.team_id, 99999), in(t.job_id, ['00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000']), equals(t.team_id, 99999), greaterOrEquals(t.first_exposure_time, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(t.first_exposure_time, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))))+ GROUP BY entity_id),+ numerator_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.amount, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ denominator_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ numerator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ sum(coalesce(accurateCastOrNull(numerator_events.value, 'Float64'), 0)) AS value+ FROM numerator_events+ JOIN exposures ON equals(exposures.entity_id, numerator_events.entity_id)+ WHERE greaterOrEquals(numerator_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC'))+ GROUP BY exposures.entity_id),+ denominator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ sum(coalesce(accurateCastOrNull(denominator_events.value, 'Float64'), 0)) AS value+ FROM denominator_events+ JOIN exposures ON equals(exposures.entity_id, denominator_events.entity_id)+ WHERE greaterOrEquals(denominator_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC'))+ GROUP BY exposures.entity_id),+ entity_metrics AS+ (SELECT exposures.variant AS variant,+ exposures.entity_id AS entity_id,+ any(coalesce(numerator_agg.value, 0)) AS numerator_value,+ any(coalesce(denominator_agg.value, 0)) AS denominator_value+ FROM exposures+ LEFT JOIN numerator_agg ON equals(exposures.entity_id, numerator_agg.entity_id)+ LEFT JOIN denominator_agg ON equals(exposures.entity_id, denominator_agg.entity_id)+ GROUP BY exposures.variant,+ exposures.entity_id)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.numerator_value) AS total_sum,+ sum(power(entity_metrics.numerator_value, 2)) AS total_sum_of_squares,+ sum(entity_metrics.denominator_value) AS denominator_sum,+ sum(power(entity_metrics.denominator_value, 2)) AS denominator_sum_squares,+ sum(multiply(entity_metrics.numerator_value, entity_metrics.denominator_value)) AS numerator_denominator_sum_product+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS load_balancing='in_order',+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentRatioMetric.test_only_count_matured_users_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id+ HAVING lessOrEquals(plus(min(toTimeZone(events.timestamp, 'UTC')), toIntervalSecond(604800)), parseDateTime64BestEffortOrNull('2020-01-15 12:00:00', 6, 'UTC'))),+ numerator_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.amount, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 00:00:00', 'UTC')), toIntervalSecond(604800))), equals(events.event, 'purchase'))),+ denominator_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 00:00:00', 'UTC')), toIntervalSecond(604800))), equals(events.event, 'purchase'))),+ numerator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ sum(coalesce(accurateCastOrNull(numerator_events.value, 'Float64'), 0)) AS value+ FROM numerator_events+ JOIN exposures ON equals(exposures.entity_id, numerator_events.entity_id)+ WHERE and(greaterOrEquals(numerator_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')), less(numerator_events.timestamp, plus(toTimeZone(exposures.first_exposure_time, 'UTC'), toIntervalSecond(604800))))+ GROUP BY exposures.entity_id),+ denominator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ sum(coalesce(accurateCastOrNull(denominator_events.value, 'Float64'), 0)) AS value+ FROM denominator_events+ JOIN exposures ON equals(exposures.entity_id, denominator_events.entity_id)+ WHERE and(greaterOrEquals(denominator_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')), less(denominator_events.timestamp, plus(toTimeZone(exposures.first_exposure_time, 'UTC'), toIntervalSecond(604800))))+ GROUP BY exposures.entity_id),+ entity_metrics AS+ (SELECT exposures.variant AS variant,+ exposures.entity_id AS entity_id,+ any(coalesce(numerator_agg.value, 0)) AS numerator_value,+ any(coalesce(denominator_agg.value, 0)) AS denominator_value+ FROM exposures+ LEFT JOIN numerator_agg ON equals(exposures.entity_id, numerator_agg.entity_id)+ LEFT JOIN denominator_agg ON equals(exposures.entity_id, denominator_agg.entity_id)+ GROUP BY exposures.variant,+ exposures.entity_id)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.numerator_value) AS total_sum,+ sum(power(entity_metrics.numerator_value, 2)) AS total_sum_of_squares,+ sum(entity_metrics.denominator_value) AS denominator_sum,+ sum(power(entity_metrics.denominator_value, 2)) AS denominator_sum_squares,+ sum(multiply(entity_metrics.numerator_value, entity_metrics.denominator_value)) AS numerator_denominator_sum_product+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentRatioMetric.test_outlier_handling_numerator_and_denominator_independent_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ numerator_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.amount, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ denominator_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'pageview'))),+ numerator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ sum(coalesce(accurateCastOrNull(numerator_events.value, 'Float64'), 0)) AS value+ FROM numerator_events+ JOIN exposures ON equals(exposures.entity_id, numerator_events.entity_id)+ WHERE greaterOrEquals(numerator_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC'))+ GROUP BY exposures.entity_id),+ denominator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ sum(coalesce(accurateCastOrNull(denominator_events.value, 'Float64'), 0)) AS value+ FROM denominator_events+ JOIN exposures ON equals(exposures.entity_id, denominator_events.entity_id)+ WHERE greaterOrEquals(denominator_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC'))+ GROUP BY exposures.entity_id),+ entity_metrics AS+ (SELECT exposures.variant AS variant,+ exposures.entity_id AS entity_id,+ any(coalesce(numerator_agg.value, 0)) AS numerator_value,+ any(coalesce(denominator_agg.value, 0)) AS denominator_value+ FROM exposures+ LEFT JOIN numerator_agg ON equals(exposures.entity_id, numerator_agg.entity_id)+ LEFT JOIN denominator_agg ON equals(exposures.entity_id, denominator_agg.entity_id)+ GROUP BY exposures.variant,+ exposures.entity_id),+ percentiles AS+ (SELECT min(entity_metrics.numerator_value) AS numerator_lower_bound,+ quantileExact(0.9)(entity_metrics.numerator_value) AS numerator_upper_bound,+ min(entity_metrics.denominator_value) AS denominator_lower_bound,+ quantileExact(0.9)(entity_metrics.denominator_value) AS denominator_upper_bound+ FROM entity_metrics),+ winsorized_entity_metrics AS+ (SELECT entity_metrics.entity_id AS entity_id,+ entity_metrics.variant AS variant,+ least(greatest(percentiles.numerator_lower_bound, entity_metrics.numerator_value), percentiles.numerator_upper_bound) AS numerator_value,+ least(greatest(percentiles.denominator_lower_bound, entity_metrics.denominator_value), percentiles.denominator_upper_bound) AS denominator_value+ FROM entity_metrics+ CROSS JOIN percentiles)+ SELECT winsorized_entity_metrics.variant AS variant,+ count(winsorized_entity_metrics.entity_id) AS num_users,+ sum(winsorized_entity_metrics.numerator_value) AS total_sum,+ sum(power(winsorized_entity_metrics.numerator_value, 2)) AS total_sum_of_squares,+ sum(winsorized_entity_metrics.denominator_value) AS denominator_sum,+ sum(power(winsorized_entity_metrics.denominator_value, 2)) AS denominator_sum_squares,+ sum(multiply(winsorized_entity_metrics.numerator_value, winsorized_entity_metrics.denominator_value)) AS numerator_denominator_sum_product+ FROM winsorized_entity_metrics+ GROUP BY winsorized_entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentRatioMetric.test_outlier_handling_numerator_only_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ numerator_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.amount, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ denominator_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'pageview'))),+ numerator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ sum(coalesce(accurateCastOrNull(numerator_events.value, 'Float64'), 0)) AS value+ FROM numerator_events+ JOIN exposures ON equals(exposures.entity_id, numerator_events.entity_id)+ WHERE greaterOrEquals(numerator_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC'))+ GROUP BY exposures.entity_id),+ denominator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ sum(coalesce(accurateCastOrNull(denominator_events.value, 'Float64'), 0)) AS value+ FROM denominator_events+ JOIN exposures ON equals(exposures.entity_id, denominator_events.entity_id)+ WHERE greaterOrEquals(denominator_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC'))+ GROUP BY exposures.entity_id),+ entity_metrics AS+ (SELECT exposures.variant AS variant,+ exposures.entity_id AS entity_id,+ any(coalesce(numerator_agg.value, 0)) AS numerator_value,+ any(coalesce(denominator_agg.value, 0)) AS denominator_value+ FROM exposures+ LEFT JOIN numerator_agg ON equals(exposures.entity_id, numerator_agg.entity_id)+ LEFT JOIN denominator_agg ON equals(exposures.entity_id, denominator_agg.entity_id)+ GROUP BY exposures.variant,+ exposures.entity_id),+ percentiles AS+ (SELECT min(entity_metrics.numerator_value) AS numerator_lower_bound,+ quantileExact(0.9)(entity_metrics.numerator_value) AS numerator_upper_bound,+ min(entity_metrics.denominator_value) AS denominator_lower_bound,+ max(entity_metrics.denominator_value) AS denominator_upper_bound+ FROM entity_metrics),+ winsorized_entity_metrics AS+ (SELECT entity_metrics.entity_id AS entity_id,+ entity_metrics.variant AS variant,+ least(greatest(percentiles.numerator_lower_bound, entity_metrics.numerator_value), percentiles.numerator_upper_bound) AS numerator_value,+ least(greatest(percentiles.denominator_lower_bound, entity_metrics.denominator_value), percentiles.denominator_upper_bound) AS denominator_value+ FROM entity_metrics+ CROSS JOIN percentiles)+ SELECT winsorized_entity_metrics.variant AS variant,+ count(winsorized_entity_metrics.entity_id) AS num_users,+ sum(winsorized_entity_metrics.numerator_value) AS total_sum,+ sum(power(winsorized_entity_metrics.numerator_value, 2)) AS total_sum_of_squares,+ sum(winsorized_entity_metrics.denominator_value) AS denominator_sum,+ sum(power(winsorized_entity_metrics.denominator_value, 2)) AS denominator_sum_squares,+ sum(multiply(winsorized_entity_metrics.numerator_value, winsorized_entity_metrics.denominator_value)) AS numerator_denominator_sum_product+ FROM winsorized_entity_metrics+ GROUP BY winsorized_entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentRatioMetric.test_outlier_handling_with_breakdown[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id,+ argMin(coalesce(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$browser`)), if(isNull(nullIf(toString(events.properties.`$browser`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$browser`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$browser`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$browser`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$browser`), '[]'), '{}'), toString(events.properties.`$browser`)))))), '$$_posthog_breakdown_null_$$'), toTimeZone(events.timestamp, 'UTC')) AS breakdown_value_1+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ numerator_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.amount, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ denominator_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'pageview'))),+ numerator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ sum(coalesce(accurateCastOrNull(numerator_events.value, 'Float64'), 0)) AS value+ FROM numerator_events+ JOIN exposures ON equals(exposures.entity_id, numerator_events.entity_id)+ WHERE greaterOrEquals(numerator_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC'))+ GROUP BY exposures.entity_id),+ denominator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ sum(coalesce(accurateCastOrNull(denominator_events.value, 'Float64'), 0)) AS value+ FROM denominator_events+ JOIN exposures ON equals(exposures.entity_id, denominator_events.entity_id)+ WHERE greaterOrEquals(denominator_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC'))+ GROUP BY exposures.entity_id),+ entity_metrics AS+ (SELECT exposures.variant AS variant,+ exposures.entity_id AS entity_id,+ any(coalesce(numerator_agg.value, 0)) AS numerator_value,+ any(coalesce(denominator_agg.value, 0)) AS denominator_value,+ exposures.breakdown_value_1 AS breakdown_value_1+ FROM exposures+ LEFT JOIN numerator_agg ON equals(exposures.entity_id, numerator_agg.entity_id)+ LEFT JOIN denominator_agg ON equals(exposures.entity_id, denominator_agg.entity_id)+ GROUP BY exposures.variant,+ exposures.entity_id,+ exposures.breakdown_value_1),+ percentiles AS+ (SELECT min(entity_metrics.numerator_value) AS numerator_lower_bound,+ quantileExact(0.95)(entity_metrics.numerator_value) AS numerator_upper_bound,+ min(entity_metrics.denominator_value) AS denominator_lower_bound,+ max(entity_metrics.denominator_value) AS denominator_upper_bound,+ entity_metrics.breakdown_value_1 AS breakdown_value_1+ FROM entity_metrics+ GROUP BY entity_metrics.breakdown_value_1),+ winsorized_entity_metrics AS+ (SELECT entity_metrics.entity_id AS entity_id,+ entity_metrics.variant AS variant,+ least(greatest(percentiles.numerator_lower_bound, entity_metrics.numerator_value), percentiles.numerator_upper_bound) AS numerator_value,+ least(greatest(percentiles.denominator_lower_bound, entity_metrics.denominator_value), percentiles.denominator_upper_bound) AS denominator_value,+ entity_metrics.breakdown_value_1 AS breakdown_value_1+ FROM entity_metrics+ JOIN percentiles ON equals(percentiles.breakdown_value_1, entity_metrics.breakdown_value_1))+ SELECT winsorized_entity_metrics.variant AS variant,+ winsorized_entity_metrics.breakdown_value_1 AS breakdown_value_1,+ count(winsorized_entity_metrics.entity_id) AS num_users,+ sum(winsorized_entity_metrics.numerator_value) AS total_sum,+ sum(power(winsorized_entity_metrics.numerator_value, 2)) AS total_sum_of_squares,+ sum(winsorized_entity_metrics.denominator_value) AS denominator_sum,+ sum(power(winsorized_entity_metrics.denominator_value, 2)) AS denominator_sum_squares,+ sum(multiply(winsorized_entity_metrics.numerator_value, winsorized_entity_metrics.denominator_value)) AS numerator_denominator_sum_product+ FROM winsorized_entity_metrics+ GROUP BY winsorized_entity_metrics.variant,+ winsorized_entity_metrics.breakdown_value_1+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentRatioMetric.test_ratio_metric_action_and_event_sources_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ numerator_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.amount, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ denominator_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'pageview'))),+ numerator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ sum(coalesce(accurateCastOrNull(numerator_events.value, 'Float64'), 0)) AS value+ FROM numerator_events+ JOIN exposures ON equals(exposures.entity_id, numerator_events.entity_id)+ WHERE greaterOrEquals(numerator_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC'))+ GROUP BY exposures.entity_id),+ denominator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ sum(coalesce(accurateCastOrNull(denominator_events.value, 'Float64'), 0)) AS value+ FROM denominator_events+ JOIN exposures ON equals(exposures.entity_id, denominator_events.entity_id)+ WHERE greaterOrEquals(denominator_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC'))+ GROUP BY exposures.entity_id),+ entity_metrics AS+ (SELECT exposures.variant AS variant,+ exposures.entity_id AS entity_id,+ any(coalesce(numerator_agg.value, 0)) AS numerator_value,+ any(coalesce(denominator_agg.value, 0)) AS denominator_value+ FROM exposures+ LEFT JOIN numerator_agg ON equals(exposures.entity_id, numerator_agg.entity_id)+ LEFT JOIN denominator_agg ON equals(exposures.entity_id, denominator_agg.entity_id)+ GROUP BY exposures.variant,+ exposures.entity_id)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.numerator_value) AS total_sum,+ sum(power(entity_metrics.numerator_value, 2)) AS total_sum_of_squares,+ sum(entity_metrics.denominator_value) AS denominator_sum,+ sum(power(entity_metrics.denominator_value, 2)) AS denominator_sum_squares,+ sum(multiply(entity_metrics.numerator_value, entity_metrics.denominator_value)) AS numerator_denominator_sum_product+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentRatioMetric.test_ratio_metric_different_math_types_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ numerator_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.amount, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ denominator_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.properties.`$session_id` AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'pageview'))),+ numerator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ coalesce(avg(accurateCastOrNull(numerator_events.value, 'Float64')), 0) AS value+ FROM numerator_events+ JOIN exposures ON equals(exposures.entity_id, numerator_events.entity_id)+ WHERE greaterOrEquals(numerator_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC'))+ GROUP BY exposures.entity_id),+ denominator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ accurateCastOrNull(count(DISTINCT multiIf(isNull(denominator_events.value), NULL, and(in(toTypeName(denominator_events.value), tuple('UUID', 'Nullable(UUID)')), equals(reinterpretAsUInt128(assumeNotNull(denominator_events.value)), 0)), NULL, equals(toString(denominator_events.value), ''), NULL, denominator_events.value)), 'Float64') AS value+ FROM denominator_events+ JOIN exposures ON equals(exposures.entity_id, denominator_events.entity_id)+ WHERE greaterOrEquals(denominator_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC'))+ GROUP BY exposures.entity_id),+ entity_metrics AS+ (SELECT exposures.variant AS variant,+ exposures.entity_id AS entity_id,+ any(coalesce(numerator_agg.value, 0)) AS numerator_value,+ any(coalesce(denominator_agg.value, 0)) AS denominator_value+ FROM exposures+ LEFT JOIN numerator_agg ON equals(exposures.entity_id, numerator_agg.entity_id)+ LEFT JOIN denominator_agg ON equals(exposures.entity_id, denominator_agg.entity_id)+ GROUP BY exposures.variant,+ exposures.entity_id)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.numerator_value) AS total_sum,+ sum(power(entity_metrics.numerator_value, 2)) AS total_sum_of_squares,+ sum(entity_metrics.denominator_value) AS denominator_sum,+ sum(power(entity_metrics.denominator_value, 2)) AS denominator_sum_squares,+ sum(multiply(entity_metrics.numerator_value, entity_metrics.denominator_value)) AS numerator_denominator_sum_product+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentRatioMetric.test_ratio_metric_same_event_different_properties_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ numerator_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.revenue, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ denominator_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.quantity, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ numerator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ sum(coalesce(accurateCastOrNull(numerator_events.value, 'Float64'), 0)) AS value+ FROM numerator_events+ JOIN exposures ON equals(exposures.entity_id, numerator_events.entity_id)+ WHERE greaterOrEquals(numerator_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC'))+ GROUP BY exposures.entity_id),+ denominator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ sum(coalesce(accurateCastOrNull(denominator_events.value, 'Float64'), 0)) AS value+ FROM denominator_events+ JOIN exposures ON equals(exposures.entity_id, denominator_events.entity_id)+ WHERE greaterOrEquals(denominator_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC'))+ GROUP BY exposures.entity_id),+ entity_metrics AS+ (SELECT exposures.variant AS variant,+ exposures.entity_id AS entity_id,+ any(coalesce(numerator_agg.value, 0)) AS numerator_value,+ any(coalesce(denominator_agg.value, 0)) AS denominator_value+ FROM exposures+ LEFT JOIN numerator_agg ON equals(exposures.entity_id, numerator_agg.entity_id)+ LEFT JOIN denominator_agg ON equals(exposures.entity_id, denominator_agg.entity_id)+ GROUP BY exposures.variant,+ exposures.entity_id)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.numerator_value) AS total_sum,+ sum(power(entity_metrics.numerator_value, 2)) AS total_sum_of_squares,+ sum(entity_metrics.denominator_value) AS denominator_sum,+ sum(power(entity_metrics.denominator_value, 2)) AS denominator_sum_squares,+ sum(multiply(entity_metrics.numerator_value, entity_metrics.denominator_value)) AS numerator_denominator_sum_product+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentRatioMetric.test_ratio_metric_with_conversion_window_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ numerator_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.amount, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(3600))), equals(events.event, 'purchase'))),+ denominator_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(3600))), equals(events.event, 'pageview'))),+ numerator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ sum(coalesce(accurateCastOrNull(numerator_events.value, 'Float64'), 0)) AS value+ FROM numerator_events+ JOIN exposures ON equals(exposures.entity_id, numerator_events.entity_id)+ WHERE and(greaterOrEquals(numerator_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')), less(numerator_events.timestamp, plus(toTimeZone(exposures.first_exposure_time, 'UTC'), toIntervalSecond(3600))))+ GROUP BY exposures.entity_id),+ denominator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ sum(coalesce(accurateCastOrNull(denominator_events.value, 'Float64'), 0)) AS value+ FROM denominator_events+ JOIN exposures ON equals(exposures.entity_id, denominator_events.entity_id)+ WHERE and(greaterOrEquals(denominator_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')), less(denominator_events.timestamp, plus(toTimeZone(exposures.first_exposure_time, 'UTC'), toIntervalSecond(3600))))+ GROUP BY exposures.entity_id),+ entity_metrics AS+ (SELECT exposures.variant AS variant,+ exposures.entity_id AS entity_id,+ any(coalesce(numerator_agg.value, 0)) AS numerator_value,+ any(coalesce(denominator_agg.value, 0)) AS denominator_value+ FROM exposures+ LEFT JOIN numerator_agg ON equals(exposures.entity_id, numerator_agg.entity_id)+ LEFT JOIN denominator_agg ON equals(exposures.entity_id, denominator_agg.entity_id)+ GROUP BY exposures.variant,+ exposures.entity_id)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.numerator_value) AS total_sum,+ sum(power(entity_metrics.numerator_value, 2)) AS total_sum_of_squares,+ sum(entity_metrics.denominator_value) AS denominator_sum,+ sum(power(entity_metrics.denominator_value, 2)) AS denominator_sum_squares,+ sum(multiply(entity_metrics.numerator_value, entity_metrics.denominator_value)) AS numerator_denominator_sum_product+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentRatioMetric.test_ratio_metric_with_parametric_aggregation_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-10 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ numerator_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^amount)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^amount)), if(isNull(nullIf(toString(events.properties.amount), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.amount, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.amount, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.amount), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.amount, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.amount, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.amount, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.amount), '[]'), '{}'), toString(events.properties.amount))))) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-10 00:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ denominator_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ events.distinct_id AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-10 00:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ numerator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ coalesce(quantile(0.95)(accurateCastOrNull(numerator_events.value, 'Float64')), 0) AS value+ FROM numerator_events+ JOIN exposures ON equals(exposures.entity_id, numerator_events.entity_id)+ WHERE greaterOrEquals(numerator_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC'))+ GROUP BY exposures.entity_id),+ denominator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ coalesce(accurateCastOrNull(uniqExact(denominator_events.value), 'Float64'), 0) AS value+ FROM denominator_events+ JOIN exposures ON equals(exposures.entity_id, denominator_events.entity_id)+ WHERE greaterOrEquals(denominator_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC'))+ GROUP BY exposures.entity_id),+ entity_metrics AS+ (SELECT exposures.variant AS variant,+ exposures.entity_id AS entity_id,+ any(coalesce(numerator_agg.value, 0)) AS numerator_value,+ any(coalesce(denominator_agg.value, 0)) AS denominator_value+ FROM exposures+ LEFT JOIN numerator_agg ON equals(exposures.entity_id, numerator_agg.entity_id)+ LEFT JOIN denominator_agg ON equals(exposures.entity_id, denominator_agg.entity_id)+ GROUP BY exposures.variant,+ exposures.entity_id)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.numerator_value) AS total_sum,+ sum(power(entity_metrics.numerator_value, 2)) AS total_sum_of_squares,+ sum(entity_metrics.denominator_value) AS denominator_sum,+ sum(power(entity_metrics.denominator_value, 2)) AS denominator_sum_squares,+ sum(multiply(entity_metrics.numerator_value, entity_metrics.denominator_value)) AS numerator_denominator_sum_product+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentRatioMetric.test_ratio_metric_zero_denominator_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ numerator_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(events.properties.amount, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'purchase'))),+ denominator_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ toTimeZone(events.timestamp, 'UTC') AS timestamp,+ coalesce(accurateCastOrNull(1, 'Float64'), 0) AS value+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'special_event'))),+ numerator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ sum(coalesce(accurateCastOrNull(numerator_events.value, 'Float64'), 0)) AS value+ FROM numerator_events+ JOIN exposures ON equals(exposures.entity_id, numerator_events.entity_id)+ WHERE greaterOrEquals(numerator_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC'))+ GROUP BY exposures.entity_id),+ denominator_agg AS+ (SELECT exposures.entity_id AS entity_id,+ sum(coalesce(accurateCastOrNull(denominator_events.value, 'Float64'), 0)) AS value+ FROM denominator_events+ JOIN exposures ON equals(exposures.entity_id, denominator_events.entity_id)+ WHERE greaterOrEquals(denominator_events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC'))+ GROUP BY exposures.entity_id),+ entity_metrics AS+ (SELECT exposures.variant AS variant,+ exposures.entity_id AS entity_id,+ any(coalesce(numerator_agg.value, 0)) AS numerator_value,+ any(coalesce(denominator_agg.value, 0)) AS denominator_value+ FROM exposures+ LEFT JOIN numerator_agg ON equals(exposures.entity_id, numerator_agg.entity_id)+ LEFT JOIN denominator_agg ON equals(exposures.entity_id, denominator_agg.entity_id)+ GROUP BY exposures.variant,+ exposures.entity_id)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.numerator_value) AS total_sum,+ sum(power(entity_metrics.numerator_value, 2)) AS total_sum_of_squares,+ sum(entity_metrics.denominator_value) AS denominator_sum,+ sum(power(entity_metrics.denominator_value, 2)) AS denominator_sum_squares,+ sum(multiply(entity_metrics.numerator_value, entity_metrics.denominator_value)) AS numerator_denominator_sum_product+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---diff --git a/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_retention_metric.new_events_schema.ambr b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_retention_metric.new_events_schema.ambrnew file mode 100644index 000000000000..287b153b9d37--- /dev/null+++ b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_retention_metric.new_events_schema.ambr@@ -0,0 +1,1337 @@+# serializer version: 1+# name: TestExperimentRetentionMetric.test_basic_retention_calculation_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ start_events AS+ (SELECT exposures.entity_id AS entity_id,+ min(toTimeZone(events.timestamp, 'UTC')) AS start_timestamp,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS start_uuid+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ INNER JOIN exposures ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), exposures.entity_id)+ WHERE and(equals(events.team_id, 99999), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'signup')), greaterOrEquals(events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id),+ completion_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ events.uuid AS completion_uuid,+ toTimeZone(events.timestamp, 'UTC') AS completion_timestamp+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(604800))), equals(events.event, 'login'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ max(if(and(isNotNull(toTimeZone(completion_events.completion_timestamp, 'UTC')), greaterOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(1))), lessOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(7)))), 1, 0)) AS value+ FROM exposures+ INNER JOIN start_events ON equals(exposures.entity_id, start_events.entity_id)+ LEFT JOIN completion_events ON and(equals(exposures.entity_id, completion_events.entity_id), and(greaterOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), toTimeZone(start_events.start_timestamp, 'UTC')), lessOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), plus(toTimeZone(start_events.start_timestamp, 'UTC'), toIntervalSecond(691200)))), notEquals(completion_events.completion_uuid, start_events.start_uuid))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares,+ count(entity_metrics.entity_id) AS denominator_sum,+ count(entity_metrics.entity_id) AS denominator_sum_squares,+ sum(entity_metrics.value) AS numerator_denominator_sum_product+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentRetentionMetric.test_only_count_matured_users_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-25 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ start_events AS+ (SELECT exposures.entity_id AS entity_id,+ min(toTimeZone(events.timestamp, 'UTC')) AS start_timestamp,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS start_uuid+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ INNER JOIN exposures ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), exposures.entity_id)+ WHERE and(equals(events.team_id, 99999), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-25 00:00:00', 'UTC')), toIntervalSecond(259200))), equals(events.event, 'signup')), and(greaterOrEquals(events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')), lessOrEquals(events.timestamp, plus(toTimeZone(exposures.first_exposure_time, 'UTC'), toIntervalSecond(259200)))))+ GROUP BY exposures.entity_id+ HAVING lessOrEquals(plus(min(toTimeZone(events.timestamp, 'UTC')), toIntervalSecond(604800)), parseDateTime64BestEffortOrNull('2020-01-25 12:00:00', 6, 'UTC'))),+ completion_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ events.uuid AS completion_uuid,+ toTimeZone(events.timestamp, 'UTC') AS completion_timestamp+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-25 00:00:00', 'UTC')), toIntervalSecond(864000))), equals(events.event, 'login'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ max(if(and(isNotNull(toTimeZone(completion_events.completion_timestamp, 'UTC')), greaterOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(1))), lessOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(7)))), 1, 0)) AS value+ FROM exposures+ INNER JOIN start_events ON equals(exposures.entity_id, start_events.entity_id)+ LEFT JOIN completion_events ON and(equals(exposures.entity_id, completion_events.entity_id), and(greaterOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), toTimeZone(start_events.start_timestamp, 'UTC')), lessOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), plus(toTimeZone(start_events.start_timestamp, 'UTC'), toIntervalSecond(691200)))), notEquals(completion_events.completion_uuid, start_events.start_uuid))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares,+ count(entity_metrics.entity_id) AS denominator_sum,+ count(entity_metrics.entity_id) AS denominator_sum_squares,+ sum(entity_metrics.value) AS numerator_denominator_sum_product+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentRetentionMetric.test_only_count_matured_users_anchors_on_start_event_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-25 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ start_events AS+ (SELECT exposures.entity_id AS entity_id,+ min(toTimeZone(events.timestamp, 'UTC')) AS start_timestamp,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS start_uuid+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ INNER JOIN exposures ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), exposures.entity_id)+ WHERE and(equals(events.team_id, 99999), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-25 00:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'signup')), greaterOrEquals(events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id+ HAVING lessOrEquals(plus(min(toTimeZone(events.timestamp, 'UTC')), toIntervalSecond(604800)), parseDateTime64BestEffortOrNull('2020-01-25 12:00:00', 6, 'UTC'))),+ completion_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ events.uuid AS completion_uuid,+ toTimeZone(events.timestamp, 'UTC') AS completion_timestamp+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-25 00:00:00', 'UTC')), toIntervalSecond(604800))), equals(events.event, 'login'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ max(if(and(isNotNull(toTimeZone(completion_events.completion_timestamp, 'UTC')), greaterOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(7))), lessOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(7)))), 1, 0)) AS value+ FROM exposures+ INNER JOIN start_events ON equals(exposures.entity_id, start_events.entity_id)+ LEFT JOIN completion_events ON and(equals(exposures.entity_id, completion_events.entity_id), and(greaterOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), toTimeZone(start_events.start_timestamp, 'UTC')), lessOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), plus(toTimeZone(start_events.start_timestamp, 'UTC'), toIntervalSecond(691200)))), notEquals(completion_events.completion_uuid, start_events.start_uuid))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares,+ count(entity_metrics.entity_id) AS denominator_sum,+ count(entity_metrics.entity_id) AS denominator_sum_squares,+ sum(entity_metrics.value) AS numerator_denominator_sum_product+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentRetentionMetric.test_only_count_matured_users_with_exposure_start[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL)), 1), '$multiple', any(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-25 00:00:00', 'UTC'))), equals(events.event, 'entered_experiment'), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL), ['control', 'test']))+ GROUP BY entity_id),+ start_events AS+ (SELECT exposures.entity_id AS entity_id,+ toTimeZone(exposures.first_exposure_time, 'UTC') AS start_timestamp,+ exposures.exposure_event_uuid AS start_uuid+ FROM exposures+ WHERE ifNull(lessOrEquals(plus(toTimeZone(exposures.first_exposure_time, 'UTC'), toIntervalSecond(604800)), parseDateTime64BestEffortOrNull('2020-01-25 12:00:00', 6, 'UTC')), 0)),+ completion_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ events.uuid AS completion_uuid,+ toTimeZone(events.timestamp, 'UTC') AS completion_timestamp+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-25 00:00:00', 'UTC')), toIntervalSecond(604800))), equals(events.event, 'return_visit'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ max(if(and(isNotNull(toTimeZone(completion_events.completion_timestamp, 'UTC')), greaterOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(7))), lessOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(7)))), 1, 0)) AS value+ FROM exposures+ INNER JOIN start_events ON equals(exposures.entity_id, start_events.entity_id)+ LEFT JOIN completion_events ON and(equals(exposures.entity_id, completion_events.entity_id), and(greaterOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), toTimeZone(start_events.start_timestamp, 'UTC')), lessOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), plus(toTimeZone(start_events.start_timestamp, 'UTC'), toIntervalSecond(691200)))), notEquals(completion_events.completion_uuid, start_events.start_uuid))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares,+ count(entity_metrics.entity_id) AS denominator_sum,+ count(entity_metrics.entity_id) AS denominator_sum_squares,+ sum(entity_metrics.value) AS numerator_denominator_sum_product+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentRetentionMetric.test_only_count_matured_users_without_conversion_window_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-25 00:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ start_events AS+ (SELECT exposures.entity_id AS entity_id,+ min(toTimeZone(events.timestamp, 'UTC')) AS start_timestamp,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS start_uuid+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ INNER JOIN exposures ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), exposures.entity_id)+ WHERE and(equals(events.team_id, 99999), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-25 00:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'signup')), greaterOrEquals(events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id+ HAVING lessOrEquals(plus(min(toTimeZone(events.timestamp, 'UTC')), toIntervalSecond(604800)), parseDateTime64BestEffortOrNull('2020-01-25 12:00:00', 6, 'UTC'))),+ completion_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ events.uuid AS completion_uuid,+ toTimeZone(events.timestamp, 'UTC') AS completion_timestamp+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-25 00:00:00', 'UTC')), toIntervalSecond(604800))), equals(events.event, 'login'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ max(if(and(isNotNull(toTimeZone(completion_events.completion_timestamp, 'UTC')), greaterOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(7))), lessOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(7)))), 1, 0)) AS value+ FROM exposures+ INNER JOIN start_events ON equals(exposures.entity_id, start_events.entity_id)+ LEFT JOIN completion_events ON and(equals(exposures.entity_id, completion_events.entity_id), and(greaterOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), toTimeZone(start_events.start_timestamp, 'UTC')), lessOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), plus(toTimeZone(start_events.start_timestamp, 'UTC'), toIntervalSecond(691200)))), notEquals(completion_events.completion_uuid, start_events.start_uuid))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares,+ count(entity_metrics.entity_id) AS denominator_sum,+ count(entity_metrics.entity_id) AS denominator_sum_squares,+ sum(entity_metrics.value) AS numerator_denominator_sum_product+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentRetentionMetric.test_retention_anchored_on_exposure_event_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL)), 1), '$multiple', any(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), equals(events.event, 'entered_experiment'), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL), ['control', 'test']))+ GROUP BY entity_id),+ start_events AS+ (SELECT exposures.entity_id AS entity_id,+ toTimeZone(exposures.first_exposure_time, 'UTC') AS start_timestamp,+ exposures.exposure_event_uuid AS start_uuid+ FROM exposures+ WHERE 1),+ completion_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ events.uuid AS completion_uuid,+ toTimeZone(events.timestamp, 'UTC') AS completion_timestamp+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(604800))), equals(events.event, 'return_visit'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ max(if(and(isNotNull(toTimeZone(completion_events.completion_timestamp, 'UTC')), greaterOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(1))), lessOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(7)))), 1, 0)) AS value+ FROM exposures+ INNER JOIN start_events ON equals(exposures.entity_id, start_events.entity_id)+ LEFT JOIN completion_events ON and(equals(exposures.entity_id, completion_events.entity_id), and(greaterOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), toTimeZone(start_events.start_timestamp, 'UTC')), lessOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), plus(toTimeZone(start_events.start_timestamp, 'UTC'), toIntervalSecond(691200)))), notEquals(completion_events.completion_uuid, start_events.start_uuid))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares,+ count(entity_metrics.entity_id) AS denominator_sum,+ count(entity_metrics.entity_id) AS denominator_sum_squares,+ sum(entity_metrics.value) AS numerator_denominator_sum_product+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentRetentionMetric.test_retention_day_zero_same_day_as_start_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ start_events AS+ (SELECT exposures.entity_id AS entity_id,+ min(toTimeZone(events.timestamp, 'UTC')) AS start_timestamp,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS start_uuid+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ INNER JOIN exposures ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), exposures.entity_id)+ WHERE and(equals(events.team_id, 99999), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'signup')), greaterOrEquals(events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id),+ completion_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ events.uuid AS completion_uuid,+ toTimeZone(events.timestamp, 'UTC') AS completion_timestamp+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'first_action'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ max(if(and(isNotNull(toTimeZone(completion_events.completion_timestamp, 'UTC')), greaterOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(0))), lessOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(0)))), 1, 0)) AS value+ FROM exposures+ INNER JOIN start_events ON equals(exposures.entity_id, start_events.entity_id)+ LEFT JOIN completion_events ON and(equals(exposures.entity_id, completion_events.entity_id), and(greaterOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), toTimeZone(start_events.start_timestamp, 'UTC')), lessOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), plus(toTimeZone(start_events.start_timestamp, 'UTC'), toIntervalSecond(86400)))), notEquals(completion_events.completion_uuid, start_events.start_uuid))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares,+ count(entity_metrics.entity_id) AS denominator_sum,+ count(entity_metrics.entity_id) AS denominator_sum_squares,+ sum(entity_metrics.value) AS numerator_denominator_sum_product+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentRetentionMetric.test_retention_exposure_start_excludes_exposure_as_its_own_completion_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL)), 1), '$multiple', any(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), equals(events.event, 'entered_experiment'), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(has(events.properties.`$feature_flags`, 'test-experiment'), if(ifNull(equals(events.properties.`$feature_flags`['test-experiment'], '$false'), 0), 'false', events.properties.`$feature_flags`['test-experiment']), NULL), ['control', 'test']))+ GROUP BY entity_id),+ start_events AS+ (SELECT exposures.entity_id AS entity_id,+ toTimeZone(exposures.first_exposure_time, 'UTC') AS start_timestamp,+ exposures.exposure_event_uuid AS start_uuid+ FROM exposures+ WHERE 1),+ completion_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ events.uuid AS completion_uuid,+ toTimeZone(events.timestamp, 'UTC') AS completion_timestamp+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(604800))), equals(events.event, 'entered_experiment'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ max(if(and(isNotNull(toTimeZone(completion_events.completion_timestamp, 'UTC')), greaterOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(0))), lessOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(7)))), 1, 0)) AS value+ FROM exposures+ INNER JOIN start_events ON equals(exposures.entity_id, start_events.entity_id)+ LEFT JOIN completion_events ON and(equals(exposures.entity_id, completion_events.entity_id), and(greaterOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), toTimeZone(start_events.start_timestamp, 'UTC')), lessOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), plus(toTimeZone(start_events.start_timestamp, 'UTC'), toIntervalSecond(691200)))), notEquals(completion_events.completion_uuid, start_events.start_uuid))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares,+ count(entity_metrics.entity_id) AS denominator_sum,+ count(entity_metrics.entity_id) AS denominator_sum_squares,+ sum(entity_metrics.value) AS numerator_denominator_sum_product+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentRetentionMetric.test_retention_first_seen_vs_last_seen_0_direct[new_events_schema.1]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ start_events AS+ (SELECT exposures.entity_id AS entity_id,+ max(toTimeZone(events.timestamp, 'UTC')) AS start_timestamp,+ argMax(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS start_uuid+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ INNER JOIN exposures ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), exposures.entity_id)+ WHERE and(equals(events.team_id, 99999), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'signup')), greaterOrEquals(events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id),+ completion_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ events.uuid AS completion_uuid,+ toTimeZone(events.timestamp, 'UTC') AS completion_timestamp+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(604800))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ max(if(and(isNotNull(toTimeZone(completion_events.completion_timestamp, 'UTC')), greaterOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(1))), lessOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(7)))), 1, 0)) AS value+ FROM exposures+ INNER JOIN start_events ON equals(exposures.entity_id, start_events.entity_id)+ LEFT JOIN completion_events ON and(equals(exposures.entity_id, completion_events.entity_id), and(greaterOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), toTimeZone(start_events.start_timestamp, 'UTC')), lessOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), plus(toTimeZone(start_events.start_timestamp, 'UTC'), toIntervalSecond(691200)))), notEquals(completion_events.completion_uuid, start_events.start_uuid))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares,+ count(entity_metrics.entity_id) AS denominator_sum,+ count(entity_metrics.entity_id) AS denominator_sum_squares,+ sum(entity_metrics.value) AS numerator_denominator_sum_product+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentRetentionMetric.test_retention_first_seen_vs_last_seen_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ start_events AS+ (SELECT exposures.entity_id AS entity_id,+ min(toTimeZone(events.timestamp, 'UTC')) AS start_timestamp,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS start_uuid+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ INNER JOIN exposures ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), exposures.entity_id)+ WHERE and(equals(events.team_id, 99999), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'signup')), greaterOrEquals(events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id),+ completion_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ events.uuid AS completion_uuid,+ toTimeZone(events.timestamp, 'UTC') AS completion_timestamp+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(604800))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ max(if(and(isNotNull(toTimeZone(completion_events.completion_timestamp, 'UTC')), greaterOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(1))), lessOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(7)))), 1, 0)) AS value+ FROM exposures+ INNER JOIN start_events ON equals(exposures.entity_id, start_events.entity_id)+ LEFT JOIN completion_events ON and(equals(exposures.entity_id, completion_events.entity_id), and(greaterOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), toTimeZone(start_events.start_timestamp, 'UTC')), lessOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), plus(toTimeZone(start_events.start_timestamp, 'UTC'), toIntervalSecond(691200)))), notEquals(completion_events.completion_uuid, start_events.start_uuid))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares,+ count(entity_metrics.entity_id) AS denominator_sum,+ count(entity_metrics.entity_id) AS denominator_sum_squares,+ sum(entity_metrics.value) AS numerator_denominator_sum_product+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentRetentionMetric.test_retention_hour_based_same_hour_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ start_events AS+ (SELECT exposures.entity_id AS entity_id,+ min(toTimeZone(events.timestamp, 'UTC')) AS start_timestamp,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS start_uuid+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ INNER JOIN exposures ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), exposures.entity_id)+ WHERE and(equals(events.team_id, 99999), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'session_start')), greaterOrEquals(events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id),+ completion_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ events.uuid AS completion_uuid,+ toTimeZone(events.timestamp, 'UTC') AS completion_timestamp+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'key_action'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ max(if(and(isNotNull(toTimeZone(completion_events.completion_timestamp, 'UTC')), greaterOrEquals(toStartOfHour(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfHour(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalHour(0))), lessOrEquals(toStartOfHour(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfHour(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalHour(0)))), 1, 0)) AS value+ FROM exposures+ INNER JOIN start_events ON equals(exposures.entity_id, start_events.entity_id)+ LEFT JOIN completion_events ON and(equals(exposures.entity_id, completion_events.entity_id), and(greaterOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), toTimeZone(start_events.start_timestamp, 'UTC')), lessOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), plus(toTimeZone(start_events.start_timestamp, 'UTC'), toIntervalSecond(3600)))), notEquals(completion_events.completion_uuid, start_events.start_uuid))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares,+ count(entity_metrics.entity_id) AS denominator_sum,+ count(entity_metrics.entity_id) AS denominator_sum_squares,+ sum(entity_metrics.value) AS numerator_denominator_sum_product+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentRetentionMetric.test_retention_multiple_completions_in_window_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ start_events AS+ (SELECT exposures.entity_id AS entity_id,+ min(toTimeZone(events.timestamp, 'UTC')) AS start_timestamp,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS start_uuid+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ INNER JOIN exposures ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), exposures.entity_id)+ WHERE and(equals(events.team_id, 99999), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'signup')), greaterOrEquals(events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id),+ completion_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ events.uuid AS completion_uuid,+ toTimeZone(events.timestamp, 'UTC') AS completion_timestamp+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(604800))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ max(if(and(isNotNull(toTimeZone(completion_events.completion_timestamp, 'UTC')), greaterOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(1))), lessOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(7)))), 1, 0)) AS value+ FROM exposures+ INNER JOIN start_events ON equals(exposures.entity_id, start_events.entity_id)+ LEFT JOIN completion_events ON and(equals(exposures.entity_id, completion_events.entity_id), and(greaterOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), toTimeZone(start_events.start_timestamp, 'UTC')), lessOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), plus(toTimeZone(start_events.start_timestamp, 'UTC'), toIntervalSecond(691200)))), notEquals(completion_events.completion_uuid, start_events.start_uuid))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares,+ count(entity_metrics.entity_id) AS denominator_sum,+ count(entity_metrics.entity_id) AS denominator_sum_squares,+ sum(entity_metrics.value) AS numerator_denominator_sum_product+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentRetentionMetric.test_retention_multiple_variants_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment-3-variants'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test_a', 'test_b']))+ GROUP BY entity_id),+ start_events AS+ (SELECT exposures.entity_id AS entity_id,+ min(toTimeZone(events.timestamp, 'UTC')) AS start_timestamp,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS start_uuid+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ INNER JOIN exposures ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), exposures.entity_id)+ WHERE and(equals(events.team_id, 99999), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'app_open')), greaterOrEquals(events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id),+ completion_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ events.uuid AS completion_uuid,+ toTimeZone(events.timestamp, 'UTC') AS completion_timestamp+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(604800))), equals(events.event, 'feature_used'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ max(if(and(isNotNull(toTimeZone(completion_events.completion_timestamp, 'UTC')), greaterOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(1))), lessOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(7)))), 1, 0)) AS value+ FROM exposures+ INNER JOIN start_events ON equals(exposures.entity_id, start_events.entity_id)+ LEFT JOIN completion_events ON and(equals(exposures.entity_id, completion_events.entity_id), and(greaterOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), toTimeZone(start_events.start_timestamp, 'UTC')), lessOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), plus(toTimeZone(start_events.start_timestamp, 'UTC'), toIntervalSecond(691200)))), notEquals(completion_events.completion_uuid, start_events.start_uuid))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares,+ count(entity_metrics.entity_id) AS denominator_sum,+ count(entity_metrics.entity_id) AS denominator_sum_squares,+ sum(entity_metrics.value) AS numerator_denominator_sum_product+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentRetentionMetric.test_retention_no_completion_events_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ start_events AS+ (SELECT exposures.entity_id AS entity_id,+ min(toTimeZone(events.timestamp, 'UTC')) AS start_timestamp,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS start_uuid+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ INNER JOIN exposures ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), exposures.entity_id)+ WHERE and(equals(events.team_id, 99999), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'signup')), greaterOrEquals(events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id),+ completion_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ events.uuid AS completion_uuid,+ toTimeZone(events.timestamp, 'UTC') AS completion_timestamp+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(604800))), equals(events.event, 'purchase'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ max(if(and(isNotNull(toTimeZone(completion_events.completion_timestamp, 'UTC')), greaterOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(1))), lessOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(7)))), 1, 0)) AS value+ FROM exposures+ INNER JOIN start_events ON equals(exposures.entity_id, start_events.entity_id)+ LEFT JOIN completion_events ON and(equals(exposures.entity_id, completion_events.entity_id), and(greaterOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), toTimeZone(start_events.start_timestamp, 'UTC')), lessOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), plus(toTimeZone(start_events.start_timestamp, 'UTC'), toIntervalSecond(691200)))), notEquals(completion_events.completion_uuid, start_events.start_uuid))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares,+ count(entity_metrics.entity_id) AS denominator_sum,+ count(entity_metrics.entity_id) AS denominator_sum_squares,+ sum(entity_metrics.value) AS numerator_denominator_sum_product+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentRetentionMetric.test_retention_start_event_before_exposure_not_excluded_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ start_events AS+ (SELECT exposures.entity_id AS entity_id,+ min(toTimeZone(events.timestamp, 'UTC')) AS start_timestamp,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS start_uuid+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ INNER JOIN exposures ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), exposures.entity_id)+ WHERE and(equals(events.team_id, 99999), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'action_performed')), greaterOrEquals(events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id),+ completion_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ events.uuid AS completion_uuid,+ toTimeZone(events.timestamp, 'UTC') AS completion_timestamp+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2020-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2020-01-15 12:00:00', 'UTC')), toIntervalSecond(604800))), equals(events.event, 'action_performed'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ max(if(and(isNotNull(toTimeZone(completion_events.completion_timestamp, 'UTC')), greaterOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(1))), lessOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(7)))), 1, 0)) AS value+ FROM exposures+ INNER JOIN start_events ON equals(exposures.entity_id, start_events.entity_id)+ LEFT JOIN completion_events ON and(equals(exposures.entity_id, completion_events.entity_id), and(greaterOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), toTimeZone(start_events.start_timestamp, 'UTC')), lessOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), plus(toTimeZone(start_events.start_timestamp, 'UTC'), toIntervalSecond(691200)))), notEquals(completion_events.completion_uuid, start_events.start_uuid))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares,+ count(entity_metrics.entity_id) AS denominator_sum,+ count(entity_metrics.entity_id) AS denominator_sum_squares,+ sum(entity_metrics.value) AS numerator_denominator_sum_product+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentRetentionMetric.test_retention_window_boundaries_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ start_events AS+ (SELECT exposures.entity_id AS entity_id,+ min(toTimeZone(events.timestamp, 'UTC')) AS start_timestamp,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS start_uuid+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ INNER JOIN exposures ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), exposures.entity_id)+ WHERE and(equals(events.team_id, 99999), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, 'signup')), greaterOrEquals(events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')))+ GROUP BY exposures.entity_id),+ completion_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ events.uuid AS completion_uuid,+ toTimeZone(events.timestamp, 'UTC') AS completion_timestamp+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(1209600))), equals(events.event, 'return_visit'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ max(if(and(isNotNull(toTimeZone(completion_events.completion_timestamp, 'UTC')), greaterOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(7))), lessOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(14)))), 1, 0)) AS value+ FROM exposures+ INNER JOIN start_events ON equals(exposures.entity_id, start_events.entity_id)+ LEFT JOIN completion_events ON and(equals(exposures.entity_id, completion_events.entity_id), and(greaterOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), toTimeZone(start_events.start_timestamp, 'UTC')), lessOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), plus(toTimeZone(start_events.start_timestamp, 'UTC'), toIntervalSecond(1296000)))), notEquals(completion_events.completion_uuid, start_events.start_uuid))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares,+ count(entity_metrics.entity_id) AS denominator_sum,+ count(entity_metrics.entity_id) AS denominator_sum_squares,+ sum(entity_metrics.value) AS numerator_denominator_sum_product+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---+# name: TestExperimentRetentionMetric.test_retention_with_conversion_window_0_direct[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ start_events AS+ (SELECT exposures.entity_id AS entity_id,+ min(toTimeZone(events.timestamp, 'UTC')) AS start_timestamp,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS start_uuid+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ INNER JOIN exposures ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), exposures.entity_id)+ WHERE and(equals(events.team_id, 99999), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(7200))), equals(events.event, 'signup')), and(greaterOrEquals(events.timestamp, toTimeZone(exposures.first_exposure_time, 'UTC')), lessOrEquals(events.timestamp, plus(toTimeZone(exposures.first_exposure_time, 'UTC'), toIntervalSecond(7200)))))+ GROUP BY exposures.entity_id),+ completion_events AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ events.uuid AS completion_uuid,+ toTimeZone(events.timestamp, 'UTC') AS completion_timestamp+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(266400))), equals(events.event, 'return_visit'))),+ entity_metrics AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ max(if(and(isNotNull(toTimeZone(completion_events.completion_timestamp, 'UTC')), greaterOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(1))), lessOrEquals(toStartOfDay(toTimeZone(completion_events.completion_timestamp, 'UTC')), plus(toStartOfDay(toTimeZone(start_events.start_timestamp, 'UTC')), toIntervalDay(3)))), 1, 0)) AS value+ FROM exposures+ INNER JOIN start_events ON equals(exposures.entity_id, start_events.entity_id)+ LEFT JOIN completion_events ON and(equals(exposures.entity_id, completion_events.entity_id), and(greaterOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), toTimeZone(start_events.start_timestamp, 'UTC')), lessOrEquals(toTimeZone(completion_events.completion_timestamp, 'UTC'), plus(toTimeZone(start_events.start_timestamp, 'UTC'), toIntervalSecond(345600)))), notEquals(completion_events.completion_uuid, start_events.start_uuid))+ GROUP BY exposures.entity_id,+ exposures.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares,+ count(entity_metrics.entity_id) AS denominator_sum,+ count(entity_metrics.entity_id) AS denominator_sum_squares,+ sum(entity_metrics.value) AS numerator_denominator_sum_product+ FROM entity_metrics+ WHERE notEmpty(variant)+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---diff --git a/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_session_properties.new_events_schema.ambr b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_session_properties.new_events_schema.ambrnew file mode 100644index 000000000000..0cac876f322b--- /dev/null+++ b/products/experiments/backend/hogql_queries/test/experiment_query_runner/__snapshots__/test_session_properties.new_events_schema.ambr@@ -0,0 +1,78 @@+# serializer version: 1+# name: TestExperimentSessionPropertyMetrics.test_session_duration_not_multiplied_across_events[new_events_schema]+ '''+ WITH exposures AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ if(greater(uniqExact(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`)))))), 1), '$multiple', any(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))))) AS variant,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_exposure_time,+ max(toTimeZone(events.timestamp, 'UTC')) AS last_exposure_time,+ argMin(events.uuid, toTimeZone(events.timestamp, 'UTC')) AS exposure_event_uuid,+ argMin(events.properties.`$session_id`, toTimeZone(events.timestamp, 'UTC')) AS exposure_session_id+ FROM events_json AS events+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), lessOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC'))), and(equals(events.event, '$feature_flag_called'), ifNull(equals(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag`)), if(isNull(nullIf(toString(events.properties.`$feature_flag`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag`), '[]'), '{}'), toString(events.properties.`$feature_flag`))))), 'test-experiment'), 0)), ifNull(notILike(toString(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.person_properties.^email)), if(isNull(nullIf(toString(events.person_properties.email), '')), NULL, if(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.person_properties.email, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.person_properties.email), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.person_properties.email, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.person_properties.email), '[]'), '{}'), toString(events.person_properties.email)))))), '%@posthog.com%'), 1), in(if(notEquals(JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), '{}'), JSONStripEmptyStringsAndNulls(toJSONString(events.properties.^`$feature_flag_response`)), if(isNull(nullIf(toString(events.properties.`$feature_flag_response`), '')), NULL, if(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'DateTime'), concat(ifNull(toString(replaceOne(substring(toString(accurateCastOrNull(events.properties.`$feature_flag_response`, 'DateTime64(9, \'UTC\')')), 1, 19), ' ', 'T')), ''), ifNull(toString(substring(toString(events.properties.`$feature_flag_response`), 20, 10)), ''), 'Z'), if(or(startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Array'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Map'), startsWith(dynamicType(accurateCast(events.properties.`$feature_flag_response`, 'Dynamic')), 'Tuple')), nullIf(nullIf(toJSONString(events.properties.`$feature_flag_response`), '[]'), '{}'), toString(events.properties.`$feature_flag_response`))))), ['control', 'test']))+ GROUP BY entity_id),+ metric_events_by_session AS+ (SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS entity_id,+ events.properties.`$session_id` AS session_id,+ any(events__session.`$session_duration`) AS session_value,+ min(toTimeZone(events.timestamp, 'UTC')) AS first_event_timestamp+ FROM events_json AS events+ LEFT JOIN+ (SELECT dateDiff('second', min(toTimeZone(raw_sessions.min_timestamp, 'UTC')), max(toTimeZone(raw_sessions.max_timestamp, 'UTC'))) AS `$session_duration`,+ raw_sessions.session_id_v7 AS session_id_v7+ FROM raw_sessions+ WHERE and(equals(raw_sessions.team_id, 99999), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC')), toIntervalDay(3))), lessOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), plus(plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0)), toIntervalDay(3))))+ GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(toUUIDOrNull(events.properties.`$session_id`)), events__session.session_id_v7)+ LEFT OUTER JOIN+ (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,+ person_distinct_id_overrides.distinct_id AS distinct_id+ FROM person_distinct_id_overrides+ WHERE equals(person_distinct_id_overrides.team_id, 99999)+ GROUP BY person_distinct_id_overrides.distinct_id+ HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id)+ WHERE and(equals(events.team_id, 99999), and(greaterOrEquals(events.timestamp, assumeNotNull(toDateTime('2024-01-01 12:00:00', 'UTC'))), less(events.timestamp, plus(assumeNotNull(toDateTime('2024-01-15 12:00:00', 'UTC')), toIntervalSecond(0))), equals(events.event, '$pageview')), isNotNull(events.properties.`$session_id`), notEquals(events.properties.`$session_id`, ''))+ GROUP BY if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id),+ events.properties.`$session_id`),+ metric_events AS+ (SELECT exposures.entity_id AS entity_id,+ exposures.variant AS variant,+ accurateCastOrNull(coalesce(metric_events_by_session.session_value, 0), 'Float64') AS value,+ metric_events_by_session.session_id AS session_id+ FROM exposures+ INNER JOIN metric_events_by_session ON and(equals(exposures.entity_id, metric_events_by_session.entity_id), greaterOrEquals(toTimeZone(metric_events_by_session.first_event_timestamp, 'UTC'), toTimeZone(exposures.first_exposure_time, 'UTC')))),+ entity_metrics AS+ (SELECT metric_events.entity_id AS entity_id,+ metric_events.variant AS variant,+ sum(coalesce(accurateCastOrNull(metric_events.value, 'Float64'), 0)) AS value+ FROM metric_events+ GROUP BY metric_events.entity_id,+ metric_events.variant)+ SELECT entity_metrics.variant AS variant,+ count(entity_metrics.entity_id) AS num_users,+ sum(entity_metrics.value) AS total_sum,+ sum(power(entity_metrics.value, 2)) AS total_sum_of_squares+ FROM entity_metrics+ GROUP BY entity_metrics.variant+ LIMIT 50000 SETTINGS join_algorithm='grace_hash',+ grace_hash_join_initial_buckets=2,+ readonly=2,+ max_execution_time=600,+ allow_experimental_object_type=1,+ max_ast_elements=4000000,+ max_expanded_ast_elements=4000000,+ max_bytes_before_external_group_by=39728447488,+ transform_null_in=1,+ optimize_min_equality_disjunction_chain_length=4294967295,+ optimize_rewrite_aggregate_function_with_if=0,+ optimize_min_inequality_conjunction_chain_length=4294967295,+ allow_experimental_join_condition=1,+ use_hive_partitioning=0+ '''+# ---diff --git a/products/feature_flags/backend/api/feature_flag.py b/products/feature_flags/backend/api/feature_flag.pyindex 508c71d85013..b0b90554cecd 100644--- a/products/feature_flags/backend/api/feature_flag.py+++ b/products/feature_flags/backend/api/feature_flag.py@@ -37,6 +37,8 @@ from rest_framework.response import Responsefrom posthog.schema import ProductKey+from posthog.hogql.constants import FEATURE_FLAG_FALSE_VARIANT_SENTINEL+from posthog.api.cohort import CohortSerializerfrom posthog.api.documentation import FeatureFlagFiltersSchemaSerializer, extend_schemafrom posthog.api.forbid_destroy_model import ForbidDestroyModel@@ -919,6 +921,23 @@ class EvaluationContextSerializerMixin(serializers.Serializer):_RUST_PROPERTY_TYPES: frozenset[str] = frozenset({*FEATURE_FLAG_PROPERTY_TYPES, "person_metadata"})+def _uses_reserved_variant_key(filters: dict) -> bool:+ """Whether a `multivariate.variants[].key` is the sentinel the ingest cleaner stores a variant named "false" under.++ Checked on the raw request shape ahead of every validation tier, so the rejection does not depend on the #50084+ rollout switch.+ """+ multivariate = filters.get("multivariate")+ if not isinstance(multivariate, dict):+ return False+ variants = multivariate.get("variants")+ if not isinstance(variants, list):+ return False+ return any(+ isinstance(variant, dict) and variant.get("key") == FEATURE_FLAG_FALSE_VARIANT_SENTINEL for variant in variants+ )++def _filters_rule_is_enforced(rule_id: str | None) -> bool:"""Whether a violation of `rule_id` rejects the write, per the #50084 staged rollout.@@ -1835,6 +1854,12 @@ class FeatureFlagSerializer(validated_data["filters"] = documentdef _validate_filters_inner(self, filters, operation: str):+ if _uses_reserved_variant_key(filters):+ raise serializers.ValidationError(+ f"The variant key {FEATURE_FLAG_FALSE_VARIANT_SENTINEL} is reserved. Choose another key.",+ code="reserved_variant_key",+ )+if self._v2_update_limits is not None:# An admitted v2 replacement passes through untouched — no v1 merge, no# normalization. It is resolved and validated in update(), against the lockeddiff --git a/products/feature_flags/backend/test/test_filters_validation.py b/products/feature_flags/backend/test/test_filters_validation.pyindex 30ffe4b5371e..ac07f37e91f5 100644--- a/products/feature_flags/backend/test/test_filters_validation.py+++ b/products/feature_flags/backend/test/test_filters_validation.py@@ -1,12 +1,14 @@from typing import Any-from django.test import SimpleTestCase+from django.test import SimpleTestCase, override_settingsfrom parameterized import parameterizedfrom rest_framework import serializersfrom rest_framework.exceptions import ErrorDetail-from products.feature_flags.backend.api.feature_flag import _reject_serde_unsafe_filters+from posthog.hogql.constants import FEATURE_FLAG_FALSE_VARIANT_SENTINEL++from products.feature_flags.backend.api.feature_flag import FeatureFlagSerializer, _reject_serde_unsafe_filtersfrom products.feature_flags.backend.api.filters_schema import FEATURE_FLAG_PROPERTY_TYPES, FeatureFlagFiltersSerializerfrom products.feature_flags.backend.encrypted_flag_payloads import REDACTED_PAYLOAD_VALUEfrom products.feature_flags.backend.filters_validation import (@@ -469,3 +471,21 @@ class TestRejectSerdeUnsafeFilters(SimpleTestCase):)def test_accepts_what_master_accepted(self, _name: str, filters: Any) -> None:_reject_serde_unsafe_filters(filters)+++class TestReservedVariantKey(SimpleTestCase):+ @parameterized.expand([("log_only", set()), ("full", {"*"})])+ def test_false_sentinel_is_rejected_as_a_variant_key(self, _name: str, enforced_rules: set[str]) -> None:+ filters = {+ "groups": [{"properties": [], "rollout_percentage": 100}],+ "multivariate": {+ "variants": [+ {"key": "control", "rollout_percentage": 50},+ {"key": FEATURE_FLAG_FALSE_VARIANT_SENTINEL, "rollout_percentage": 50},+ ]+ },+ }+ with override_settings(FEATURE_FLAG_FILTERS_ENFORCED_RULES=enforced_rules):+ serializer = FeatureFlagSerializer(data={"filters": filters}, partial=True)+ assert not serializer.is_valid()+ assert serializer.errors["filters"][0].code == "reserved_variant_key"diff --git a/products/product_analytics/backend/hogql_queries/retention/test/test_retention_query_runner.py b/products/product_analytics/backend/hogql_queries/retention/test/test_retention_query_runner.pyindex ac633c84a41c..70ecbfcb13a7 100644--- a/products/product_analytics/backend/hogql_queries/retention/test/test_retention_query_runner.py+++ b/products/product_analytics/backend/hogql_queries/retention/test/test_retention_query_runner.py@@ -15,10 +15,8 @@ from posthog.test.base import (flush_persons_and_events,snapshot_clickhouse_queries,)-from unittest import skipIffrom unittest.mock import MagicMock, patch-from django.conf import settingsfrom django.test import override_settingsfrom parameterized import parameterized@@ -2960,10 +2958,6 @@ class TestRetention(ClickhouseTestMixin, APIBaseTest):],)- @skipIf(- settings.CLICKHOUSE_HOGQL_USE_NEW_EVENTS_SCHEMA,- "the native-JSON events table keeps flags in the $feature_flags map, which HogQL does not read yet",- )def test_retention_first_time_ever_breakdown_does_not_inflate_buckets(self):# First-ever retention with a breakdown by an event property that was# captured later than the user's first event (e.g. a flag rolled outdiff --git a/products/product_analytics/backend/hogql_queries/trends/test/test_trends_data_warehouse_query.py b/products/product_analytics/backend/hogql_queries/trends/test/test_trends_data_warehouse_query.pyindex dd77414f2c28..1067da477802 100644--- a/products/product_analytics/backend/hogql_queries/trends/test/test_trends_data_warehouse_query.py+++ b/products/product_analytics/backend/hogql_queries/trends/test/test_trends_data_warehouse_query.py@@ -10,10 +10,8 @@ from posthog.test.base import (flush_persons_and_events,snapshot_clickhouse_queries,)-from unittest import skipIffrom unittest.mock import patch-from django.conf import settingsfrom django.test import override_settingsfrom parameterized import parameterized@@ -515,10 +513,6 @@ class TestTrendsDataWarehouseQuery(ClickhouseTestMixin, BaseTest):assert TrendsQueryRunner(team=self.team, query=trends_query)._is_breakdown_filter_field_boolean() is expected- @skipIf(- settings.CLICKHOUSE_HOGQL_USE_NEW_EVENTS_SCHEMA,- "the native-JSON events table keeps flags in the $feature_flags map, which HogQL does not read yet",- )def test_trends_breakdown_with_event_property(self):table_name = self.setup_data_warehouse()diff --git a/services/hogql-language-service/internal/completion/functions.go b/services/hogql-language-service/internal/completion/functions.goindex bf9f631d8191..557df3078bfc 100644--- a/services/hogql-language-service/internal/completion/functions.go+++ b/services/hogql-language-service/internal/completion/functions.go@@ -35,33 +35,33 @@ h3ToCenterChild h3ToChildren h3ToGeo h3ToGeoBoundary h3ToParent h3ToString h3UnihasTokenCaseInsensitiveOrNull hasTokenOrNull hex hop hopEnd hopStart hypot if ifNotFinite ifnull ilike in indexHint indexOf initcap intDiv intDivOrZero intExp10 intExp2 isFiniteisIPAddressInRange isIPv4String isIPv6String isInfinite isLikelyBot isNaN isNotNull isValidJSON isValidUTF8 isnull json_agg kurtPop kurtPopIf kurtSamp kurtSampIf lag lagInFrame languageCodeToName last_value lcmlead leadInFrame least left leftPad leftPadUTF8 length lengthUTF8 less lessOrEquals lgamma like ln locate log log10 log1p log2 lookupDomainType lookupOrganicMediumType-lookupOrganicSourceType lookupPaidMediumType lookupPaidSourceType lower lowerUTF8 lpad ltrim make_date make_interval make_timestamp make_timestamptz map mapAdd mapApply mapContains mapContainsKeyLike mapExtractKeyLike mapFilter mapFromArrays mapKeys-mapPopulateSeries mapSubtract mapUpdate mapValues match matchesAction max max2 maxArgMax maxArgMaxIf maxArgMaxOrDefault maxArgMaxOrDefaultIf maxArgMaxOrNull maxArgMaxOrNullIf maxArgMin maxArgMinIf maxArgMinOrDefault maxArgMinOrDefaultIf maxArgMinOrNull maxArgMinOrNullIf-maxArray maxArrayIf maxArrayOrDefault maxArrayOrDefaultIf maxArrayOrNull maxArrayOrNullIf maxForEach maxForEachIf maxForEachOrDefault maxForEachOrDefaultIf maxForEachOrNull maxForEachOrNullIf maxIf maxIntersections maxIntersectionsIf maxIntersectionsPosition maxIntersectionsPositionIf maxMap maxMapIf maxMapOrDefault-maxMapOrDefaultIf maxMapOrNull maxMapOrNullIf maxMerge maxMergeIf maxMergeOrDefault maxMergeOrDefaultIf maxMergeOrNull maxMergeOrNullIf maxOrDefault maxOrDefaultIf maxOrNull maxOrNullIf maxState maxStateIf maxStateOrDefault maxStateOrDefaultIf maxStateOrNull maxStateOrNullIf md5-median medianArgMax medianArgMaxIf medianArgMaxOrDefault medianArgMaxOrDefaultIf medianArgMaxOrNull medianArgMaxOrNullIf medianArgMin medianArgMinIf medianArgMinOrDefault medianArgMinOrDefaultIf medianArgMinOrNull medianArgMinOrNullIf medianArray medianArrayIf medianArrayOrDefault medianArrayOrDefaultIf medianArrayOrNull medianArrayOrNullIf medianBFloat16-medianBFloat16If medianDeterministic medianDeterministicIf medianExact medianExactHigh medianExactHighIf medianExactIf medianExactLow medianExactLowIf medianExactWeighted medianExactWeightedIf medianForEach medianForEachIf medianForEachOrDefault medianForEachOrDefaultIf medianForEachOrNull medianForEachOrNullIf medianIf medianMap medianMapIf-medianMapOrDefault medianMapOrDefaultIf medianMapOrNull medianMapOrNullIf medianMerge medianMergeIf medianMergeOrDefault medianMergeOrDefaultIf medianMergeOrNull medianMergeOrNullIf medianOrDefault medianOrDefaultIf medianOrNull medianOrNullIf medianState medianStateIf medianStateOrDefault medianStateOrDefaultIf medianStateOrNull medianStateOrNullIf-medianTDigest medianTDigestIf medianTDigestWeighted medianTDigestWeightedIf medianTiming medianTimingIf medianTimingWeighted medianTimingWeightedIf min min2 minArgMax minArgMaxIf minArgMaxOrDefault minArgMaxOrDefaultIf minArgMaxOrNull minArgMaxOrNullIf minArgMin minArgMinIf minArgMinOrDefault minArgMinOrDefaultIf-minArgMinOrNull minArgMinOrNullIf minArray minArrayIf minArrayOrDefault minArrayOrDefaultIf minArrayOrNull minArrayOrNullIf minForEach minForEachIf minForEachOrDefault minForEachOrDefaultIf minForEachOrNull minForEachOrNullIf minIf minMap minMapIf minMapOrDefault minMapOrDefaultIf minMapOrNull-minMapOrNullIf minMerge minMergeIf minMergeOrDefault minMergeOrDefaultIf minMergeOrNull minMergeOrNullIf minOrDefault minOrDefaultIf minOrNull minOrNullIf minState minStateIf minStateOrDefault minStateOrDefaultIf minStateOrNull minStateOrNullIf minus modulo moduloOrZero-monthName multiFuzzyMatchAllIndices multiFuzzyMatchAny multiFuzzyMatchAnyIndex multiIf multiMatchAllIndices multiMatchAny multiMatchAnyIndex multiSearchAllPositions multiSearchAllPositionsCaseInsensitive multiSearchAllPositionsCaseInsensitiveUTF8 multiSearchAllPositionsUTF8 multiSearchAny multiSearchAnyCaseInsensitive multiSearchAnyCaseInsensitiveUTF8 multiSearchAnyUTF8 multiSearchFirstIndex multiSearchFirstIndexCaseInsensitive multiSearchFirstIndexCaseInsensitiveUTF8 multiSearchFirstIndexUTF8-multiSearchFirstPosition multiSearchFirstPositionCaseInsensitive multiSearchFirstPositionCaseInsensitiveUTF8 multiSearchFirstPositionUTF8 multiply multiplyDecimal negate netloc ngramDistance ngramDistanceCaseInsensitive ngramDistanceCaseInsensitiveUTF8 ngramDistanceUTF8 ngramSearch ngramSearchCaseInsensitive ngramSearchCaseInsensitiveUTF8 ngramSearchUTF8 ngrams not notEmpty notEquals-notILike notIn notLike now nowInBlock nth_value nullif or parseDateTime parseDateTimeBestEffort path pathFull percentile_cont percentile_disc pi plus pointInEllipses pointInPolygon port position-positionCaseInsensitive positionCaseInsensitiveUTF8 positionUTF8 positiveModulo pow power protocol quantile quantileExact quantileIf quantiles quantilesIf quantilesMerge quantilesMergeIf quantilesState quantilesStateIf queryString queryStringAndFragment radians rand-range rank recordingButton regexpExtract regexpQuoteMeta reinterpretAsFloat32 reinterpretAsFloat64 reinterpretAsInt128 reinterpretAsInt16 reinterpretAsInt256 reinterpretAsInt32 reinterpretAsInt64 reinterpretAsInt8 reinterpretAsUInt128 reinterpretAsUInt16 reinterpretAsUInt256 reinterpretAsUInt32 reinterpretAsUInt64 reinterpretAsUInt8 reinterpretAsUUID-repeat replace replaceAll replaceOne replaceRegexpAll replaceRegexpOne reverse reverseUTF8 right rightPad rightPadUTF8 round roundAge roundBankers roundDown roundDuration roundToExp2 rowNumberInAllBlocks rowNumberInBlock row_number-rpad rtrim sign simpleLinearRegression simpleLinearRegressionIf sin sinh skewPop skewPopIf skewSamp skewSampIf sortablesemver sparkline splitByChar splitByNonAlpha splitByRegexp splitByString splitByWhitespace split_part sqrt-startsWith stddevPop stddevPopIf stddevSamp stddevSampIf stringToH3 string_agg subBitmap substring substringUTF8 subtractDays subtractHours subtractMinutes subtractMonths subtractQuarters subtractSeconds subtractWeeks subtractYears sum sumArgMax-sumArgMaxIf sumArgMaxOrDefault sumArgMaxOrDefaultIf sumArgMaxOrNull sumArgMaxOrNullIf sumArgMin sumArgMinIf sumArgMinOrDefault sumArgMinOrDefaultIf sumArgMinOrNull sumArgMinOrNullIf sumArray sumArrayIf sumArrayOrDefault sumArrayOrDefaultIf sumArrayOrNull sumArrayOrNullIf sumForEach sumForEachIf sumForEachMerge-sumForEachOrDefault sumForEachOrDefaultIf sumForEachOrNull sumForEachOrNullIf sumForEachState sumIf sumMap sumMapIf sumMapMerge sumMapMergeIf sumMapOrDefault sumMapOrDefaultIf sumMapOrNull sumMapOrNullIf sumMerge sumMergeIf sumMergeOrDefault sumMergeOrDefaultIf sumMergeOrNull sumMergeOrNullIf-sumOrDefault sumOrDefaultIf sumOrNull sumOrNullIf sumState sumStateIf sumStateOrDefault sumStateOrDefaultIf sumStateOrNull sumStateOrNullIf sumWithOverflow sumWithOverflowIf tan tgamma theilsU theilsUIf throwIf timeSlot timeSlots timeStampAdd-timeStampSub timeZoneOf timeZoneOffset timezone toBool toDate toDateTime toDateTime64 toDateTimeUS toDayOfMonth toDayOfWeek toDayOfYear toDecimal toFloat toFloat64OrNull toFloatOrDefault toFloatOrNull toFloatOrZero toHour toIPv4-toIPv4OrDefault toIPv4OrNull toIPv4OrZero toIPv6 toIPv6OrDefault toIPv6OrNull toIPv6OrZero toISOWeek toISOYear toInt toIntOrDefault toIntOrZero toIntervalDay toIntervalHour toIntervalMinute toIntervalMonth toIntervalQuarter toIntervalSecond toIntervalWeek toIntervalYear-toJSONString toLastDayOfMonth toLastDayOfWeek toMinute toModifiedJulianDay toMonday toMonth toNullable toNullableString toQuarter toSecond toStartOfDay toStartOfFifteenMinutes toStartOfFiveMinutes toStartOfHour toStartOfISOYear toStartOfInterval toStartOfMinute toStartOfMonth toStartOfQuarter-toStartOfSecond toStartOfTenMinutes toStartOfWeek toStartOfYear toString toTime toTimeZone toTypeName toUUID toUUIDOrDefault toUnixTimestamp toUnixTimestamp64Milli toValidUTF8 toWeek toYYYYMM toYYYYMMDD toYYYYMMDDhhmmss toYear toYearWeek to_char-to_date to_timestamp today tokens topK topLevelDomain transform translate translateUTF8 trim trimLeft trimRight trunc tryBase58Decode tryBase64Decode tumble tumbleEnd tumbleStart tuple tupleDivide-tupleDivideByNumber tupleElement tupleHammingDistance tupleMinus tupleMultiply tupleMultiplyByNumber tupleNegate tuplePlus tupleToNameValuePairs unhex uniq uniqCombined uniqCombined64 uniqExact uniqExactIf uniqExactMerge uniqExactState uniqHLL12 uniqHLL12If uniqIf-uniqMap uniqMapMerge uniqMapMergeIf uniqMerge uniqMergeIf uniqState uniqStateIf uniqTheta uniqThetaIf uniqUpToMerge uniqueSurveySubmissionsFilter untuple upper upperUTF8 varPop varPopIf varSamp varSampIf width_bucket windowFunnel-xor yesterday+lookupOrganicSourceType lookupPaidMediumType lookupPaidSourceType lower lowerUTF8 lpad ltrim make_date make_interval make_timestamp make_timestamptz map mapAdd mapApply mapContains mapContainsKeyLike mapExists mapExtractKeyLike mapFilter mapFromArrays+mapKeys mapPopulateSeries mapSubtract mapUpdate mapValues match matchesAction max max2 maxArgMax maxArgMaxIf maxArgMaxOrDefault maxArgMaxOrDefaultIf maxArgMaxOrNull maxArgMaxOrNullIf maxArgMin maxArgMinIf maxArgMinOrDefault maxArgMinOrDefaultIf maxArgMinOrNull+maxArgMinOrNullIf maxArray maxArrayIf maxArrayOrDefault maxArrayOrDefaultIf maxArrayOrNull maxArrayOrNullIf maxForEach maxForEachIf maxForEachOrDefault maxForEachOrDefaultIf maxForEachOrNull maxForEachOrNullIf maxIf maxIntersections maxIntersectionsIf maxIntersectionsPosition maxIntersectionsPositionIf maxMap maxMapIf+maxMapOrDefault maxMapOrDefaultIf maxMapOrNull maxMapOrNullIf maxMerge maxMergeIf maxMergeOrDefault maxMergeOrDefaultIf maxMergeOrNull maxMergeOrNullIf maxOrDefault maxOrDefaultIf maxOrNull maxOrNullIf maxState maxStateIf maxStateOrDefault maxStateOrDefaultIf maxStateOrNull maxStateOrNullIf+md5 median medianArgMax medianArgMaxIf medianArgMaxOrDefault medianArgMaxOrDefaultIf medianArgMaxOrNull medianArgMaxOrNullIf medianArgMin medianArgMinIf medianArgMinOrDefault medianArgMinOrDefaultIf medianArgMinOrNull medianArgMinOrNullIf medianArray medianArrayIf medianArrayOrDefault medianArrayOrDefaultIf medianArrayOrNull medianArrayOrNullIf+medianBFloat16 medianBFloat16If medianDeterministic medianDeterministicIf medianExact medianExactHigh medianExactHighIf medianExactIf medianExactLow medianExactLowIf medianExactWeighted medianExactWeightedIf medianForEach medianForEachIf medianForEachOrDefault medianForEachOrDefaultIf medianForEachOrNull medianForEachOrNullIf medianIf medianMap+medianMapIf medianMapOrDefault medianMapOrDefaultIf medianMapOrNull medianMapOrNullIf medianMerge medianMergeIf medianMergeOrDefault medianMergeOrDefaultIf medianMergeOrNull medianMergeOrNullIf medianOrDefault medianOrDefaultIf medianOrNull medianOrNullIf medianState medianStateIf medianStateOrDefault medianStateOrDefaultIf medianStateOrNull+medianStateOrNullIf medianTDigest medianTDigestIf medianTDigestWeighted medianTDigestWeightedIf medianTiming medianTimingIf medianTimingWeighted medianTimingWeightedIf min min2 minArgMax minArgMaxIf minArgMaxOrDefault minArgMaxOrDefaultIf minArgMaxOrNull minArgMaxOrNullIf minArgMin minArgMinIf minArgMinOrDefault+minArgMinOrDefaultIf minArgMinOrNull minArgMinOrNullIf minArray minArrayIf minArrayOrDefault minArrayOrDefaultIf minArrayOrNull minArrayOrNullIf minForEach minForEachIf minForEachOrDefault minForEachOrDefaultIf minForEachOrNull minForEachOrNullIf minIf minMap minMapIf minMapOrDefault minMapOrDefaultIf+minMapOrNull minMapOrNullIf minMerge minMergeIf minMergeOrDefault minMergeOrDefaultIf minMergeOrNull minMergeOrNullIf minOrDefault minOrDefaultIf minOrNull minOrNullIf minState minStateIf minStateOrDefault minStateOrDefaultIf minStateOrNull minStateOrNullIf minus modulo+moduloOrZero monthName multiFuzzyMatchAllIndices multiFuzzyMatchAny multiFuzzyMatchAnyIndex multiIf multiMatchAllIndices multiMatchAny multiMatchAnyIndex multiSearchAllPositions multiSearchAllPositionsCaseInsensitive multiSearchAllPositionsCaseInsensitiveUTF8 multiSearchAllPositionsUTF8 multiSearchAny multiSearchAnyCaseInsensitive multiSearchAnyCaseInsensitiveUTF8 multiSearchAnyUTF8 multiSearchFirstIndex multiSearchFirstIndexCaseInsensitive multiSearchFirstIndexCaseInsensitiveUTF8+multiSearchFirstIndexUTF8 multiSearchFirstPosition multiSearchFirstPositionCaseInsensitive multiSearchFirstPositionCaseInsensitiveUTF8 multiSearchFirstPositionUTF8 multiply multiplyDecimal negate netloc ngramDistance ngramDistanceCaseInsensitive ngramDistanceCaseInsensitiveUTF8 ngramDistanceUTF8 ngramSearch ngramSearchCaseInsensitive ngramSearchCaseInsensitiveUTF8 ngramSearchUTF8 ngrams not notEmpty+notEquals notILike notIn notLike now nowInBlock nth_value nullif or parseDateTime parseDateTimeBestEffort path pathFull percentile_cont percentile_disc pi plus pointInEllipses pointInPolygon port+position positionCaseInsensitive positionCaseInsensitiveUTF8 positionUTF8 positiveModulo pow power protocol quantile quantileExact quantileIf quantiles quantilesIf quantilesMerge quantilesMergeIf quantilesState quantilesStateIf queryString queryStringAndFragment radians+rand range rank recordingButton regexpExtract regexpQuoteMeta reinterpretAsFloat32 reinterpretAsFloat64 reinterpretAsInt128 reinterpretAsInt16 reinterpretAsInt256 reinterpretAsInt32 reinterpretAsInt64 reinterpretAsInt8 reinterpretAsUInt128 reinterpretAsUInt16 reinterpretAsUInt256 reinterpretAsUInt32 reinterpretAsUInt64 reinterpretAsUInt8+reinterpretAsUUID repeat replace replaceAll replaceOne replaceRegexpAll replaceRegexpOne reverse reverseUTF8 right rightPad rightPadUTF8 round roundAge roundBankers roundDown roundDuration roundToExp2 rowNumberInAllBlocks rowNumberInBlock+row_number rpad rtrim sign simpleLinearRegression simpleLinearRegressionIf sin sinh skewPop skewPopIf skewSamp skewSampIf sortablesemver sparkline splitByChar splitByNonAlpha splitByRegexp splitByString splitByWhitespace split_part+sqrt startsWith stddevPop stddevPopIf stddevSamp stddevSampIf stringToH3 string_agg subBitmap substring substringUTF8 subtractDays subtractHours subtractMinutes subtractMonths subtractQuarters subtractSeconds subtractWeeks subtractYears sum+sumArgMax sumArgMaxIf sumArgMaxOrDefault sumArgMaxOrDefaultIf sumArgMaxOrNull sumArgMaxOrNullIf sumArgMin sumArgMinIf sumArgMinOrDefault sumArgMinOrDefaultIf sumArgMinOrNull sumArgMinOrNullIf sumArray sumArrayIf sumArrayOrDefault sumArrayOrDefaultIf sumArrayOrNull sumArrayOrNullIf sumForEach sumForEachIf+sumForEachMerge sumForEachOrDefault sumForEachOrDefaultIf sumForEachOrNull sumForEachOrNullIf sumForEachState sumIf sumMap sumMapIf sumMapMerge sumMapMergeIf sumMapOrDefault sumMapOrDefaultIf sumMapOrNull sumMapOrNullIf sumMerge sumMergeIf sumMergeOrDefault sumMergeOrDefaultIf sumMergeOrNull+sumMergeOrNullIf sumOrDefault sumOrDefaultIf sumOrNull sumOrNullIf sumState sumStateIf sumStateOrDefault sumStateOrDefaultIf sumStateOrNull sumStateOrNullIf sumWithOverflow sumWithOverflowIf tan tgamma theilsU theilsUIf throwIf timeSlot timeSlots+timeStampAdd timeStampSub timeZoneOf timeZoneOffset timezone toBool toDate toDateTime toDateTime64 toDateTimeUS toDayOfMonth toDayOfWeek toDayOfYear toDecimal toFloat toFloat64OrNull toFloatOrDefault toFloatOrNull toFloatOrZero toHour+toIPv4 toIPv4OrDefault toIPv4OrNull toIPv4OrZero toIPv6 toIPv6OrDefault toIPv6OrNull toIPv6OrZero toISOWeek toISOYear toInt toIntOrDefault toIntOrZero toIntervalDay toIntervalHour toIntervalMinute toIntervalMonth toIntervalQuarter toIntervalSecond toIntervalWeek+toIntervalYear toJSONString toLastDayOfMonth toLastDayOfWeek toMinute toModifiedJulianDay toMonday toMonth toNullable toNullableString toQuarter toSecond toStartOfDay toStartOfFifteenMinutes toStartOfFiveMinutes toStartOfHour toStartOfISOYear toStartOfInterval toStartOfMinute toStartOfMonth+toStartOfQuarter toStartOfSecond toStartOfTenMinutes toStartOfWeek toStartOfYear toString toTime toTimeZone toTypeName toUUID toUUIDOrDefault toUnixTimestamp toUnixTimestamp64Milli toValidUTF8 toWeek toYYYYMM toYYYYMMDD toYYYYMMDDhhmmss toYear toYearWeek+to_char to_date to_timestamp today tokens topK topLevelDomain transform translate translateUTF8 trim trimLeft trimRight trunc tryBase58Decode tryBase64Decode tumble tumbleEnd tumbleStart tuple+tupleDivide tupleDivideByNumber tupleElement tupleHammingDistance tupleMinus tupleMultiply tupleMultiplyByNumber tupleNegate tuplePlus tupleToNameValuePairs unhex uniq uniqCombined uniqCombined64 uniqExact uniqExactIf uniqExactMerge uniqExactState uniqHLL12 uniqHLL12If+uniqIf uniqMap uniqMapMerge uniqMapMergeIf uniqMerge uniqMergeIf uniqState uniqStateIf uniqTheta uniqThetaIf uniqUpToMerge uniqueSurveySubmissionsFilter untuple upper upperUTF8 varPop varPopIf varSamp varSampIf width_bucket+windowFunnel xor yesterday`)