Skip to content

fix: run_sql semicolons in strings, Paths 409, raw-SQL alert preview for SQL users - #3148

Merged
Makisuo merged 1 commit into
mainfrom
fix/run-sql-semicolons-paths-alert-preview
Nov 30, 2026
Merged

Makisuo merged 1 commit into
mainfrom
fix/run-sql-semicolons-paths-alert-preview

Conversation

@Makisuo

@Makisuo Makisuo commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Three fixes that came out of reading last week's production browser sessions and MCP calls.

run_sql: ; inside a string or comment

The warehouse SQL endpoint splits the request text on every ; before parsing, so SELECT 'a;b' or arrayStringConcat(x, '; ') failed with "Single quoted string is not closed". That was 80 failures across 5 users last week, including one agent that retried 68 times in 24s. Our own gate and statement parser already handle literals correctly.

prepareRawSql now passes the query through escapeHiddenSemicolons:

  • a ; inside a string literal becomes \x3B, which the warehouse reads back as ;;
  • a ; inside a -- or /* */ comment is replaced with a space;
  • quoted identifiers are left alone, because the gate refuses backslash escapes in them.

It uses the same lexing rules as maskLiteralsAndComments, so it handles exactly the semicolons the validator accepted as hidden. I checked the escaped form on prod with a read-only query: 'a\x3Bb' returns a;b with length 3, and a bare ; in a comment fails just as it does in a string.

Product Analytics Paths: HTTP 409 on every request

Every /internal/query-engine/product-events-paths request this week returned 502, from a warehouse 409 Unable to resolve resources in query (warehouse.config_source=org-jwt). An org-scoped token resolves a CTE only at the level that declares it, and the query read path_hops from inside the join subqueries.

The hop rows are now read as subqueries. ClickHouse re-runs a CTE body at every read anyway, so the work is unchanged, though the SQL text is longer. On prod with the org-scoped token, the old compiled SQL fails with the same 409 and the new SQL returns 38 rows. The test that pinned the CTE structure now checks that the query declares no CTE. The SQL baseline is refreshed; only the Paths entries changed.

Alert preview of a raw-SQL rule

previewRule required alerts:manage plus query:sql to preview a raw-SQL rule. Members and analysts hold query:sql and can run that same SQL in Explore, but got a 403 on preview. One user clicked Preview five times and left.

A preview now needs telemetry:read plus query:sql. Saving or testing the rule still needs alerts:manage. New API tests:

  • a role with telemetry:read and query:sql can preview but not save;
  • a role with alerts:manage but no query:sql is refused, and the warehouse is never called.

Verification

  • packages/query-engine: raw-sql, Paths, runtime and SQL baseline tests
  • packages/backend: AlertsService.rules and AlertsService.evaluation tests (50)
  • apps/api: alerts.http.test.ts preview tests
  • tsc passes for query-engine, backend, api and ai
  • oxlint -c .oxlintrc.effect.json, plain oxlint and oxfmt --check pass on the changed files

No UI changes.

🤖 Generated with Claude Code


Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

…for SQL users

- run_sql / raw SQL: the warehouse SQL endpoint splits the request on every `;`,
  so `'a;b'` or a `;` in a comment failed as an unclosed string. Escape it as
  `\x3B` in string literals and drop it from comments before sending.
- Product Analytics Paths: read the hop rows as subqueries instead of a CTE.
  An org-scoped token resolves a CTE only at its declaring level, so every
  Paths request has failed with HTTP 409 since org-scoped reads became mandatory.
- Alert preview of a raw-SQL rule now needs `query:sql` (plus `telemetry:read`),
  not `alerts:manage`: members and analysts can already run that SQL in Explore.
  Saving the rule still needs `alerts:manage`.
@coderabbitai

coderabbitai Bot commented Nov 29, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 50 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 8 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 80e07b31-a173-429d-820f-fb267543b220
Commits

Reviewing files that changed from the base of the PR and between 21edb92 and aa81f91.

Files selected for processing: 7

  • apps/api/src/routes/v2/alerts.http.test.ts
  • packages/backend/src/services/alerts/AlertsService.ts
  • packages/query-engine/src/__sql_baseline__/catalog.sql
  • packages/query-engine/src/ch/queries/product-events-paths.test.ts
  • packages/query-engine/src/ch/queries/product-events-paths.ts
  • packages/query-engine/src/runtime/raw-sql.test.ts
  • packages/query-engine/src/runtime/raw-sql.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@maple-review-bot

maple-review-bot Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Maple review

🟢 Confidence 10/10 · safe to merge. The escaping is a pure text transform pinned end to end by a prepareRawSql test, and the preview relaxation still sits behind telemetry:read plus the key scope check.
no findings · tests covered · risk high

Three independent fixes: ; inside raw-SQL strings and comments is escaped before the warehouse sees it, the Paths query drops its CTE so an org-scoped token can resolve it, and a raw-SQL rule preview now needs query:sql instead of alerts:manage. Safe to merge.

  • escapeHiddenSemicolons rewrites ; in literals/comments in prepareRawSql
  • productEventsPathsQuery inlines hop rows as subqueries, declaring no CTE
  • Raw-SQL rule preview requires query:sql; saving still requires alerts:manage
  • Two API tests pin the preview permission split

aa81f91 · What was checked, coverage and telemetry details are on the Maple / review check. Reply "won't fix" to dismiss a finding, or mention @maple-review-bot to ask about one.

@Makisuo
Makisuo merged commit 4cff5c8 into main Nov 30, 2026
49 checks passed
@Makisuo
Makisuo deleted the fix/run-sql-semicolons-paths-alert-preview branch November 30, 2026 00:02
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.

1 participant