Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
226 changes: 225 additions & 1 deletion bridge/__tests__/connectors/clickhouse.batch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ describe("ClickHouse getSchemaMetadataBatch unit tests", () => {
expect(userTable!.columns[0].is_primary_key).toBe(true);
expect(userTable!.primaryKeys).toHaveLength(1);
expect(userTable!.primaryKeys[0].column_name).toBe("id");
expect(userTable!.reportedPrimaryKeys).toEqual(["id"]);
expect(result.indexMetadataError).toBeUndefined();
expect(result.columnMetadataErrors).toBeUndefined();

const orderTable = result.tables.get("orders");
expect(orderTable).toBeDefined();
Expand Down Expand Up @@ -191,7 +194,7 @@ describe("ClickHouse getSchemaMetadataBatch unit tests", () => {
const q = String(query);

// listTables query
if (q.includes("FROM system.tables")) {
if (q.includes("FROM system.tables") && !q.includes("primary_key")) {
return {
json: async () => [
{ schema: "testdb", name: "student", type: "BASE TABLE" },
Expand All @@ -201,6 +204,16 @@ describe("ClickHouse getSchemaMetadataBatch unit tests", () => {
};
}

if (q.includes("primary_key, sorting_key FROM system.tables")) {
return {
json: async () => [
{ name: "student", primary_key: "id", sorting_key: "id" },
{ name: "persons", primary_key: "tuple()", sorting_key: "tuple()" },
{ name: "TestTable", primary_key: "tuple()", sorting_key: "tuple()" },
],
};
}

// Batch queries fail
if (q.includes("ORDER BY `table`")) {
throw new Error("Batch query failed");
Expand Down Expand Up @@ -254,6 +267,7 @@ describe("ClickHouse getSchemaMetadataBatch unit tests", () => {
expect(studentMeta).toBeDefined();
expect(studentMeta!.columns).toHaveLength(2);
expect(studentMeta!.primaryKeys[0].column_name).toBe("id");
expect(studentMeta!.reportedPrimaryKeys).toEqual(["id"]);
});

it("should filter tables when array of table names is passed", async () => {
Expand Down Expand Up @@ -304,4 +318,214 @@ describe("ClickHouse getSchemaMetadataBatch unit tests", () => {
expect(result.tables.has("student")).toBe(true);
expect(result.tables.has("persons")).toBe(false);
});

it("distinguishes reportedPrimaryKeys from listPrimaryKeys first-column UI fallback for ORDER BY tuple() tables", async () => {
mockQuery.mockImplementation(async ({ query }: any) => {
const q = String(query);

if (q.includes("FROM system.tables")) {
return {
json: async () => [
{ schema: "testdb", name: "logs", type: "BASE TABLE" },
{ schema: "testdb", name: "users", type: "BASE TABLE" },
],
};
}

if (q.includes("FROM system.columns") && !q.includes("is_in_primary_key = 1")) {
if (q.includes("ORDER BY `table`")) {
return {
json: async () => [
{
table: "logs",
name: "ts",
type: "DateTime",
not_nullable: 1,
is_in_primary_key: 0,
is_in_sorting_key: 0,
},
{
table: "users",
name: "id",
type: "UInt64",
not_nullable: 1,
is_in_primary_key: 1,
is_in_sorting_key: 1,
},
],
};
}
return {
json: async () => [{ name: "ts", type: "DateTime", not_nullable: 1 }],
};
}

if (q.includes("is_in_primary_key = 1")) {
if (q.includes("ORDER BY `table`")) {
return {
json: async () => [{ table: "users", column_name: "id" }],
};
}
return { json: async () => [] };
}

return { json: async () => [] };
});

const result = await clickhouseConnector.getSchemaMetadataBatch(mockConfig, "testdb");
const logs = result.tables.get("logs")!;
const users = result.tables.get("users")!;

expect(logs.reportedPrimaryKeys).toEqual([]);
expect(logs.primaryKeys).toEqual([{ column_name: "ts" }]);
expect(users.reportedPrimaryKeys).toEqual(["id"]);
expect(users.primaryKeys).toEqual([{ column_name: "id" }]);
});

it("reports indexMetadataError when CLICKHOUSE_BATCH_GET_INDEXES throws without hiding empty index success", async () => {
mockQuery.mockImplementation(async ({ query }: any) => {
const q = String(query);

if (q.includes("FROM system.tables")) {
return {
json: async () => [{ schema: "testdb", name: "events", type: "BASE TABLE" }],
};
}
if (q.includes("FROM system.columns") && !q.includes("is_in_primary_key = 1")) {
return {
json: async () => [
{
table: "events",
name: "id",
type: "UInt64",
not_nullable: 1,
is_in_primary_key: 1,
is_in_sorting_key: 1,
},
],
};
}
if (q.includes("is_in_primary_key = 1")) {
return {
json: async () => [{ table: "events", column_name: "id" }],
};
}
if (q.includes("system.data_skipping_indices")) {
throw new Error("Permission denied on system.data_skipping_indices");
}

return { json: async () => [] };
});

const result = await clickhouseConnector.getSchemaMetadataBatch(mockConfig, "testdb");
expect(
(result.indexMetadataError as any)?.message ?? String(result.indexMetadataError),
).toContain("Permission denied");
expect(result.tables.get("events")!.indexes).toEqual([]);
});

it("records columnMetadataErrors for a single failing table while preserving other loaded tables", async () => {
mockQuery.mockImplementation(async ({ query, query_params }: any) => {
const q = String(query);

if (q.includes("FROM system.tables")) {
return {
json: async () => [
{ schema: "testdb", name: "users", type: "BASE TABLE" },
{ schema: "testdb", name: "broken_table", type: "BASE TABLE" },
],
};
}
if (
q.includes("ORDER BY `table`") &&
q.includes("FROM system.columns") &&
!q.includes("is_in_primary_key = 1")
) {
return {
json: async () => [
{
table: "users",
name: "id",
type: "UInt64",
not_nullable: 1,
is_in_primary_key: 1,
is_in_sorting_key: 1,
},
],
};
}
if (
q.includes(
"WHERE database = {target_schema: String} AND `table` = {target_table: String}",
) &&
!q.includes("is_in_primary_key = 1")
) {
if (query_params?.target_table === "broken_table") {
throw new Error("Table metadata corrupted");
}
}
if (q.includes("is_in_primary_key = 1")) {
return { json: async () => [{ table: "users", column_name: "id" }] };
}
return { json: async () => [] };
});

const result = await clickhouseConnector.getSchemaMetadataBatch(mockConfig, "testdb");
expect(result.tables.get("users")!.columns).toHaveLength(1);
expect(result.tables.get("broken_table")!.columns).toEqual([]);
expect(result.columnMetadataErrors).toEqual({
broken_table: "Table metadata corrupted",
});
expect(result.columnMetadataError).toContain("broken_table");
});

it("throws when batch columns query fails and no table recovers via fallback", async () => {
mockQuery.mockImplementation(async ({ query }: any) => {
const q = String(query);

if (q.includes("FROM system.tables")) {
return {
json: async () => [{ schema: "testdb", name: "users", type: "BASE TABLE" }],
};
}
if (q.includes("FROM system.columns") && !q.includes("is_in_primary_key = 1")) {
throw new Error("ClickHouse columns unavailable");
}
return { json: async () => [] };
});

await expect(clickhouseConnector.getSchemaMetadataBatch(mockConfig, "testdb")).rejects.toThrow(
/Failed to retrieve ClickHouse column metadata/,
);
});

it("does not promote fallback listPrimaryKeys to reportedPrimaryKeys", async () => {
mockQuery.mockImplementation(async ({ query }: any) => {
const q = String(query);
if (q.includes("FROM system.tables")) {
return { json: async () => [{ schema: "testdb", name: "events", type: "BASE TABLE" }] };
}
if (q.includes("is_in_primary_key = 1")) {
return { json: async () => [] };
}
if (q.includes("FROM system.columns")) {
return {
json: async () => [
{ table: "events", name: "id", type: "UInt64", not_nullable: 1, is_in_primary_key: 0 },
],
};
}
return { json: async () => [] };
});

const mockListPks = jest
.spyOn(clickhouseConnector, "listPrimaryKeys")
.mockResolvedValue([{ column_name: "id" }]);

const result = await clickhouseConnector.getSchemaMetadataBatch(mockConfig, "testdb");
const events = result.tables.get("events")!;
expect(events.primaryKeys).toEqual([{ column_name: "id" }]);
expect(events.reportedPrimaryKeys).toEqual([]);
mockListPks.mockRestore();
});
});
Loading
Loading