Repository navigation
test(core): plugin-storage numeric filters/order compare lexically on Postgres - #2150
vedanshujain wants to merge 1 commit into
Conversation
|
|
All contributors have signed the CLA ✍️ ✅ |
… 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
5f2ab6d to
a8c0e5f
Compare
|
I have read the CLA Document and I hereby sign the CLA |
|
Closing in favor of #2152 |
There was a problem hiding this comment.
The diagnosis is right and the test is a clean, focused repro. #1898 switched Postgres plugin-storage access to (data)::jsonb->>'field', which fixed the parse error and string/boolean filters but still compares the extracted text for ranges and ordering. The new file seeds values (9/10/100) where lexical and numeric order diverge, so it correctly fails on current Postgres for the gte filter, count, and orderBy cases while staying green on SQLite.
I read PluginStorageRepository, storage-query, dialect-helpers, storage-indexes, test-db, the _plugin_storage migration, and the existing storage-dialect.test.ts. The assertions are accurate, the dialect harness is used correctly, and the test doesn't duplicate the existing storage-dialect.test.ts cases (which use 1/2/3 and therefore can't expose lexical ordering). No AGENTS.md violations: test-only so no changeset/i18n/Discussion required, and the AI disclosure is checked.
The only concern is merge readiness: this PR adds tests that are deliberately red on Postgres main. That will break pnpm test in any CI job that sets EMDASH_TEST_PG. Per AGENTS.md's TDD flow, the failing test should land together with the production fix (or the known-failing Postgres cases should be marked it.fails while the repro is shared). I would not merge this as-is.
| stock: number; | ||
| } | ||
|
|
||
| describeEachDialect("Plugin storage numeric comparison on Postgres", (dialect) => { |
There was a problem hiding this comment.
[needs fixing] This describeEachDialect block intentionally contains three Postgres assertions that fail on current main (the gte range filter, count, and orderBy cases). Those failures are exactly the bug being demonstrated, but they also mean this file will keep pnpm test red whenever EMDASH_TEST_PG is configured. A deliberately failing test suite on main is not mergeable.
Either land this together with the production fix, or mark the known-failing Postgres cases as expected failures so CI stays green while the repro is shared. For example:
| describeEachDialect("Plugin storage numeric comparison on Postgres", (dialect) => { | |
| const itFailsOnPostgres = dialect === "postgres" ? it.fails : it; | |
| // (a) The load-bearing case: a lexical `>=` includes stock 9 in a `>= 10` | |
| // filter — a silent over-count / oversell. | |
| itFailsOnPostgres("numeric RangeFilter { gte: 10 } returns exactly {10, 100}, never 9", async () => { | |
| const repo = await seedProducts(); | |
| const result = await repo.query({ where: { stock: { gte: 10 } } }); | |
| expect(result.items.map((i) => i.id).toSorted()).toEqual(["p10", "p100"]); | |
| expect(result.items.map((i) => i.data.stock).toSorted((a, b) => a - b)).toEqual([10, 100]); | |
| }); | |
| // (b) Same lexical bug via the aggregate path. | |
| itFailsOnPostgres("count({ stock: { gte: 10 } }) is numerically correct (2, not 3)", async () => { | |
| const repo = await seedProducts(); | |
| expect(await repo.count({ stock: { gte: 10 } })).toBe(2); | |
| }); |
(Apply the same wrapper to the orderBy case as well.) Once the production fix lands, the it.fails cases will start passing and Vitest will flag them so you can remove the wrapper.
What does this PR do?
This adds failing tests that will be fixed by #2152
Plugin storage still compares numeric field values lexically on Postgres.
_plugin_storage.datais a plaintextcolumn. #1898 (fixing #920) added the(data)::jsonb->>'field'cast, which correctly resolved theoperator does not exist: text ->> unknownparse error and the boolean/equality filters — but->>still returns text, and the query / count / order paths compare that text directly. So a numeric guard is evaluated lexically:The user-visible consequences are a silent over-count / oversell and wrong ordering.
Reproduction — this PR adds a dialect-parametric integration suite (via the existing
describeEachDialectharness) atpackages/core/tests/integration/plugins/storage-postgres-numeric-regression.test.ts. It seedsstockvalues9 / 10 / 100(chosen so lexical and numeric order disagree):main)query({ where: { stock: { gte: 10 } } }){10, 100}{9, 10, 100}count({ stock: { gte: 10 } })23createStorageIndexesquery({ orderBy: { stock: 'asc' } })[9, 10, 100][10, 100, 9]SQLite passes throughout because
json_extractalready returns a typed value — which is why this went unnoticed.EMDASH_TEST_PG=postgres://… pnpm --filter emdash exec vitest run \ tests/integration/plugins/storage-postgres-numeric-regression.test.tsSuggested fix direction: apply a type-guarded numeric cast to the extracted value in the numeric comparison and
ORDER BYpaths on Postgres (cast tonumericonly when thejsonbvalue is a number, so non-numeric/nullrows are excluded rather than erroring) — mirroring the typed value SQLite'sjson_extractalready returns. Happy to follow up with the fix if the diagnosis looks right.Related: #920 (closed by #1898). No existing issue tracks the remaining numeric case.
Type of change
Checklist
pnpm typecheckpassespnpm lintpassespnpm testpasses (or targeted tests for my change) — intentionally unchecked: the Postgres assertions fail onmain, which is the bug this PR demonstrates; SQLite passespnpm formathas been runAI-generated code disclosure
Screenshots / test output
Postgres dialect on current
main(RED on a/b/d, GREEN on c; all SQLite green):