feat(sql): add ClickHouse function completion
This commit is contained in:
1 parent
a83925d6ba
commit
9bc1c1b0fb
16 files changed
+2326
-53
No files matched your search
@@ -0,0 +1,35 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { generateAggregateCombinatorCandidates } from "@/lib/sql/clickhouse/aggregateCombinators";
|
||||
import { CLICKHOUSE_FUNCTION_REGISTRY } from "@/lib/sql/clickhouse/functionRegistry";
|
||||
|
||||
describe("ClickHouse aggregate combinators", () => {
|
||||
it("generates If with an appended condition argument", () => {
|
||||
const sumIf = generateAggregateCombinatorCandidates("sumIf", 20).find((item) => item.name === "sumIf");
|
||||
expect(sumIf?.signatures[0].parameterGroups).toEqual([["value", "condition"]]);
|
||||
});
|
||||
|
||||
it("allows Array before If and rejects the reverse order", () => {
|
||||
expect(generateAggregateCombinatorCandidates("uniqArrayIf", 20).some((item) => item.name === "uniqArrayIf")).toBe(true);
|
||||
expect(generateAggregateCombinatorCandidates("uniqIfArray", 20).some((item) => item.name === "uniqIfArray")).toBe(false);
|
||||
});
|
||||
|
||||
it("preserves parametric aggregate groups for State", () => {
|
||||
const state = generateAggregateCombinatorCandidates("quantilesTDigestState", 20).find((item) => item.name === "quantilesTDigestState");
|
||||
expect(state?.signatures[0].parameterGroups).toEqual([["level", "...levels"], ["expression"]]);
|
||||
});
|
||||
|
||||
it("bounds generated results", () => {
|
||||
expect(generateAggregateCombinatorCandidates("", 7)).toHaveLength(7);
|
||||
});
|
||||
|
||||
it("does not generate aggregate combinators for window functions", () => {
|
||||
for (const name of ["rankIf", "denseRankState", "percentRankIf", "cume_distState", "ntileIf"] as const) {
|
||||
expect(generateAggregateCombinatorCandidates(name, 20).some((item) => item.name === name)).toBe(false);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("contains ordinary and parametric aggregate definitions", () => {
|
||||
expect(CLICKHOUSE_FUNCTION_REGISTRY.get("uniqExact")).toMatchObject({ kind: "aggregate" });
|
||||
expect(CLICKHOUSE_FUNCTION_REGISTRY.get("quantilesTDigest")?.signatures[0].parameterGroups).toEqual([["level", "...levels"], ["expression"]]);
|
||||
});
|
||||
@@ -0,0 +1,66 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { CLICKHOUSE_FUNCTION_REGISTRY, createClickHouseFunctionRegistry } from "@/lib/sql/clickhouse/functionRegistry";
|
||||
import { CLICKHOUSE_FUNCTION_CATEGORY_MANIFEST, CLICKHOUSE_REGULAR_FUNCTIONS } from "@/lib/sql/clickhouse/regularFunctions";
|
||||
import { CLICKHOUSE_TABLE_FUNCTIONS } from "@/lib/sql/clickhouse/tableFunctions";
|
||||
import type { ClickHouseFunctionDefinition } from "@/lib/sql/clickhouse/functionTypes";
|
||||
|
||||
const toStartOfDay: ClickHouseFunctionDefinition = {
|
||||
name: "toStartOfDay",
|
||||
kind: "regular",
|
||||
category: "date-time",
|
||||
signatures: [{ parameterGroups: [["value", "time_zone?"]], returnType: "DateTime" }],
|
||||
aliases: ["startOfDay"],
|
||||
};
|
||||
|
||||
describe("ClickHouse function registry", () => {
|
||||
it("looks up canonical names case-insensitively and preserves overloads", () => {
|
||||
const registry = createClickHouseFunctionRegistry([toStartOfDay]);
|
||||
|
||||
expect(registry.get("TOSTARTOFDAY")).toEqual(toStartOfDay);
|
||||
expect(registry.search("tostart", 20)).toEqual([toStartOfDay]);
|
||||
expect(registry.search("startof", 20)).toEqual([toStartOfDay]);
|
||||
});
|
||||
|
||||
it("rejects duplicate canonical names case-insensitively", () => {
|
||||
expect(() => createClickHouseFunctionRegistry([toStartOfDay, { ...toStartOfDay, name: "TOSTARTOFDAY" }])).toThrow(/duplicate/i);
|
||||
});
|
||||
|
||||
it("rejects an invalid preferred signature index", () => {
|
||||
expect(() => createClickHouseFunctionRegistry([{ ...toStartOfDay, preferredSignature: 2 }])).toThrow(/preferred signature/i);
|
||||
});
|
||||
|
||||
it("keeps the checked-in category manifest and inventory counts aligned", () => {
|
||||
for (const entry of CLICKHOUSE_FUNCTION_CATEGORY_MANIFEST) {
|
||||
expect(CLICKHOUSE_REGULAR_FUNCTIONS.filter((definition) => definition.category === entry.category)).toHaveLength(entry.minimumCount);
|
||||
}
|
||||
});
|
||||
|
||||
it.each([
|
||||
["arrayMap", "array"],
|
||||
["toStartOfDay", "date-time"],
|
||||
["JSONExtractString", "json"],
|
||||
["cityHash64", "hash"],
|
||||
["URLHierarchy", "url"],
|
||||
["lagInFrame", "window"],
|
||||
] as const)("contains %s with canonical casing and category %s", (name, category) => {
|
||||
expect(CLICKHOUSE_FUNCTION_REGISTRY.get(name)).toMatchObject({ name, category });
|
||||
});
|
||||
|
||||
it("treats names shared with Object.prototype as ordinary ClickHouse functions", () => {
|
||||
expect(CLICKHOUSE_FUNCTION_REGISTRY.get("toString")?.signatures.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("models window function aliases and exact argument lists", () => {
|
||||
for (const name of ["rank", "dense_rank", "denseRank", "percent_rank", "percentRank", "cume_dist"] as const) {
|
||||
expect(CLICKHOUSE_FUNCTION_REGISTRY.get(name)).toMatchObject({ kind: "window", signatures: [{ parameterGroups: [[]] }] });
|
||||
}
|
||||
expect(CLICKHOUSE_FUNCTION_REGISTRY.get("ntile")).toMatchObject({
|
||||
kind: "window",
|
||||
signatures: [{ parameterGroups: [["buckets"]] }],
|
||||
});
|
||||
});
|
||||
|
||||
it.each(["numbers", "file", "url", "s3", "remote", "postgresql", "mysql"] as const)("contains the %s table function", (name) => {
|
||||
expect(CLICKHOUSE_TABLE_FUNCTIONS.some((definition) => definition.name === name && definition.kind === "table")).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -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, {
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { getSqlFunctionSignatureHelp } from "@/lib/sql/sqlCompletion";
|
||||
|
||||
describe("ClickHouse signature help", () => {
|
||||
it("returns every overload and highlights the active ordinary parameter", () => {
|
||||
const sql = "SELECT toStartOfInterval(ts, ";
|
||||
const help = getSqlFunctionSignatureHelp(sql, sql.length, "clickhouse");
|
||||
expect(help?.name).toBe("toStartOfInterval");
|
||||
expect(help?.overloads.length).toBeGreaterThan(1);
|
||||
expect(help?.overloads[0].activeGroup).toBe(0);
|
||||
expect(help?.overloads[0].activeParameter).toBe(1);
|
||||
});
|
||||
|
||||
it("resolves the second parameter group of a parametric aggregate", () => {
|
||||
const sql = "SELECT quantilesTDigest(0.5, 0.9)(value";
|
||||
const help = getSqlFunctionSignatureHelp(sql, sql.length, "clickhouse");
|
||||
expect(help?.name).toBe("quantilesTDigest");
|
||||
expect(help?.overloads[0]).toMatchObject({ activeGroup: 1, activeParameter: 0 });
|
||||
expect(help?.overloads[0].parameterGroups).toEqual([["level", "...levels"], ["expression"]]);
|
||||
});
|
||||
|
||||
it("keeps MySQL signature help as one overload and one parameter group", () => {
|
||||
const sql = "SELECT DATE_ADD(created_at, ";
|
||||
const help = getSqlFunctionSignatureHelp(sql, sql.length, "mysql");
|
||||
expect(help?.overloads).toHaveLength(1);
|
||||
expect(help?.overloads[0].parameterGroups).toEqual([["date", "INTERVAL expr unit"]]);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user