Skip to content

feat(nosql): diagnose ORDER BY inside a subquery#3148

Open
bk201- wants to merge 1 commit into
mainfrom
feat/nosql-order-by-in-subquery-diagnostic
Open

feat(nosql): diagnose ORDER BY inside a subquery#3148
bk201- wants to merge 1 commit into
mainfrom
feat/nosql-order-by-in-subquery-diagnostic

Conversation

@bk201-

@bk201- bk201- commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Summary

Azure Cosmos DB NoSQL does not support ORDER BY inside a subquery — FIRST(…),
LAST(…), ARRAY(…), EXISTS(…), scalar (SELECT …), and FROM (SELECT …). The
backend rejects such queries at execution time with HTTP 400, even though:

  • the native sql.y grammar accepts them (every subquery embeds a full sql_query,
    which carries an optional ORDER BY), and
  • the language reference does not document the restriction.

Because it is a semantic (engine) rule rather than a grammatical one, the parser can't
catch it — so this PR adds a static diagnostic instead, surfacing the error in the editor
before the user runs the query.

Confirmed against production Cosmos DB: every subquery form with ORDER BY returns 400,
while the same subqueries without ORDER BY (and a top-level ORDER BY) work.

Refs Azure/azure-cosmos-db-emulator-docker#311 (originally mis-reported as "FIRST()
unsupported"; the real discriminator is the nested ORDER BY).

Changes

  • New detector src/diagnostics/orderByInSubquery.ts — AST walk that flags an
    ORDER BY on any non-root query. Takes the already-parsed AST (no re-parse, no import
    cycle); reports on the ORDER BY clause range.
  • Wired into SqlLanguageService.getDiagnostics (single- and multi-query) as
    ORDER_BY_IN_SUBQUERY, severity Error. Exported from the package entry point.
  • Tests src/diagnostics/orderByInSubquery.test.ts (15) — FIRST/LAST/ARRAY/EXISTS/
    (SELECT …)/FROM (…), nested and multiple occurrences, negative cases, and service
    integration (single + multi-query offsets).
  • Docs/fixtures — note in the ORDER_BY keyword doc; SQ-05/SQ-06 reclassified in
    test-suite.md from "emulator-only known limitation" to a Cosmos DB language
    limitation, with the diagnostic referenced.

Validation

  • npm run build (tsc) — clean.
  • npm run lint — oxlint + eslint, 0 errors.
  • npm run prettier-fix — applied.
  • Package tests: 531 passed, 1 skipped; new detector 15/15.

No user-facing string changes outside the diagnostic message.

Azure Cosmos DB rejects ORDER BY inside any subquery (FIRST/LAST/ARRAY/EXISTS/(SELECT ...)/FROM (...)) with HTTP 400, even though the grammar accepts it and the docs do not mention the restriction. Add an AST-based ORDER_BY_IN_SUBQUERY diagnostic (severity Error) wired into single- and multi-query diagnostics, plus unit tests, a keyword-doc note, and reclassify the SQ-05/SQ-06 fixtures from emulator-only to a Cosmos DB language limitation. Refs Azure/azure-cosmos-db-emulator-docker#311.
@bk201- bk201- requested a review from a team as a code owner June 11, 2026 16:35
@github-actions

Copy link
Copy Markdown
Contributor

🔬 NoSQL language-service integration

Commit: c450cb6
Pull Request: #3148 feat(nosql): diagnose ORDER BY inside a subquery

🧪 Result

  • NoSQL integration tests: ✅ success

📥 Artifacts (run)

@github-actions

Copy link
Copy Markdown
Contributor

🔨 Build, Lint & Test

🔗 Source

📦 Package Information

🧪 Test Results

  • Unit Tests: ✅ success
  • Integration Tests (extension host): ✅ success

📥 Artifacts (run)

✅ Build Status

Build and local tests passed. See sibling comments below for E2E and NoSQL integration results.

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