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(insights): offer only shared columns for multi-table warehouse breakdowns (#102807)

Thomas Obermüllera02915efe79c
diff --git a/frontend/src/lib/components/TaxonomicFilter/dataWarehouseColumns.test.ts b/frontend/src/lib/components/TaxonomicFilter/dataWarehouseColumns.test.ts
index bcb319e21b38..785dac3cb975 100644
--- a/frontend/src/lib/components/TaxonomicFilter/dataWarehouseColumns.test.ts
+++ b/frontend/src/lib/components/TaxonomicFilter/dataWarehouseColumns.test.ts
@@ -85,4 +85,34 @@ describe('dataWarehouseColumnsWithJoins', () => {
).map((column) => column.name)
).toEqual(['total'])
})
+
+ it.each([
+ [false, ['id', 'country']],
+ [true, ['id', 'country', 'campaign.name']],
+ ])(
+ 'offers only the columns every table shares when an insight spans several (includeJoinedColumns=%s)',
+ (includeJoinedColumns, expected) => {
+ const columns = dataWarehouseColumnsWithJoins(
+ ['orders', 'refunds'],
+ {
+ orders: table('orders', [
+ field('id', 'string'),
+ field('country', 'string'),
+ field('amount', 'integer'),
+ field('campaign', 'lazy_table', 'campaigns'),
+ ]),
+ refunds: table('refunds', [
+ field('id', 'string'),
+ field('country', 'string'),
+ field('reason', 'string'),
+ field('campaign', 'lazy_table', 'campaigns'),
+ ]),
+ campaigns: table('campaigns', [field('name', 'string')]),
+ },
+ { includeJoinedColumns }
+ )
+
+ expect(columns.map((column) => column.name)).toEqual(expected)
+ }
+ )
})
diff --git a/frontend/src/lib/components/TaxonomicFilter/dataWarehouseColumns.ts b/frontend/src/lib/components/TaxonomicFilter/dataWarehouseColumns.ts
index 09681eb3aa1c..87a3eb798724 100644
--- a/frontend/src/lib/components/TaxonomicFilter/dataWarehouseColumns.ts
+++ b/frontend/src/lib/components/TaxonomicFilter/dataWarehouseColumns.ts
@@ -22,42 +22,60 @@ export function dataWarehouseColumnsWithJoins(
tableNames: string[],
tablesMap: Record<string, DatabaseSchemaTable>,
{ includeJoinedColumns }: { includeJoinedColumns: boolean }
+): DatabaseSchemaField[] {
+ const columnsPerTable = tableNames.map((tableName) => columnsForTable(tableName, tablesMap, includeJoinedColumns))
+
+ const [firstTableColumns, ...remainingTableColumns] = columnsPerTable
+ if (remainingTableColumns.length === 0) {
+ return firstTableColumns ?? []
+ }
+
+ // A breakdown resolves against each series' own table, so a column that only some of the
+ // tables have fails the whole insight.
+ const remainingColumnNames = remainingTableColumns.map(
+ (tableColumns) => new Set(tableColumns.map((column) => column.name))
+ )
+ return firstTableColumns.filter((column) => remainingColumnNames.every((names) => names.has(column.name)))
+}
+
+function columnsForTable(
+ tableName: string,
+ tablesMap: Record<string, DatabaseSchemaTable>,
+ includeJoinedColumns: boolean
): DatabaseSchemaField[] {
const columns: DatabaseSchemaField[] = []
- for (const tableName of tableNames) {
- for (const field of Object.values(tablesMap[tableName]?.fields ?? {})) {
- if (!HIDDEN_FIELD_TYPES.includes(field.type)) {
- columns.push(field)
+ for (const field of Object.values(tablesMap[tableName]?.fields ?? {})) {
+ if (!HIDDEN_FIELD_TYPES.includes(field.type)) {
+ columns.push(field)
+ continue
+ }
+ if (!includeJoinedColumns || !field.table) {
+ continue
+ }
+ // `field.table` carries the joined table's printed HogQL name, which is backquoted when
+ // the name is not a bare identifier. A connector-synced table is registered under its
+ // dotted chain, so it arrives as a backquoted "stripe.campaigns" while `tablesMap` is
+ // keyed by the bare form. Strip the quotes as a fallback, the same way the SQL editor
+ // sidebar does in `normalizeTableLookupKey`.
+ const joinedTable = tablesMap[field.table] ?? tablesMap[field.table.replaceAll('`', '')]
+ for (const joinedField of Object.values(joinedTable?.fields ?? {})) {
+ if (HIDDEN_FIELD_TYPES.includes(joinedField.type)) {
continue
}
- if (!includeJoinedColumns || !field.table) {
+ // A traverser reaches another field through a chain, and the chain can end at a table
+ // instead of a value. `events.person` is one, so a warehouse table joined to `events`
+ // would offer a breakdown the printer rejects with "Can't select a table when a
+ // column is expected". Telling the two apart needs the chain resolved, so the scalar
+ // traversers such as `$virt_revenue` on persons and groups are dropped as well.
+ if (joinedField.type === 'field_traverser') {
continue
}
- // `field.table` carries the joined table's printed HogQL name, which is backquoted when
- // the name is not a bare identifier. A connector-synced table is registered under its
- // dotted chain, so it arrives as a backquoted "stripe.campaigns" while `tablesMap` is
- // keyed by the bare form. Strip the quotes as a fallback, the same way the SQL editor
- // sidebar does in `normalizeTableLookupKey`.
- const joinedTable = tablesMap[field.table] ?? tablesMap[field.table.replaceAll('`', '')]
- for (const joinedField of Object.values(joinedTable?.fields ?? {})) {
- if (HIDDEN_FIELD_TYPES.includes(joinedField.type)) {
- continue
- }
- // A traverser reaches another field through a chain, and the chain can end at a table
- // instead of a value. `events.person` is one, so a warehouse table joined to `events`
- // would offer a breakdown the printer rejects with "Can't select a table when a
- // column is expected". Telling the two apart needs the chain resolved, so the scalar
- // traversers such as `$virt_revenue` on persons and groups are dropped as well.
- if (joinedField.type === 'field_traverser') {
- continue
- }
- columns.push({
- ...joinedField,
- name: `${field.name}.${joinedField.name}`,
- hogql_value: `${field.hogql_value}.${joinedField.hogql_value}`,
- })
- }
+ columns.push({
+ ...joinedField,
+ name: `${field.name}.${joinedField.name}`,
+ hogql_value: `${field.hogql_value}.${joinedField.hogql_value}`,
+ })
}
}