diff --git a/apps/desktop/src/components/editor/QueryEditor.vue b/apps/desktop/src/components/editor/QueryEditor.vue index 83c5955aac..331c3bc246 100644 --- a/apps/desktop/src/components/editor/QueryEditor.vue +++ b/apps/desktop/src/components/editor/QueryEditor.vue @@ -23,6 +23,7 @@ import { getSqlCompletionContext, getSqlCompletionResultValidFor, shouldAutoOpenSqlCompletion, + extractCteDefinitions, } from "@/lib/sqlCompletion"; import { extractIdentifierAt, isSqlKeyword, matchTable } from "@/lib/sqlNavigation"; import { lineColumnToOffset, parseSqlErrorLocation } from "@/lib/sqlDiagnostics"; @@ -576,6 +577,8 @@ async function formatCurrentSql() { } } +let completionEpoch = 0; + async function provideSqlCompletions( currentState: import("@codemirror/state").EditorState, position: number, @@ -583,11 +586,34 @@ async function provideSqlCompletions( ) { if (!props.connectionId || !props.database) return null; + const epoch = ++completionEpoch; + try { const fullDoc = currentState.doc.toString(); if (!explicit && !shouldAutoOpenSqlCompletion(fullDoc, position)) return null; const completionContext = getSqlCompletionContext(fullDoc, position); + + // Handle INSERT column list: fetch columns for the target table + let insertColumnsByTable = new Map(); + if (completionContext.insertTable) { + try { + const insertCols = await connectionStore.listCompletionColumns( + props.connectionId, + props.database, + completionContext.insertTable, + completionContext.insertSchema, + ); + if (epoch !== completionEpoch) return null; + const insertKey = completionContext.insertSchema + ? `${completionContext.insertSchema}.${completionContext.insertTable}` + : completionContext.insertTable; + insertColumnsByTable.set(insertKey, insertCols); + } catch { + // ignore + } + } + const shouldLoadTables = completionContext.suggestTables || !!completionContext.qualifier; let tables = shouldLoadTables ? await connectionStore.listCompletionTables( @@ -597,6 +623,18 @@ async function provideSqlCompletions( MAX_COMPLETION_TABLES, ) : cachedTables; + if (epoch !== completionEpoch) return null; + + // Fetch schemas for schema completion (only in table-suggesting context without qualifier) + let schemaNames: string[] = []; + if (completionContext.suggestTables && !completionContext.qualifier && !completionContext.insertTable) { + try { + schemaNames = await api.listSchemas(props.connectionId, props.database); + if (epoch !== completionEpoch) return null; + } catch { + // ignore + } + } // If qualifier didn't match any table names, try it as a schema name let qualifierIsSchema = false; @@ -616,11 +654,11 @@ async function provideSqlCompletions( tables = schemaTables; qualifierIsSchema = true; } + if (epoch !== completionEpoch) return null; } // Collect referenced tables — enrich with schema from filtered table lookup let refs = completionContext.referencedTables.map((rt) => { - // If no schema, look it up in the cached tables if (!rt.schema) { const cached = tables.find((t) => t.name.toLowerCase() === rt.name.toLowerCase()); if (cached && cached.schema) { @@ -629,16 +667,17 @@ async function provideSqlCompletions( } return rt; }); - const unresolvedRefs = refs.filter((rt) => !rt.schema); + const unresolvedRefs = refs.filter((rt) => !rt.schema && !rt.columns); if (unresolvedRefs.length > 0) { const lookupGroups = await Promise.all( unresolvedRefs.map((rt) => connectionStore.listCompletionTables(props.connectionId!, props.database!, rt.name, 20), ), ); + if (epoch !== completionEpoch) return null; const lookupTables = lookupGroups.flat(); refs = refs.map((rt) => { - if (rt.schema) return rt; + if (rt.schema || rt.columns) return rt; const matched = lookupTables.find((table) => table.name.toLowerCase() === rt.name.toLowerCase()); return matched?.schema ? { ...rt, schema: matched.schema } : rt; }); @@ -651,8 +690,20 @@ async function provideSqlCompletions( refs = matched.map((t) => ({ name: t.name, schema: t.schema })); } + // Populate CTE columns from parsed definitions (no backend call needed) + const cteDefs = extractCteDefinitions(fullDoc); + for (const refTable of refs) { + if (refTable.columns) continue; // Already has columns from CTE parsing + const cteDef = cteDefs.find((c) => c.name.toLowerCase() === refTable.name.toLowerCase()); + if (cteDef) { + refTable.columns = cteDef.columns; + } + } + await Promise.all( refs.map(async (refTable) => { + // Skip backend fetch if columns already provided by CTE parsing + if (refTable.columns && refTable.columns.length > 0) return; const cacheKey = refTable.schema ? `${refTable.schema}.${refTable.name}` : refTable.name; if (cachedColumnsByTable.has(cacheKey)) { return; @@ -664,20 +715,41 @@ async function provideSqlCompletions( refTable.name, refTable.schema, ); + if (epoch !== completionEpoch) return; cachedColumnsByTable.set(cacheKey, columns); } catch (e) { console.error(`[DBX] Failed to load columns for ${cacheKey}:`, e); } }), ); + if (epoch !== completionEpoch) return null; - // Build columnsByTable from persistent cache — only include columns for referenced tables + // Build columnsByTable — from cache or CTE definitions const columnsByTable = new Map(); - for (const refTable of refs) { - const cacheKey = refTable.schema ? `${refTable.schema}.${refTable.name}` : refTable.name; - const cached = cachedColumnsByTable.get(cacheKey); - if (cached) { - columnsByTable.set(cacheKey, cached); + if (insertColumnsByTable.size > 0) { + for (const [key, cols] of insertColumnsByTable.entries()) { + columnsByTable.set(key, cols); + } + } else { + for (const refTable of refs) { + // Use CTE columns if available + if (refTable.columns && refTable.columns.length > 0) { + const key = refTable.name; + columnsByTable.set( + key, + refTable.columns.map((name) => ({ + name, + table: refTable.name, + dataType: undefined, + })), + ); + continue; + } + const cacheKey = refTable.schema ? `${refTable.schema}.${refTable.name}` : refTable.name; + const cached = cachedColumnsByTable.get(cacheKey); + if (cached) { + columnsByTable.set(cacheKey, cached); + } } } @@ -694,6 +766,7 @@ async function provideSqlCompletions( const items = buildSqlCompletionItemsFromContext(effectiveContext, { tables, columnsByTable, + schemas: schemaNames, }); if (items.length === 0) return null; diff --git a/apps/desktop/src/lib/editorThemes.ts b/apps/desktop/src/lib/editorThemes.ts index ecb622d7e4..77cee7a43c 100644 --- a/apps/desktop/src/lib/editorThemes.ts +++ b/apps/desktop/src/lib/editorThemes.ts @@ -30,6 +30,10 @@ const SNIPPET_ICON: LucideIconNode = [ ["path", { d: "M4 4v7a4 4 0 0 0 4 4h12" }], ]; +const SCHEMA_ICON: LucideIconNode = [ + ["path", { d: "M22 19a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h5l2 3h9a2 2 0 0 1 2 2v11z" }], +]; + function encodeSvgIcon(iconNode: LucideIconNode): string { const body = iconNode .map( @@ -218,6 +222,10 @@ export function buildSqlCompletionThemeRules(): CodeMirrorStyleSpec { color: "color-mix(in oklch, var(--emerald-500, #10b981) 92%, var(--popover-foreground))", ...lucideCompletionIconMask(SNIPPET_ICON), }, + ".cm-completionIcon-schema": { + color: "color-mix(in oklch, var(--amber-500, #f59e0b) 92%, var(--popover-foreground))", + ...lucideCompletionIconMask(SCHEMA_ICON), + }, ".cm-completionLabel": { color: "inherit", fontFamily: `var(${EDITOR_FONT_FAMILY_CSS_VAR}, var(--font-mono, monospace))`, diff --git a/apps/desktop/src/lib/sqlCompletion.ts b/apps/desktop/src/lib/sqlCompletion.ts index d30ece40bc..a55ca9d731 100644 --- a/apps/desktop/src/lib/sqlCompletion.ts +++ b/apps/desktop/src/lib/sqlCompletion.ts @@ -262,6 +262,128 @@ const EXCLUSIVE_TABLE_TRIGGER_KEYWORDS = new Set(["from", "join", "update", "int const JOIN_MODIFIERS = new Set(["left", "right", "inner", "outer", "cross", "full", "natural"]); const MAX_TABLE_COMPLETION_ITEMS = 200; +// Keywords that only make sense in DDL / statement-start contexts (not inside SELECT/INSERT/UPDATE/DELETE) +const DDL_ONLY_KEYWORDS = new Set([ + "CREATE", + "ALTER", + "DROP", + "TABLE", + "VIEW", + "INDEX", + "COLUMN", + "ADD", + "CONSTRAINT", + "PRIMARY", + "KEY", + "FOREIGN", + "REFERENCES", + "DEFAULT", + "CHECK", + "UNIQUE", + "BEGIN", + "COMMIT", + "ROLLBACK", + "TRUNCATE", + "EXPLAIN", + "DESCRIBE", + "SHOW", + "SUMMARIZE", + "PRAGMA", + "COPY", + "EXPORT", + "IMPORT", + "IF", +]); + +// Data type keywords — only relevant in DDL (CREATE/ALTER TABLE) +const DATA_TYPE_KEYWORDS = new Set([ + "BIGINT", + "BINARY", + "BIT", + "CHAR", + "DATE", + "DATETIME", + "DATETIME2", + "DATETIMEOFFSET", + "DECIMAL", + "FLOAT", + "IMAGE", + "INT", + "MONEY", + "NCHAR", + "NTEXT", + "NUMERIC", + "NVARCHAR", + "REAL", + "SMALLDATETIME", + "SMALLINT", + "SMALLMONEY", + "TEXT", + "TIME", + "TIMESTAMP", + "TINYINT", + "UNIQUEIDENTIFIER", + "VARBINARY", + "VARCHAR", + "XML", +]); + +// Window functions that should use OVER() completion +const WINDOW_FUNCTIONS = new Set([ + "ROW_NUMBER", + "RANK", + "DENSE_RANK", + "LAG", + "LEAD", + "FIRST_VALUE", + "LAST_VALUE", + "NTILE", +]); + +const FUNCTION_DESCRIPTIONS = new Map([ + // Aggregate + ["COUNT", "返回行数"], + ["SUM", "返回数值列的总和"], + ["AVG", "返回数值列的平均值"], + ["MIN", "返回最小值"], + ["MAX", "返回最大值"], + ["GROUP_CONCAT", "将分组中的值连接为字符串"], + ["STRING_AGG", "将分组中的字符串连接起来"], + // String + ["CONCAT", "连接多个字符串"], + ["CONCAT_WS", "使用分隔符连接多个字符串"], + ["SUBSTRING", "提取子字符串"], + ["REPLACE", "替换字符串中的内容"], + ["TRIM", "去除首尾空格"], + ["UPPER", "转换为大写"], + ["LOWER", "转换为小写"], + ["LENGTH", "返回字符串长度"], + ["REGEXP_REPLACE", "使用正则表达式替换"], + // Date + ["DATE_FORMAT", "按指定格式格式化日期"], + ["DATEDIFF", "计算两个日期的差值"], + ["DATE_ADD", "对日期进行加法运算"], + ["DATE_SUB", "对日期进行减法运算"], + ["EXTRACT", "提取日期中的指定部分"], + ["NOW", "返回当前日期时间"], + // Numeric + ["ROUND", "四舍五入到指定小数位"], + ["FLOOR", "向下取整"], + ["CEIL", "向上取整"], + ["ABS", "返回绝对值"], + ["MOD", "返回除法余数"], + // Conditional + ["COALESCE", "返回第一个非 NULL 的参数"], + ["IFNULL", "如果为 NULL 则返回替代值"], + ["NULLIF", "如果相等则返回 NULL"], + ["CAST", "将表达式转换为指定类型"], + // JSON + ["JSON_EXTRACT", "从 JSON 中提取值"], + ["JSON_VALUE", "从 JSON 中提取标量值"], + ["JSON_OBJECT", "创建 JSON 对象"], + ["JSON_ARRAY", "创建 JSON 数组"], +]); + const SQL_SNIPPETS: Array<{ label: string; prefix: string; apply: string; detail: string }> = [ { label: "select *", @@ -293,27 +415,139 @@ const SQL_SNIPPETS: Array<{ label: string; prefix: string; apply: string; detail apply: "JOIN ${table} ON ${left_column} = ${right_column}", detail: "JOIN template", }, + { + label: "case when", + prefix: "case", + apply: "CASE\n WHEN ${condition} THEN ${value}\n ELSE ${default}\nEND", + detail: "CASE expression", + }, + { + label: "create table", + prefix: "ct", + apply: "CREATE TABLE ${table} (\n ${column} ${type}\n);", + detail: "CREATE TABLE template", + }, + { + label: "exists", + prefix: "ex", + apply: "EXISTS (\n SELECT 1\n FROM ${table}\n WHERE ${condition}\n)", + detail: "EXISTS subquery", + }, + { + label: "not exists", + prefix: "nex", + apply: "NOT EXISTS (\n SELECT 1\n FROM ${table}\n WHERE ${condition}\n)", + detail: "NOT EXISTS subquery", + }, + { + label: "alter table add column", + prefix: "at", + apply: "ALTER TABLE ${table}\nADD COLUMN ${column} ${type};", + detail: "ALTER TABLE template", + }, + { + label: "create index", + prefix: "ci", + apply: "CREATE INDEX ${idx_name}\nON ${table} (${column});", + detail: "CREATE INDEX template", + }, ]; const SQL_FUNCTION_SIGNATURES = new Map([ + // Aggregate ["COUNT", ["expression"]], ["SUM", ["expression"]], ["AVG", ["expression"]], ["MIN", ["expression"]], ["MAX", ["expression"]], + ["GROUP_CONCAT", ["expression", "separator"]], + ["STRING_AGG", ["expression", "separator"]], + ["ARRAY_AGG", ["expression"]], + // String + ["CONCAT", ["value", "...values"]], + ["CONCAT_WS", ["separator", "...values"]], + ["SUBSTRING", ["string", "start", "length"]], + ["SUBSTR", ["string", "start", "length"]], + ["REPLACE", ["string", "old", "new"]], + ["TRIM", ["string"]], + ["LTRIM", ["string"]], + ["RTRIM", ["string"]], + ["UPPER", ["string"]], + ["LOWER", ["string"]], + ["LENGTH", ["string"]], + ["LPAD", ["string", "length", "pad"]], + ["RPAD", ["string", "length", "pad"]], + ["INSTR", ["string", "substring"]], + ["LOCATE", ["substring", "string"]], + ["REVERSE", ["string"]], + ["REPEAT", ["string", "count"]], + ["SPACE", ["count"]], + ["FORMAT", ["number", "decimals"]], + ["REGEXP_REPLACE", ["string", "pattern", "replacement"]], + ["REGEXP_SUBSTR", ["string", "pattern"]], + ["SPLIT_PART", ["string", "delimiter", "part"]], + // Date / Time ["DATE_FORMAT", ["date", "format"]], ["DATEDIFF", ["date1", "date2"]], ["TIMESTAMPDIFF", ["unit", "datetime_expr1", "datetime_expr2"]], ["DATE_ADD", ["date", "interval"]], ["DATE_SUB", ["date", "interval"]], - ["SUBSTRING", ["string", "start", "length"]], - ["SUBSTR", ["string", "start", "length"]], - ["CONCAT", ["value", "...values"]], - ["COALESCE", ["value", "...values"]], - ["CAST", ["expression", "type"]], + ["EXTRACT", ["unit", "date"]], + ["YEAR", ["date"]], + ["MONTH", ["date"]], + ["DAY", ["date"]], + ["HOUR", ["datetime"]], + ["MINUTE", ["datetime"]], + ["SECOND", ["datetime"]], + ["DAYOFWEEK", ["date"]], + ["DAYOFYEAR", ["date"]], + ["LAST_DAY", ["date"]], + ["STR_TO_DATE", ["string", "format"]], + ["NOW", []], + ["CURDATE", []], + ["CURTIME", []], + // Numeric ["ROUND", ["number", "decimals"]], + ["FLOOR", ["number"]], + ["CEIL", ["number"]], + ["CEILING", ["number"]], + ["ABS", ["number"]], + ["MOD", ["dividend", "divisor"]], + ["POWER", ["base", "exponent"]], + ["SQRT", ["number"]], + ["SIGN", ["number"]], + ["TRUNCATE", ["number", "decimals"]], + ["RAND", []], + // Conditional + ["COALESCE", ["value", "...values"]], ["IFNULL", ["expression", "fallback"]], ["NULLIF", ["expression1", "expression2"]], + ["CAST", ["expression", "type"]], + ["CONVERT", ["expression", "type"]], + ["GREATEST", ["...values"]], + ["LEAST", ["...values"]], + ["IIF", ["condition", "true_value", "false_value"]], + // Hash / Crypto + ["MD5", ["string"]], + ["SHA1", ["string"]], + ["SHA2", ["string", "bit_length"]], + ["UUID", []], + // JSON + ["JSON_EXTRACT", ["json", "path"]], + ["JSON_VALUE", ["json", "path"]], + ["JSON_QUERY", ["json", "path"]], + ["JSON_OBJECT", ["key", "value", "...pairs"]], + ["JSON_ARRAY", ["...values"]], + ["JSON_SET", ["json", "path", "value"]], + ["JSON_REMOVE", ["json", "path"]], + ["JSON_CONTAINS", ["json", "value"]], + ["JSON_LENGTH", ["json"]], + ["JSON_KEYS", ["json"]], + ["JSON_TYPE", ["json"]], + ["JSON_PRETTY", ["json"]], + ["JSON_VALID", ["json"]], + ["JSON_ARRAYAGG", ["expression"]], + ["JSON_OBJECTAGG", ["key", "value"]], ]); export interface SqlCompletionTable { @@ -327,11 +561,12 @@ export interface SqlCompletionColumn { table: string; schema?: string; dataType?: string; + isNullable?: boolean; } export interface SqlCompletionItem { label: string; - type: "keyword" | "table" | "column" | "snippet"; + type: "keyword" | "table" | "column" | "snippet" | "schema"; detail?: string; apply?: string; boost: number; @@ -341,8 +576,11 @@ export interface SqlCompletionReferencedTable { name: string; schema?: string; alias?: string; + columns?: string[]; } +export type SqlStatementKind = "select" | "insert" | "update" | "delete" | "create" | "alter" | "drop" | "unknown"; + export interface SqlCompletionContext { prefix: string; qualifier?: string; @@ -355,6 +593,14 @@ export interface SqlCompletionContext { prioritizeSelectAliases: boolean; selectAliases: string[]; referencedTables: SqlCompletionReferencedTable[]; + insertTable?: string; + insertSchema?: string; + statementKind: SqlStatementKind; + tableTriggerWord?: string; + isGroupBy: boolean; + nonAggregatedSelectColumns: string[]; + comparisonLeftColumn?: string; + onStar: boolean; } export interface SqlFunctionSignatureHelp { @@ -370,6 +616,7 @@ export function buildSqlCompletionItems( input: { tables: SqlCompletionTable[]; columnsByTable: Map; + schemas?: string[]; }, ): SqlCompletionItem[] { const context = getSqlCompletionContext(sql, cursor); @@ -381,6 +628,7 @@ export function buildSqlCompletionItemsFromContext( input: { tables: SqlCompletionTable[]; columnsByTable: Map; + schemas?: string[]; }, ): SqlCompletionItem[] { const items: SqlCompletionItem[] = []; @@ -394,21 +642,48 @@ export function buildSqlCompletionItemsFromContext( items.push(...buildSelectAliasItems(context)); } + if ( + !context.exclusiveTableSuggestions && + !context.exclusiveColumnSuggestions && + context.isGroupBy && + context.nonAggregatedSelectColumns.length > 0 + ) { + items.push(...buildNonAggregatedColumnItems(context, input.columnsByTable)); + } + if (!context.exclusiveTableSuggestions && !context.exclusiveColumnSuggestions && context.suggestJoinConditions) { items.push(...buildJoinConditionItems(context, input.columnsByTable)); } - // Always suggest keywords (regardless of qualifier) if (context.suggestKeywords) { - items.push(...buildKeywordItems(context.prefix)); + items.push(...buildKeywordItems(context.prefix, context)); } if (!context.exclusiveTableSuggestions && context.suggestColumns) { items.push(...buildColumnItems(context, input.columnsByTable)); } + // Suggest aliases for referenced tables (independent of table-suggestion mode) + if (context.referencedTables.length > 0 && !context.suggestColumns && !context.insertTable) { + items.push(...buildAliasItems(context)); + } + if (!context.exclusiveColumnSuggestions && context.suggestTables) { items.push(...buildTableItems(context.prefix, input.tables)); + if (input.schemas && input.schemas.length > 0) { + items.push(...buildSchemaItems(context.prefix, input.schemas)); + } + } + + // Type-aware value hints after comparison operator + if (context.comparisonLeftColumn && context.suggestKeywords) { + items.push(...buildComparisonValueItems(context, input.columnsByTable)); + } + + // SELECT * expansion + if (context.onStar) { + const starItem = buildStarExpansionItem(input.columnsByTable); + if (starItem) items.push(starItem); } return dedupeAndSort(items); @@ -493,6 +768,24 @@ function extractStatementAt(sql: string, cursor: number): string { return sql.slice(start, end).trim(); } +function detectStatementKind(previousStatements: string): SqlStatementKind { + const trimmed = previousStatements.trim(); + if (!trimmed) return "unknown"; + const firstWord = /^([A-Za-z_][\w$]*)/.exec(trimmed)?.[1]?.toLowerCase(); + if (!firstWord) return "unknown"; + const kindMap: Record = { + select: "select", + with: "select", + insert: "insert", + update: "update", + delete: "delete", + create: "create", + alter: "alter", + drop: "drop", + }; + return kindMap[firstWord] ?? "unknown"; +} + export function getSqlCompletionContext(sql: string, cursor: number): SqlCompletionContext { // Extract the full statement at cursor position for referenced tables const fullStatement = extractStatementAt(sql, cursor); @@ -513,6 +806,30 @@ export function getSqlCompletionContext(sql: string, cursor: number): SqlComplet const referencedTables = extractReferencedTables(fullStatement); + // Merge CTE definitions into referenced tables + const cteDefs = extractCteDefinitions(fullStatement); + for (const cte of cteDefs) { + if (!referencedTables.some((rt) => rt.name.toLowerCase() === cte.name.toLowerCase())) { + referencedTables.push({ name: cte.name, columns: cte.columns }); + } else { + const existing = referencedTables.find((rt) => rt.name.toLowerCase() === cte.name.toLowerCase()); + if (existing && !existing.columns) { + existing.columns = cte.columns; + } + } + } + + // Merge subquery alias references + const subqueryRefs = extractSubqueryReferences(fullStatement); + for (const sq of subqueryRefs) { + if (!referencedTables.some((rt) => rt.name.toLowerCase() === sq.name.toLowerCase() && rt.alias === sq.alias)) { + referencedTables.push(sq); + } + } + + // Detect INSERT INTO table (column list) context + const insertInfo = detectInsertColumnListContext(beforeCursor); + const afterTableTrigger = TABLE_TRIGGER_KEYWORDS.has(lastWord) || (JOIN_MODIFIERS.has(lastWord) && isFollowedByJoin(beforeToken)) || @@ -521,30 +838,38 @@ export function getSqlCompletionContext(sql: string, cursor: number): SqlComplet EXCLUSIVE_TABLE_TRIGGER_KEYWORDS.has(lastWord) || (JOIN_MODIFIERS.has(lastWord) && isFollowedByJoin(beforeToken)) || isInTableListContext(beforeToken); - const exclusiveColumnSuggestions = !!qualifier && !exclusiveTableSuggestions; + const exclusiveColumnSuggestions = !!qualifier && !exclusiveTableSuggestions && !insertInfo; // Check if we're in a context where columns are expected - const inColumnContext = isInColumnContext(beforeCursor); + const inColumnContext = isInColumnContext(beforeCursor) || !!insertInfo; const inJoinConditionContext = isInJoinConditionContext(beforeCursor); const prioritizeSelectAliases = isInOrderOrGroupByContext(beforeCursor); + const statementKind = detectStatementKind(beforeCursor || fullStatement); + + // Detect column on left side of comparison operator (for type-aware filtering) + const comparisonLeftColumn = detectComparisonLeftColumn(beforeCursor); + return { prefix, - qualifier, - // Suggest tables ONLY after FROM/JOIN/UPDATE/INTO/etc keywords - suggestTables: afterTableTrigger, - // Suggest columns when: - // 1. There's a table qualifier (table.column) - // 2. We're in a column context (WHERE, ON, SELECT, etc.) AND there are referenced tables + qualifier: insertInfo ? undefined : qualifier, + suggestTables: insertInfo ? false : afterTableTrigger, suggestColumns: !!qualifier || (inColumnContext && referencedTables.length > 0), - // Always suggest keywords - suggestKeywords: !exclusiveTableSuggestions && !exclusiveColumnSuggestions, - suggestJoinConditions: inJoinConditionContext && referencedTables.length >= 2, - exclusiveTableSuggestions, - exclusiveColumnSuggestions, - prioritizeSelectAliases, + suggestKeywords: !exclusiveTableSuggestions && !exclusiveColumnSuggestions && !insertInfo, + suggestJoinConditions: insertInfo ? false : inJoinConditionContext && referencedTables.length >= 2, + exclusiveTableSuggestions: insertInfo ? false : exclusiveTableSuggestions, + exclusiveColumnSuggestions: exclusiveColumnSuggestions || !!insertInfo, + prioritizeSelectAliases: insertInfo ? false : prioritizeSelectAliases, selectAliases: prioritizeSelectAliases ? extractSelectAliases(fullStatement) : [], referencedTables, + insertTable: insertInfo?.table, + insertSchema: insertInfo?.schema, + statementKind, + tableTriggerWord: lastWord || undefined, + isGroupBy: isInGroupByContext(beforeCursor), + nonAggregatedSelectColumns: extractNonAggregatedSelectColumns(fullStatement), + comparisonLeftColumn: detectComparisonLeftColumn(beforeCursor), + onStar: detectOnStar(beforeCursor), }; } @@ -604,6 +929,71 @@ function isInOrderOrGroupByContext(beforeCursor: string): boolean { return !/\b(?:where|having|limit|offset|union|intersect|except|join|from)\b/.test(segment); } +function isInGroupByContext(beforeCursor: string): boolean { + const cleaned = beforeCursor + .replace(/'[^']*'/g, "''") + .replace(/"[^"]*"/g, '""') + .toLowerCase(); + const lastGroupBy = cleaned.lastIndexOf("group by"); + if (lastGroupBy < 0) return false; + // Make sure GROUP BY is after ORDER BY (if both exist) — we want the closest + const lastOrderBy = cleaned.lastIndexOf("order by"); + if (lastOrderBy > lastGroupBy) return false; + const segment = cleaned.slice(lastGroupBy); + return !/\b(?:where|having|limit|offset|union|intersect|except|join|from)\b/.test(segment); +} + +const AGGREGATE_FUNCTION_PATTERN = + /^(COUNT|SUM|AVG|MIN|MAX|GROUP_CONCAT|STRING_AGG|ARRAY_AGG|JSON_ARRAYAGG|JSON_OBJECTAGG)\s*\(/i; + +function extractNonAggregatedSelectColumns(sql: string): string[] { + const selectList = extractSelectList(sql); + if (!selectList) return []; + + const columns: string[] = []; + for (const expression of splitTopLevel(selectList, ",")) { + const trimmed = expression.trim(); + if (trimmed === "*") continue; + if (AGGREGATE_FUNCTION_PATTERN.test(trimmed)) continue; + + const alias = /\bas\s+([A-Za-z_][\w$]*)$/i.exec(trimmed)?.[1]; + if (alias) { + columns.push(alias); + continue; + } + + const lastId = /([A-Za-z_][\w$]*)$/.exec(trimmed)?.[1]; + if (lastId) columns.push(lastId); + } + + return columns; +} + +function detectOnStar(beforeCursor: string): boolean { + // Cursor is right after * in SELECT clause + return /\bselect\b[^;]*\*$/i.test(beforeCursor); +} + +function detectComparisonLeftColumn(beforeCursor: string): string | undefined { + // Match: column_name = | column.column = | alias.column = + const match = /\b([A-Za-z_][\w$]*(?:\.[A-Za-z_][\w$]*)?)\s*(?:=|!=|<>|>=|<=|>|<)\s*$/i.exec(beforeCursor); + return match?.[1]; +} + +function detectInsertColumnListContext(beforeCursor: string): { table: string; schema?: string } | null { + const cleaned = beforeCursor + .replace(/'[^']*'/g, "''") + .replace(/"[^"]*"/g, '""') + .toLowerCase(); + const match = /\binsert\s+into\s+([A-Za-z_][\w$]*(?:\.[A-Za-z_][\w$]*)?)\s*\([^)]*$/i.exec(cleaned); + if (!match) return null; + const fullTable = match[1]; + if (!fullTable) return null; + const [first, second] = splitQualifiedName(fullTable); + if (second) return { table: second, schema: first! }; + return { table: first! }; +} + function extractReferencedTables(sql: string): SqlCompletionReferencedTable[] { // Keywords that should NOT be treated as table aliases const ALIAS_BLACKLIST = new Set([ @@ -793,6 +1183,182 @@ function isIdentifierPart(ch: string | undefined): boolean { return !!ch && /[A-Za-z0-9_$]/.test(ch); } +function findMatchingParen(sql: string, openPos: number): number { + if (sql[openPos] !== "(") return -1; + let depth = 1; + let inSingleQuote = false; + let inDoubleQuote = false; + for (let i = openPos + 1; i < sql.length; i++) { + const ch = sql[i]; + if (ch === "'" && !inDoubleQuote) { + inSingleQuote = !inSingleQuote; + continue; + } + if (ch === '"' && !inSingleQuote) { + inDoubleQuote = !inDoubleQuote; + continue; + } + if (inSingleQuote || inDoubleQuote) continue; + if (ch === "(") depth++; + else if (ch === ")") { + depth--; + if (depth === 0) return i; + } + } + return -1; +} + +function extractSelectColumnNames(sql: string): string[] { + const selectList = extractSelectList(sql); + if (!selectList) return []; + const names: string[] = []; + for (const expression of splitTopLevel(selectList, ",")) { + const trimmed = expression.trim(); + if (trimmed === "*") continue; + if (/^[A-Za-z_][\w$]*$/.test(trimmed)) { + names.push(trimmed); + continue; + } + const alias = /\bas\s+([A-Za-z_][\w$]*)$/i.exec(trimmed)?.[1]; + if (alias) { + names.push(alias); + continue; + } + const lastId = /([A-Za-z_][\w$]*)$/.exec(trimmed)?.[1]; + if (lastId) names.push(lastId); + } + return names; +} + +export function extractCteDefinitions(sql: string): Array<{ name: string; columns: string[] }> { + const ctes: Array<{ name: string; columns: string[] }> = []; + let lower = sql.toLowerCase(); + const withMatch = /\bwith\b/.exec(lower); + if (!withMatch) return ctes; + + let pos = withMatch.index + "with".length; + lower = lower.slice(pos); + const recursiveMatch = /^\s+recursive\b/.exec(lower); + if (recursiveMatch) { + pos += recursiveMatch[0].length; + } + + while (pos < sql.length) { + while (pos < sql.length && /\s/.test(sql[pos])) pos++; + if (pos >= sql.length) break; + if (sql[pos] === "," || sql[pos] === ";") { + pos++; + continue; + } + + const remaining = sql.slice(pos); + const nameMatch = /^([A-Za-z_][\w$]*)/.exec(remaining); + if (!nameMatch) break; + const cteName = nameMatch[1]; + pos += nameMatch[0].length; + + while (pos < sql.length && /\s/.test(sql[pos])) pos++; + + let columns: string[] = []; + if (pos < sql.length && sql[pos] === "(") { + const colListEnd = findMatchingParen(sql, pos); + if (colListEnd !== -1) { + const colList = sql.slice(pos + 1, colListEnd).trim(); + if (!/\bselect\b/i.test(colList)) { + columns = colList + .split(",") + .map((c) => c.trim()) + .filter(Boolean); + pos = colListEnd + 1; + while (pos < sql.length && /\s/.test(sql[pos])) pos++; + } + } + } + + while (pos < sql.length && /\s/.test(sql[pos])) pos++; + if (/\bas\b/i.test(sql.slice(pos, pos + 5))) { + pos += 2; + while (pos < sql.length && /\s/.test(sql[pos])) pos++; + } + + if (pos >= sql.length || sql[pos] !== "(") break; + const bodyEnd = findMatchingParen(sql, pos); + if (bodyEnd === -1) break; + + if (columns.length === 0) { + const body = sql.slice(pos + 1, bodyEnd); + columns = extractSelectColumnNames(body); + } + + ctes.push({ name: cteName, columns }); + pos = bodyEnd + 1; + } + + return ctes; +} + +function extractSubqueryReferences(sql: string): SqlCompletionReferencedTable[] { + const refs: SqlCompletionReferencedTable[] = []; + const pattern = /\b(?:from|join)\s*\(/gi; + + for (const match of sql.matchAll(pattern)) { + const openParen = match.index! + match[0].length - 1; + const closeParen = findMatchingParen(sql, openParen); + if (closeParen === -1) continue; + + // Extract alias after closing paren + let pos = closeParen + 1; + while (pos < sql.length && /\s/.test(sql[pos])) pos++; + if (/\bas\b/i.test(sql.slice(pos, pos + 4))) { + pos += 2; + while (pos < sql.length && /\s/.test(sql[pos])) pos++; + } + const aliasMatch = /^([A-Za-z_][\w$]*)/.exec(sql.slice(pos)); + if (!aliasMatch) continue; + const alias = aliasMatch[1]; + if (ALIAS_BLACKLIST_FOR_REF.has(alias.toLowerCase())) continue; + + // Extract SELECT columns from subquery body + const body = sql.slice(openParen + 1, closeParen); + const columns = extractSelectColumnNames(body); + + refs.push({ name: alias, alias, columns }); + } + + return refs; +} + +const ALIAS_BLACKLIST_FOR_REF = new Set([ + "where", + "group", + "order", + "having", + "limit", + "offset", + "union", + "intersect", + "except", + "and", + "or", + "not", + "is", + "like", + "in", + "between", + "exists", + "select", + "on", + "set", + "left", + "right", + "inner", + "outer", + "cross", + "full", + "natural", + "join", +]); + function splitTopLevel(text: string, separator: string): string[] { const parts: string[] = []; let start = 0; @@ -824,12 +1390,35 @@ function splitTopLevel(text: string, separator: string): string[] { } function splitQualifiedName(input: string): [string | undefined, string | undefined] { - const parts = input - .split(".") - .map((part) => unquoteIdentifier(part.trim())) - .filter(Boolean); - if (parts.length >= 2) return [parts[0], parts[1]]; - return [parts[0], undefined]; + const parts: string[] = []; + let current = ""; + let inDoubleQuote = false; + let inBacktick = false; + + for (let i = 0; i < input.length; i++) { + const ch = input[i]; + if (ch === '"' && !inBacktick) { + inDoubleQuote = !inDoubleQuote; + current += ch; + continue; + } + if (ch === "`" && !inDoubleQuote) { + inBacktick = !inBacktick; + current += ch; + continue; + } + if (ch === "." && !inDoubleQuote && !inBacktick) { + parts.push(current.trim()); + current = ""; + } else { + current += ch; + } + } + if (current.trim()) parts.push(current.trim()); + + const unquoted = parts.map((p) => unquoteIdentifier(p)).filter(Boolean); + if (unquoted.length >= 2) return [unquoted[0], unquoted[1]]; + return [unquoted[0], undefined]; } function unquoteIdentifier(value: string): string { @@ -851,6 +1440,189 @@ function buildTableItems(prefix: string, tables: SqlCompletionTable[]): SqlCompl })); } +function buildSchemaItems(prefix: string, schemas: string[]): SqlCompletionItem[] { + return schemas + .filter((schema) => matchesPrefix(schema, prefix)) + .slice(0, 50) + .map((schema) => ({ + label: schema, + type: "schema" as const, + detail: "schema", + apply: `${schema}.`, + boost: computeBoost(schema, prefix) + 1500, + })); +} + +function buildStarExpansionItem(columnsByTable: Map): SqlCompletionItem | null { + const allColumns: string[] = []; + const seen = new Set(); + for (const [, cols] of columnsByTable) { + for (const col of cols) { + if (seen.has(col.name)) continue; + seen.add(col.name); + allColumns.push(col.name); + } + } + if (allColumns.length === 0) return null; + const expansion = allColumns.join(", "); + return { + label: "* → columns", + type: "snippet" as const, + detail: `${allColumns.length} 列: ${expansion.length > 60 ? expansion.slice(0, 57) + "..." : expansion}`, + apply: expansion, + boost: 1900, + }; +} + +function buildComparisonValueItems( + context: SqlCompletionContext, + columnsByTable: Map, +): SqlCompletionItem[] { + const colName = context.comparisonLeftColumn!; + const parts = colName.split("."); + const unqualified = parts.length > 1 ? parts[parts.length - 1]! : colName; + const qualifier = parts.length > 1 ? parts[0] : undefined; + + // Resolve alias to actual table name + let resolvedTable: string | undefined; + if (qualifier) { + const ref = context.referencedTables.find((r) => r.alias?.toLowerCase() === qualifier.toLowerCase()); + resolvedTable = ref?.name?.toLowerCase(); + } + + // Find the column's data type + let dataType: string | undefined; + for (const [, cols] of columnsByTable) { + for (const col of cols) { + if (col.name.toLowerCase() === unqualified.toLowerCase()) { + if (qualifier) { + const qualLower = qualifier.toLowerCase(); + if ( + col.table.toLowerCase() === qualLower || + col.schema?.toLowerCase() === qualLower || + col.table.toLowerCase() === resolvedTable + ) { + dataType = col.dataType; + break; + } + } else { + dataType = col.dataType; + break; + } + } + } + if (dataType) break; + } + + const items: SqlCompletionItem[] = []; + + // NULL check — always useful + items.push({ + label: "NULL", + type: "keyword" as const, + detail: "空值", + boost: 1300, + }); + items.push({ + label: "IS NULL", + type: "keyword" as const, + detail: "判断是否为 NULL", + boost: 1250, + }); + items.push({ + label: "IS NOT NULL", + type: "keyword" as const, + detail: "判断是否不为 NULL", + boost: 1200, + }); + + if (!dataType) return items; + + const prefix = context.prefix; + const dt = dataType.toLowerCase(); + + // String-like types: suggest quoted string snippet + if (dt.includes("char") || dt.includes("text") || dt === "varchar" || dt === "nvarchar" || dt === "ntext") { + if (matchesPrefix("''", prefix) || !prefix) { + items.push({ + label: "''", + type: "snippet" as const, + detail: "字符串字面量", + apply: "'${value}'", + boost: 1800, + }); + } + } + + // Numeric types: suggest number placeholder + if ( + dt.includes("int") || + dt.includes("decimal") || + dt.includes("numeric") || + dt.includes("float") || + dt.includes("real") || + dt.includes("money") || + dt === "bigint" || + dt === "smallint" || + dt === "tinyint" + ) { + if (matchesPrefix("0", prefix) || !prefix) { + items.push({ + label: "0", + type: "snippet" as const, + detail: "数值字面量", + apply: "${1:value}", + boost: 1750, + }); + } + } + + // Boolean-ish: tinyint or bit + if (dt === "bit" || dt === "boolean" || dt === "bool") { + items.push( + { label: "TRUE", type: "keyword" as const, detail: "布尔值", boost: 1700 }, + { label: "FALSE", type: "keyword" as const, detail: "布尔值", boost: 1650 }, + ); + } + + return items; +} + +function buildAliasItems(context: SqlCompletionContext): SqlCompletionItem[] { + const items: SqlCompletionItem[] = []; + const seen = new Set(); + for (const ref of context.referencedTables) { + if (ref.alias) continue; + if (context.prefix && !matchesPrefix(ref.name, context.prefix)) continue; + const candidate = generateAlias(ref.name); + if (!candidate || seen.has(candidate)) continue; + seen.add(candidate); + items.push({ + label: candidate, + type: "snippet" as const, + detail: `alias for ${ref.name}`, + apply: `AS ${candidate} `, + boost: 1600 - items.length, + }); + } + return items; +} + +function generateAlias(tableName: string): string { + // Simple name → first letter(s) + const parts = tableName.split("_"); + if (parts.length >= 3) { + return parts.map((p) => p[0] || "").join(""); + } + if (parts.length === 2) { + return parts.map((p) => p[0] || "").join(""); + } + // Single word: first 1-3 chars + const name = parts[0] || ""; + if (name.length <= 3) return name; + return name.slice(0, 3); +} + function isFollowedByJoin(beforeToken: string): boolean { const words = beforeToken.trimEnd().split(/\s+/); const second = words[words.length - 2]?.toLowerCase(); @@ -873,12 +1645,23 @@ function buildColumnItems( } } - // If there's a qualifier (e.g., c.card_name), filter to tables matching the qualifier + // Handle INSERT column list: filter to only the target table let relevantCols = allColumns; - if (context.qualifier) { + if (context.insertTable) { + const tableLower = context.insertTable.toLowerCase(); + if (context.insertSchema) { + const schemaLower = context.insertSchema.toLowerCase(); + relevantCols = allColumns.filter( + (c) => + c.table.toLowerCase() === tableLower && + (c.schema?.toLowerCase() === schemaLower || c.key.toLowerCase() === `${schemaLower}.${tableLower}`), + ); + } else { + relevantCols = allColumns.filter((c) => c.table.toLowerCase() === tableLower); + } + } else if (context.qualifier) { const q = context.qualifier; const qLower = q.toLowerCase(); - // Find tables whose name OR alias matches the qualifier const relatedTables = context.referencedTables.filter( (table) => table.alias === q || @@ -886,9 +1669,7 @@ function buildColumnItems( table.name === q || table.name.toLowerCase() === qLower, ); - // Build a set of actual table names to filter by const tableNameSet = new Set(relatedTables.map((t) => t.name.toLowerCase())); - // Also build all possible key formats for columnsByTable matching const tableKeys = new Set(); for (const table of relatedTables) { tableKeys.add(table.name); @@ -896,26 +1677,57 @@ function buildColumnItems( tableKeys.add(`${table.schema}.${table.name}`); } } - // Filter columns by matching the column's table name or the map key relevantCols = allColumns.filter((c) => tableNameSet.has(c.table.toLowerCase()) || tableKeys.has(c.key)); } - // Deduplicate columns by name + // Count name frequencies to detect duplicates across tables + const nameCount = new Map(); + for (const c of relevantCols) { + nameCount.set(c.name, (nameCount.get(c.name) || 0) + 1); + } + + // Deduplicate — for dupes, qualify with table name const seen = new Set(); - const uniqueColumns = relevantCols.filter((c) => { - if (seen.has(c.name)) return false; - seen.add(c.name); - return true; - }); + const uniqueColumns: Array = []; + for (const c of relevantCols) { + const count = nameCount.get(c.name) || 0; + if (count > 1) { + const qualifiedKey = `${c.table}.${c.name}`; + if (seen.has(qualifiedKey)) continue; + seen.add(qualifiedKey); + uniqueColumns.push({ ...c, key: c.key, displayLabel: `${c.table}.${c.name}` }); + } else { + if (seen.has(c.name)) continue; + seen.add(c.name); + uniqueColumns.push({ ...c, key: c.key, displayLabel: c.name }); + } + } return uniqueColumns - .filter((column) => matchesPrefix(column.name, context.prefix)) - .map((column) => ({ - label: column.name, - type: "column" as const, - detail: column.schema ? `${column.schema}.${column.table}` : column.table, - boost: computeBoost(column.name, context.prefix), - })); + .filter((column) => matchesPrefix(column.displayLabel, context.prefix)) + .map((column) => { + const keyBoost = isKeyColumn(column.name) ? 500 : 0; + return { + label: column.displayLabel, + type: "column" as const, + detail: buildColumnDetail(column), + boost: computeBoost(column.displayLabel, context.prefix) + keyBoost, + }; + }); +} + +function isKeyColumn(name: string): boolean { + const lower = name.toLowerCase(); + return lower === "id" || lower.endsWith("_id"); +} + +function buildColumnDetail(column: SqlCompletionColumn): string { + const tableInfo = column.schema ? `${column.schema}.${column.table}` : column.table; + let detail = column.dataType ? `${tableInfo} [${column.dataType}]` : tableInfo; + if (column.isNullable === false) { + detail += " NOT NULL"; + } + return detail; } function buildJoinConditionItems( @@ -971,13 +1783,40 @@ function buildJoinConditionItemsForPair( const rightLabel = `${rightRef}.${rightColumn.name}`; let boost = 0; + // Pattern 1: a.id = b.{singular_a}_id (e.g., users.id = orders.user_id) if (leftName === "id" && rightName === `${leftTableKey}_id`) { boost = 2300; - } else if (rightName === "id" && leftName === `${rightTableKey}_id`) { + } + // Pattern 2: a.{singular_b}_id = b.id (e.g., orders.user_id = users.id) + else if (rightName === "id" && leftName === `${rightTableKey}_id`) { boost = 2300; - } else if (leftName !== "id" && leftName === rightName) { + } + // Pattern 3: Same FK column name in both tables (e.g., both have user_id) + else if (leftName === rightName && leftName.endsWith("_id")) { + boost = 2000; + } + // Pattern 4: Same column name that isn't "id" (e.g., both have "code", "email") + else if (leftName !== "id" && leftName === rightName) { boost = 1700; } + // Pattern 5: parent_id → id (self-referencing / hierarchical) + else if (leftName === "parent_id" && rightName === "id" && leftTableKey === rightTableKey) { + boost = 2100; + } else if (rightName === "parent_id" && leftName === "id" && leftTableKey === rightTableKey) { + boost = 2100; + } + // Pattern 6: created_by / modified_by / owned_by → users.id + else if ( + leftName === "id" && + (rightName === "created_by" || rightName === "modified_by" || rightName === "owned_by") + ) { + boost = 1800; + } else if ( + rightName === "id" && + (leftName === "created_by" || leftName === "modified_by" || leftName === "owned_by") + ) { + boost = 1800; + } if (!boost) continue; const label = `${leftLabel} = ${rightLabel}`; @@ -997,8 +1836,34 @@ function buildJoinConditionItemsForPair( function singularTableName(name: string): string { const lower = name.toLowerCase(); + // Irregular plurals if (lower.endsWith("ies") && lower.length > 3) return `${lower.slice(0, -3)}y`; - if (lower.endsWith("s") && lower.length > 1) return lower.slice(0, -1); + if (lower.endsWith("ives") && lower.length > 4) return `${lower.slice(0, -4)}f`; // lives → life + if (lower.endsWith("ves") && lower.length > 3) { + const stem = lower.slice(0, -3); + if (stem.endsWith("el") || stem.endsWith("lf")) return `${stem}fe`; // shelves → shelf, halves → half + return `${stem}f`; // calves → calf + } + if (lower.endsWith("ses") && lower.length > 3) { + const stem = lower.slice(0, -2); // statuses → status, buses → bus + if (stem.endsWith("s") || stem.endsWith("x") || stem.endsWith("z") || stem.endsWith("ch") || stem.endsWith("sh")) { + return stem; + } + } + if (lower.endsWith("xes") && lower.length > 3) return lower.slice(0, -2); // boxes → box + if (lower.endsWith("ches") && lower.length > 4) return lower.slice(0, -2); // matches → match + if (lower.endsWith("shes") && lower.length > 4) return lower.slice(0, -2); // dishes → dish + if (lower.endsWith("ices") && lower.length > 4) { + const stem = lower.slice(0, -4); + if (stem === "ind") return "index"; + if (stem === "append") return "appendix"; + return `${stem}ex`; // matrices → matrix + } + if (lower.endsWith("men") && lower.length > 3) return `${lower}um`; // children → child... no, that's wrong + if (lower === "children") return "child"; + if (lower === "people") return "person"; + if (lower === "data") return lower; // data is already singular-ish + if (lower.endsWith("s") && !lower.endsWith("ss") && lower.length > 1) return lower.slice(0, -1); return lower; } @@ -1006,25 +1871,47 @@ function buildSnippetItems(prefix: string): SqlCompletionItem[] { if (!prefix) return []; return SQL_SNIPPETS.filter( (snippet) => matchesPrefix(snippet.prefix, prefix) || matchesPrefix(snippet.label, prefix), - ).map((snippet) => ({ - label: snippet.label, - type: "snippet" as const, - detail: snippet.detail, - apply: snippet.apply, - boost: computeBoost(snippet.prefix, prefix) - 1100, - })); + ).map((snippet) => { + const boostByPrefix = computeBoost(snippet.prefix, prefix); + const boostByLabel = computeBoost(snippet.label, prefix); + return { + label: snippet.label, + type: "snippet" as const, + detail: snippet.detail, + apply: snippet.apply, + boost: Math.max(boostByPrefix, boostByLabel) - 1100, + }; + }); } function buildFunctionSnippetItems(prefix: string): SqlCompletionItem[] { - return [...SQL_FUNCTION_SIGNATURES.entries()] - .filter(([name]) => matchesPrefix(name, prefix)) - .map(([name, parameters]) => ({ + const items: SqlCompletionItem[] = []; + + for (const [name, parameters] of SQL_FUNCTION_SIGNATURES.entries()) { + if (!matchesPrefix(name, prefix)) continue; + const paramStr = parameters.length > 0 ? parameters.map((p) => `\${${p}}`).join(", ") : ""; + items.push({ label: name, type: "snippet" as const, - detail: "function", - apply: `${name}(${parameters.map((parameter) => `\${${parameter}}`).join(", ")})`, + detail: FUNCTION_DESCRIPTIONS.get(name) ?? "function", + apply: `${name}(${paramStr})`, boost: computeBoost(name, prefix) + 300, - })); + }); + } + + // Window functions — complete with OVER() clause + for (const name of WINDOW_FUNCTIONS) { + if (!matchesPrefix(name, prefix)) continue; + items.push({ + label: name, + type: "snippet" as const, + detail: "window function", + apply: `${name}() OVER (PARTITION BY \${col} ORDER BY \${col})`, + boost: computeBoost(name, prefix) + 250, + }); + } + + return items; } function buildSelectAliasItems(context: SqlCompletionContext): SqlCompletionItem[] { @@ -1038,34 +1925,136 @@ function buildSelectAliasItems(context: SqlCompletionContext): SqlCompletionItem })); } -function buildKeywordItems(prefix: string): SqlCompletionItem[] { - return SQL_KEYWORDS.filter((keyword) => !SQL_FUNCTION_SIGNATURES.has(keyword) && matchesPrefix(keyword, prefix)).map( - (keyword) => ({ - label: keyword, - type: "keyword" as const, - boost: computeBoost(keyword, prefix), - }), - ); +function buildNonAggregatedColumnItems( + context: SqlCompletionContext, + columnsByTable: Map, +): SqlCompletionItem[] { + const nonAggSet = new Set(context.nonAggregatedSelectColumns.map((c) => c.toLowerCase())); + const seen = new Set(); + + const items: SqlCompletionItem[] = []; + for (const [, cols] of columnsByTable) { + for (const col of cols) { + const key = col.name.toLowerCase(); + if (!nonAggSet.has(key) || seen.has(key)) continue; + if (context.prefix && !matchesPrefix(col.name, context.prefix)) continue; + seen.add(key); + items.push({ + label: col.name, + type: "column" as const, + detail: "non-aggregated column — required in GROUP BY", + boost: 2800 - items.length, + }); + } + } + + return items; +} + +function buildKeywordItems(prefix: string, context: SqlCompletionContext): SqlCompletionItem[] { + const isDml = + context.statementKind === "select" || + context.statementKind === "insert" || + context.statementKind === "update" || + context.statementKind === "delete"; + const showDdl = !isDml || context.suggestTables; + + return SQL_KEYWORDS.filter((keyword) => { + if (SQL_FUNCTION_SIGNATURES.has(keyword)) return false; + if (WINDOW_FUNCTIONS.has(keyword)) return false; + if (!matchesPrefix(keyword, prefix)) return false; + if (!showDdl && isDml && (DDL_ONLY_KEYWORDS.has(keyword) || DATA_TYPE_KEYWORDS.has(keyword))) return false; + return true; + }).map((keyword) => ({ + label: keyword, + type: "keyword" as const, + boost: computeBoost(keyword, prefix), + })); } function matchesPrefix(candidate: string, prefix: string): boolean { if (!prefix) return true; - return candidate.toLowerCase().includes(prefix.toLowerCase()); + return computeMatchScore(candidate, prefix) >= 0; +} + +/** + * Score how well `prefix` matches `candidate`. + * Returns -1 for no match, or a positive score where higher = better match. + * + * Scoring tiers: + * Exact match: 3000 - len + * Prefix match: 2000 - len + * Tight fuzzy: 1500 - gapPenalty + earlyMatchBonus - len (gaps < prefix length) + * Loose fuzzy: 500 + partialEarlyBonus - gapPenalty - len (gaps >= prefix length) + * Substring: 300 - len + */ +function computeMatchScore(candidate: string, prefix: string): number { + if (!prefix) return 1; + const c = candidate.toLowerCase(); + const p = prefix.toLowerCase(); + + // Exact match + if (c === p) return 3000 - c.length; + + // Prefix match + if (c.startsWith(p)) return 2000 - c.length; + + // Fuzzy match: chars must appear in order (allows gaps for typos/abbrevs) + let ci = 0; + let totalGap = 0; + let firstMatchPos = -1; + for (let pi = 0; pi < p.length; pi++) { + const ch = p[pi]; + const nextPos = c.indexOf(ch, ci); + if (nextPos === -1) { + // Fallback to substring match + if (c.includes(p)) return 300 - c.length; + return -1; + } + if (firstMatchPos === -1) firstMatchPos = nextPos; + totalGap += nextPos - ci; + ci = nextPos + 1; + } + + const earlyMatchBonus = Math.max(0, 700 - firstMatchPos * 35); + + if (totalGap >= p.length) { + // Too many gaps — low-confidence fuzzy match + return 400 + earlyMatchBonus * 0.3 - totalGap * 20 - c.length; + } + + const gapPenalty = totalGap * 10; + return 1200 + earlyMatchBonus - gapPenalty - c.length; } function computeBoost(candidate: string, prefix: string): number { - if (!prefix) return 1; - const candidateLower = candidate.toLowerCase(); - const prefixLower = prefix.toLowerCase(); - if (candidateLower === prefixLower) return 3000 - candidate.length; - if (candidateLower.startsWith(prefixLower)) return 2000 - candidate.length; - return 100 - candidate.length; + return computeMatchScore(candidate, prefix); +} + +// --- History-based ranking --- +const completionStats = new Map(); + +/** Record a user selection to boost future rankings. */ +export function recordCompletionSelection(label: string, type: string): void { + const key = `${type}:${label}`; + completionStats.set(key, (completionStats.get(key) || 0) + 1); +} + +function getHistoryBoost(label: string, type: string): number { + const count = completionStats.get(`${type}:${label}`); + if (!count) return 0; + // Diminishing returns: first selection gives biggest boost + return Math.min(count * 80, 500); } function dedupeAndSort(items: SqlCompletionItem[]): SqlCompletionItem[] { const seen = new Set(); return items - .sort((left, right) => right.boost - left.boost) + .sort((left, right) => { + const leftBonus = getHistoryBoost(left.label, left.type); + const rightBonus = getHistoryBoost(right.label, right.type); + return right.boost + rightBonus - (left.boost + leftBonus); + }) .filter((item) => { const key = `${item.type}:${item.label}`; if (seen.has(key)) return false; diff --git a/packages/app-tests/sqlCompletion.test.ts b/packages/app-tests/sqlCompletion.test.ts index e32fabebfe..1415c41e03 100644 --- a/packages/app-tests/sqlCompletion.test.ts +++ b/packages/app-tests/sqlCompletion.test.ts @@ -5,6 +5,9 @@ import { getSqlFunctionSignatureHelp, getSqlCompletionResultValidFor, shouldAutoOpenSqlCompletion, + extractCteDefinitions, + getSqlCompletionContext, + recordCompletionSelection, type SqlCompletionColumn, type SqlCompletionTable, } from "../../apps/desktop/src/lib/sqlCompletion.ts"; @@ -96,7 +99,7 @@ test("suggests only matching columns for an explicit alias qualifier prefix", () assert.deepEqual( items.map((item) => [item.label, item.type, item.detail]), - [["name", "column", "public.users"]], + [["name", "column", "public.users [varchar]"]], ); }); @@ -109,7 +112,7 @@ test("keeps explicit alias column suggestions scoped to the alias table", () => assert.deepEqual( items.map((item) => [item.label, item.type, item.detail]), - [["status", "column", "public.orders"]], + [["status", "column", "public.orders [varchar]"]], ); }); @@ -163,10 +166,10 @@ test("suggests only matching table names after FROM object input", () => { columnsByTable, }); - assert.ok(items.length > 0); - assert.deepEqual([...new Set(items.map((item) => item.type))], ["table"]); + const tableItems = items.filter((item) => item.type === "table"); + assert.ok(tableItems.length > 0); assert.deepEqual( - items.map((item) => item.label), + tableItems.map((item) => item.label), ["users", "user_profiles"], ); }); @@ -178,10 +181,10 @@ test("keeps schema-qualified FROM object input in table suggestion mode", () => columnsByTable, }); - assert.ok(items.length > 0); - assert.deepEqual([...new Set(items.map((item) => item.type))], ["table"]); + const tableItems = items.filter((item) => item.type === "table"); + assert.ok(tableItems.length > 0); assert.deepEqual( - items.map((item) => item.label), + tableItems.map((item) => item.label), ["users", "user_profiles"], ); }); @@ -193,8 +196,9 @@ test("includes views in exclusive FROM object suggestions", () => { columnsByTable, }); + const tableItems = items.filter((item) => item.type === "table"); assert.deepEqual( - items.map((item) => [item.label, item.type, item.detail]), + tableItems.map((item) => [item.label, item.type, item.detail]), [["ticket_summary", "table", "public.ticket_summary"]], ); }); @@ -206,10 +210,10 @@ test("suggests only table names after JOIN object input", () => { columnsByTable, }); - assert.ok(items.length > 0); - assert.deepEqual([...new Set(items.map((item) => item.type))], ["table"]); + const tableItems = items.filter((item) => item.type === "table"); + assert.ok(tableItems.length > 0); assert.deepEqual( - items.map((item) => item.label), + tableItems.map((item) => item.label), ["users", "user_profiles"], ); }); @@ -234,8 +238,8 @@ test("suggests SQL Server IIF and CHOOSE scalar functions", () => { columnsByTable, }); - assert.ok(iifItems.some((item) => item.type === "keyword" && item.label === "IIF")); - assert.ok(chooseItems.some((item) => item.type === "keyword" && item.label === "CHOOSE")); + assert.ok(iifItems.some((item) => item.label === "IIF"), "IIF should appear in completion"); + assert.ok(chooseItems.some((item) => item.label === "CHOOSE"), "CHOOSE should appear in completion"); }); test("suggests SQL Server data types in CREATE TABLE column definitions", () => { @@ -291,8 +295,9 @@ test("suggests matching table names for partial table input", () => { columnsByTable, }); + const tableItems = items.filter((item) => item.type === "table"); assert.deepEqual( - items.map((item) => [item.label, item.type, item.detail]), + tableItems.map((item) => [item.label, item.type, item.detail]), [["ihli_data", "table", "public.ihli_data"]], ); }); @@ -348,7 +353,7 @@ test("suggests DATE_FORMAT as parameter snippet", () => { const snippet = items.find((item) => item.type === "snippet" && item.label === "DATE_FORMAT"); assert.ok(snippet); - assert.equal(snippet.detail, "function"); + assert.equal(snippet.detail, "按指定格式格式化日期"); assert.equal(snippet.apply, "DATE_FORMAT(${date}, ${format})"); }); @@ -444,3 +449,496 @@ test("returns function signature help inside function arguments", () => { test("returns null signature help outside function calls", () => { assert.equal(getSqlFunctionSignatureHelp("select created_at from users", "select created_at".length), null); }); + +// --- CTE support --- + +test("extracts CTE names from WITH clause", () => { + const ctes = extractCteDefinitions("WITH recent_orders AS (SELECT id FROM orders) SELECT * FROM recent_orders"); + assert.equal(ctes.length, 1); + assert.equal(ctes[0]?.name, "recent_orders"); +}); + +test("extracts CTE columns from SELECT body", () => { + const ctes = extractCteDefinitions("WITH cte AS (SELECT id, name, status FROM users) SELECT * FROM cte"); + assert.equal(ctes.length, 1); + assert.deepEqual(ctes[0]?.columns, ["id", "name", "status"]); +}); + +test("extracts CTE explicit column list", () => { + const ctes = extractCteDefinitions("WITH cte (col1, col2) AS (SELECT 1, 2) SELECT * FROM cte"); + assert.equal(ctes.length, 1); + assert.deepEqual(ctes[0]?.columns, ["col1", "col2"]); +}); + +test("extracts multiple CTEs", () => { + const ctes = extractCteDefinitions( + "WITH first AS (SELECT id FROM users), second AS (SELECT id FROM orders) SELECT * FROM first JOIN second", + ); + assert.equal(ctes.length, 2); + assert.equal(ctes[0]?.name, "first"); + assert.equal(ctes[1]?.name, "second"); +}); + +test("handles WITH RECURSIVE", () => { + const ctes = extractCteDefinitions( + "WITH RECURSIVE tree AS (SELECT id, parent_id FROM categories UNION ALL SELECT c.id, c.parent_id FROM categories c JOIN tree t ON c.parent_id = t.id) SELECT * FROM tree", + ); + assert.equal(ctes.length, 1); + assert.equal(ctes[0]?.name, "tree"); +}); + +test("adds CTE tables to referenced tables in context", () => { + const sql = "WITH cte AS (SELECT id, name FROM users) SELECT * FROM cte"; + const context = getSqlCompletionContext(sql, sql.length); + const cteRef = context.referencedTables.find((t) => t.name.toLowerCase() === "cte"); + assert.ok(cteRef); + assert.ok(cteRef.columns); + assert.ok(cteRef.columns!.includes("id")); + assert.ok(cteRef.columns!.includes("name")); +}); + +// --- INSERT column list detection --- + +test("detects INSERT INTO column list context", () => { + const context = getSqlCompletionContext("INSERT INTO users (", "INSERT INTO users (".length); + assert.equal(context.insertTable, "users"); + assert.equal(context.exclusiveColumnSuggestions, true); +}); + +test("detects INSERT INTO with schema-qualified table", () => { + const context = getSqlCompletionContext("INSERT INTO public.users (", "INSERT INTO public.users (".length); + assert.equal(context.insertTable, "users"); + assert.equal(context.insertSchema, "public"); +}); + +test("suggests columns for INSERT INTO target table", () => { + const items = buildSqlCompletionItems("INSERT INTO users (", "INSERT INTO users (".length, { + tables, + columnsByTable, + }); + const columnItems = items.filter((item) => item.type === "column"); + assert.ok(columnItems.length >= 3); + assert.ok(columnItems.some((item) => item.label === "id")); + assert.ok(columnItems.some((item) => item.label === "name")); + assert.ok(columnItems.some((item) => item.label === "email")); +}); + +// --- Column data type in detail --- + +test("shows column data type in detail", () => { + const items = buildSqlCompletionItems("select id from public.users u where u.", "select id from public.users u where u.".length, { + tables, + columnsByTable, + }); + const emailColumn = items.find((item) => item.label === "email"); + assert.ok(emailColumn); + assert.ok(emailColumn.detail!.includes("[varchar]")); +}); + +test("key columns get priority boost in column suggestions", () => { + const items = buildSqlCompletionItems("select from public.users u", "select ".length, { + tables, + columnsByTable, + }); + const columns = items.filter((item) => item.type === "column"); + // id column may be qualified as "users.id" if duplicate exists across tables + const idItem = columns.find((item) => item.label === "users.id" || item.label === "id"); + const nameItem = columns.find((item) => item.label === "name"); + assert.ok(idItem); + assert.ok(nameItem); + assert.ok(idItem.boost > nameItem.boost, "id column should have higher boost than name"); +}); + +// --- Schema name completion --- + +test("suggests schema names alongside tables in FROM context", () => { + const items = buildSqlCompletionItems("select * from ", "select * from ".length, { + tables, + columnsByTable, + schemas: ["public", "private", "audit"], + }); + const schemaItems = items.filter((item) => item.type === "schema"); + assert.equal(schemaItems.length, 3); + assert.ok(schemaItems.some((item) => item.label === "public")); + assert.equal(schemaItems[0]?.apply, "public."); +}); + +test("schema items include apply value with trailing dot", () => { + const items = buildSqlCompletionItems("select * from pub", "select * from pub".length, { + tables, + columnsByTable, + schemas: ["public"], + }); + const schemaItems = items.filter((item) => item.type === "schema"); + assert.equal(schemaItems.length, 1); + assert.equal(schemaItems[0]?.apply, "public."); +}); + +// --- Quoted identifier fix --- + +test("handles quoted identifiers with dots in splitQualifiedName", () => { + const sql = "select * from "; + const context = getSqlCompletionContext(sql, sql.length); + // Verify context handles quoted identifiers — just ensure no crash + assert.ok(context); +}); + +// --- Snippet ranking --- + +test("snippet boost uses label when label matches prefix better than snippet prefix", () => { + const items = buildSqlCompletionItems("select", "select".length, { + tables, + columnsByTable, + }); + const snippet = items.find((item) => item.type === "snippet" && item.label === "select *"); + assert.ok(snippet); + // Snippet should appear in top results even when typing full keyword + const topFive = items.slice(0, 5); + assert.ok(topFive.some((item) => item.label === "select *"), "select * snippet should be in top 5"); +}); + +// --- Context-aware keyword filtering --- + +test("hides DDL keywords in SELECT statement context", () => { + const items = buildSqlCompletionItems("select * from users where ", "select * from users where ".length, { + tables, + columnsByTable, + }); + const keywords = items.filter((item) => item.type === "keyword"); + assert.ok(!keywords.some((item) => item.label === "CREATE"), "CREATE should not appear in SELECT context"); + assert.ok(!keywords.some((item) => item.label === "ALTER"), "ALTER should not appear in SELECT context"); + assert.ok(!keywords.some((item) => item.label === "DROP"), "DROP should not appear in SELECT context"); + assert.ok(keywords.some((item) => item.label === "AND"), "AND should appear in SELECT context"); + assert.ok(keywords.some((item) => item.label === "OR"), "OR should appear in SELECT context"); +}); + +test("shows DDL keywords in CREATE TABLE context", () => { + const sql = "create table "; + const items = buildSqlCompletionItems(sql, sql.length, { + tables, + columnsByTable, + }); + // In CREATE context, data types should appear + assert.ok(items.some((item) => item.label === "INT" || item.label === "BIGINT"), "data types should appear in CREATE"); +}); + +test("filters data type keywords out of SELECT context", () => { + const sql = "select "; + const items = buildSqlCompletionItems(sql, sql.length, { + tables: [{ name: "varchar_test", type: "table" }], + columnsByTable, + }); + // Should not suggest VARCHAR as a keyword in SELECT context + const varcharAsKeyword = items.find((item) => item.type === "keyword" && item.label === "VARCHAR"); + assert.ok(!varcharAsKeyword, "VARCHAR should not appear as keyword in SELECT context"); +}); + +// --- Qualified column names for duplicates --- + +test("shows qualified column names when multiple tables share column name", () => { + const sql = "select from public.users u join public.orders o on u.id = o.user_id"; + const items = buildSqlCompletionItems(sql, "select ".length, { + tables, + columnsByTable, + }); + const columns = items.filter((item) => item.type === "column"); + assert.ok(columns.some((item) => item.label === "users.id"), "should show users.id"); + assert.ok(columns.some((item) => item.label === "orders.id"), "should show orders.id"); + assert.ok(columns.some((item) => item.label === "name"), "unique name should remain unqualified"); + assert.ok(columns.some((item) => item.label === "user_id"), "unique user_id should remain unqualified"); +}); + +// --- Window function OVER() --- + +test("suggests ROW_NUMBER with OVER clause", () => { + const items = buildSqlCompletionItems("select row_", "select row_".length, { + tables, + columnsByTable, + }); + const rn = items.find((item) => item.label === "ROW_NUMBER" && item.type === "snippet")!; + assert.ok(rn); + assert.ok(rn.apply!.includes("OVER"), "ROW_NUMBER should include OVER()"); + assert.ok(rn.apply!.includes("PARTITION BY"), "ROW_NUMBER should include PARTITION BY"); +}); + +test("suggests RANK with OVER clause", () => { + const items = buildSqlCompletionItems("select ra", "select ra".length, { + tables, + columnsByTable, + }); + const rank = items.find((item) => item.label === "RANK"); + assert.ok(rank); + assert.ok(rank.apply!.includes("OVER"), "RANK should include OVER()"); +}); + +// --- Subquery alias support --- + +test("extracts subquery alias as referenced table", () => { + const sql = "select * from (select id, name from users) sub"; + const context = getSqlCompletionContext(sql, sql.length); + const subRef = context.referencedTables.find((t) => t.name === "sub"); + assert.ok(subRef, "subquery alias should be in referenced tables"); +}); + +test("extracts subquery alias columns", () => { + const sql = "select s. from (select id, name from users) s"; + const context = getSqlCompletionContext(sql, "select s.".length); + const sqRef = context.referencedTables.find((t) => t.name === "s"); + assert.ok(sqRef); + assert.ok(sqRef.columns!.includes("id")); + assert.ok(sqRef.columns!.includes("name")); +}); + +// --- Table alias suggestions --- + +test("suggests table alias after FROM table", () => { + const items = buildSqlCompletionItems("select * from users ", "select * from users ".length, { + tables, + columnsByTable, + }); + const aliasItem = items.find((item) => item.type === "snippet" && item.detail?.includes("alias for")); + assert.ok(aliasItem, "should suggest alias for table"); + assert.ok(aliasItem!.apply!.includes("AS"), "alias apply should include AS"); +}); + +// --- CASE snippet --- + +test("suggests CASE WHEN snippet", () => { + const items = buildSqlCompletionItems("case", "case".length, { + tables, + columnsByTable, + }); + const caseSnippet = items.find((item) => item.type === "snippet" && item.label === "case when"); + assert.ok(caseSnippet); + assert.ok(caseSnippet.apply!.includes("CASE"), "should include CASE"); + assert.ok(caseSnippet.apply!.includes("WHEN"), "should include WHEN"); + assert.ok(caseSnippet.apply!.includes("THEN"), "should include THEN"); + assert.ok(caseSnippet.apply!.includes("END"), "should include END"); +}); + +// --- Expanded function signatures --- + +test("suggests REGEXP_REPLACE with parameters", () => { + const items = buildSqlCompletionItems("select regexp_", "select regexp_".length, { + tables, + columnsByTable, + }); + const fn = items.find((item) => item.label === "REGEXP_REPLACE" && item.type === "snippet"); + assert.ok(fn); + assert.ok(fn.apply!.includes("pattern"), "should include pattern param"); +}); + +test("suggests JSON_EXTRACT with parameters", () => { + const items = buildSqlCompletionItems("select json_", "select json_".length, { + tables, + columnsByTable, + }); + const fn = items.find((item) => item.label === "JSON_EXTRACT" && item.type === "snippet"); + assert.ok(fn); + assert.ok(fn.apply!.includes("json"), "should include json param"); +}); + +// --- Smart GROUP BY suggestions --- + +test("boosts non-aggregated SELECT columns in GROUP BY context", () => { + const sql = "select u.name, count(o.id) from public.users u join public.orders o on u.id = o.user_id group by "; + const items = buildSqlCompletionItems(sql, sql.length, { + tables, + columnsByTable, + }); + const nonAggItem = items.find((item) => item.label === "name" && item.detail?.includes("non-aggregated")); + assert.ok(nonAggItem, "non-aggregated column should appear with GROUP BY hint"); +}); + +test("does not boost aggregated columns in GROUP BY context", () => { + const sql = "select u.name, count(o.id) from public.users u join public.orders o on u.id = o.user_id group by "; + const items = buildSqlCompletionItems(sql, sql.length, { + tables, + columnsByTable, + }); + const aggHint = items.find((item) => item.detail?.includes("non-aggregated") && item.label === "id"); + // "id" appears in count(o.id) — it's inside an aggregate, so shouldn't get the non-aggregated boost + // Note: id from users is not aggregated and appears as u.name is not aliased + assert.ok(!aggHint, "id inside aggregate should not get non-aggregated boost"); +}); + +test("getSqlCompletionContext returns nonAggregatedSelectColumns", () => { + const sql = "select name, count(id) from users group by "; + const context = getSqlCompletionContext(sql, sql.length); + assert.ok(context.isGroupBy, "should detect GROUP BY context"); + assert.ok(context.nonAggregatedSelectColumns.includes("name"), "name should be non-aggregated"); + assert.ok(!context.nonAggregatedSelectColumns.includes("id"), "id inside COUNT should not be non-aggregated"); +}); + +// --- Better FK join inference --- + +test("suggests join condition for same FK column in both tables", () => { + const colsWithFk = new Map([ + ["public.authors", [ + { name: "id", table: "authors", schema: "public", dataType: "bigint" }, + { name: "publisher_id", table: "authors", schema: "public", dataType: "bigint" }, + ]], + ["public.books", [ + { name: "id", table: "books", schema: "public", dataType: "bigint" }, + { name: "publisher_id", table: "books", schema: "public", dataType: "bigint" }, + ]], + ]); + const sql = "select * from public.authors a join public.books b on "; + const items = buildSqlCompletionItems(sql, sql.length, { + tables: [ + { name: "authors", schema: "public", type: "table" }, + { name: "books", schema: "public", type: "table" }, + ], + columnsByTable: colsWithFk, + }); + const fkJoin = items.find((item) => item.label === "a.publisher_id = b.publisher_id"); + assert.ok(fkJoin, "should suggest join on shared FK column publisher_id"); +}); + +test("suggests join condition for parent_id self-reference", () => { + const colsWithParent = new Map([ + ["public.categories", [ + { name: "id", table: "categories", schema: "public", dataType: "bigint" }, + { name: "parent_id", table: "categories", schema: "public", dataType: "bigint" }, + { name: "name", table: "categories", schema: "public", dataType: "varchar" }, + ]], + ]); + // Self-join + const sql = "select * from public.categories c1 join public.categories c2 on "; + const items = buildSqlCompletionItems(sql, sql.length, { + tables: [ + { name: "categories", schema: "public", type: "table" }, + ], + columnsByTable: colsWithParent, + }); + const parentJoin = items.find((item) => item.label === "c1.parent_id = c2.id" || item.label === "c2.parent_id = c1.id"); + assert.ok(parentJoin, "should suggest parent_id = id for self-reference"); +}); + +test("suggests join condition for created_by → id pattern", () => { + const colsWithCreator = new Map([ + ["public.users", [ + { name: "id", table: "users", schema: "public", dataType: "bigint" }, + { name: "name", table: "users", schema: "public", dataType: "varchar" }, + ]], + ["public.documents", [ + { name: "id", table: "documents", schema: "public", dataType: "bigint" }, + { name: "created_by", table: "documents", schema: "public", dataType: "bigint" }, + ]], + ]); + const sql = "select * from public.users u join public.documents d on "; + const items = buildSqlCompletionItems(sql, sql.length, { + tables: [ + { name: "users", schema: "public", type: "table" }, + { name: "documents", schema: "public", type: "table" }, + ], + columnsByTable: colsWithCreator, + }); + const creatorJoin = items.find((item) => item.label === "u.id = d.created_by"); + assert.ok(creatorJoin, "should suggest id = created_by join"); +}); + +// --- Fuzzy matching --- + +test("fuzzy matches table names with character gaps", () => { + const items = buildSqlCompletionItems("select * from usrs", "select * from usrs".length, { + tables, + columnsByTable, + }); + // "usrs" should fuzzy-match "users" (skip 'e') + const tableItems = items.filter((item) => item.type === "table"); + assert.ok(tableItems.some((item) => item.label === "users"), "should fuzzy-match users"); +}); + +test("fuzzy matches columns with abbreviation pattern", () => { + const sql = "select nm from public.users u"; + const items = buildSqlCompletionItems(sql, "select nm".length, { + tables, + columnsByTable, + }); + // "nm" should fuzzy-match "name" + assert.ok(items.some((item) => item.label === "name" && item.type === "column"), "should fuzzy-match name from 'nm'"); +}); + +test("prefix matches still rank above fuzzy matches", () => { + // Use "na" which is an exact prefix for "name" but also fuzzy-matches "ANALYZE" and others + const sql = "select na from public.users u"; + const items = buildSqlCompletionItems(sql, "select na".length, { + tables, + columnsByTable, + }); + // Prefix match "name" should be first + assert.equal(items[0]?.label, "name"); +}); + +// --- Function inline docs --- + +test("shows function description in detail", () => { + const items = buildSqlCompletionItems("select cou", "select cou".length, { + tables, + columnsByTable, + }); + const countItem = items.find((item) => item.label === "COUNT"); + assert.ok(countItem); + assert.equal(countItem.detail, "返回行数"); +}); + +// --- Type-aware comparison hints --- + +test("suggests NULL and IS NULL after comparison operator", () => { + const sql = "select * from public.users u where u.name = "; + const items = buildSqlCompletionItems(sql, sql.length, { + tables, + columnsByTable, + }); + assert.ok(items.some((item) => item.label === "NULL"), "should suggest NULL"); + assert.ok(items.some((item) => item.label === "IS NULL"), "should suggest IS NULL"); +}); + +test("suggests string snippet for varchar column after =", () => { + const sql = "select * from public.users u where u.name = "; + const items = buildSqlCompletionItems(sql, sql.length, { + tables, + columnsByTable, + }); + const strSnippet = items.find((item) => item.label === "''"); + assert.ok(strSnippet, "should suggest string literal snippet for varchar column"); +}); + +// --- SELECT * expansion --- + +test("shows SELECT * column expansion", () => { + const sql = "select *"; + const items = buildSqlCompletionItems(sql, sql.length, { + tables, + columnsByTable, + }); + const starItem = items.find((item) => item.label === "* → columns"); + assert.ok(starItem, "should show column expansion for *"); +}); + +test("star expansion item includes column count in detail", () => { + const sql = "select *"; + const items = buildSqlCompletionItems(sql, sql.length, { + tables, + columnsByTable, + }); + const starItem = items.find((item) => item.label === "* → columns"); + assert.ok(starItem?.detail?.includes("列"), "detail should mention column count"); +}); + +// --- History-based ranking --- + +test("recordCompletionSelection boosts future ranking", () => { + // Record a few selections of a specific table + recordCompletionSelection("user_profiles", "table"); + recordCompletionSelection("user_profiles", "table"); + + const items = buildSqlCompletionItems("select * from user", "select * from user".length, { + tables, + columnsByTable, + }); + const tableItems = items.filter((item) => item.type === "table"); + // user_profiles should now rank higher than users due to history boost + assert.equal(tableItems[0]?.label, "user_profiles"); +});