Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
4f91839
Bulldozer DB
N2D4 Mar 24, 2026
9694c33
declareGroupByTable
N2D4 Mar 24, 2026
09ba416
Fix Prisma schema
N2D4 Mar 24, 2026
4232a94
declareMapTable
N2D4 Mar 24, 2026
31b6ac6
Performance tests
N2D4 Mar 24, 2026
2f7f09a
Load tests
N2D4 Mar 24, 2026
863ee05
Interface updates
N2D4 Mar 24, 2026
109cf5d
Bulldozer Studio
N2D4 Mar 25, 2026
53f7302
Remove unnecessary table
N2D4 Mar 25, 2026
006cf5e
Add flat map interface
N2D4 Mar 25, 2026
4cdc057
FlatMap table
N2D4 Mar 25, 2026
49dc922
Flat map fuzz tests
N2D4 Mar 25, 2026
3eccc22
Build MapTable from FlatMapTable
N2D4 Mar 25, 2026
e041e7d
Filter tables
N2D4 Mar 25, 2026
e2b0d3d
Limit tables
N2D4 Mar 25, 2026
f7f21aa
Speed up fuzzing
N2D4 Mar 26, 2026
90f61ec
Concat table
N2D4 Mar 26, 2026
69b3d4f
Bulldozer Studio: Better node placing algorithm
N2D4 Mar 26, 2026
e306770
Add left-join table
N2D4 Mar 26, 2026
5036c2a
Sort table
N2D4 Mar 27, 2026
d55470b
LFold table
N2D4 Mar 27, 2026
43d7304
Left join table
N2D4 Mar 27, 2026
a33faa0
Improve left join performance
N2D4 Mar 27, 2026
aaeb7d1
Improved performance for most tables
N2D4 Mar 27, 2026
ef9915a
Refactor Bulldozer into individual files
N2D4 Mar 27, 2026
037d20f
Merge branch 'dev' into bulldozer-db
N2D4 Mar 27, 2026
2403c17
Update apps/backend/src/lib/bulldozer/db/tables/group-by-table.ts
N2D4 Mar 27, 2026
d3a2daa
Performance improvements
N2D4 Mar 28, 2026
b459240
PR comments
N2D4 Mar 29, 2026
c01d931
Some more perf changes
N2D4 Mar 30, 2026
2d49d23
Fix various comparison key bugs
N2D4 Mar 31, 2026
2bb89c2
Performance improvements
N2D4 Mar 31, 2026
199cdf2
Lint fixes
N2D4 Apr 1, 2026
1dc76dc
Merge remote-tracking branch 'origin/dev' into bulldozer-db
N2D4 Apr 1, 2026
e4a5221
More fixes...
N2D4 Apr 3, 2026
e353232
Various changes
N2D4 Apr 6, 2026
4b400e2
Comments from Aman
N2D4 Apr 8, 2026
5b69a18
Fix tests
N2D4 Apr 8, 2026
a7f999f
Improve perf tests
N2D4 Apr 10, 2026
19abfb3
TimeFold table
N2D4 Apr 11, 2026
9cb5f5b
Clean up Prisma schema
N2D4 Apr 11, 2026
e3c3865
Make migration warnings into errors
N2D4 Apr 13, 2026
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
Prev Previous commit
Next Next commit
Fix various comparison key bugs
  • Loading branch information
N2D4 committed Mar 31, 2026
commit 2d49d2308fc877b18c5a90f2381ecb2a420cd7d2
68 changes: 51 additions & 17 deletions apps/backend/src/lib/bulldozer/db/bulldozer-sort-helpers-sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,13 +570,17 @@ export const BULLDOZER_SORT_HELPERS_SQL = deindent`
END;
$$;

CREATE OR REPLACE FUNCTION pg_temp.bulldozer_sort_bulk_init_from_table(groups_path jsonb[], source_table_name text)
CREATE OR REPLACE FUNCTION pg_temp.bulldozer_sort_bulk_init_from_table(groups_path jsonb[], source_table_name text, compare_sort_keys_sql text)
RETURNS text LANGUAGE plpgsql AS $$
DECLARE
current_group_key jsonb;
ordered_rows jsonb[];
root_row_identifier text;
row_count integer;
is_order_compatible boolean;
current_index integer;
cmp integer;
current_row jsonb;
BEGIN
FOR current_group_key IN EXECUTE format('SELECT DISTINCT "groupKey" FROM %I', source_table_name)
LOOP
Expand All @@ -593,22 +597,52 @@ export const BULLDOZER_SORT_HELPERS_SQL = deindent`
CONTINUE;
END IF;

root_row_identifier := pg_temp.bulldozer_sort_build_balanced_group(
groups_path,
current_group_key,
ordered_rows,
1,
row_count,
1
);
PERFORM pg_temp.bulldozer_sort_put_group_metadata(
groups_path,
current_group_key,
root_row_identifier,
ordered_rows[1]->>'rowIdentifier',
ordered_rows[row_count]->>'rowIdentifier',
row_count
);
is_order_compatible := TRUE;
FOR current_index IN 2..row_count
LOOP
cmp := pg_temp.bulldozer_sort_compare_row_keys(
compare_sort_keys_sql,
ordered_rows[current_index - 1]->'rowSortKey',
ordered_rows[current_index - 1]->>'rowIdentifier',
ordered_rows[current_index]->'rowSortKey',
ordered_rows[current_index]->>'rowIdentifier'
);
IF cmp > 0 THEN
is_order_compatible := FALSE;
EXIT;
END IF;
END LOOP;

