From 1e4131b07cb557422c619e8074d21b0eefacb486 Mon Sep 17 00:00:00 2001 From: Allen Date: Thu, 1 Oct 2026 15:16:38 +0800 Subject: [PATCH] feat(editor): add clickhouse modifier keyword completions --- .../sql/sqlCompletion.context.spec.ts | 33 +++++++++++++++++++ apps/desktop/src/lib/sql/sqlCompletion.ts | 6 +++- 2 files changed, 38 insertions(+), 1 deletion(-) 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 484ea5bd5a..238f641f9b 100644 --- a/apps/desktop/src/lib/__tests__/sql/sqlCompletion.context.spec.ts +++ b/apps/desktop/src/lib/__tests__/sql/sqlCompletion.context.spec.ts @@ -603,6 +603,39 @@ describe("sqlCompletion database functions", () => { expect(ntileItems.find((item) => item.label === "ntile")?.apply).toBe("ntile(${buckets})"); }); + it("suggests ClickHouse FINAL and query modifiers as keywords", () => { + const sql = "SELECT * FROM events FI"; + const items = buildSqlCompletionItems(sql, sql.length, { + databaseType: "clickhouse", + tables: [{ name: "events", type: "table" }], + columnsByTable: new Map(), + }); + + expect(items.find((item) => item.label === "FINAL")).toMatchObject({ type: "keyword" }); + }); + + it("suggests ClickHouse PREWHERE and SETTINGS as keywords", () => { + const sql = "SELECT * FROM events SETT"; + const items = buildSqlCompletionItems(sql, sql.length, { + databaseType: "clickhouse", + tables: [{ name: "events", type: "table" }], + columnsByTable: new Map(), + }); + + expect(items.find((item) => item.label === "SETTINGS")).toMatchObject({ type: "keyword" }); + }); + + it("does not leak ClickHouse FINAL to MySQL", () => { + const sql = "SELECT * FROM events FI"; + const items = buildSqlCompletionItems(sql, sql.length, { + databaseType: "mysql", + tables: [{ name: "events", type: "table" }], + columnsByTable: new Map(), + }); + + expect(items.some((item) => item.label === "FINAL")).toBe(false); + }); + it("does not leak ClickHouse-only functions to MySQL", () => { const sql = "SELECT tostart"; const items = buildSqlCompletionItems(sql, sql.length, { diff --git a/apps/desktop/src/lib/sql/sqlCompletion.ts b/apps/desktop/src/lib/sql/sqlCompletion.ts index 5b2df1c35e..f60e2165f7 100644 --- a/apps/desktop/src/lib/sql/sqlCompletion.ts +++ b/apps/desktop/src/lib/sql/sqlCompletion.ts @@ -461,6 +461,10 @@ const MYSQL_SQL_KEYWORDS = [ const MANTICORESEARCH_SQL_KEYWORDS = ["FACET", "MATCH", "SHOW", "SHOW META", "SHOW TABLES", "CALL", "CALL PQ", "PQ", "META", "TABLES", "OPTION", "WITHIN GROUP ORDER BY"]; +// ClickHouse query grammar adds FINAL, PREWHERE, SAMPLE and the TOTALS/ROLLUP/ +// CUBE grouping modifiers plus DDL vocabulary. Keep them scoped to ClickHouse. +const CLICKHOUSE_SQL_KEYWORDS = ["FINAL", "PREWHERE", "SAMPLE", "TOTALS", "ROLLUP", "CUBE", "GLOBAL", "ARRAY", "ENGINE", "PARTITION", "TTL", "CODEC", "SETTINGS", "FORMAT", "OUTFILE", "ATTACH", "DETACH", "OPTIMIZE", "SYSTEM", "KILL", "MATERIALIZED", "PROJECTION", "GRANULARITY"]; + const SQLITE_SQL_KEYWORDS = ["AUTOINCREMENT", "INTEGER", "BLOB", "BOOLEAN", "WITHOUT ROWID", "VACUUM", "PRAGMA", "JSON_EXTRACT", "JSON_SET", "STRFTIME"]; // DuckDB extends the common SQL grammar with analytical clauses, relation @@ -698,7 +702,7 @@ const DATABASE_SQL_KEYWORDS: Partial> = { "oceanbase-oracle": ORACLE_SQL_KEYWORDS, manticoresearch: MANTICORESEARCH_SQL_KEYWORDS, duckdb: ["COMMENT", ...DUCKDB_SQL_KEYWORDS], - clickhouse: ["COMMENT"], + clickhouse: ["COMMENT", ...CLICKHOUSE_SQL_KEYWORDS], doris: ["COMMENT", "MATERIALIZED", "MATERIALIZED VIEW", "DISTRIBUTED BY HASH", "DUPLICATE KEY", "AGGREGATE KEY", "UNIQUE KEY", "PRIMARY KEY", "PROPERTIES", "PARTITION BY", "BUCKETS", "LATERAL VIEW", "EXPLODE", "ARRAY", "MAP", "STRUCT", "BITMAP", "HLL"], starrocks: ["COMMENT", "MATERIALIZED", "MATERIALIZED VIEW", "DISTRIBUTED BY HASH", "DUPLICATE KEY", "PROPERTIES", "PARTITION BY", "BUCKETS", "LATERAL VIEW"], dameng: ["COMMENT"],