: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(sql-editor): limit table categories to 15 rows (#129119)
Jovan Sakovic60e159eeef6f
diff --git a/frontend/src/scenes/data-warehouse/editor/sidebar/QueryDatabase.tsx b/frontend/src/scenes/data-warehouse/editor/sidebar/QueryDatabase.tsxindex ef015b10de8b..8f923ed655fd 100644--- a/frontend/src/scenes/data-warehouse/editor/sidebar/QueryDatabase.tsx+++ b/frontend/src/scenes/data-warehouse/editor/sidebar/QueryDatabase.tsx@@ -147,7 +147,7 @@ export const QueryDatabase = ({searchText,joinsByFieldName,editingDraftId,- displayedTreeData,+ visibleTreeData,expandedItemIds,connectionId,activeDraggedViewId,@@ -415,8 +415,8 @@ export const QueryDatabase = ({// searching, so an unfiltered section would sit above the results still listing// everything — and keeping the search term on this side means an embedder doesn't have to// mount this logic just to read it.- return getQueryDatabaseTreeData(displayedTreeData, extraTreeSections, searchText)- }, [extraTreeSections, displayedTreeData, searchText])+ return getQueryDatabaseTreeData(visibleTreeData, extraTreeSections, searchText)+ }, [extraTreeSections, visibleTreeData, searchText])const tree = (<LemonTree@@ -461,6 +461,9 @@ export const QueryDatabase = ({setEditingDraft('')}}onItemClick={(item) => {+ if (item?.record?.type === 'show-all-tables') {+ posthog.capture('sql-editor-show-all-tables-clicked')+ }// Handle draft clicks - focus existing tab or create new oneif (item && item.record?.type === 'draft') {router.actions.push(urls.sqlEditor({ draftId: item.record.draft.id }))diff --git a/frontend/src/scenes/data-warehouse/editor/sidebar/queryDatabaseLogic.test.ts b/frontend/src/scenes/data-warehouse/editor/sidebar/queryDatabaseLogic.test.tsindex d7f2c196aeda..9e769ca2eb5f 100644--- a/frontend/src/scenes/data-warehouse/editor/sidebar/queryDatabaseLogic.test.ts+++ b/frontend/src/scenes/data-warehouse/editor/sidebar/queryDatabaseLogic.test.ts@@ -1,6 +1,7 @@import { expectLogic } from 'kea-test-utils'import { FEATURE_FLAGS } from 'lib/constants'+import type { TreeDataItem } from 'lib/lemon-ui/LemonTree/LemonTree'import { featureFlagLogic } from 'lib/logic/featureFlagLogic'import { newInternalTab } from 'lib/utils/newInternalTab'import {@@ -678,6 +679,47 @@ describe('queryDatabaseLogic', () => {logic.unmount()})+ it.each([+ ['PostHog', 'posthog'],+ ['System', 'system'],+ ] as const)('limits the default %s category and keeps all search matches', (categoryName, tableType) => {+ initKeaTests()+ const logic = queryDatabaseLogic()+ logic.mount()+ const names = Array.from({ length: 20 }, (_, index) => `${tableType}.example_${index + 1}`)+ databaseTableListLogic.findMounted()!.actions.loadDatabaseSuccess({+ tables: {+ events: { id: 'events', name: 'events', type: 'posthog', fields: {} },+ ...Object.fromEntries(names.map((name) => [name, { id: name, name, type: tableType, fields: {} }])),+ },+ joins: [],+ })++ const category = (): TreeDataItem =>+ logic.values.visibleTreeData+ .find((item) => item.record?.type === 'sources')!+ .children!.find((item) => item.name === categoryName)!++ expect(category().children).toHaveLength(16)+ expect(category().children?.at(-1)?.name).toEqual('Show all')+ expect(+ logic.values.visibleTreeData+ .find((item) => item.id === 'sources')!+ .children!.find((item) => item.name === 'Popular')?.children+ ).toHaveLength(1)++ logic.actions.setSearchTerm('example')+ expect(category().children).toHaveLength(20)+ expect(category().children?.every((item) => item.record?.type === 'table')).toEqual(true)++ logic.actions.clearSearch()+ expect(category().children).toHaveLength(16)+ category().children?.at(-1)?.onClick?.()+ expect(category().children).toHaveLength(tableType === 'posthog' ? 21 : 20)++ logic.unmount()+ })+it('combines status search tokens with names and clears them when locating a table', async () => {initKeaTests()const logic = queryDatabaseLogic()@@ -1118,6 +1160,89 @@ describe('queryDatabaseLogic', () => {jest.clearAllMocks()})+ it.each([15, 18])('shows all %s tables through expansion and search', (count) => {+ const databaseLogic = databaseTableListLogic.findMounted()!+ const tables = Object.fromEntries(+ ['public', 'archive'].flatMap((schema) =>+ Array.from({ length: count }, (_, index) => {+ const name = `${schema}.example_${String(index + 1).padStart(2, '0')}`+ return [+ name,+ {+ id: name,+ name,+ type: 'data_warehouse',+ fields: {},+ source: {+ id: 'source-id',+ status: 'Running',+ source_type: 'Postgres',+ prefix: '',+ access_method: 'direct',+ },+ },+ ]+ })+ )+ )+ databaseLogic.actions.loadDatabaseSuccess({ tables, joins: [] } as any)++ const category = (): any => logic.values.visibleTreeData.find((item) => item.name === 'public')+ expect(category().children.filter((item: any) => item.record?.type === 'table')).toHaveLength(15)+ expect(category().children.at(-1).name).toEqual(count > 15 ? 'Show all' : 'public.example_15')+ expect(logic.values.displayedTreeData.find((item) => item.name === 'public')?.children).toHaveLength(count)++ if (count > 15) {+ const focusItem = jest.fn()+ logic.actions.setTreeRef({ current: { focusItem, getVisibleItems: () => [] } })+ category().children.at(-1).onClick()+ expect(category().children).toHaveLength(count)+ expect(category().children.at(-1).name).toEqual('public.example_18')+ expect(logic.values.visibleTreeData.find((item) => item.name === 'archive')?.children).toHaveLength(16)+ expect(focusItem).toHaveBeenCalledWith('table-public.example_16')+ }++ logic.actions.setSearchTerm('example')+ expect(logic.values.visibleTreeData.every((item) => item.children?.length === count)).toEqual(true)+ expect(logic.values.visibleTreeData.flatMap((item) => item.children ?? [])).toHaveLength(count * 2)++ logic.actions.clearSearch()+ expect(category().children).toHaveLength(count)+ databaseLogic.actions.setConnection('another-source')+ databaseLogic.actions.loadDatabaseSuccess({ tables, joins: [] } as any)+ expect(category().children).toHaveLength(count > 15 ? 16 : 15)+ })++ it.each([1, 15, 18])('reveals only the necessary tables when locating table %i', (tableNumber) => {+ databaseTableListLogic.findMounted()!.actions.loadDatabaseSuccess({+ tables: Object.fromEntries(+ Array.from({ length: 18 }, (_, index) => {+ const name = `public.example_${String(index + 1).padStart(2, '0')}`+ return [name, { id: name, name, type: 'data_warehouse', fields: {} }]+ })+ ),+ joins: [],+ } as any)++ const focusItem = jest.fn()+ logic.actions.setTreeRef({ current: { focusItem, getVisibleItems: () => [] } })+ const tableName = `public.example_${String(tableNumber).padStart(2, '0')}`+ logic.actions.locateTable(tableName)++ expect(logic.values.visibleTreeData[0].children).toHaveLength(tableNumber <= 15 ? 16 : 18)+ expect(logic.values.visibleTreeData[0].children?.at(-1)?.name).toEqual(+ tableNumber <= 15 ? 'Show all' : 'public.example_18'+ )+ expect(logic.values.expandedItemIds).toContain(`table-${tableName}`)+ expect(focusItem).toHaveBeenLastCalledWith(`table-${tableName}`, {+ scrollPosition: 'top-third',+ behavior: 'smooth',+ })+ if (tableNumber <= 15) {+ expect(focusItem).toHaveBeenCalledTimes(1)+ }+ })+it('shows a direct schema failure and retry at the root of the sidebar', () => {databaseTableListLogic.findMounted()?.actions.loadDatabaseFailure('Connection failed.')diff --git a/frontend/src/scenes/data-warehouse/editor/sidebar/queryDatabaseLogic.tsx b/frontend/src/scenes/data-warehouse/editor/sidebar/queryDatabaseLogic.tsxindex 249c0f170821..84d0bd54e755 100644--- a/frontend/src/scenes/data-warehouse/editor/sidebar/queryDatabaseLogic.tsx+++ b/frontend/src/scenes/data-warehouse/editor/sidebar/queryDatabaseLogic.tsx@@ -55,7 +55,11 @@ import { HOGQL_METRIC_DEFINITION_KIND } from 'products/data_catalog/frontend/comimport type { DataCatalogMetricApi } from 'products/data_catalog/frontend/generated/api.schemas'import { metricsLogic } from 'products/data_catalog/frontend/metricsLogic'import { SourceIcon, mapUrlToProvider } from 'products/data_warehouse/frontend/shared/components/SourceIcon'-import { groupDirectConnectionTableNodesBySchema } from 'products/data_warehouse/frontend/shared/connectionTableTree'+import {+ TABLE_CATEGORY_LIMIT,+ groupDirectConnectionTableNodesBySchema,+ limitTableTreeCategories,+} from 'products/data_warehouse/frontend/shared/connectionTableTree'import {createVirtualTableField,resolveFieldTraverserTarget,@@ -1850,6 +1854,7 @@ export interface queryDatabaseLogicValues {searchTreeSourceContext: SearchTreeSourceContextselectedDirectSource: ExternalDataSourceConnectionOptionApi | undefinedselectedSchema: DatabaseSchemaDataWarehouseTable | DatabaseSchemaTable | DataWarehouseSavedQuerySummary | null+ shownTableFoldersByConnection: Record<string, string[]>sidebarOverlayTreeItems: TreeItem[]sidebarPosthogTables: DatabaseSchemaTable[]syncMoreNoticeDismissed: boolean@@ -1857,6 +1862,7 @@ export interface queryDatabaseLogicValues {treeData: TreeDataItem[]treeDataContext: TreeDataContexttreeRef: EditorSidebarTreeRef+ visibleTreeData: TreeDataItem[]}// Generated by kea-typegen. Update if you're an agent, ignore if you're human.@@ -2140,6 +2146,13 @@ export interface queryDatabaseLogicActions {setTreeRef: (ref: EditorSidebarTreeRef | null) => {ref: EditorSidebarTreeRef}+ showAllTables: (+ folderId: string,+ connectionId: string | null+ ) => {+ connectionId: string | null+ folderId: string+ }startDraggingView: (viewId: string) => {viewId: string}@@ -2288,6 +2301,12 @@ export interface queryDatabaseLogicMeta {allTablesMap: Record<string, DatabaseSchemaTable>,materializationStatusFilters: ('Completed' | 'Failed' | 'Running')[]) => TreeDataItem[]+ visibleTreeData: (+ displayedTreeData: TreeDataItem[],+ searchText: string,+ shownTableFoldersByConnection: Record<string, string[]>,+ connectionId: string | null+ ) => TreeDataItem[]activeExpandedFolderIds: (searchText: string,expandedSearchFolders: string[],@@ -2332,6 +2351,7 @@ export const queryDatabaseLogic = kea<queryDatabaseLogicType>([}),setExpandedFolders: (folderIds: string[], connectionId?: string | null) => ({ folderIds, connectionId }),setExpandedSearchFolders: (folderIds: string[]) => ({ folderIds }),+ showAllTables: (folderId: string, connectionId: string | null) => ({ folderId, connectionId }),toggleFolderOpen: (folderId: string, isExpanded: boolean) => ({ folderId, isExpanded }),setTreeRef: (ref: EditorSidebarTreeRef | null) => ({ ref }),setSearchTerm: (searchTerm: string) => ({ searchTerm }),@@ -2458,6 +2478,15 @@ export const queryDatabaseLogic = kea<queryDatabaseLogicType>([],})),reducers({+ shownTableFoldersByConnection: [+ {} as Record<string, string[]>,+ {+ showAllTables: (state, { folderId, connectionId }) => {+ const key = getExpandedFoldersConnectionKey(connectionId)+ return { ...state, [key]: Array.from(new Set([...(state[key] ?? []), folderId])) }+ },+ },+ ],metricsLoadFailed: [false,{@@ -2827,6 +2856,13 @@ export const queryDatabaseLogic = kea<queryDatabaseLogicType>([}const tableId = path[path.length - 1].id+ if (!findTreeItemById(values.visibleTreeData, tableId)) {+ path.forEach((item) => {+ if (item.record?.type === 'source-folder') {+ actions.showAllTables(item.id, values.connectionId)+ }+ })+ }actions.setExpandedFolders(Array.from(new Set([...values.expandedFolders, ...path.map((item) => item.id)])),values.connectionId@@ -2842,6 +2878,14 @@ export const queryDatabaseLogic = kea<queryDatabaseLogicType>([}return {+ showAllTables: ({ folderId }) => {+ const firstRevealedTable = findTreeItemById(values.displayedTreeData, folderId)?.children?.[+ TABLE_CATEGORY_LIMIT+ ]+ if (firstRevealedTable) {+ values.treeRef?.current?.focusItem(firstRevealedTable.id)+ }+ },locateTable: ({ tableName }) => {revealLocatedTable(tableName)},@@ -3804,6 +3848,22 @@ export const queryDatabaseLogic = kea<queryDatabaseLogicType>([]},],+ visibleTreeData: [+ (s) => [s.displayedTreeData, s.searchText, s.shownTableFoldersByConnection, s.connectionId],+ (+ displayedTreeData: TreeDataItem[],+ searchText: string,+ shownTableFoldersByConnection: Record<string, string[]>,+ connectionId: string | null+ ): TreeDataItem[] =>+ searchText+ ? displayedTreeData+ : limitTableTreeCategories(+ displayedTreeData,+ shownTableFoldersByConnection[getExpandedFoldersConnectionKey(connectionId)] ?? [],+ (folderId) => actions.showAllTables(folderId, connectionId)+ ),+ ],activeExpandedFolderIds: [(s) => [s.searchText, s.expandedSearchFolders, s.expandedFolders],(searchText: string, expandedSearchFolders: string[], expandedFolders: string[]): string[] => {diff --git a/products/data_warehouse/frontend/shared/connectionTableTree.tsx b/products/data_warehouse/frontend/shared/connectionTableTree.tsxindex 4f583e289fe3..ba98fef04c6c 100644--- a/products/data_warehouse/frontend/shared/connectionTableTree.tsx+++ b/products/data_warehouse/frontend/shared/connectionTableTree.tsx@@ -1,7 +1,43 @@-import { IconFolder } from '@posthog/icons'+import { IconChevronDown, IconFolder } from '@posthog/icons'import { TreeDataItem } from 'lib/lemon-ui/LemonTree/LemonTree'+export const TABLE_CATEGORY_LIMIT = 15++export const limitTableTreeCategories = (+ items: TreeDataItem[],+ shownFolderIds: string[],+ showAllTables: (folderId: string) => void+): TreeDataItem[] =>+ items.map((item) => {+ if (!item.children || item.record?.type === 'table') {+ return item+ }++ if (+ item.record?.type === 'source-folder' &&+ item.children.length > TABLE_CATEGORY_LIMIT &&+ !shownFolderIds.includes(item.id)+ ) {+ return {+ ...item,+ children: [+ ...item.children.slice(0, TABLE_CATEGORY_LIMIT),+ {+ id: `${item.id}-show-all-tables`,+ name: 'Show all',+ displayName: <span className="text-link">Show all</span>,+ icon: <IconChevronDown />,+ record: { type: 'show-all-tables' },+ onClick: () => showAllTables(item.id),+ },+ ],+ }+ }++ return { ...item, children: limitTableTreeCategories(item.children, shownFolderIds, showAllTables) }+ })+const getDirectConnectionSchemaName = (tableNode: TreeDataItem, defaultSchemaName?: string | null): string | null => {const tableName =tableNode.record?.type === 'table' ? (tableNode.record.table?.name ?? tableNode.name) : tableNode.name