: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(bi): open join key checks from source context (#124729)
Marius Andrab9220923c10f
diff --git a/docs/published/handbook/engineering/data-warehouse.md b/docs/published/handbook/engineering/data-warehouse.mdindex 81e130f8127e..01ce85ce1174 100644--- a/docs/published/handbook/engineering/data-warehouse.md+++ b/docs/published/handbook/engineering/data-warehouse.md@@ -422,6 +422,8 @@ The BI assistant receives the current worksheet draft, including its unsaved nam## Connected fields in BI mode+**Check key uniqueness** opens an editable SQL check for repeated or null keys in a configured relationship's joined table. Press **Run** in the SQL editor to execute it. The check scans the full joined table, ignores worksheet filters, and returns at most 100 key groups. It does not check missing matches or prove that a join is safe for every aggregation. Expression-based keys require a custom SQL check.+Below Dimensions and Measures, **Connections** lists the selected table's linked tables and views.Expand a connection to load its dimensions, measures, and nested connections. Inside connections,fields and further links appear without section headings.diff --git a/frontend/snapshots.yml b/frontend/snapshots.ymlindex ab1001b9d932..c43230792290 100644--- a/frontend/snapshots.yml+++ b/frontend/snapshots.yml@@ -6956,6 +6956,10 @@ snapshots:hash: v1.k794b7964.8d8cbf69107589434273ed038b882ce9e93026446d051749f32b0a7e1f04d2c8.yff6d37Q2svsHrtAs0TwZcLs3UNT4ZBl4am4-0yka1Uproducts-business-intelligence-worksheet-exploration--nested-panes-narrow--light:hash: v1.k794b7964.10c232baf0e2d4c1afcc5fb2be5acddc52a30dc4404875e96de9c0e2070614f4.RMRmcYG-Phs-D2knyQeqbJHzqJKwrp57f9c5g1Tgd_0+ products-business-intelligence-worksheet-exploration--relationship-key-check--dark:+ hash: v1.k794b7964.2a11a78e396147fd11cc26642853e3cc96e277c51ca2e2abed410b7b26de00b5.BCmW-8EYSpraeogrER9QgS7iPpgfF0cKuZpI9PjvKPs+ products-business-intelligence-worksheet-exploration--relationship-key-check--light:+ hash: v1.k794b7964.7cadfcd686a396debfecfb0e8479f27b3e05d17a97d705d8abb3b2c232d346be.pOtL9jQKqz5nFkHkCrl_1hOAvKF5qOvCYPg2ihea0HUproducts-business-intelligence-worksheet-exploration--scatter-selection--dark:hash: v1.k794b7964.ee16fb7d4d6e6dc398f1c164ce88f1ac8d7226449171daa2ad74695a64b5b306.Ia7P6JWA8-O6kyHAk-a_LzZplfWxDHaDifEW5vvYXzgproducts-business-intelligence-worksheet-exploration--scatter-selection--light:diff --git a/products/business_intelligence/frontend/BISourceContext.tsx b/products/business_intelligence/frontend/BISourceContext.tsxindex 6c0bd5053610..8f8bb28dc1fc 100644--- a/products/business_intelligence/frontend/BISourceContext.tsx+++ b/products/business_intelligence/frontend/BISourceContext.tsx@@ -11,6 +11,7 @@ import { TableCertificationTag } from 'products/data_warehouse/frontend/shared/cimport { captureBIWorksheetAction } from './biEditorAnalytics'import { biEditorLogic } from './biEditorLogic'+import { joinKeyCheckQuery } from './biJoinKeyCheck'import { biSourceContextLogic } from './biSourceContextLogic'export function BISourceContext({ table }: { table: DatabaseSchemaTable }): JSX.Element {@@ -44,14 +45,34 @@ export function BISourceContext({ table }: { table: DatabaseSchemaTable }): JSX.</LemonButton><div className="flex flex-col gap-2"><span className="font-semibold">Relationships and review status</span>- {joins.map((join) => (- <div key={join.id} className="break-words">- {join.source_table_name}.{join.source_table_key} → {join.joining_table_name}.- {join.joining_table_key}- <div className="text-secondary">Access with {join.field_name}</div>- <LemonTag type="success">Configured</LemonTag>- </div>- ))}+ {joins.map((join) => {+ const keyCheck = joinKeyCheckQuery(join)+ return (+ <div key={join.id} className="break-words">+ {join.source_table_name}.{join.source_table_key} → {join.joining_table_name}.+ {join.joining_table_key}+ <div className="text-secondary">Access with {join.field_name}</div>+ <LemonTag type="success">Configured</LemonTag>+ <LemonButton+ size="xsmall"+ sideIcon={<IconExternal />}+ to={keyCheck ? urls.sqlEditor({ query: keyCheck }) : undefined}+ disabledReason={+ keyCheck ? undefined : 'Check expression-based join keys in the SQL editor'+ }+ tooltip="Opens SQL to find repeated or null keys in the joined table. Repeated keys can multiply worksheet totals."+ >+ Check key uniqueness+ </LemonButton>+ </div>+ )+ })}+ {joins.length > 0 && (+ <span className="text-secondary">+ Configured relationships do not guarantee unique keys. Key checks cover the full joined+ table, without worksheet filters, and do not check for missing matches.+ </span>+ )}{relationships?.results.map((proposal) => (<div key={proposal.id} className="break-words">{proposal.source_table_name}.{proposal.source_table_key} → {proposal.joining_table_name}.diff --git a/products/business_intelligence/frontend/BusinessIntelligenceScene.stories.tsx b/products/business_intelligence/frontend/BusinessIntelligenceScene.stories.tsxindex 3dba228db958..76a750c28a6b 100644--- a/products/business_intelligence/frontend/BusinessIntelligenceScene.stories.tsx+++ b/products/business_intelligence/frontend/BusinessIntelligenceScene.stories.tsx@@ -111,6 +111,8 @@ const meta: Meta = {mswDecorator({get: {'/api/projects/:team_id/insights/': { count: 0, results: [] },+ '/api/projects/:team_id/data_catalog/relationship_proposals/': { results: [], next: null },+ '/api/projects/:team_id/warehouse_column_annotations/': { results: [] },'/api/projects/:team_id/warehouse_saved_queries/': { results: [] },'/api/projects/:team_id/external_data_sources/connections/': [],'/api/projects/:team_id/external_data_sources/direct_connection_options/': [],@@ -157,6 +159,16 @@ const meta: Meta = {return [200,{+ joins: [+ {+ id: 'example-order-account',+ source_table_name: 'example_orders',+ source_table_key: 'account_id',+ joining_table_name: 'example_accounts',+ joining_table_key: 'id',+ field_name: 'account',+ },+ ],tables: {example_legacy_orders: {id: 'example_legacy_orders',@@ -264,6 +276,19 @@ const meta: Meta = {}export default meta+export const RelationshipKeyCheck: StoryObj = {+ render: () => <WorksheetStage config={config} />,+ play: async () => {+ await userEvent.click(+ await waitFor(() => {+ const summary = document.querySelector('[data-attr="bi-source-context"] summary')+ expect(summary).toBeTruthy()+ return summary as HTMLElement+ })+ )+ },+}+export const FiltersAndHierarchy: StoryObj = { render: () => <WorksheetStage config={config} /> }export const Narrow: StoryObj = { render: () => <WorksheetStage config={config} width={520} /> }export const SQLPreview: StoryObj = { render: () => <WorksheetStage config={config} sqlPreview /> }diff --git a/products/business_intelligence/frontend/biJoinKeyCheck.test.ts b/products/business_intelligence/frontend/biJoinKeyCheck.test.tsnew file mode 100644index 000000000000..ec6bbff88291--- /dev/null+++ b/products/business_intelligence/frontend/biJoinKeyCheck.test.ts@@ -0,0 +1,40 @@+import { urls } from 'scenes/urls'++import { joinKeyCheckQuery } from './biJoinKeyCheck'++it('opens a full-table check for repeated and null nested keys as an editable SQL draft', () => {+ const query = joinKeyCheckQuery({+ id: 'example',+ joining_table_name: 'example_accounts',+ joining_table_key: 'properties.account_id',+ })!+ const url = new URL(urls.sqlEditor({ query }), 'https://example.com')+ expect(url.searchParams.get('open_query')).toBe(query)+ expect(query).toContain('SELECT properties.account_id AS join_key\n FROM example_accounts')+ expect(query).toContain('HAVING count(*) > 1 OR isNull(join_key)')+ expect(query).toContain('LIMIT 100')+})++it('quotes table names and refuses expression keys instead of checking a different key', () => {+ expect(+ joinKeyCheckQuery({ id: 'example', joining_table_name: 'example orders', joining_table_key: 'id' })+ ).toContain('FROM "example orders"')+ for (const key of [+ '"Account ID"',+ 'properties."account-id"',+ '"account.id"',+ '`Account ID`',+ '"account""id"',+ '"account\\"id"',+ ]) {+ expect(+ joinKeyCheckQuery({ id: 'example', joining_table_name: 'example_accounts', joining_table_key: key })+ ).toContain(`SELECT ${key} AS join_key`)+ }+ for (const key of ['lower(email)', '"id', '"id"; SELECT 1', 'id -- comment', 'id + 1']) {+ expect(+ joinKeyCheckQuery({ id: 'example', joining_table_name: 'example_accounts', joining_table_key: key })+ ).toBeNull()+ }+ expect(joinKeyCheckQuery({ id: 'example', joining_table_name: 'example_accounts' })).toBeNull()+})diff --git a/products/business_intelligence/frontend/biJoinKeyCheck.ts b/products/business_intelligence/frontend/biJoinKeyCheck.tsnew file mode 100644index 000000000000..9c6463fb706f--- /dev/null+++ b/products/business_intelligence/frontend/biJoinKeyCheck.ts@@ -0,0 +1,32 @@+import { escapeRawPropertyAsHogQLIdentifier } from '~/queries/utils'+import { DataWarehouseViewLink } from '~/types'++const fieldIdentifier =+ /[A-Za-z_$][\w$]*|"(?:[^"\\]|\\(?:[bBfFrRnNtT0aAvV\\"]|[xX][0-9a-fA-F]{2})|"")*"|`(?:[^`\\]|\\(?:[bBfFrRnNtT0aAvV\\']|[xX][0-9a-fA-F]{2})|``)*`/+const fieldReference = new RegExp(`^(?:${fieldIdentifier.source})(?:\\s*\\.\\s*(?:${fieldIdentifier.source}))*$`)++export function joinKeyCheckQuery(join: DataWarehouseViewLink): string | null {+ if (!join.joining_table_name || !join.joining_table_key || !fieldReference.test(join.joining_table_key)) {+ return null+ }+ const table = join.joining_table_name.split('.').map(escapeRawPropertyAsHogQLIdentifier).join('.')+ const key = join.joining_table_key+ .match(new RegExp(fieldIdentifier.source, 'g'))!+ .map((identifier) =>+ identifier.startsWith('"') || identifier.startsWith('`')+ ? identifier+ : escapeRawPropertyAsHogQLIdentifier(identifier)+ )+ .join('.')+ return `SELECT+ join_key,+ count(*) AS matching_rows+FROM (+ SELECT ${key} AS join_key+ FROM ${table}+)+GROUP BY join_key+HAVING count(*) > 1 OR isNull(join_key)+ORDER BY matching_rows DESC+LIMIT 100`+}