дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-05 01:31:41 +02:00
feat(database): add ClickHouse data sources (#865)
Adds ClickHouse to Data Sources over its HTTP interface (HTTP/HTTPS, SSH tunnels, session settings, server-side cancellation): browse databases, tables and columns, inspect DDL, run queries and EXPLAIN plans, with ClickHouse dialect completion. Table data stays read-only and manual transactions are rejected, since ClickHouse keys are not unique and HTTP has no transactions. Results are streamed as JSONCompactEachRowWithNamesAndTypes and parsing stops at the row limit, because result_overflow_mode=break still returns a whole block; errors show only the server's "Code: …" message, including failures raised mid-stream. Fixes #862 Co-authored-by: kkMihai <[email protected]>
Цей коміт міститься в:
1 джерело
c5e188d1e3
коміт
e04e5dd57d
39 files changed
+1921
-120
No files matched your search
@@ -1,8 +1,9 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { DbCompletion } from "@/platform/ipc";
|
||||
import type { DbCompletion, DbDriver } from "@/platform/ipc";
|
||||
import { analyze, templateContexts, valuesSlot } from "./analyze";
|
||||
import { aliasFor, quoteIdent, sqlCandidates, type SqlCandidate } from "./complete";
|
||||
import { inLiteral, lex } from "./lexer";
|
||||
import { identName, inLiteral, lex } from "./lexer";
|
||||
import { DATA_TYPES, statementStarters } from "./dialect";
|
||||
import { bindParams, findParams } from "./params";
|
||||
import { SqlResolver } from "./resolve";
|
||||
|
||||
@@ -65,17 +66,13 @@ const model: DbCompletion = {
|
||||
};
|
||||
|
||||
/** Analysis at the `|` of `sql`. */
|
||||
function at(sql: string, driver: "sqlite" | "postgres" | "mysql" = "sqlite") {
|
||||
function at(sql: string, driver: DbDriver = "sqlite") {
|
||||
const pos = sql.indexOf("|");
|
||||
const text = sql.slice(0, pos) + sql.slice(pos + 1);
|
||||
return analyze(text, pos, driver);
|
||||
}
|
||||
|
||||
function complete(
|
||||
sql: string,
|
||||
driver: "sqlite" | "postgres" | "mysql" = "sqlite",
|
||||
m: DbCompletion = model,
|
||||
): SqlCandidate[] {
|
||||
function complete(sql: string, driver: DbDriver = "sqlite", m: DbCompletion = model): SqlCandidate[] {
|
||||
return sqlCandidates({ model: m, analysis: at(sql, driver), driver, sourceName: "shop" }).sort(
|
||||
(a, b) => b.boost - a.boost,
|
||||
);
|
||||
@@ -83,6 +80,14 @@ function complete(
|
||||
const labels = (c: SqlCandidate[], kind?: string) => c.filter((x) => !kind || x.kind === kind).map((x) => x.label);
|
||||
|
||||
describe("lexer", () => {
|
||||
it("reads ClickHouse backticks and escaped quotes without exposing literal parameters", () => {
|
||||
const sql = String.raw`select "a\\b\"c", ` + "`my column`" + String.raw`, 'it\'s ?; text', ?`;
|
||||
const tokens = lex(sql, "clickhouse").tokens;
|
||||
expect(identName(tokens[1]!)).toBe('a\\b"c');
|
||||
expect(tokens.filter((t) => t.kind === "quoted").map(identName)).toEqual(['a\\b"c', "my column"]);
|
||||
expect(findParams(sql, "clickhouse").map((p) => p.name)).toEqual(["?1"]);
|
||||
});
|
||||
|
||||
it("skips comments and knows literals", () => {
|
||||
const l = lex("select 'a;b', \"Col\" -- c\nfrom t /* x */ where a::int = $1", "postgres");
|
||||
expect(l.tokens.map((t) => t.text)).toEqual([
|
||||
@@ -164,6 +169,42 @@ describe("analysis", () => {
|
||||
});
|
||||
|
||||
describe("completion candidates", () => {
|
||||
it("keeps ClickHouse schema, table and column names distinct by case", () => {
|
||||
const catalog: DbCompletion = {
|
||||
defaultSchema: "analytics",
|
||||
foreignKeys: [],
|
||||
tables: [
|
||||
{ schema: "analytics", name: "Events", kind: "table", columns: [col("UpperOnly")] },
|
||||
{ schema: "analytics", name: "events", kind: "table", columns: [col("Value"), col("value")] },
|
||||
{ schema: "Analytics", name: "events", kind: "table", columns: [col("OtherSchema")] },
|
||||
],
|
||||
};
|
||||
expect(labels(complete("select e.| from events e", "clickhouse", catalog))).toEqual(["Value", "value"]);
|
||||
expect(labels(complete("select e.| from Events e", "clickhouse", catalog))).toEqual(["UpperOnly"]);
|
||||
expect(labels(complete("select e.| from Analytics.events e", "clickhouse", catalog))).toEqual(["OtherSchema"]);
|
||||
const problems = (sql: string) => new SqlResolver(sql, "clickhouse", catalog).problems().map((p) => p.message);
|
||||
expect(problems("select Value, value from events")).toEqual([]);
|
||||
expect(problems("select VALUE from events")).toEqual(["Unable to resolve column 'VALUE'"]);
|
||||
expect(problems("select UpperOnly from events")).toEqual(["Unable to resolve column 'UpperOnly'"]);
|
||||
expect(problems("select OtherSchema from Analytics.events")).toEqual([]);
|
||||
expect(problems("select OtherSchema from analytics.events")).toEqual(["Unable to resolve column 'OtherSchema'"]);
|
||||
expect(
|
||||
at("with Events as (select 1 as UpperOnly) select | from events e", "clickhouse").refs.find(
|
||||
(ref) => ref.name === "events",
|
||||
)?.kind,
|
||||
).toBe("table");
|
||||
});
|
||||
|
||||
it("offers ClickHouse SQL and keeps case-sensitive function and type names", () => {
|
||||
expect(statementStarters("clickhouse")).toEqual(expect.arrayContaining(["SHOW DATABASES", "ALTER TABLE"]));
|
||||
expect(statementStarters("clickhouse")).not.toEqual(expect.arrayContaining(["BEGIN", "SET search_path TO"]));
|
||||
expect(DATA_TYPES.clickhouse).toContain("UInt64");
|
||||
expect(complete("SELECT | FROM users", "clickhouse").find((c) => c.label === "toDateTime")).toMatchObject({
|
||||
snippet: "toDateTime(${})",
|
||||
});
|
||||
expect(quoteIdent('a\\b"c', "clickhouse")).toBe('"a\\\\b\\"c"');
|
||||
});
|
||||
|
||||
it("offers statement starters in the user's case", () => {
|
||||
expect(labels(complete("|"))).toContain("SELECT");
|
||||
expect(labels(complete("select 1; ins|"))).toContain("insert into");
|
||||
@@ -388,6 +429,15 @@ describe("resolve", () => {
|
||||
});
|
||||
|
||||
describe("params", () => {
|
||||
it("binds whole ClickHouse typed parameters without treating their types as names", () => {
|
||||
const sql = "select {value:UInt64}, {label: Nullable(String)}, '{skip:String}'";
|
||||
const params = findParams(sql, "clickhouse");
|
||||
expect(params.map((p) => p.name)).toEqual(["{value:UInt64}", "{label: Nullable(String)}"]);
|
||||
expect(bindParams(sql, params, { "{value:UInt64}": "12", "{label: Nullable(String)}": "'test'" })).toBe(
|
||||
"select 12, 'test', '{skip:String}'",
|
||||
);
|
||||
});
|
||||
|
||||
it("finds parameters per dialect and binds values", () => {
|
||||
const names = (sql: string, driver: "sqlite" | "postgres" | "mysql" = "sqlite", n = 0) =>
|
||||
findParams(sql, driver, n).map((p) => p.name);
|
||||
|
||||
Reference in new issue
Заблокувати користувача