Skip to content

fix(core): compare plugin-storage numeric filters/order numerically on Postgres - #2152

Merged
ascorbic merged 2 commits into
emdash-cms:mainfrom
vedanshujain:fix/plugin-storage-postgres-numeric
Jul 21, 2026
Merged

ascorbic merged 2 commits into
emdash-cms:mainfrom
vedanshujain:fix/plugin-storage-postgres-numeric

Conversation

@vedanshujain

@vedanshujain vedanshujain commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes the remaining Postgres numeric bug in plugin storage. _plugin_storage.data is a text column. #1898 (fixing #920) added the (data)::jsonb->>'field' cast, which resolved the text ->> unknown parse error and boolean/= 1 filters — but the extracted value is still text, so numeric guards compared lexically:

'9' >= '10'   →  TRUE (lexical)     9 >= 10   →  false (numeric)

Consequences on Postgres (green on SQLite, whose json_extract is typed):

  • query({ where: { stock: { gte: 10 } } }) over 9 / 10 / 100 matched 9 too — a silent over-count / oversell.
  • count({ stock: { gte: 10 } }) returned 3 instead of 2.
  • orderBy({ stock: 'asc' }) returned [10, 100, 9] instead of [9, 10, 100].

The fix

  • pluginDataExtractExpr(field, { numeric }) — adds an optional type-guarded ::numeric cast (guarded by jsonb_typeof / json_type). Not a bare ::numeric: a bare cast throws invalid input syntax for type numeric the moment one scanned row stores a non-number in that field (documents are schemaless). Guarded, a non-number yields NULL (no match) — total and parity-correct with SQLite.
  • pluginDataOrderExpr(field) — orders over the jsonb-native value (->), whose btree ordering is numeric among numbers and total across heterogeneous data.
  • buildCondition chooses numeric-vs-text per operand from its JS type; orderBy uses the jsonb-native expression. SQLite is unchanged.

