Skip to content

Navigation Menu

Sign in
Sign up

Remove enum declaration-order SQL rewriting - #30223

Open
StevenMcClankerton wants to merge 1 commit into
main from
remove-order-by-decl
Open

Remove enum declaration-order SQL rewriting #30223
StevenMcClankerton wants to merge 1 commit into
main from
remove-order-by-decl

Conversation

@StevenMcClankerton

@StevenMcClankerton StevenMcClankerton commented Sep 7, 2026
edited by coderabbitai Bot
Loading

Copy link
Copy Markdown
Contributor

Linked issue

n/a — small change; no ticket.

At a glance

ORDER BY "Task"."priority" ASC, "Task"."id" ASC

For a text-backed enum declared as low, high, medium, this now sorts by stored value: high, low, medium, rather than wrapping the column in array_position(...).

Summary

Enum metadata should not silently change SQL ordering. Remove the declaration-order rewrite and let the database evaluate the original expressions.

Decision

Render ORDER BY and DISTINCT ON expressions normally, without consulting enum value sets.

Notes for the reviewer

This changes text-backed enum ordering. PostgreSQL native enum types retain PostgreSQL's own declaration-order semantics: this PR introduces no casts or replacement ordering policy. Schema, codecs, and enum member declarations are unchanged.

How it fits together

  1. Use the regular expression renderer for ordering.
  2. Do the same for DISTINCT ON, preserving its expression match with ORDER BY.
  3. Remove the now-unused table-source lookup and enum-order helpers.

Behavior changes & evidence

  • Qualified and unqualified enum columns sort by stored value, including normal ASC NULL placement; DISTINCT ON keeps the original expression. Implementation: sql-renderer.ts. SQL and PGlite execution evidence: order-by-enum.integration.test.ts.

Testing performed

  • PostgreSQL adapter suite: 867 passed, 3 expected failures, 1 skipped.
  • Enum ordering integration suite rerun after commit formatting: 7 passed.
  • PostgreSQL adapter typecheck: passed.
  • Biome checks, focused dependency lint, and git diff --check: passed.

Skill update

No skill update required: query authoring APIs are unchanged, and the adapter now follows the database's ordinary ordering semantics rather than introducing an enum-specific convention.

Checklist

  • All commits are DCO signed off.
  • I read CONTRIBUTING.md; this change is scoped to one logical concern.
  • Tests are updated.
  • The PR title uses a Linear ticket prefix — not applicable; no ticket.
  • The Skill update section is filled in.

Alternatives considered

  • Keep declaration-order emulation: rejected because enum metadata should not override the requested SQL expression.
  • Cast native PostgreSQL enums to text: not chosen because the requested behavior is to leave ORDER BY unchanged, not impose another sorting policy.

Summary by CodeRabbit

  • Bug Fixes
    • PostgreSQL enum-backed columns now sort by their stored text values rather than enum declaration order.
    • ORDER BY and DISTINCT ON queries retain straightforward column expressions, including when handling NULL values.

Signed-off-by: Steven McClankerton <tatarintsev@prisma.io>

coderabbitai Bot commented Sep 7, 2026
edited
Loading

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

i️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Team

Run ID: 4abb0b71-0f3f-4aed-a2fc-b8da4036d420

📥 Commits

Reviewing files that changed from the base of the PR and between 534e04f and 143b1a5.

📒 Files selected for processing (2)
  • packages/3-targets/6-adapters/postgres/src/core/sql-renderer.ts
  • packages/3-targets/6-adapters/postgres/test/migrations/order-by-enum.integration.test.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

The Postgres SQL renderer no longer rewrites enum-backed ORDER BY or DISTINCT ON expressions with array_position. Integration tests now expect direct column references and value-based ordering, including NULL and DISTINCT ON cases.

Changes

Postgres enum ordering

Layer / File(s) Summary
Remove enum ordering rewrites
packages/3-targets/6-adapters/postgres/src/core/sql-renderer.ts
ORDER BY and DISTINCT ON expressions now use renderExpr. The enum-specific source mapping, value resolution, and array_position rendering logic were removed.
Update enum ordering integration tests
packages/3-targets/6-adapters/postgres/test/migrations/order-by-enum.integration.test.ts
Tests now assert direct SQL expressions, value ordering, NULLS LAST behavior, and matching direct expressions for DISTINCT ON and ORDER BY.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to 143b1

PostgreSQL text-backed enums now sort by stored value without injected SQL rewriting, while native enum ordering remains database-managed. The updated behavior and integration expectations are aligned, with no remaining concrete merge-blocking risk.

Suggested reviewers: wmadden-electric

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: removing enum declaration-order SQL rewriting from the PostgreSQL adapter.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch remove-order-by-decl

Comment @coderabbitai help to get the list of available commands.

pkg-pr-new Bot commented Sep 7, 2026

Copy link
Copy Markdown

Open in StackBlitz

@prisma/orm-extension-arktype-json
npm i https://pkg.pr.new/@prisma/orm-extension-arktype-json@30223
@prisma/orm-extension-middleware-cache
npm i https://pkg.pr.new/@prisma/orm-extension-middleware-cache@30223
@prisma/orm-extension-paradedb
npm i https://pkg.pr.new/@prisma/orm-extension-paradedb@30223
@prisma/orm-extension-pgvector
npm i https://pkg.pr.new/@prisma/orm-extension-pgvector@30223
@prisma/orm-extension-postgis
npm i https://pkg.pr.new/@prisma/orm-extension-postgis@30223
@prisma/orm-extension-supabase
npm i https://pkg.pr.new/@prisma/orm-extension-supabase@30223
@prisma/orm-family-mongo
npm i https://pkg.pr.new/@prisma/orm-family-mongo@30223
@prisma/orm-family-sql
npm i https://pkg.pr.new/@prisma/orm-family-sql@30223
@prisma/orm-framework
npm i https://pkg.pr.new/@prisma/orm-framework@30223
@prisma/orm-mongo
npm i https://pkg.pr.new/@prisma/orm-mongo@30223
@prisma/orm-postgres
npm i https://pkg.pr.new/@prisma/orm-postgres@30223
@prisma/orm-sqlite
npm i https://pkg.pr.new/@prisma/orm-sqlite@30223
@prisma/orm-target-mongo
npm i https://pkg.pr.new/@prisma/orm-target-mongo@30223
@prisma/orm-target-postgres
npm i https://pkg.pr.new/@prisma/orm-target-postgres@30223
@prisma/orm-target-sqlite
npm i https://pkg.pr.new/@prisma/orm-target-sqlite@30223
@prisma/orm-toolchain
npm i https://pkg.pr.new/@prisma/orm-toolchain@30223

commit: 143b1a5

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size
postgres / no-emit 174.55 KB (-0.21% 🔽)
postgres / emit 151.75 KB (-0.24% 🔽)
mongo / no-emit 101.09 KB (0%)
mongo / emit 90.95 KB (0%)
cf-worker / no-emit 198.38 KB (-0.26% 🔽)
cf-worker / emit 172.9 KB (-0.3% 🔽)

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

Reviewers

@SevInf SevInf SevInf approved these changes

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /