: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
fix(surveys): count responses with one event scan instead of two (#122808)
Vasco de Krijger3ff1e178b14c
diff --git a/products/surveys/backend/api/test/__snapshots__/test_survey.ambr b/products/surveys/backend/api/test/__snapshots__/test_survey.ambrindex 7c1b73b64ecd..b45db530b70f 100644--- a/products/surveys/backend/api/test/__snapshots__/test_survey.ambr+++ b/products/surveys/backend/api/test/__snapshots__/test_survey.ambr@@ -2,14 +2,15 @@# name: TestResponsesCount.test_responses_count'''/* user_id:0 request:_snapshot_ */- SELECT replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$survey_id'), ''), 'null'), '^"|"$', '') AS survey_id,+ SELECT survey_id AS survey_id,count() AS `count()`- FROM events- WHERE and(equals(events.team_id, 99999), or(equals(events.event, 'survey sent'), and(in(events.event, tuple('survey dismissed', 'survey abandoned')), equals(coalesce(JSONExtractString(events.properties, '$survey_partially_completed'), ''), 'true'))), greaterOrEquals(events.timestamp, toDateTime64('2024-01-21 14:40:09.000000', 6, 'UTC')), globalIn(events.uuid,- (SELECT argMax(events.uuid, tuple(toTimeZone(events.timestamp, 'UTC'), events.uuid)) AS `argMax(uuid, tuple(toTimeZone(timestamp, 'UTC'), uuid))`- FROM events- WHERE and(equals(events.team_id, 99999), or(equals(events.event, 'survey sent'), and(in(events.event, tuple('survey dismissed', 'survey abandoned')), equals(coalesce(JSONExtractString(events.properties, '$survey_partially_completed'), ''), 'true'))), greaterOrEquals(events.timestamp, toDateTime64('2024-01-21 14:40:09.000000', 6, 'UTC')))- GROUP BY replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$survey_id'), ''), 'null'), '^"|"$', ''), if(ifNull(equals(coalesce(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$survey_submission_id'), ''), 'null'), '^"|"$', ''), ''), ''), 0), toString(events.uuid), replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$survey_submission_id'), ''), 'null'), '^"|"$', '')))))+ FROM+ (SELECT replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$survey_id'), ''), 'null'), '^"|"$', '') AS survey_id,+ argMax(events.uuid, tuple(toTimeZone(events.timestamp, 'UTC'), events.uuid)) AS response_uuid+ FROM events+ WHERE and(equals(events.team_id, 99999), or(equals(events.event, 'survey sent'), and(in(events.event, tuple('survey dismissed', 'survey abandoned')), equals(coalesce(JSONExtractString(events.properties, '$survey_partially_completed'), ''), 'true'))), greaterOrEquals(events.timestamp, toDateTime64('2024-01-21 14:40:09.000000', 6, 'UTC')))+ GROUP BY survey_id,+ if(ifNull(equals(coalesce(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$survey_submission_id'), ''), 'null'), '^"|"$', ''), ''), ''), 0), toString(events.uuid), replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$survey_submission_id'), ''), 'null'), '^"|"$', '')))GROUP BY survey_idLIMIT 50000 SETTINGS readonly=2,max_execution_time=60,@@ -28,14 +29,15 @@# name: TestResponsesCount.test_responses_count_only_after_first_survey_started'''/* user_id:0 request:_snapshot_ */- SELECT replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$survey_id'), ''), 'null'), '^"|"$', '') AS survey_id,+ SELECT survey_id AS survey_id,count() AS `count()`- FROM events- WHERE and(equals(events.team_id, 99999), or(equals(events.event, 'survey sent'), and(in(events.event, tuple('survey dismissed', 'survey abandoned')), equals(coalesce(JSONExtractString(events.properties, '$survey_partially_completed'), ''), 'true'))), greaterOrEquals(events.timestamp, toDateTime64('2024-04-25 14:40:09.000000', 6, 'UTC')), globalIn(events.uuid,- (SELECT argMax(events.uuid, tuple(toTimeZone(events.timestamp, 'UTC'), events.uuid)) AS `argMax(uuid, tuple(toTimeZone(timestamp, 'UTC'), uuid))`- FROM events- WHERE and(equals(events.team_id, 99999), or(equals(events.event, 'survey sent'), and(in(events.event, tuple('survey dismissed', 'survey abandoned')), equals(coalesce(JSONExtractString(events.properties, '$survey_partially_completed'), ''), 'true'))), greaterOrEquals(events.timestamp, toDateTime64('2024-04-25 14:40:09.000000', 6, 'UTC')))- GROUP BY replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$survey_id'), ''), 'null'), '^"|"$', ''), if(ifNull(equals(coalesce(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$survey_submission_id'), ''), 'null'), '^"|"$', ''), ''), ''), 0), toString(events.uuid), replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$survey_submission_id'), ''), 'null'), '^"|"$', '')))))+ FROM+ (SELECT replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$survey_id'), ''), 'null'), '^"|"$', '') AS survey_id,+ argMax(events.uuid, tuple(toTimeZone(events.timestamp, 'UTC'), events.uuid)) AS response_uuid+ FROM events+ WHERE and(equals(events.team_id, 99999), or(equals(events.event, 'survey sent'), and(in(events.event, tuple('survey dismissed', 'survey abandoned')), equals(coalesce(JSONExtractString(events.properties, '$survey_partially_completed'), ''), 'true'))), greaterOrEquals(events.timestamp, toDateTime64('2024-04-25 14:40:09.000000', 6, 'UTC')))+ GROUP BY survey_id,+ if(ifNull(equals(coalesce(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$survey_submission_id'), ''), 'null'), '^"|"$', ''), ''), ''), 0), toString(events.uuid), replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$survey_submission_id'), ''), 'null'), '^"|"$', '')))GROUP BY survey_idLIMIT 50000 SETTINGS readonly=2,max_execution_time=60,@@ -54,14 +56,15 @@# name: TestResponsesCount.test_responses_count_with_partial_responses'''/* user_id:0 request:_snapshot_ */- SELECT replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$survey_id'), ''), 'null'), '^"|"$', '') AS survey_id,+ SELECT survey_id AS survey_id,count() AS `count()`- FROM events- WHERE and(equals(events.team_id, 99999), or(equals(events.event, 'survey sent'), and(in(events.event, tuple('survey dismissed', 'survey abandoned')), equals(coalesce(JSONExtractString(events.properties, '$survey_partially_completed'), ''), 'true'))), greaterOrEquals(events.timestamp, toDateTime64('2024-06-10 11:00:00.000000', 6, 'UTC')), globalIn(events.uuid,- (SELECT argMax(events.uuid, tuple(toTimeZone(events.timestamp, 'UTC'), events.uuid)) AS `argMax(uuid, tuple(toTimeZone(timestamp, 'UTC'), uuid))`- FROM events- WHERE and(equals(events.team_id, 99999), or(equals(events.event, 'survey sent'), and(in(events.event, tuple('survey dismissed', 'survey abandoned')), equals(coalesce(JSONExtractString(events.properties, '$survey_partially_completed'), ''), 'true'))), greaterOrEquals(events.timestamp, toDateTime64('2024-06-10 11:00:00.000000', 6, 'UTC')))- GROUP BY replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$survey_id'), ''), 'null'), '^"|"$', ''), if(ifNull(equals(coalesce(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$survey_submission_id'), ''), 'null'), '^"|"$', ''), ''), ''), 0), toString(events.uuid), replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$survey_submission_id'), ''), 'null'), '^"|"$', '')))))+ FROM+ (SELECT replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$survey_id'), ''), 'null'), '^"|"$', '') AS survey_id,+ argMax(events.uuid, tuple(toTimeZone(events.timestamp, 'UTC'), events.uuid)) AS response_uuid+ FROM events+ WHERE and(equals(events.team_id, 99999), or(equals(events.event, 'survey sent'), and(in(events.event, tuple('survey dismissed', 'survey abandoned')), equals(coalesce(JSONExtractString(events.properties, '$survey_partially_completed'), ''), 'true'))), greaterOrEquals(events.timestamp, toDateTime64('2024-06-10 11:00:00.000000', 6, 'UTC')))+ GROUP BY survey_id,+ if(ifNull(equals(coalesce(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$survey_submission_id'), ''), 'null'), '^"|"$', ''), ''), ''), 0), toString(events.uuid), replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$survey_submission_id'), ''), 'null'), '^"|"$', '')))GROUP BY survey_idLIMIT 50000 SETTINGS readonly=2,max_execution_time=60,diff --git a/products/surveys/backend/api/test/__snapshots__/test_survey.new_events_schema.ambr b/products/surveys/backend/api/test/__snapshots__/test_survey.new_events_schema.ambrindex c200aa5861fb..fc9dfa05a884 100644--- a/products/surveys/backend/api/test/__snapshots__/test_survey.new_events_schema.ambr+++ b/products/surveys/backend/api/test/__snapshots__/test_survey.new_events_schema.ambr@@ -2,14 +2,15 @@# name: TestResponsesCount.test_responses_count[new_events_schema]'''/* user_id:0 request:_snapshot_ */- SELECT if(notEquals(toJSONString(events.properties.^`$survey_id`), '{}'), toJSONString(events.properties.^`$survey_id`), if(isNull(nullIf(toString(events.properties.`$survey_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$survey_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$survey_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$survey_id`), '[]'), '{}'), toString(events.properties.`$survey_id`)))) AS survey_id,+ SELECT survey_id AS survey_id,count() AS `count()`- FROM events_json AS events- WHERE and(equals(events.team_id, 99999), or(equals(events.event, 'survey sent'), and(in(events.event, tuple('survey dismissed', 'survey abandoned')), equals(coalesce(JSONExtractString(ifNull(if(notEquals(toJSONString(events.properties.^`$survey_partially_completed`), '{}'), toJSONString(events.properties.^`$survey_partially_completed`), if(isNull(nullIf(toString(events.properties.`$survey_partially_completed`), '')), NULL, nullIf(nullIf(toJSONString(events.properties.`$survey_partially_completed`), '[]'), '{}'))), '')), ''), 'true'))), greaterOrEquals(events.timestamp, toDateTime64('2024-01-21 14:40:09.000000', 6, 'UTC')), globalIn(events.uuid,- (SELECT argMax(events.uuid, tuple(toTimeZone(events.timestamp, 'UTC'), events.uuid)) AS `argMax(uuid, tuple(toTimeZone(timestamp, 'UTC'), uuid))`- FROM events_json AS events- WHERE and(equals(events.team_id, 99999), or(equals(events.event, 'survey sent'), and(in(events.event, tuple('survey dismissed', 'survey abandoned')), equals(coalesce(JSONExtractString(ifNull(if(notEquals(toJSONString(events.properties.^`$survey_partially_completed`), '{}'), toJSONString(events.properties.^`$survey_partially_completed`), if(isNull(nullIf(toString(events.properties.`$survey_partially_completed`), '')), NULL, nullIf(nullIf(toJSONString(events.properties.`$survey_partially_completed`), '[]'), '{}'))), '')), ''), 'true'))), greaterOrEquals(events.timestamp, toDateTime64('2024-01-21 14:40:09.000000', 6, 'UTC')))- GROUP BY if(notEquals(toJSONString(events.properties.^`$survey_id`), '{}'), toJSONString(events.properties.^`$survey_id`), if(isNull(nullIf(toString(events.properties.`$survey_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$survey_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$survey_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$survey_id`), '[]'), '{}'), toString(events.properties.`$survey_id`)))), if(ifNull(equals(coalesce(if(notEquals(toJSONString(events.properties.^`$survey_submission_id`), '{}'), toJSONString(events.properties.^`$survey_submission_id`), if(isNull(nullIf(toString(events.properties.`$survey_submission_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$survey_submission_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$survey_submission_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$survey_submission_id`), '[]'), '{}'), toString(events.properties.`$survey_submission_id`)))), ''), ''), 0), toString(events.uuid), if(notEquals(toJSONString(events.properties.^`$survey_submission_id`), '{}'), toJSONString(events.properties.^`$survey_submission_id`), if(isNull(nullIf(toString(events.properties.`$survey_submission_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$survey_submission_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$survey_submission_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$survey_submission_id`), '[]'), '{}'), toString(events.properties.`$survey_submission_id`))))))))+ FROM+ (SELECT if(notEquals(toJSONString(events.properties.^`$survey_id`), '{}'), toJSONString(events.properties.^`$survey_id`), if(isNull(nullIf(toString(events.properties.`$survey_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$survey_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$survey_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$survey_id`), '[]'), '{}'), toString(events.properties.`$survey_id`)))) AS survey_id,+ argMax(events.uuid, tuple(toTimeZone(events.timestamp, 'UTC'), events.uuid)) AS response_uuid+ FROM events_json AS events+ WHERE and(equals(events.team_id, 99999), or(equals(events.event, 'survey sent'), and(in(events.event, tuple('survey dismissed', 'survey abandoned')), equals(coalesce(JSONExtractString(ifNull(if(notEquals(toJSONString(events.properties.^`$survey_partially_completed`), '{}'), toJSONString(events.properties.^`$survey_partially_completed`), if(isNull(nullIf(toString(events.properties.`$survey_partially_completed`), '')), NULL, nullIf(nullIf(toJSONString(events.properties.`$survey_partially_completed`), '[]'), '{}'))), '')), ''), 'true'))), greaterOrEquals(events.timestamp, toDateTime64('2024-01-21 14:40:09.000000', 6, 'UTC')))+ GROUP BY survey_id,+ if(ifNull(equals(coalesce(if(notEquals(toJSONString(events.properties.^`$survey_submission_id`), '{}'), toJSONString(events.properties.^`$survey_submission_id`), if(isNull(nullIf(toString(events.properties.`$survey_submission_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$survey_submission_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$survey_submission_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$survey_submission_id`), '[]'), '{}'), toString(events.properties.`$survey_submission_id`)))), ''), ''), 0), toString(events.uuid), if(notEquals(toJSONString(events.properties.^`$survey_submission_id`), '{}'), toJSONString(events.properties.^`$survey_submission_id`), if(isNull(nullIf(toString(events.properties.`$survey_submission_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$survey_submission_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$survey_submission_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$survey_submission_id`), '[]'), '{}'), toString(events.properties.`$survey_submission_id`))))))GROUP BY survey_idLIMIT 50000 SETTINGS readonly=2,max_execution_time=60,@@ -30,14 +31,15 @@# name: TestResponsesCount.test_responses_count_only_after_first_survey_started[new_events_schema]'''/* user_id:0 request:_snapshot_ */- SELECT if(notEquals(toJSONString(events.properties.^`$survey_id`), '{}'), toJSONString(events.properties.^`$survey_id`), if(isNull(nullIf(toString(events.properties.`$survey_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$survey_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$survey_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$survey_id`), '[]'), '{}'), toString(events.properties.`$survey_id`)))) AS survey_id,+ SELECT survey_id AS survey_id,count() AS `count()`- FROM events_json AS events- WHERE and(equals(events.team_id, 99999), or(equals(events.event, 'survey sent'), and(in(events.event, tuple('survey dismissed', 'survey abandoned')), equals(coalesce(JSONExtractString(ifNull(if(notEquals(toJSONString(events.properties.^`$survey_partially_completed`), '{}'), toJSONString(events.properties.^`$survey_partially_completed`), if(isNull(nullIf(toString(events.properties.`$survey_partially_completed`), '')), NULL, nullIf(nullIf(toJSONString(events.properties.`$survey_partially_completed`), '[]'), '{}'))), '')), ''), 'true'))), greaterOrEquals(events.timestamp, toDateTime64('2024-04-25 14:40:09.000000', 6, 'UTC')), globalIn(events.uuid,- (SELECT argMax(events.uuid, tuple(toTimeZone(events.timestamp, 'UTC'), events.uuid)) AS `argMax(uuid, tuple(toTimeZone(timestamp, 'UTC'), uuid))`- FROM events_json AS events- WHERE and(equals(events.team_id, 99999), or(equals(events.event, 'survey sent'), and(in(events.event, tuple('survey dismissed', 'survey abandoned')), equals(coalesce(JSONExtractString(ifNull(if(notEquals(toJSONString(events.properties.^`$survey_partially_completed`), '{}'), toJSONString(events.properties.^`$survey_partially_completed`), if(isNull(nullIf(toString(events.properties.`$survey_partially_completed`), '')), NULL, nullIf(nullIf(toJSONString(events.properties.`$survey_partially_completed`), '[]'), '{}'))), '')), ''), 'true'))), greaterOrEquals(events.timestamp, toDateTime64('2024-04-25 14:40:09.000000', 6, 'UTC')))- GROUP BY if(notEquals(toJSONString(events.properties.^`$survey_id`), '{}'), toJSONString(events.properties.^`$survey_id`), if(isNull(nullIf(toString(events.properties.`$survey_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$survey_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$survey_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$survey_id`), '[]'), '{}'), toString(events.properties.`$survey_id`)))), if(ifNull(equals(coalesce(if(notEquals(toJSONString(events.properties.^`$survey_submission_id`), '{}'), toJSONString(events.properties.^`$survey_submission_id`), if(isNull(nullIf(toString(events.properties.`$survey_submission_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$survey_submission_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$survey_submission_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$survey_submission_id`), '[]'), '{}'), toString(events.properties.`$survey_submission_id`)))), ''), ''), 0), toString(events.uuid), if(notEquals(toJSONString(events.properties.^`$survey_submission_id`), '{}'), toJSONString(events.properties.^`$survey_submission_id`), if(isNull(nullIf(toString(events.properties.`$survey_submission_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$survey_submission_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$survey_submission_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$survey_submission_id`), '[]'), '{}'), toString(events.properties.`$survey_submission_id`))))))))+ FROM+ (SELECT if(notEquals(toJSONString(events.properties.^`$survey_id`), '{}'), toJSONString(events.properties.^`$survey_id`), if(isNull(nullIf(toString(events.properties.`$survey_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$survey_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$survey_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$survey_id`), '[]'), '{}'), toString(events.properties.`$survey_id`)))) AS survey_id,+ argMax(events.uuid, tuple(toTimeZone(events.timestamp, 'UTC'), events.uuid)) AS response_uuid+ FROM events_json AS events+ WHERE and(equals(events.team_id, 99999), or(equals(events.event, 'survey sent'), and(in(events.event, tuple('survey dismissed', 'survey abandoned')), equals(coalesce(JSONExtractString(ifNull(if(notEquals(toJSONString(events.properties.^`$survey_partially_completed`), '{}'), toJSONString(events.properties.^`$survey_partially_completed`), if(isNull(nullIf(toString(events.properties.`$survey_partially_completed`), '')), NULL, nullIf(nullIf(toJSONString(events.properties.`$survey_partially_completed`), '[]'), '{}'))), '')), ''), 'true'))), greaterOrEquals(events.timestamp, toDateTime64('2024-04-25 14:40:09.000000', 6, 'UTC')))+ GROUP BY survey_id,+ if(ifNull(equals(coalesce(if(notEquals(toJSONString(events.properties.^`$survey_submission_id`), '{}'), toJSONString(events.properties.^`$survey_submission_id`), if(isNull(nullIf(toString(events.properties.`$survey_submission_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$survey_submission_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$survey_submission_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$survey_submission_id`), '[]'), '{}'), toString(events.properties.`$survey_submission_id`)))), ''), ''), 0), toString(events.uuid), if(notEquals(toJSONString(events.properties.^`$survey_submission_id`), '{}'), toJSONString(events.properties.^`$survey_submission_id`), if(isNull(nullIf(toString(events.properties.`$survey_submission_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$survey_submission_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$survey_submission_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$survey_submission_id`), '[]'), '{}'), toString(events.properties.`$survey_submission_id`))))))GROUP BY survey_idLIMIT 50000 SETTINGS readonly=2,max_execution_time=60,@@ -58,14 +60,15 @@# name: TestResponsesCount.test_responses_count_with_partial_responses[new_events_schema]'''/* user_id:0 request:_snapshot_ */- SELECT if(notEquals(toJSONString(events.properties.^`$survey_id`), '{}'), toJSONString(events.properties.^`$survey_id`), if(isNull(nullIf(toString(events.properties.`$survey_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$survey_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$survey_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$survey_id`), '[]'), '{}'), toString(events.properties.`$survey_id`)))) AS survey_id,+ SELECT survey_id AS survey_id,count() AS `count()`- FROM events_json AS events- WHERE and(equals(events.team_id, 99999), or(equals(events.event, 'survey sent'), and(in(events.event, tuple('survey dismissed', 'survey abandoned')), equals(coalesce(JSONExtractString(ifNull(if(notEquals(toJSONString(events.properties.^`$survey_partially_completed`), '{}'), toJSONString(events.properties.^`$survey_partially_completed`), if(isNull(nullIf(toString(events.properties.`$survey_partially_completed`), '')), NULL, nullIf(nullIf(toJSONString(events.properties.`$survey_partially_completed`), '[]'), '{}'))), '')), ''), 'true'))), greaterOrEquals(events.timestamp, toDateTime64('2024-06-10 11:00:00.000000', 6, 'UTC')), globalIn(events.uuid,- (SELECT argMax(events.uuid, tuple(toTimeZone(events.timestamp, 'UTC'), events.uuid)) AS `argMax(uuid, tuple(toTimeZone(timestamp, 'UTC'), uuid))`- FROM events_json AS events- WHERE and(equals(events.team_id, 99999), or(equals(events.event, 'survey sent'), and(in(events.event, tuple('survey dismissed', 'survey abandoned')), equals(coalesce(JSONExtractString(ifNull(if(notEquals(toJSONString(events.properties.^`$survey_partially_completed`), '{}'), toJSONString(events.properties.^`$survey_partially_completed`), if(isNull(nullIf(toString(events.properties.`$survey_partially_completed`), '')), NULL, nullIf(nullIf(toJSONString(events.properties.`$survey_partially_completed`), '[]'), '{}'))), '')), ''), 'true'))), greaterOrEquals(events.timestamp, toDateTime64('2024-06-10 11:00:00.000000', 6, 'UTC')))- GROUP BY if(notEquals(toJSONString(events.properties.^`$survey_id`), '{}'), toJSONString(events.properties.^`$survey_id`), if(isNull(nullIf(toString(events.properties.`$survey_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$survey_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$survey_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$survey_id`), '[]'), '{}'), toString(events.properties.`$survey_id`)))), if(ifNull(equals(coalesce(if(notEquals(toJSONString(events.properties.^`$survey_submission_id`), '{}'), toJSONString(events.properties.^`$survey_submission_id`), if(isNull(nullIf(toString(events.properties.`$survey_submission_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$survey_submission_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$survey_submission_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$survey_submission_id`), '[]'), '{}'), toString(events.properties.`$survey_submission_id`)))), ''), ''), 0), toString(events.uuid), if(notEquals(toJSONString(events.properties.^`$survey_submission_id`), '{}'), toJSONString(events.properties.^`$survey_submission_id`), if(isNull(nullIf(toString(events.properties.`$survey_submission_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$survey_submission_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$survey_submission_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$survey_submission_id`), '[]'), '{}'), toString(events.properties.`$survey_submission_id`))))))))+ FROM+ (SELECT if(notEquals(toJSONString(events.properties.^`$survey_id`), '{}'), toJSONString(events.properties.^`$survey_id`), if(isNull(nullIf(toString(events.properties.`$survey_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$survey_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$survey_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$survey_id`), '[]'), '{}'), toString(events.properties.`$survey_id`)))) AS survey_id,+ argMax(events.uuid, tuple(toTimeZone(events.timestamp, 'UTC'), events.uuid)) AS response_uuid+ FROM events_json AS events+ WHERE and(equals(events.team_id, 99999), or(equals(events.event, 'survey sent'), and(in(events.event, tuple('survey dismissed', 'survey abandoned')), equals(coalesce(JSONExtractString(ifNull(if(notEquals(toJSONString(events.properties.^`$survey_partially_completed`), '{}'), toJSONString(events.properties.^`$survey_partially_completed`), if(isNull(nullIf(toString(events.properties.`$survey_partially_completed`), '')), NULL, nullIf(nullIf(toJSONString(events.properties.`$survey_partially_completed`), '[]'), '{}'))), '')), ''), 'true'))), greaterOrEquals(events.timestamp, toDateTime64('2024-06-10 11:00:00.000000', 6, 'UTC')))+ GROUP BY survey_id,+ if(ifNull(equals(coalesce(if(notEquals(toJSONString(events.properties.^`$survey_submission_id`), '{}'), toJSONString(events.properties.^`$survey_submission_id`), if(isNull(nullIf(toString(events.properties.`$survey_submission_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$survey_submission_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$survey_submission_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$survey_submission_id`), '[]'), '{}'), toString(events.properties.`$survey_submission_id`)))), ''), ''), 0), toString(events.uuid), if(notEquals(toJSONString(events.properties.^`$survey_submission_id`), '{}'), toJSONString(events.properties.^`$survey_submission_id`), if(isNull(nullIf(toString(events.properties.`$survey_submission_id`), '')), NULL, if(and(in(ascii(toString(events.properties.`$survey_submission_id`)), tuple(91, 123)), in(ascii(toJSONString(events.properties.`$survey_submission_id`)), tuple(91, 123))), nullIf(nullIf(toJSONString(events.properties.`$survey_submission_id`), '[]'), '{}'), toString(events.properties.`$survey_submission_id`))))))GROUP BY survey_idLIMIT 50000 SETTINGS readonly=2,max_execution_time=60,diff --git a/products/surveys/backend/responses/stats.py b/products/surveys/backend/responses/stats.pyindex 85e9c5bcb175..e5aad8ccc643 100644--- a/products/surveys/backend/responses/stats.py+++ b/products/surveys/backend/responses/stats.py@@ -381,27 +381,29 @@ def get_survey_responses_count(return {}placeholders: dict[str, ast.Expr] = {"since": ast.Constant(value=earliest_start_date)}- conditions = [f"uuid IN {_latest_response_event_uuids_subquery(' AND timestamp >= {since}')}"]+ scan_conditions = ""+ if survey_ids:+ scan_conditions = "\n AND properties.$survey_id IN {survey_ids}"+ placeholders["survey_ids"] = ast.Constant(value=survey_ids)+ archived_filter = ""if exclude_archived:archived_uuids = get_archived_response_uuids(None, team.pk)if archived_uuids:- conditions.append("uuid NOT IN {archived_uuids}")+ archived_filter = "\n WHERE response_uuid NOT IN {archived_uuids}"placeholders["archived_uuids"] = ast.Constant(value=sorted(archived_uuids))- if survey_ids:- conditions.append("properties.$survey_id IN {survey_ids}")- placeholders["survey_ids"] = ast.Constant(value=survey_ids)-- condition_sql = "".join(f"\n AND {condition}" for condition in conditions)-# HogQL adds a 100-row LIMIT to a top-level SELECT without one, which would silently drop# surveys past the first hundred; MAX_SELECT_RETURNED_ROWS is the most a query may return.query = f"""- SELECT properties.$survey_id AS survey_id, count()- FROM events- WHERE {RESPONSE_EVENT_FILTER}- AND timestamp >= {{since}}{condition_sql}+ SELECT survey_id, count()+ FROM (+ SELECT properties.$survey_id AS survey_id, argMax(uuid, tuple(timestamp, uuid)) AS response_uuid+ FROM events+ WHERE {RESPONSE_EVENT_FILTER}+ AND timestamp >= {{since}}{scan_conditions}+ GROUP BY survey_id, {SUBMISSION_GROUPING_KEY}+ ){archived_filter}GROUP BY survey_idLIMIT {MAX_SELECT_RETURNED_ROWS}"""