The index path (text ->>, already fixed by #1898) is intentionally untouched — see the note in generateCreateIndexSql.

Tests

Adds a dialect-parametric regression suite (storage-postgres-numeric-regression.test.ts) and updates the buildCondition unit assertions to the type-guarded SQL. Full run on both dialects:

EMDASH_TEST_PG=postgres://… pnpm --filter emdash exec vitest run tests/unit/plugins tests/integration/plugins
# storage-query (unit) + storage / storage-indexes / storage-dialect / numeric-regression (integration): all green

Related: #2150 (failing-test companion), #1898, #920.

Type of change

  • Bug fix
  • Feature (requires maintainer-approved Discussion)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

  • I have read CONTRIBUTING.md
  • pnpm typecheck passes
  • pnpm lint passes
  • pnpm test passes (or targeted tests for my change) — plugin-storage unit + integration suites, both dialects
  • pnpm format has been run
  • I have added/updated tests for my changes (if applicable)
  • User-visible strings in the admin UI are wrapped for translation (if applicable) — N/A
  • I have added a changeset (if this PR changes a published package)
  • New features link to an approved Discussion — N/A, bug fix

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: Claude Opus 4.8 (Claude Code)

Screenshots / test output

tests/integration/plugins/storage-postgres-numeric-regression.test.ts  Tests  8 passed (8)   # [sqlite] ×4 + [postgres] ×4
tests/unit/plugins/storage-query.test.ts                                Tests  46 passed

vedanshujain and others added 2 commits July 20, 2026 16:58
… Postgres

`_plugin_storage.data` is a `text` column. emdash-cms#1898 added the `(data)::jsonb->>'…'`
cast so extraction no longer errors and boolean/equality filters work, but `->>`
still yields text and the query/count/order paths compare it directly. On
Postgres that means numeric guards compare lexically — `'9' >= '10'` is TRUE —
so a `{ stock: { gte: 10 } }` filter over 9/10/100 silently includes 9
(over-count / oversell) and numeric orderBy returns [10, 100, 9].

Adds a dialect-parametric suite (describeEachDialect) pinning the remaining
failure modes. SQLite's json_extract is typed, so it stays green; the Postgres
dialect fails on the range/count/order cases on current main. Postgres runs only
when EMDASH_TEST_PG is set.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01TX9YciGFRZX9aF2UcQ6rUW
…cally on Postgres

`_plugin_storage.data` is a `text` column. After the `(data)::jsonb->>'field'`
cast (emdash-cms#1898) the extracted value is still text, so numeric guards compared
lexically on Postgres — `'9' >= '10'` is TRUE — and `{ stock: { gte: 10 } }`
over 9/10/100 matched 9 (silent over-count / oversell), while numeric `orderBy`
sorted `[10, 100, 9]`.

- Add `pluginDataExtractExpr` with an optional **type-guarded** `::numeric` cast
  (guarded by `jsonb_typeof`/`json_type`), so numeric comparisons are numeric and
  total: a non-number stored value yields NULL (no match) instead of aborting the
  query with `invalid input syntax for type numeric`.
- Add `pluginDataOrderExpr` that orders over the jsonb-native value (`->`), whose
  btree ordering is numeric among numbers and total across heterogeneous data.
- `buildCondition` picks numeric-vs-text per operand from its JS type; `orderBy`
  uses the jsonb-native expression. SQLite is unchanged (`json_extract` is typed).

Builds on emdash-cms#1898 (jsonb cast + boolean/`= 1` fix); this is the remaining numeric
piece. Regression suite runs on both dialects (Postgres via EMDASH_TEST_PG).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01TX9YciGFRZX9aF2UcQ6rUW
@changeset-bot

changeset-bot Bot commented Jul 20, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 61a819c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 16 packages
Name Type
emdash Patch
@emdash-cms/cloudflare Patch
@emdash-cms/sandbox-workerd Patch
@emdash-cms/fixture-perf-site Patch
@emdash-cms/perf-demo-site Patch
@emdash-cms/cache-demo-site Patch
@emdash-cms/do-demo-site Patch
@emdash-cms/do-solo-demo-site Patch
@emdash-cms/admin Patch
@emdash-cms/auth Patch
@emdash-cms/blocks Patch
@emdash-cms/gutenberg-to-portable-text Patch
@emdash-cms/x402 Patch
create-emdash Patch
@emdash-cms/auth-atproto Patch
@emdash-cms/plugin-embeds Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is the right fix for the right problem. The Postgres plugin-storage numeric comparison/ordering bug is real and silently dangerous (over-count/oversell on gte/lte, wrong orderBy). The chosen approach is sound:

  • It infers numeric-vs-text per condition from the JS type of the bound operand, which is the only signal available.
  • It uses a type-guarded cast on Postgres (jsonb_typeof(...) = 'number' → ::numeric) so schemaless documents with non-numeric values don’t abort the query, matching SQLite’s tolerant behavior.
  • It orders by the jsonb-native value (->) on Postgres, which sorts numbers numerically and stays total across mixed types.
  • It validates the interpolated field name before building SQL, consistent with AGENTS.md and existing helpers.
  • It adds a dialect-parametric integration regression test and updates unit assertions to the new type-guarded SQLite output.

I checked the changed files in database/dialect-helpers.ts, database/repositories/plugin-storage.ts, plugins/storage-query.ts, the new storage-postgres-numeric-regression.test.ts, and the updated storage-query.test.ts. I also traced consumers (PluginStorageRepository.query/count, storage-indexes uses the untouched jsonExtractExpr, buildOrderByClause remains an exported helper not used by the repository). I did not find a logic bug, regression, SQL-injection surface, missing locale concern (plugin storage is not content-localized), or AGENTS.md violation.

One minority note: the PR description says “SQLite is unchanged,” but numeric conditions on SQLite now also emit the CASE WHEN json_type(...) IN ('integer', 'real') THEN json_extract(...) guard. For documents that store a number-looking value as a JSON string, old SQLite equality/range queries would match via SQLite’s cross-type comparison, whereas the new query will not. This is parity-correct with Postgres and arguably the correct behavior for a numeric filter, but it is a subtle behavior change on SQLite for non-conforming mixed-type data. This is not a blocker.

The changeset is present and well-written. I could not run pnpm test/typecheck/lint (no shell/tooling), so I’m taking the author’s report of green plugin-storage unit + integration runs on both dialects as unverified but plausible given the static shape of the diff.

LGTM.

@github-actions github-actions Bot added review/approved Approved; no new commits since and removed review/needs-review No maintainer or bot review yet labels Jul 20, 2026

@ascorbic ascorbic left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks

@pkg-pr-new

pkg-pr-new Bot commented Jul 21, 2026

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/admin@2152

@emdash-cms/auth

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/auth@2152

@emdash-cms/auth-atproto

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/auth-atproto@2152

@emdash-cms/blocks

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/blocks@2152

@emdash-cms/cloudflare

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/cloudflare@2152

@emdash-cms/contentful-to-portable-text

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/contentful-to-portable-text@2152

emdash

npm i https://pkg.pr.new/emdash-cms/emdash@2152

create-emdash

npm i https://pkg.pr.new/emdash-cms/emdash/create-emdash@2152

@emdash-cms/gutenberg-to-portable-text

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/gutenberg-to-portable-text@2152

@emdash-cms/plugin-cli

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/plugin-cli@2152

@emdash-cms/plugin-types

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/plugin-types@2152

@emdash-cms/registry-client

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/registry-client@2152

@emdash-cms/registry-lexicons

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/registry-lexicons@2152

@emdash-cms/registry-verification

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/registry-verification@2152

@emdash-cms/sandbox-workerd

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/sandbox-workerd@2152

@emdash-cms/x402

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/x402@2152

@emdash-cms/plugin-ai-moderation

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/plugin-ai-moderation@2152

@emdash-cms/plugin-atproto

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/plugin-atproto@2152

@emdash-cms/plugin-audit-log

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/plugin-audit-log@2152

@emdash-cms/plugin-color

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/plugin-color@2152

@emdash-cms/plugin-embeds

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/plugin-embeds@2152

@emdash-cms/plugin-field-kit

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/plugin-field-kit@2152

@emdash-cms/plugin-forms

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/plugin-forms@2152

@emdash-cms/plugin-webhook-notifier

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/plugin-webhook-notifier@2152

commit: 61a819c

@ascorbic
ascorbic merged commit b22a3d5 into emdash-cms:main Jul 21, 2026
53 checks passed
swissky pushed a commit to swissky/emdash that referenced this pull request Jul 21, 2026
…n Postgres (emdash-cms#2152)

* test(core): plugin-storage numeric filters/order compare lexically on Postgres

`_plugin_storage.data` is a `text` column. emdash-cms#1898 added the `(data)::jsonb->>'…'`
cast so extraction no longer errors and boolean/equality filters work, but `->>`
still yields text and the query/count/order paths compare it directly. On
Postgres that means numeric guards compare lexically — `'9' >= '10'` is TRUE —
so a `{ stock: { gte: 10 } }` filter over 9/10/100 silently includes 9
(over-count / oversell) and numeric orderBy returns [10, 100, 9].

Adds a dialect-parametric suite (describeEachDialect) pinning the remaining
failure modes. SQLite's json_extract is typed, so it stays green; the Postgres
dialect fails on the range/count/order cases on current main. Postgres runs only
when EMDASH_TEST_PG is set.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01TX9YciGFRZX9aF2UcQ6rUW

* fix(core): compare plugin-storage numeric filters and ordering numerically on Postgres

`_plugin_storage.data` is a `text` column. After the `(data)::jsonb->>'field'`
cast (emdash-cms#1898) the extracted value is still text, so numeric guards compared
lexically on Postgres — `'9' >= '10'` is TRUE — and `{ stock: { gte: 10 } }`
over 9/10/100 matched 9 (silent over-count / oversell), while numeric `orderBy`
sorted `[10, 100, 9]`.

- Add `pluginDataExtractExpr` with an optional **type-guarded** `::numeric` cast
  (guarded by `jsonb_typeof`/`json_type`), so numeric comparisons are numeric and
  total: a non-number stored value yields NULL (no match) instead of aborting the
  query with `invalid input syntax for type numeric`.
- Add `pluginDataOrderExpr` that orders over the jsonb-native value (`->`), whose
  btree ordering is numeric among numbers and total across heterogeneous data.
- `buildCondition` picks numeric-vs-text per operand from its JS type; `orderBy`
  uses the jsonb-native expression. SQLite is unchanged (`json_extract` is typed).

Builds on emdash-cms#1898 (jsonb cast + boolean/`= 1` fix); this is the remaining numeric
piece. Regression suite runs on both dialects (Postgres via EMDASH_TEST_PG).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01TX9YciGFRZX9aF2UcQ6rUW

---------

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants