Skip to content

🐛 Fixed a null among the values of $in or $nin selecting the wrong rows - #316

Closed
rob-ghost wants to merge 1 commit into
mainfrom
fix/dya-309-null-in-not-in-list
Closed

rob-ghost wants to merge 1 commit into
mainfrom
fix/dya-309-null-in-not-in-list

Conversation

@rob-ghost

@rob-ghost rob-ghost commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

ref https://linear.app/ghost/issue/DYA-309

Problem

mongo-knex compiled a list of values that holds null into SQL's IN or NOT IN with the null inside it. SQL's IN never matches a NULL, and a NULL inside NOT IN makes it match no row.

{image: {$in: [null, 'a.jpg']}}   // image in (NULL, 'a.jpg')      -> leaves out the rows with no image
{image: {$nin: [null, 'a.jpg']}}  // image not in (NULL, 'a.jpg')  -> selects nothing

The parser produces these lists from any filter that lists null, such as image:[null,'a.jpg'] and image:-[null,'a.jpg']. nql produces the second from image:-null+image:-'a.jpg' too, because combineNeFilters combines the negations of one key into one $nin.

Solution

One fix, in mongo-knex: a null among the values of an $in or $nin is compared on its own.

{image: {$in: [null, 'a.jpg']}}   // (image is null or image in ('a.jpg'))
{image: {$nin: [null, 'a.jpg']}}  // (image is not null and image not in ('a.jpg'))
{image: {$nin: [null]}}           // (image is not null)

It applies on a column and inside a relation's subquery, an $elemMatch and a $nor, under $and and $or. nql has no source change.

What changes, by filter

Filter Before After
key:-null+key:-v on a column no rows the rows with a value other than v
key:-null+key:-v on a one-to-one relation also the rows with no value the rows with a value other than v
key:[null,v] on a column or a one-to-one relation only the rows with v the rows with no value or v
key:-[null,v] on a column no rows the rows with a value other than v
key:-[null,v] on a one-to-one relation also the rows with no value the rows with a value other than v
A list with null on a many-to-many relation's column that can be NULL, such as tags.description:[null,'x'], tags.description:-[null,'x'] or inside an $elemMatch a related row with no value was ignored a related row with no value counts as null
A list with null on a many-to-many key that is never NULL, such as tag:[null,news], tag:-null+tag:-news, primary_tag:-null+primary_tag:-news unchanged
key:null and key:-null on their own unchanged

Each NQL filter in the rows that change now selects the same rows from the database as nql's in-memory matcher does on the same data, in the tests below and on a fixture shaped like Ghost's posts.

Decisions

  • combineNeFilters is left as it is. An earlier version of this PR also kept $ne: null out of the combine. On a column or a one-to-one relation that fixed nothing this change doesn't fix on its own. On a many-to-many relation it made things worse, because key:-null on its own compiles there to "no related row whose value isn't null", which is the rows with no related row at all. With Ghost's post expansions, primary_tag:-null+primary_tag:-news and primary_author:-null+primary_author:-joe went from the posts with another public primary tag or author to no posts, tag:-null+tag:-news went from the posts without news to only the untagged posts, tags.description:-null+tags.description:-x undid this fix, and tag:news+posts_tags.sort_order:-null+posts_tags.sort_order:-1 became an SQL syntax error. That commit is dropped.

What it changes for Ghost

Once Ghost bumps both packages:

  • Every model on the filter plugin, which is every model but members: the rows in the table above, on the Admin and Content APIs, in themes' {{#get}} and in the SQL side of routes.yaml collection filters. Ghost Admin builds none of these shapes, so only hand-written filters reach them.
  • Members: Ghost compiles members filters with its own pipeline in ghost/core/server/lib/filters, whose combineNeFilters keeps $ne: null out of the combine, as the dropped commit did. So for members the list forms change (name:[null,'x'] now also selects the members with no name, name:-[null,'x'] selects the named members other than x instead of no one), and label:-null+label:-vip keeps selecting only the members with no label, while label:-[null,vip] selects the members without vip. That rule is being decided separately.

Version bump

A patch release of @tryghost/mongo-knex (0.13.0 to 0.13.1), and of @tryghost/nql (0.14.0 to 0.14.1) for the dependency, since it has no change of its own. Ghost then bumps both in its catalog.

Validation

There's no review site, as this is a library. The tests below prove the fix.

Testing

  • mongo-knex: integration tests for a null in $in and $nin on a column, inside $and, $or and $nor, on a one-to-one relation, on a many-to-many relation's column that can be NULL, and inside an $elemMatch. They fail without the fix. Two of them catch changes the earlier tests let through: joining the two halves of a $nin with OR, and splitting a list inside an $or with AND.
  • nql: integration tests on the members fixtures, where each filter selects the same members from the database as the in-memory matcher on the same data: lists with null on a column, a one-to-one relation and a many-to-many relation's column that can be NULL, key:-null+key:-v on a column and a one-to-one relation, label:-null+label:-beta, and a list inside an OR.
  • The one-to-one test "not equal and in grouping" expected $in: [null] to match no post, so it pinned the fault. Post 5, whose meta_description is NULL, is now selected too.

Suggested follow-ups

Faults in the same area that this PR doesn't change:

  • On a many-to-many relation key:-null on its own selects the rows with no related row, and key:null selects nothing: tag:-null returns the untagged posts, where the in-memory matcher returns every post. The one-to-one branch flips a null inside its negated subquery and the many-to-many branch doesn't. With Ghost's post expansions, primary_tag:-null and primary_author:-null select no post at all. Ghost's members filter matrix pins label:-null as the members with no label.
  • The one-to-one null flip changes the statement it reads, so a query compiled twice gets the opposite condition the second time: calling toQuery() on a query for posts_meta.meta_title:-null and then running it returns the posts without a meta title.
  • Inside an $or, the one-to-one null flip doesn't flip orWhereNotNull, and the subquery is joined with AND: featured:true,posts_meta.meta_title:-null selects no post.
  • A join-table condition beside a relation statement: tag:news+posts_tags.sort_order:-null is an SQL syntax error, posts_tags.sort_order:-[null,1] there is rejected by knex, and tag:news+posts_tags.sort_order:-null+posts_tags.sort_order:-1 drops its sort order condition. That code is being reworked for DYA-308.

@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 536a7fd9-b207-4efb-bef9-2da60ac974bd
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@codecov-commenter

codecov-commenter commented Nov 27, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 29.16667% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.00%. Comparing base (fb0d9f8) to head (7b7b980).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
packages/mongo-knex/lib/convertor.js 29.16% 16 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #316      +/-   ##
==========================================
- Coverage   85.56%   85.00%   -0.57%     
==========================================
  Files           9        9              
  Lines        2418     2441      +23     
  Branches      510      511       +1     
==========================================
+ Hits         2069     2075       +6     
- Misses        343      359      +16     
- Partials        6        7       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@rob-ghost
rob-ghost marked this pull request as ready for review November 27, 2026 01:37
ref https://linear.app/ghost/issue/DYA-309

A list of values was compiled to SQL's IN or NOT IN with the null inside it. SQL's IN never matches a NULL, so `{image: {$in: [null, 'a.jpg']}}` left out the rows with no image, and a NULL inside NOT IN makes it match no row, so `{image: {$nin: [null, 'a.jpg']}}` selected nothing. The parser produces both from a filter that lists null, such as `image:[null,'a.jpg']` and `image:-[null,'a.jpg']`, and nql produces the second from `image:-null+image:-'a.jpg'` as well, because it combines the negations of one key into one list. The null is now compared on its own, as `(image IS NULL OR image IN ('a.jpg'))` and `(image IS NOT NULL AND image NOT IN ('a.jpg'))`, on a column and inside a relation's subquery or an $elemMatch alike, so each of these filters selects the same rows from the database as nql matches in memory. On a many-to-many relation, `tags.description:-[null,'x']` now also leaves out the posts with a tag that has no description. The one-to-one test "not equal and in grouping" expected `$in: [null]` to match no post, so it pinned this fault: post 5, whose meta_description is NULL, is now selected too.
@rob-ghost
rob-ghost force-pushed the fix/dya-309-null-in-not-in-list branch from 3810362 to 7b7b980 Compare November 30, 2026 10:11
@rob-ghost rob-ghost changed the title 🐛 Fixed filters that leave out null and another value selecting the wrong rows 🐛 Fixed a null among the values of $in or $nin selecting the wrong rows Nov 30, 2026
@rob-ghost rob-ghost closed this Nov 30, 2026
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.

2 participants