Skip to content

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

fix(surveys): load results for a survey with one question (#94754)

posthog[bot]715eef4f0e93
diff --git a/frontend/src/scenes/surveys/utils.test.ts b/frontend/src/scenes/surveys/utils.test.ts
index 2aed81641f63..3adcd4eb105e 100644
--- a/frontend/src/scenes/surveys/utils.test.ts
+++ b/frontend/src/scenes/surveys/utils.test.ts
@@ -1184,6 +1184,37 @@ describe('survey utils', () => {
expect(query!.match(/argMaxIf\(q0_raw/g)).toHaveLength(1)
})
+ it.each([
+ ['rating', { id: 'q-rating', type: SurveyQuestionType.Rating, question: 'How was it?' }],
+ ['open', { id: 'q-open', type: SurveyQuestionType.Open, question: 'Why?' }],
+ [
+ 'single choice',
+ {
+ id: 'q-choice',
+ type: SurveyQuestionType.SingleChoice,
+ question: 'Pick one',
+ choices: ['a', 'b'],
+ },
+ ],
+ ])('builds a valid query for a survey with only one required %s question', (_type, question) => {
+ const survey = { ...buildSurvey(true), questions: [question] } as Survey
+
+ const query = buildAggregateQuery(survey, buildFilters(survey))
+
+ // These questions each emit one label-pair expression, and HogQL rejects arrayConcat
+ // with a single argument, so the results tab failed to load.
+ expect(query).not.toContain('arrayConcat')
+ expect(query).toContain('arrayJoin(if(isNotNull(q0_answer)')
+ })
+
+ it('concatenates the label pairs when a survey emits more than one expression', () => {
+ const survey = buildSurvey(true)
+
+ const query = buildAggregateQuery(survey, buildFilters(survey))
+
+ expect(query).toContain('arrayJoin(arrayConcat(')
+ })
+
it('does not alias the merged timestamp back onto the column the merge orders by', () => {
const survey = buildSurvey(true)
diff --git a/frontend/src/scenes/surveys/utils.ts b/frontend/src/scenes/surveys/utils.ts
index d082ba682937..870b415329cc 100644
--- a/frontend/src/scenes/surveys/utils.ts
+++ b/frontend/src/scenes/surveys/utils.ts
@@ -892,14 +892,19 @@ export function buildAggregateQuery(survey: Survey, filters: SurveyQueryFilters)
const mergedSubmissions = buildMergedSubmissionsSubquery(survey, filters, questions)
+ // arrayConcat needs two arguments or more. A survey that emits one pair expression, such as a
+ // single rating question, goes straight to arrayJoin.
+ const allLabelPairs =
+ labelPairs.length === 1
+ ? labelPairs[0]
+ : `arrayConcat(\n ${labelPairs.join(',\n ')}\n )`
+
return `SELECT
tupleElement(question_label, 1) AS question_id,
tupleElement(question_label, 2) AS label,
count() AS cnt
FROM (
- SELECT arrayJoin(arrayConcat(
- ${labelPairs.join(',\n ')}
- )) AS question_label
+ SELECT arrayJoin(${allLabelPairs}) AS question_label
FROM (
${mergedSubmissions}
)