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(bi): avoid full catalog copies in connection tree (#112516)

Paul D'Ambra7298c38a25c9
diff --git a/products/business_intelligence/frontend/biConnectionTree.test.ts b/products/business_intelligence/frontend/biConnectionTree.test.ts
index 4b683eea14e9..c95257af4eaa 100644
--- a/products/business_intelligence/frontend/biConnectionTree.test.ts
+++ b/products/business_intelligence/frontend/biConnectionTree.test.ts
@@ -31,6 +31,44 @@ const tables = {
}
describe('BI connections', () => {
+ it('resolves aliases from a table named __proto__', () => {
+ const protoTable = table('__proto__', [
+ field('person', 'lazy_table', { table: 'persons' }),
+ field('alias', 'field_traverser', { chain: ['person'] }),
+ ])
+ const catalog = new Proxy(tables, {
+ get: (target, name, receiver) => (name === '__proto__' ? protoTable : Reflect.get(target, name, receiver)),
+ })
+ const connections = buildBIConnections({ ...source, table: '__proto__' }, catalog, [], {}, true)
+ expect(connections.map((connection) => connection.name)).toEqual(['alias', 'person'])
+ })
+
+ it('drags nested dimensions and measures using paths from the original source', () => {
+ const catalog = new Proxy(tables, {
+ ownKeys: () => {
+ throw new Error('Connection traversal must not enumerate the full table catalog')
+ },
+ })
+ const [person] = buildBIConnections(source, catalog, ['["person"]', '["person","company"]'], {}, true)
+ const [company] = person.connections
+
+ expect(company.connections).toEqual([])
+ const dimension = company.fields.dimensions[0]
+ const measure = company.fields.measures[0]
+ expect(dimension).toMatchObject({ name: 'person.company.name', expression: 'person.company.name', source })
+ expect(measure).toMatchObject({ name: 'person.company.annual_revenue', type: 'decimal', source })
+ const value = getBIDropTarget(measure, 'rows').field as BIField
+ const query = buildBIQuery({
+ ...DEFAULT_BI_CONFIG,
+ source,
+ rows: [dimension],
+ values: [{ field: value, aggregation: 'sum' }],
+ })
+ expect(query?.query).toContain('person.company.name')
+ expect(query?.query).toContain('sum(person.company.annual_revenue)')
+ expect(query?.query).toContain('FROM events')
+ })
+
it.each([
['events', 'person'],
['stripe_charges', 'customer'],
diff --git a/products/business_intelligence/frontend/biConnectionTree.ts b/products/business_intelligence/frontend/biConnectionTree.ts
index e5b4d8389b19..2425f6072db2 100644
--- a/products/business_intelligence/frontend/biConnectionTree.ts
+++ b/products/business_intelligence/frontend/biConnectionTree.ts
@@ -19,6 +19,8 @@ export interface BIConnection {
connections: BIConnection[]
}
+type TableLookup = Record<string, Pick<DatabaseSchemaTable, 'name' | 'fields'>>
+
function getHydrationKey(table: DatabaseSchemaTable): string {
return table.type === 'posthog' ? table.id : table.name
}
@@ -71,11 +73,19 @@ export function buildBIConnections(
const expanded = new Set(expandedIds)
const getTable = (name?: string): DatabaseSchemaTable | null =>
name ? (tables[name] ?? tables[name.replaceAll('`', '')] ?? null) : null
- const visit = (tableName: string, fields: DatabaseSchemaField[], path: string[]): BIConnection[] => {
- const tableLookup = {
- ...tables,
- [tableName]: { name: tableName, fields: Object.fromEntries(fields.map((field) => [field.name, field])) },
- }
+ const getTableLookup = (tableName: string, fields: DatabaseSchemaField[]): TableLookup => {
+ const lookup: TableLookup = Object.create(tables)
+ Object.defineProperty(lookup, tableName, {
+ value: { name: tableName, fields: Object.fromEntries(fields.map((field) => [field.name, field])) },
+ })
+ return lookup
+ }
+ const visit = (
+ tableName: string,
+ fields: DatabaseSchemaField[],
+ path: string[],
+ tableLookup: TableLookup
+ ): BIConnection[] => {
return fields
.flatMap((originalField): BIConnection[] => {
let pendingTableName: string | undefined
@@ -120,13 +130,7 @@ export function buildBIConnections(
}
const childFields = getConnectionFields(field, table)
const nestedTableName = table?.name ?? field.table ?? tableName
- const childLookup = {
- ...tables,
- [nestedTableName]: {
- name: nestedTableName,
- fields: Object.fromEntries(childFields.map((child) => [child.name, child])),
- },
- }
+ const childLookup = getTableLookup(nestedTableName, childFields)
const scalarFields = childFields.map((child) => {
const resolved =
child.type === 'field_traverser'
@@ -142,13 +146,14 @@ export function buildBIConnections(
source,
childPath
),
- connections: visit(nestedTableName, childFields, childPath),
+ connections: visit(nestedTableName, childFields, childPath, childLookup),
},
]
})
.sort((left, right) => left.name.localeCompare(right.name))
}
- return visit(source.table, Object.values(getTable(source.table)?.fields ?? {}), [])
+ const sourceFields = Object.values(getTable(source.table)?.fields ?? {})
+ return visit(source.table, sourceFields, [], getTableLookup(source.table, sourceFields))
}
export function getPendingBIConnectionTables(connections: BIConnection[]): string[] {