Repository navigation
fix(knowledge): count knowledge-base list totals with a lateral aggregate #8560
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
a263c6d
fix(knowledge): count knowledge-base list totals with a lateral aggre…
waleedlatif1 3fcf7e2
test(knowledge): pin per-base document reads in the counted list plan
waleedlatif1 4f72a21
test(knowledge): tear down every fixture row in the list totals plan …
waleedlatif1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
apps/sim/lib/knowledge/__integration__/list-totals-plan.integration.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| /** | ||
| * The counted knowledge-base list must total each base through that base's own documents, never | ||
| * through a set-based join over the whole document table. The reader's ACL tokens are shared by | ||
| * every tenant, so a join the planner drives from the ACL index instead of `knowledge_base_id` | ||
| * reads every tenant's documents to total one base. Nested loops are disabled while planning so | ||
| * the planner reaches for exactly that join wherever the query shape still allows it. | ||
| */ | ||
| import type { Principal } from '@sim/auth/principal' | ||
| import * as schema from '@sim/db/schema' | ||
| import { document, organization, user, workspace } from '@sim/db/schema' | ||
| import { readTestDatabaseUrl } from '@sim/db/testing/test-infrastructure' | ||
| import { withUtcTimestamps } from '@sim/db/timestamps' | ||
| import { generateId } from '@sim/utils/id' | ||
| import { eq, inArray } from 'drizzle-orm' | ||
| import { drizzle, type PostgresJsDatabase } from 'drizzle-orm/postgres-js' | ||
| import postgres from 'postgres' | ||
| import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const database = vi.hoisted(() => ({ | ||
| current: undefined as PostgresJsDatabase<typeof import('@sim/db/schema')> | undefined, | ||
| /** Every statement the app issues, so the test can EXPLAIN the exact SQL it ran. */ | ||
| statements: [] as { sql: string; params: unknown[] }[], | ||
| })) | ||
| vi.mock('@sim/db', async (importOriginal) => ({ | ||
| ...(await importOriginal<typeof import('@sim/db')>()), | ||
| get db() { | ||
| if (!database.current) throw new Error('Test database not initialized') | ||
| return database.current | ||
| }, | ||
| })) | ||
|
|
||
| import { | ||
| createKnowledgeAclFixtureIds, | ||
| seedKnowledgeAclFixture, | ||
| } from '@/lib/knowledge/__integration__/seed-source-access-fixture' | ||
| import { listKnowledgeBases } from '@/lib/knowledge/application/knowledge-bases' | ||
|
|
||
| interface PlanNode { | ||
| 'Node Type': string | ||
| 'Relation Name'?: string | ||
| 'Index Cond'?: string | ||
| 'Recheck Cond'?: string | ||
| Filter?: string | ||
| Plans?: PlanNode[] | ||
| } | ||
|
|
||
| function documentScans(node: PlanNode): PlanNode[] { | ||
| const own = node['Relation Name'] === 'document' ? [node] : [] | ||
| return [...own, ...(node.Plans ?? []).flatMap(documentScans)] | ||
| } | ||
|
|
||
| const ids = createKnowledgeAclFixtureIds() | ||
| const reader: Principal = { kind: 'session', userId: ids.bobId, sessionId: 'fixture-reader' } | ||
| const connection = postgres( | ||
| readTestDatabaseUrl(), | ||
| withUtcTimestamps({ | ||
| max: 2, | ||
| prepare: false, | ||
| fetch_types: false, | ||
| connection: {}, | ||
| onnotice: () => {}, | ||
| }) | ||
| ) | ||
|
|
||
| describe('knowledge-base list totals plan', () => { | ||
| beforeAll(async () => { | ||
| vi.stubGlobal('fetch', async () => { | ||
| throw new Error('Unexpected provider request in knowledge-base totals plan test') | ||
| }) | ||
| database.current = drizzle(connection, { | ||
| schema, | ||
| logger: { | ||
| logQuery(sql, params) { | ||
| database.statements.push({ sql, params }) | ||
| }, | ||
| }, | ||
| }) | ||
| await seedKnowledgeAclFixture(ids, { connectorType: 'google_drive' }) | ||
| await database.current.insert(document).values({ | ||
| id: generateId(), | ||
| knowledgeBaseId: ids.knowledgeBaseId, | ||
| filename: 'Workspace handbook', | ||
| fileUrl: 'https://fixture.test/shared', | ||
| fileSize: 10, | ||
| mimeType: 'text/plain', | ||
| tokenCount: 10, | ||
| processingStatus: 'completed', | ||
| }) | ||
| }) | ||
|
|
||
| afterAll(async () => { | ||
| await database.current?.delete(workspace).where(eq(workspace.id, ids.workspaceId)) | ||
|
waleedlatif1 marked this conversation as resolved.
waleedlatif1 marked this conversation as resolved.
|
||
| await database.current?.delete(organization).where(eq(organization.id, ids.organizationId)) | ||
| await database.current?.delete(user).where(inArray(user.id, [ids.aliceId, ids.bobId])) | ||
| await connection.end() | ||
| vi.unstubAllGlobals() | ||
| }) | ||
|
|
||
| it('totals each base through its own documents even when the planner prefers a hash join', async () => { | ||
| database.statements.length = 0 | ||
| const { knowledgeBases } = await listKnowledgeBases.execute({ | ||
| principal: reader, | ||
| input: { workspaceId: ids.workspaceId }, | ||
| }) | ||
| expect(knowledgeBases.map(({ knowledgeBase }) => knowledgeBase)).toEqual([ | ||
| expect.objectContaining({ id: ids.knowledgeBaseId, docCount: 1, tokenCount: 10 }), | ||
| ]) | ||
|
|
||
| const counted = database.statements.filter( | ||
| ({ sql }) => | ||
| /^select .* from "knowledge_base" /.test(sql) && | ||
| sql.includes('"document"."knowledge_base_id" = "knowledge_base"."id"') | ||
| ) | ||
| expect(counted).toHaveLength(1) | ||
| const plan = await connection.begin(async (tx) => { | ||
| await tx`SET LOCAL enable_nestloop = off` | ||
| const [row] = await tx.unsafe( | ||
| `EXPLAIN (FORMAT JSON) ${counted[0].sql}`, | ||
| counted[0].params as Parameters<typeof tx.unsafe>[1] | ||
| ) | ||
| return (row['QUERY PLAN'] as { Plan: PlanNode }[])[0].Plan | ||
| }) | ||
|
|
||
| const scans = documentScans(plan) | ||
| expect(scans.length).toBeGreaterThan(0) | ||
| for (const scan of scans) { | ||
| const conditions = [scan['Index Cond'], scan['Recheck Cond'], scan.Filter].join(' ') | ||
| expect(conditions, `${scan['Node Type']} on document`).toContain( | ||
| '(knowledge_base_id = knowledge_base.id)' | ||
| ) | ||
| } | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.