feat(spatial): add SRID-aware geometry previews
This commit is contained in:
1 parent
ee26b2a580
commit
a6a072294a
57 files changed
+2771
-384
No files matched your search
@@ -0,0 +1,42 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { appendQueryResultSegment } from "@/stores/queryStore";
|
||||
import type { QueryResult } from "@/types/database";
|
||||
|
||||
function make(rows: number, spatial?: QueryResult["spatial_columns"]): QueryResult {
|
||||
return {
|
||||
columns: ["geom"],
|
||||
column_types: ["geometry"],
|
||||
spatial_columns: spatial,
|
||||
rows: Array.from({ length: rows }, (_, i) => [`POINT(${i} ${i})`]),
|
||||
affected_rows: 0,
|
||||
execution_time_ms: 0,
|
||||
} as unknown as QueryResult;
|
||||
}
|
||||
|
||||
describe("appendQueryResultSegment spatial merge", () => {
|
||||
it("keeps the first non-null SRID per column across pages", () => {
|
||||
const previous = make(2, [{ column_index: 0, srid: 4326 }]);
|
||||
const segment = make(2, [{ column_index: 0, srid: 3857 }]);
|
||||
const merged = appendQueryResultSegment(previous, segment, 100);
|
||||
expect(merged.spatial_columns).toEqual([{ column_index: 0, srid: 4326 }]);
|
||||
expect(merged.rows).toHaveLength(4);
|
||||
});
|
||||
|
||||
it("adopts the segment SRID when the previous page was unknown", () => {
|
||||
const previous = make(1, [{ column_index: 0, srid: null }]);
|
||||
const segment = make(1, [{ column_index: 0, srid: 4490 }]);
|
||||
const merged = appendQueryResultSegment(previous, segment, 100);
|
||||
expect(merged.spatial_columns).toEqual([{ column_index: 0, srid: 4490 }]);
|
||||
});
|
||||
|
||||
it("concatenates per-cell SRIDs across pages without collapsing", () => {
|
||||
const previous = make(2, [{ column_index: 0, srid: 4326 }]);
|
||||
previous.spatial_values = [[4326], [3857]];
|
||||
const segment = make(2, [{ column_index: 0, srid: null }]);
|
||||
segment.spatial_values = [[3857], [4490]];
|
||||
|
||||
const merged = appendQueryResultSegment(previous, segment, 100);
|
||||
// Every row keeps its own SRID; a later page does not overwrite earlier cells.
|
||||
expect(merged.spatial_values).toEqual([[4326], [3857], [3857], [4490]]);
|
||||
});
|
||||
});
|
||||
@@ -157,7 +157,7 @@ function exactTotalFromIncompletePage(result: QueryResult, pageLimit: number | u
|
||||
return (pageOffset ?? 0) + result.rows.length;
|
||||
}
|
||||
|
||||
function appendQueryResultSegment(previous: QueryResult, segment: QueryResult, maxRows: number): QueryResult {
|
||||
export function appendQueryResultSegment(previous: QueryResult, segment: QueryResult, maxRows: number): QueryResult {
|
||||
if (segment.execution_error) throw new Error(String(segment.rows[0]?.[0] ?? "Failed to load the next result segment"));
|
||||
if (previous.columns.length !== segment.columns.length || previous.columns.some((column, index) => column !== segment.columns[index])) {
|
||||
throw new Error("Result columns changed while loading the next segment");
|
||||
@@ -169,12 +169,25 @@ function appendQueryResultSegment(previous: QueryResult, segment: QueryResult, m
|
||||
if (existing.length !== previous.rows.length || next.length !== segment.rows.length) return undefined;
|
||||
return [...existing, ...next.slice(0, appendedRowCount)];
|
||||
};
|
||||
const sridByColumn = new Map<number, number | null>();
|
||||
for (const column of previous.spatial_columns ?? []) {
|
||||
sridByColumn.set(column.column_index, column.srid);
|
||||
}
|
||||
for (const column of segment.spatial_columns ?? []) {
|
||||
const existing = sridByColumn.get(column.column_index) ?? null;
|
||||
sridByColumn.set(column.column_index, existing ?? column.srid);
|
||||
}
|
||||
const spatial_columns = Array.from(sridByColumn.entries())
|
||||
.map(([column_index, srid]) => ({ column_index, srid }))
|
||||
.sort((a, b) => a.column_index - b.column_index);
|
||||
// Keep prior row objects intact so source-index based dirty/new/deleted state
|
||||
// remains valid, while bounding the in-memory result by the configured cap.
|
||||
return markQueryResultRowsRaw({
|
||||
...segment,
|
||||
appended_from_row_count: previous.rows.length,
|
||||
rows: [...previous.rows, ...segment.rows.slice(0, appendedRowCount)],
|
||||
spatial_columns: spatial_columns.length > 0 ? spatial_columns : undefined,
|
||||
spatial_values: appendParallelValues(previous.spatial_values, segment.spatial_values),
|
||||
mongo_documents: appendParallelValues(previous.mongo_documents, segment.mongo_documents),
|
||||
mongo_copy_documents: appendParallelValues(previous.mongo_copy_documents, segment.mongo_copy_documents),
|
||||
execution_time_ms: (previous.execution_time_ms ?? 0) + (segment.execution_time_ms ?? 0),
|
||||
|
||||
Reference in new issue
Block a user