25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
fix(sql): stop flagging ORDER BY aliases as unknown columns
zipg6886fde70921
diff --git a/apps/desktop/src/lib/__tests__/sql/semantic/references.spec.ts b/apps/desktop/src/lib/__tests__/sql/semantic/references.spec.tsindex 5fa32b2880..f0762f6930 100644--- a/apps/desktop/src/lib/__tests__/sql/semantic/references.spec.ts+++ b/apps/desktop/src/lib/__tests__/sql/semantic/references.spec.ts@@ -31,6 +31,33 @@ describe("sqlSemanticReferences shared consumers", () => {expect(diagnostics.map((diagnostic) => diagnostic.message)).toEqual(["Unknown column ro.missing"]);});+ it("does not flag a SELECT alias used by DuckDB ORDER BY", () => {+ const sql = `SELECT SUM(price * amount) AS total, api_key_name FROM amounts GROUP BY api_key_name ORDER BY total DESC`;+ const aliasStart = sql.indexOf("total", sql.indexOf("ORDER BY"));+ const analysis: SqlReferenceAnalysis = {+ tables: [+ {+ name: "amounts",+ span: span(sql.indexOf("amounts") + 1, sql.indexOf("amounts") + "amounts".length),+ },+ ],+ columns: [+ {+ name: "total",+ span: span(aliasStart + 1, aliasStart + "total".length),+ },+ ],+ };++ const diagnostics = buildSqlSemanticDiagnostics(analysis, {+ tables: [],+ columnsByTable: new Map([["amounts", [{ name: "price" }, { name: "amount" }, { name: "api_key_name" }]]]),+ sql,+ });++ expect(diagnostics).toEqual([]);+ });+it("resolves navigation targets from subquery aliases and projected columns", () => {const { sql, cursor } = sqlFixtureCursor("SELECT sq.user_| FROM (SELECT id, name AS user_name FROM users) sq");const model = buildSqlSemanticModel(sql, cursor);diff --git a/apps/desktop/src/lib/sql/semantic/diagnostics.ts b/apps/desktop/src/lib/sql/semantic/diagnostics.tsindex f782f3bfa6..32b459aec4 100644--- a/apps/desktop/src/lib/sql/semantic/diagnostics.ts+++ b/apps/desktop/src/lib/sql/semantic/diagnostics.ts@@ -280,6 +280,14 @@ export function buildSqlSemanticDiagnostics(analysis: SqlReferenceAnalysis, scheif (tdengineStableTables.has(tableReferenceKey(table))) continue;}+ // SQL engines allow SELECT projection aliases in ORDER BY/GROUP BY (and,+ // for example, DuckDB also allows them in HAVING). The reference parser+ // reports those aliases as ordinary columns, so metadata-only validation+ // would otherwise show a false "Unknown column" diagnostic. Reuse the+ // completion context, which already extracts aliases and knows when they+ // are visible, to keep diagnostics aligned with executable SQL semantics.+ if (schema.sql && isVisibleProjectionAlias(schema.sql, column.span, column.name)) continue;+const displayName = column.qualifier ? `${column.qualifier}.${column.name}` : column.name;diagnostics.push({span: trimSqlTextSpanWhitespace(schema.sql, column.span),@@ -319,6 +327,14 @@ export function isSqlVirtualTableReference(table: { name: string; schema?: strinreturn databaseType === "mysql" && !table.schema && normalizeName(table.name) === "dual";}+function isVisibleProjectionAlias(sql: string, span: SqlTextSpan, name: string): boolean {+ const range = sqlTextSpanToOffsetRange(sql, span);+ if (!range) return false;+ const context = getSqlCompletionContext(sql, range.to);+ if (!context.prioritizeSelectAliases) return false;+ return context.selectAliases.some((alias) => normalizeName(alias) === normalizeName(name));+}+function trimSqlTextSpanWhitespace(sql: string | undefined, span: SqlTextSpan): SqlTextSpan {if (!sql) return span;const range = sqlTextSpanToOffsetRange(sql, span);