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
40 changes: 40 additions & 0 deletions src/sql/recent-query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,43 @@ test("analyze sets isSelectQuery=false for DELETE with EXISTS subquery", async (
const rq = await RecentQuery.analyze(data, testHash, 1000);
expect(rq.isSelectQuery).toBe(false);
});

// --- displayQuery via analyze ---

test("analyze populates displayQuery for wide SELECTs", async () => {
const data = makeRawQuery({
query:
'SELECT "u"."id", "u"."email", "u"."first_name", "u"."last_name", "u"."created_at", "u"."updated_at", "u"."stripe_customer_id" FROM "users" "u" WHERE "u"."id" = $1',
});
const rq = await RecentQuery.analyze(data, testHash, 1000);
// Normalize whitespace because the analyzer prettier-formats the query
// before compacting; the site applies the same normalization on render.
const normalized = rq.displayQuery?.replace(/\s+/g, " ").trim();
expect(normalized).toBe(
`SELECT ... FROM "users" "u" WHERE "u"."id" = $1;`,
);
});

test("analyze leaves displayQuery undefined for narrow SELECTs", async () => {
const data = makeRawQuery({ query: "SELECT id FROM users WHERE id = $1" });
const rq = await RecentQuery.analyze(data, testHash, 1000);
expect(rq.displayQuery).toBeUndefined();
});

test("analyze leaves displayQuery undefined for non-SELECTs", async () => {
const data = makeRawQuery({
query:
"INSERT INTO archive SELECT a, b, c, d, e, f, g, h FROM users WHERE active = false",
});
const rq = await RecentQuery.analyze(data, testHash, 1000);
expect(rq.displayQuery).toBeUndefined();
});

test("analyze leaves displayQuery undefined for UNION", async () => {
const data = makeRawQuery({
query:
"SELECT a, b, c, d, e, f, g FROM t UNION SELECT a, b, c, d, e, f, g FROM u",
});
const rq = await RecentQuery.analyze(data, testHash, 1000);
expect(rq.displayQuery).toBeUndefined();
});
22 changes: 21 additions & 1 deletion src/sql/recent-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import prettierPluginSql from "prettier-plugin-sql";
import type { SegmentedQueryCache } from "../sync/seen-cache.ts";
import {
Analyzer,
compactSelectList,
DiscoveredColumnReference,
Nudge,
PostgresQueryBuilder,
Expand All @@ -13,6 +14,7 @@ import {
} from "@query-doctor/core";
import { parse } from "@libpg-query/parser";
import z from "zod";
import { log } from "../log.ts";
import type { LiveQueryOptimization } from "../remote/optimization.ts";

/**
Expand All @@ -24,6 +26,7 @@ export class RecentQuery {
private static rewriter = new PssRewriter();

readonly formattedQuery: string;
readonly displayQuery?: string;
readonly username: string;
readonly query: string;
readonly meanTime: number;
Expand Down Expand Up @@ -52,6 +55,7 @@ export class RecentQuery {
this.username = data.username;
this.query = data.query;
this.formattedQuery = data.formattedQuery;
this.displayQuery = data.displayQuery;
this.meanTime = data.meanTime;
this.calls = data.calls;
this.rows = data.rows;
Expand Down Expand Up @@ -119,8 +123,9 @@ export class RecentQuery {
);
const analysis = await analyzer.analyze(formattedQuery);
const query = this.rewriteQuery(analysis.queryWithoutTags);
const displayQuery = await RecentQuery.computeDisplayQuery(query);
return new RecentQuery(
{ ...data, query, formattedQuery },
{ ...data, query, formattedQuery, displayQuery },
analysis.referencedTables,
analysis.indexesToCheck,
analysis.tags,
Expand Down Expand Up @@ -152,6 +157,20 @@ export class RecentQuery {
}
}

private static async computeDisplayQuery(
query: string,
): Promise<string | undefined> {
try {
return compactSelectList(query, await parse(query));
} catch (error) {
log.debug(
`displayQuery: parse failed (${(error as Error).message})`,
"display-query",
);
return undefined;
}
}

static isSelectQuery(data: RawRecentQuery): boolean {
return /^\s*select/i.test(data.query);
}
Expand Down Expand Up @@ -180,6 +199,7 @@ export type RawRecentQuery = {
username: string;
query: string;
formattedQuery: string;
displayQuery?: string;
meanTime: number;
calls: string;
rows: string;
Expand Down
Loading