diff --git a/apps/desktop/src/lib/__tests__/sql/sqlCompletion.context.spec.ts b/apps/desktop/src/lib/__tests__/sql/sqlCompletion.context.spec.ts index 27dcf6fcbe..e3daa75a0f 100644 --- a/apps/desktop/src/lib/__tests__/sql/sqlCompletion.context.spec.ts +++ b/apps/desktop/src/lib/__tests__/sql/sqlCompletion.context.spec.ts @@ -1815,3 +1815,32 @@ describe("line block statement boundary", () => { expect(context.referencedTables.map((table) => table.name)).toEqual(expect.arrayContaining(["users"])); }); }); + +describe("select-list function argument completion", () => { + const options = { + databaseType: "mysql" as const, + tables: [{ name: "daily_statistic", type: "table" as const }], + columnsByTable: new Map([["daily_statistic", ["CNKI_CITATIONS", "CNKI_DOWNLOADS", "stat_date"].map((name) => ({ name, table: "daily_statistic" }))]]), + }; + + const aggregateTemplates = ["SELECT\n SUM(CNKI_CITATIONS) AS cnkiCitations,\n SUM(CNKI_DOW|) AS cnkiDownloads\nFROM\n daily_statistic", "SELECT stat_date, CNKI_CITATIONS, COUNT(CNKI_DOW|) FROM daily_statistic", "SELECT stat_date, CNKI_CITATIONS, ROUND(AVG(CNKI_DOW|), 2) FROM daily_statistic"]; + + it.each(aggregateTemplates)("suggests columns inside an aggregate after earlier projections: %s", (template) => { + const cursor = template.indexOf("|"); + const sql = template.replace("|", ""); + const items = buildSqlCompletionItems(sql, cursor, options); + + expect(items.filter((item) => item.type === "column").map((item) => item.label)).toContain("CNKI_DOWNLOADS"); + }); + + it("does not offer select-list-only items inside an aggregate", () => { + const sql = "SELECT stat_date, CNKI_CITATIONS, SUM() FROM daily_statistic"; + const cursor = sql.indexOf("SUM(") + "SUM(".length; + const items = buildSqlCompletionItems(sql, cursor, options); + + expect(items.filter((item) => item.type === "column").map((item) => item.label)).toContain("CNKI_DOWNLOADS"); + expect(items.some((item) => item.type === "snippet" && item.label === "daily_statistic.*")).toBe(false); + expect(items.filter((item) => item.type === "column").every((item) => item.batchSelectionMode === undefined)).toBe(true); + expect(getSqlCompletionContext(sql, cursor, options).selectListColumnContext).toBe(false); + }); +}); diff --git a/apps/desktop/src/lib/sql/sqlCompletion.ts b/apps/desktop/src/lib/sql/sqlCompletion.ts index 6c95baf7d9..5f050c931d 100644 --- a/apps/desktop/src/lib/sql/sqlCompletion.ts +++ b/apps/desktop/src/lib/sql/sqlCompletion.ts @@ -2558,8 +2558,9 @@ export function getSqlCompletionContext(sql: string, cursor: number, options: Sq } // Check if we're in a context where columns are expected - const selectListColumnContext = isInSelectListContext(beforeCursor); - const inColumnContext = selectListColumnContext || isInColumnContext(beforeCursor, options.databaseType) || !!insertInfo; + const selectListScan = scanSelectListContext(beforeCursor); + const selectListColumnContext = selectListScan.inSelectList; + const inColumnContext = selectListColumnContext || selectListScan.inSelectListParens || isInColumnContext(beforeCursor, options.databaseType) || !!insertInfo; const inJoinConditionContext = isInJoinConditionContext(beforeCursor); const prioritizeSelectAliases = isInOrderOrGroupByContext(beforeCursor, options.databaseType); const inCallRoutineContext = isCallRoutineContext(beforeCursor); @@ -2884,11 +2885,21 @@ function isInColumnContext(beforeCursor: string, databaseType?: DatabaseType): b } function isInSelectListContext(beforeCursor: string): boolean { + return scanSelectListContext(beforeCursor).inSelectList; +} + +/** + * `inSelectList` is true when the cursor sits directly in an open SELECT list. + * `inSelectListParens` is true when it sits inside parentheses nested in one, + * such as a function argument: `SELECT a, SUM(|`. + */ +function scanSelectListContext(beforeCursor: string): { inSelectList: boolean; inSelectListParens: boolean } { let depth = 0; let inSingleQuote = false; let inDoubleQuote = false; let inBacktick = false; const selectOpenByDepth = new Map(); + const nestedInSelectByDepth = new Map(); for (let i = 0; i < beforeCursor.length; i++) { const ch = beforeCursor[i] ?? ""; @@ -2930,11 +2941,14 @@ function isInSelectListContext(beforeCursor: string): boolean { continue; } if (ch === "(") { + const inheritsSelectList = selectOpenByDepth.get(depth) === true || nestedInSelectByDepth.get(depth) === true; depth++; + nestedInSelectByDepth.set(depth, inheritsSelectList); continue; } if (ch === ")") { selectOpenByDepth.delete(depth); + nestedInSelectByDepth.delete(depth); depth = Math.max(0, depth - 1); continue; } @@ -2945,13 +2959,15 @@ function isInSelectListContext(beforeCursor: string): boolean { const word = beforeCursor.slice(i, end).toLowerCase(); if (word === "select") { selectOpenByDepth.set(depth, true); + nestedInSelectByDepth.delete(depth); } else if (word === "from") { selectOpenByDepth.set(depth, false); + nestedInSelectByDepth.delete(depth); } i = end - 1; } - return selectOpenByDepth.get(depth) === true; + return { inSelectList: selectOpenByDepth.get(depth) === true, inSelectListParens: nestedInSelectByDepth.get(depth) === true }; } function isInJoinConditionContext(beforeCursor: string): boolean {