feat(sql): add ClickHouse function completion

This commit is contained in:
Justin Gao authored and GitHub committed 2026-07-31 14:38:50 +08:00
1 parent a83925d6ba
commit 9bc1c1b0fb
16 files changed
+2326 -53

No files matched your search

@@ -15,6 +15,73 @@ describe("sqlCompletion keyword snippets", () => {
});
describe("sqlCompletion database functions", () => {
it("suggests ClickHouse functions with canonical casing and preferred placeholders", () => {
const sql = "SELECT tostart";
const items = buildSqlCompletionItems(sql, sql.length, {
databaseType: "clickhouse",
tables: [],
columnsByTable: new Map(),
});
expect(items.find((item) => item.label === "toStartOfDay")).toMatchObject({
type: "function",
apply: "toStartOfDay(${value})",
});
});
it("uses exact ClickHouse window function placeholders", () => {
const denseRankSql = "SELECT dense_";
const denseRankItems = buildSqlCompletionItems(denseRankSql, denseRankSql.length, {
databaseType: "clickhouse",
tables: [],
columnsByTable: new Map(),
});
expect(denseRankItems.find((item) => item.label === "dense_rank")?.apply).toBe("dense_rank()");
const ntileSql = "SELECT nti";
const ntileItems = buildSqlCompletionItems(ntileSql, ntileSql.length, {
databaseType: "clickhouse",
tables: [],
columnsByTable: new Map(),
});
expect(ntileItems.find((item) => item.label === "ntile")?.apply).toBe("ntile(${buckets})");
});
it("does not leak ClickHouse-only functions to MySQL", () => {
const sql = "SELECT tostart";
const items = buildSqlCompletionItems(sql, sql.length, {
databaseType: "mysql",
tables: [],
columnsByTable: new Map(),
});
expect(items.some((item) => item.label === "toStartOfDay")).toBe(false);
});
it("suggests only ClickHouse table functions alongside tables after FROM", () => {
const sql = "SELECT * FROM num";
const items = buildSqlCompletionItems(sql, sql.length, {
databaseType: "clickhouse",
tables: [{ name: "number_events", type: "table" }],
columnsByTable: new Map(),
});
expect(items).toEqual(expect.arrayContaining([expect.objectContaining({ label: "numbers", type: "function" }), expect.objectContaining({ label: "number_events", type: "table" })]));
expect(items.some((item) => item.label === "toStartOfDay")).toBe(false);
});
it("does not insert a duplicate opening parenthesis before an existing call", () => {
const sql = "SELECT toStart()";
const cursor = "SELECT toStart".length;
const items = buildSqlCompletionItems(sql, cursor, {
databaseType: "clickhouse",
tables: [],
columnsByTable: new Map(),
});
expect(items.find((item) => item.label === "toStartOfDay")?.apply).toBe("toStartOfDay");
});
it("suggests MySQL Unix timestamp functions with function snippets", () => {
const fromUnixSql = "SELECT from_unix";
const fromUnixItems = buildSqlCompletionItems(fromUnixSql, fromUnixSql.length, {