feat(neo4j): expand node query properties into result columns

This commit is contained in:
Hand Sonic authored and GitHub committed 2026-10-01 15:52:30 +08:00
1 parent 38a00c7cb7
commit 660071870b
12 files changed
+433 -29

No files matched your search

@@ -0,0 +1,129 @@
import { computed, effectScope, nextTick, ref } from "vue";
import { describe, expect, it } from "vitest";
import { extractNeo4jNodeCells, projectNeo4jNodeResult, type Neo4jNodeProperty } from "@/lib/neo4j/neo4jNodeResult";
import { useNeo4jNodeTableResult } from "@/composables/useNeo4jNodeTableResult";
import { appendQueryResultSegment } from "@/stores/queryStore";
import { buildTabResultSnapshot, decodeTabResultSnapshot, encodeTabResultSnapshot } from "@/lib/tabs/tabResultCache";
import type { QueryResult, QueryTab } from "@/types/database";
function node(properties: Neo4jNodeProperty[], display = "(:QA)") {
return { __dbx_neo4j_node: "v1", display, properties };
}
function property(name: string, value: string, type = "String"): Neo4jNodeProperty {
return { name, type, value };
}
function result(columns: string[], rows: unknown[][]): QueryResult {
const value = { columns, rows, affected_rows: 0, execution_time_ms: 1 } as QueryResult;
extractNeo4jNodeCells(value);
return value;
}
describe("Neo4j node table projection", () => {
it("qualifies multiple node aliases, keeps scalars and exact integers, and fills absent properties with null", () => {
const source = result(
["p", "c", "score"],
[
[node([property("name", "Ada"), property("age", "9007199254740993", "Integer")]), node([property("name", "QA Labs")]), "7"],
[node([property("name", "Bob")]), null, "8"],
],
);
const projected = projectNeo4jNodeResult(source);
expect(projected.columns).toEqual(["p", "c", "score", "p.name", "p.age", "c.name"]);
expect(projected.rows).toEqual([
["(:QA)", "(:QA)", "7", "Ada", "9007199254740993", "QA Labs"],
["(:QA)", null, "8", "Bob", null, null],
]);
expect(projected.hidden_column_indexes).toEqual([0, 1]);
expect(projected.column_types?.[4]).toBe("Integer");
expect(source.columns).toEqual(["p", "c", "score"]);
expect(source.rows[0]).toEqual(["(:QA)", "(:QA)", "7"]);
expect(projectNeo4jNodeResult(projected)).toBe(projected);
});
it("retains empty and mixed node/scalar source cells, including node-looking strings", () => {
const literal = JSON.stringify(node([property("fake", "no")]));
const source = result(["n"], [[node([property("value", "42", "Integer")])], [node([])], [literal], [null], [node([property("value", "text")])]]);
const projected = projectNeo4jNodeResult(source);
expect(projected.hidden_column_indexes).toEqual([]);
expect(projected.rows[2]).toEqual([literal, null]);
expect(projected.column_types?.[1]).toBe("Any");
expect(projectNeo4jNodeResult(result(["n"], [[node([])]]))).toMatchObject({ columns: ["n"], rows: [["(:QA)"]] });
});
it("disambiguates existing aliases, dots and prototype-like property names without losing values", () => {
const projected = projectNeo4jNodeResult(result(["n", "n.name", "n.name (2)"], [[node([property("name", "Ada"), property("__proto__", "safe"), property("a.b", "dot")]), "scalar", "another"]]));
expect(projected.columns).toEqual(["n", "n.name", "n.name (2)", "n.name (3)", "n.__proto__", "n.a.b"]);
expect(projected.rows[0]?.slice(3)).toEqual(["Ada", "safe", "dot"]);
});
it("does not normalize unknown versions, malformed envelopes or ordinary scalar results", () => {
const envelopes = [{ ...node([]), __dbx_neo4j_node: "v2" }, node([{ ...property("bad", "x"), value: {} } as unknown as Neo4jNodeProperty]), node([property("a", "x"), property("a", "y")])];
const source = result(
["n"],
envelopes.map((value) => [value]),
);
expect(source.neo4j_node_cells).toBeUndefined();
expect(source.rows).toEqual(envelopes.map((value) => [value]));
const scalar = result(["name"], [["Ada"], [null]]);
expect(projectNeo4jNodeResult(scalar)).toBe(scalar);
});
it("keeps generated ordinals stable across cursor pages and clips metadata at the row cap", () => {
const first = result(["n"], [[null], [node([property("name", "Ada")])]]);
const second = { columns: ["n"], rows: [[node([property("late", "yes")])], [node([property("discarded", "no")])]], affected_rows: 0, execution_time_ms: 1, has_more: true } as QueryResult;
const merged = appendQueryResultSegment(first, second, 3);
expect(merged.columns).toEqual(["n"]);
expect(merged.neo4j_node_cells?.map((cell) => cell.row_index)).toEqual([1, 2]);
const projected = projectNeo4jNodeResult(merged);
expect(projected.columns).toEqual(["n", "n.name", "n.late"]);
expect(projected.rows).toEqual([
[null, null, null],
["(:QA)", "Ada", null],
["(:QA)", null, "yes"],
]);
expect(merged.has_more).toBe(false);
const emptyFirst = appendQueryResultSegment(result(["n"], [[null]]), result(["n"], [[node([property("new", "yes")])]]), 10);
expect(projectNeo4jNodeResult(emptyFirst).rows).toEqual([
[null, null],
["(:QA)", "yes"],
]);
});
it("retains typed properties through active and historical result cache restore without cursor ids", () => {
const source = { ...result(["n"], [[node([property("age", "9007199254740993", "Integer")])]]), session_id: "private-cursor" };
const tab = { result: source, results: [source], resultRuns: [{ id: "run", title: "Run", sequence: 1, sql: "RETURN n", createdAt: 1, result: source, results: [source] }] } as QueryTab;
const snapshot = buildTabResultSnapshot(tab)!;
expect(snapshot.result?.neo4j_node_cells).not.toBe(source.neo4j_node_cells);
const restored = decodeTabResultSnapshot(encodeTabResultSnapshot(snapshot));
for (const value of [restored.result, restored.results?.[0], restored.resultRuns?.[0].result, restored.resultRuns?.[0].results?.[0]]) {
expect(value?.session_id).toBeUndefined();
expect(projectNeo4jNodeResult(value!).rows).toEqual([["(:QA)", "9007199254740993"]]);
}
});
it("sorts only presentation rows with exact integers and resets on result changes", async () => {
const scope = effectScope();
const source = ref(result(["n"], [[node([property("age", "9007199254740993", "Integer")])], [node([property("age", "9007199254740992", "Integer")])]]));
source.value.large_value_cells = [{ row_index: 0, column_index: 0, original_bytes: 100 }];
const key = ref("first");
const table = scope.run(() =>
useNeo4jNodeTableResult(
computed(() => source.value),
computed(() => key.value),
),
)!;
table.setSort("n.age", 1, "asc");
expect(table.result.value?.rows.map((row) => row[1])).toEqual(["9007199254740992", "9007199254740993"]);
expect(table.result.value?.large_value_cells?.[0].row_index).toBe(1);
expect(source.value.neo4j_node_cells?.[0].properties[0].value).toBe("9007199254740993");
table.setSort("n.age", 1, null);
expect(table.result.value?.rows[0]?.[1]).toBe("9007199254740993");
table.setSort("n.age", 1, "desc");
key.value = "second";
await nextTick();
expect(table.sort.value).toBeUndefined();
scope.stop();
});
});
@@ -0,0 +1,114 @@
import type { QueryResult } from "@/types/database";
type CellValue = QueryResult["rows"][number][number];
export interface Neo4jNodeProperty {
name: string;
type: string;
value: CellValue;
}
export interface Neo4jNodeCell {
row_index: number;
column_index: number;
properties: Neo4jNodeProperty[];
}
function isCellValue(value: unknown): value is CellValue {
return value === null || typeof value === "string" || typeof value === "boolean" || (typeof value === "number" && Number.isFinite(value));
}
function nodeEnvelope(value: unknown): { display: string; properties: Neo4jNodeProperty[] } | undefined {
if (!value || typeof value !== "object") return;
const cell = value as Record<string, unknown>;
if (cell.__dbx_neo4j_node !== "v1" || typeof cell.display !== "string" || !Array.isArray(cell.properties)) return;
const names = new Set<string>();
for (const value of cell.properties) {
if (!value || typeof value !== "object") return;
const property = value as Record<string, unknown>;
if (typeof property.name !== "string" || typeof property.type !== "string" || !isCellValue(property.value) || names.has(property.name)) return;
names.add(property.name);
}
return { display: cell.display, properties: cell.properties as Neo4jNodeProperty[] };
}
/** Normalize typed wire cells once, keeping primitive rows and original columns. */
export function extractNeo4jNodeCells(result: QueryResult): void {
let rows: QueryResult["rows"] | undefined;
let cells: Neo4jNodeCell[] | undefined;
result.rows.forEach((row, row_index) => {
row.forEach((value, column_index) => {
if (column_index >= result.columns.length) return;
const cell = nodeEnvelope(value);
if (!cell) return;
rows ??= result.rows.map((row) => [...row]);
cells ??= [...(result.neo4j_node_cells ?? [])];
rows[row_index]![column_index] = cell.display;
cells.push({ row_index, column_index, properties: cell.properties });
});
});
if (rows) result.rows = rows;
if (cells) result.neo4j_node_cells = cells;
}
export function appendNeo4jNodeCells(previous: QueryResult, segment: QueryResult, appendedRowCount: number): Neo4jNodeCell[] | undefined {
const cells = [...(previous.neo4j_node_cells ?? []), ...(segment.neo4j_node_cells ?? []).filter((cell) => cell.row_index < appendedRowCount).map((cell) => ({ ...cell, row_index: cell.row_index + previous.rows.length }))];
return cells.length ? cells : undefined;
}
/** Append alias-qualified properties without changing source ordinals or cursors. */
export function projectNeo4jNodeResult(result: QueryResult): QueryResult {
if (!result.neo4j_node_cells?.length) return result;
const columns = [...result.columns];
const types = result.columns.map((_, index) => result.column_types?.[index] ?? "Unknown");
const usedNames = new Set(columns);
const propertiesByColumn = new Map<number, Map<string, number>>();
const nodeRowsByColumn = new Map<number, Set<number>>();
const keepOriginal = new Set<number>();
const propertyTypes = new Map<number, Set<string>>();
const cells = result.neo4j_node_cells.filter((cell) => cell.row_index >= 0 && cell.row_index < result.rows.length && cell.column_index >= 0 && cell.column_index < result.columns.length);
for (const cell of cells) {
const source = cell.column_index;
let nodeRows = nodeRowsByColumn.get(source);
if (!nodeRows) nodeRowsByColumn.set(source, (nodeRows = new Set()));
nodeRows.add(cell.row_index);
if (cell.properties.length === 0) keepOriginal.add(source);
let properties = propertiesByColumn.get(source);
if (!properties) propertiesByColumn.set(source, (properties = new Map()));
for (const property of cell.properties) {
let index = properties.get(property.name);
if (index === undefined) {
const base = `${result.columns[source]}.${property.name}`;
let name = base;
for (let suffix = 2; usedNames.has(name); suffix++) name = `${base} (${suffix})`;
index = columns.length;
columns.push(name);
usedNames.add(name);
properties.set(property.name, index);
propertyTypes.set(index, new Set());
}
if (property.type !== "Null") propertyTypes.get(index)!.add(property.type);
}
}
if (columns.length === result.columns.length) return result;
for (const [index, observed] of propertyTypes) types[index] = observed.size === 1 ? [...observed][0]! : observed.size === 0 ? "Null" : "Any";
const rows = result.rows.map((row) => [...row, ...Array<CellValue>(columns.length - result.columns.length).fill(null)]);
for (const cell of cells) {
for (const property of cell.properties) rows[cell.row_index]![propertiesByColumn.get(cell.column_index)!.get(property.name)!] = property.value;
}
const hidden = new Set(result.hidden_column_indexes);
for (const [source, nodeRows] of nodeRowsByColumn) {
// Empty nodes and mixed scalar/node columns must retain their source value.
if (!keepOriginal.has(source) && result.rows.every((row, index) => row[source] == null || nodeRows.has(index))) hidden.add(source);
}
return {
...result,
columns,
column_types: types,
column_sortables: columns.map((_, index) => result.column_sortables?.[index] ?? true),
hidden_column_indexes: [...hidden],
rows,
neo4j_node_cells: undefined,
spatial_values: result.spatial_values?.map((row) => [...row, ...Array<number | null>(columns.length - result.columns.length).fill(null)]),
};
}
@@ -58,6 +58,7 @@ export interface TabResultSnapshot {
interface ColumnarQueryResult {
columns: string[];
neo4j_node_cells?: QueryResult["neo4j_node_cells"];
spatial_columns?: QueryResult["spatial_columns"];
spatial_values?: QueryResult["spatial_values"];
large_value_cells?: QueryResult["large_value_cells"];
@@ -355,6 +356,7 @@ function stripSessionIds(result: QueryResult | undefined): QueryResult | undefin
if (!result) return undefined;
return {
columns: [...result.columns],
neo4j_node_cells: result.neo4j_node_cells ? clonePlain(result.neo4j_node_cells) : undefined,
execution_error: result.execution_error,
statement_index: result.statement_index,
column_types: result.column_types ? [...result.column_types] : undefined,
@@ -420,6 +422,7 @@ function toColumnarResult(result: QueryResult | undefined): ColumnarQueryResult
}
const metadata = removeUndefinedFields({
columns: [...result.columns],
neo4j_node_cells: result.neo4j_node_cells ? clonePlain(result.neo4j_node_cells) : undefined,
execution_error: result.execution_error,
statement_index: result.statement_index,
column_types: result.column_types ? [...result.column_types] : undefined,
@@ -457,6 +460,7 @@ function fromColumnarResult(result: ColumnarQueryResult | undefined): QueryResul
const rows = Array.from({ length: result.rowCount }, (_, rowIndex) => result.columnValues.map((values) => values[rowIndex] ?? null));
return {
columns: [...result.columns],
neo4j_node_cells: result.neo4j_node_cells ? clonePlain(result.neo4j_node_cells) : undefined,
execution_error: result.execution_error,
statement_index: result.statement_index,
column_types: result.column_types ? [...result.column_types] : undefined,