IF is_order_compatible THEN
root_row_identifier := pg_temp.bulldozer_sort_build_balanced_group(
groups_path,
current_group_key,
ordered_rows,
1,
row_count,
1
);
PERFORM pg_temp.bulldozer_sort_put_group_metadata(
groups_path,
current_group_key,
root_row_identifier,
ordered_rows[1]->>'rowIdentifier',
ordered_rows[row_count]->>'rowIdentifier',
row_count
);
ELSE
FOREACH current_row IN ARRAY ordered_rows
LOOP
PERFORM pg_temp.bulldozer_sort_insert(
groups_path,
current_group_key,
compare_sort_keys_sql,
current_row->>'rowIdentifier',
current_row->'rowSortKey',
current_row->'rowData'
);
END LOOP;
END IF;
END LOOP;

RETURN source_table_name;
Expand Down
25 changes: 14 additions & 11 deletions apps/backend/src/lib/bulldozer/db/index.perf.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -847,18 +847,21 @@ describe.sequential("bulldozer db performance (real postgres)", () => {
expect(filteredDeltaRows.map((row) => ({ rowIdentifier: row.rowidentifier, rowData: row.rowdata }))).toEqual([
{ rowIdentifier: "seed-100000:1", rowData: { team: "delta", value: 999 } },
]);
const groupedSubsetSql = `
SELECT *
FROM (${toQueryableSqlQuery(groupedByTeam.listRowsInGroup({
groupKey: expr(`to_jsonb('beta'::text)`),
start: "start",
end: "end",
startInclusive: true,
endInclusive: true,
}))}) AS "rows"
LIMIT ${LOAD_SUBSET_ITERATION_ROW_COUNT}
`;
// Warm once so we measure steady-state subset iteration instead of first-touch planner/cache cost.
await sql.unsafe(groupedSubsetSql);
const groupedSubsetFromStart = await measureMs(`load iterate groupedByTeam subset from start (${LOAD_SUBSET_ITERATION_ROW_COUNT} rows)`, async () => {
return await sql.unsafe(`
SELECT *
FROM (${toQueryableSqlQuery(groupedByTeam.listRowsInGroup({
groupKey: expr(`to_jsonb('beta'::text)`),
start: "start",
end: "end",
startInclusive: true,
endInclusive: true,
}))}) AS "rows"
LIMIT ${LOAD_SUBSET_ITERATION_ROW_COUNT}
`);
return await sql.unsafe(groupedSubsetSql);
});
expect(groupedSubsetFromStart.elapsedMs).toBeLessThanOrEqual(LOAD_SUBSET_ITERATION_MAX_MS);
expect(groupedSubsetFromStart.result).toHaveLength(LOAD_SUBSET_ITERATION_ROW_COUNT);
Expand Down
6 changes: 3 additions & 3 deletions apps/backend/src/lib/bulldozer/db/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2882,7 +2882,7 @@ describe.sequential("declareStoredTable (real postgres)", () => {
]);
});

it.fails("known issue: sortTable bulk init should respect descending comparator", async () => {
test("sortTable bulk init respects descending comparator", async () => {
const { fromTable, groupedTable, sortedTable } = createDescendingSortedTable();
await runStatements(fromTable.init());
await runStatements(fromTable.setRow("a1", expr(`'{"team":"alpha","value":1}'::jsonb`)));
Expand All @@ -2901,7 +2901,7 @@ describe.sequential("declareStoredTable (real postgres)", () => {
expect(alphaRows.map((row) => row.rowidentifier)).toEqual(["a3", "a2", "a1"]);
});

it.fails("known issue: limitTable should honor source comparator for top-N", async () => {
test("limitTable honors source comparator for top-N", async () => {
const { fromTable, groupedTable, sortedTable, limitedTable } = createDescendingLimitedTable();
await runStatements(fromTable.init());
await runStatements(fromTable.setRow("a1", expr(`'{"team":"alpha","value":1}'::jsonb`)));
Expand All @@ -2921,7 +2921,7 @@ describe.sequential("declareStoredTable (real postgres)", () => {
expect(alphaRows.map((row) => row.rowidentifier)).toEqual(["a3", "a2"]);
});

it.fails("known issue: lFoldTable read order should match source comparator", async () => {
test("lFoldTable read order matches source comparator", async () => {
const { fromTable, groupedTable, sortedTable, lFoldTable } = createDescendingLFoldTable();
await runStatements(fromTable.init());
await runStatements(fromTable.setRow("a1", expr(`'{"team":"alpha","value":1}'::jsonb`)));
Expand Down
6 changes: 3 additions & 3 deletions apps/backend/src/lib/bulldozer/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ export function toQueryableSqlQuery(query: SqlQuery): string {
}
export function toExecutableSqlStatements(statements: SqlStatement[]): string {
const requiresSortHelpers = statements.some((statement) => statement.sql.includes("pg_temp.bulldozer_sort_"));
const requiresSortSequentialExecutor = requiresSortHelpers;
if (!requiresSortSequentialExecutor) {
const requiresSequentialExecutor = requiresSortHelpers || statements.some((statement) => statement.requiresSequentialExecution === true);
if (!requiresSequentialExecutor) {
return deindent`
WITH __dummy_statement_1__ AS (SELECT 1),
${statements.map(statement => deindent`
Expand All @@ -80,7 +80,7 @@ export function toExecutableSqlStatements(statements: SqlStatement[]): string {
`;
}).join("\n\n");
return deindent`
${BULLDOZER_SORT_HELPERS_SQL}
${requiresSortHelpers ? BULLDOZER_SORT_HELPERS_SQL : ""}

${executableStatements}
`;
Expand Down
Loading
Loading