Skip to content

feat(sql): emit compact displayQuery preview for wide SELECTs#127

Open
veksen wants to merge 1 commit intomainfrom
veksen/display-query
Open

feat(sql): emit compact displayQuery preview for wide SELECTs#127
veksen wants to merge 1 commit intomainfrom
veksen/display-query

Conversation

@veksen
Copy link
Copy Markdown
Member

@veksen veksen commented Apr 21, 2026

Summary

  • Add computeDisplayQuery() that parses with libpg_query and scans tokens with paren-depth tracking to splice the top-level SELECT target list out of the original query string, yielding previews like SELECT ... FROM users WHERE id = \$1.
  • Expose displayQuery?: string on RecentQuery (populated in analyze() next to formattedQuery) so it flows through the existing WS queryProcessed frame and HTTP payloads automatically. Skips non-SELECT, UNION/INTERSECT/EXCEPT, targetless SELECTs, short lists (≤2 targets AND ≤40 chars), and parse failures — site falls back to query when undefined.
  • Byte-aware splice (TextEncoder/TextDecoder) so libpg_query's UTF-8 offsets stay correct for queries with multi-byte characters before FROM.

Site side: Query-Doctor/Site#2800

Test plan

  • npx vitest run src/sql/display-query.test.ts src/sql/recent-query.test.ts — 52 tests, covering below-threshold, wide SELECT, subquery/CTE both directions, COUNT(*), DISTINCT, line comment in target list, UNION/INTERSECT/EXCEPT, trailing ;, unparseable, non-SELECT, 'FROM' string literal, TRIM(FROM …), lowercase keywords, multi-byte splice
  • npx vitest run full suite — 195 tests pass
  • npm run typecheck clean

🤖 Generated with Claude Code

Splices the top-level SELECT target list out of the query string using
libpg_query AST offsets + token scan for the terminating FROM keyword, so
the site can render "SELECT ... FROM users WHERE id = $1" instead of a
dozen columns crowding a line-clamp cell. Skips non-SELECT, set-ops, short
lists (≤2 targets and ≤40 chars), and parse failures — the site falls
back to query when undefined.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Query Doctor Analysis

View full run details

2 queries analyzed

2 pre-existing issues

Using assumed statistics (10000000 rows/table). For better results, sync production stats.

@Xetera
Copy link
Copy Markdown
Collaborator

Xetera commented Apr 22, 2026

I think this code has to be part of core to work with pglite too. I'm also concerned about adding yet another big string representation copy into an already large response that could be derived from the raw query

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants