Repository navigation
Conversation
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars.
|
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
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
force-pushed
the
fix/dya-309-null-in-not-in-list
branch
from
November 30, 2026 10:11
3810362 to
7b7b980
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ref https://linear.app/ghost/issue/DYA-309
Problem
mongo-knexcompiled a list of values that holds null into SQL'sINorNOT INwith the null inside it. SQL'sINnever matches a NULL, and a NULL insideNOT INmakes it match no row.The parser produces these lists from any filter that lists null, such as
image:[null,'a.jpg']andimage:-[null,'a.jpg'].nqlproduces the second fromimage:-null+image:-'a.jpg'too, becausecombineNeFilterscombines the negations of one key into one$nin.Solution
One fix, in
mongo-knex: a null among the values of an$inor$ninis compared on its own.It applies on a column and inside a relation's subquery, an
$elemMatchand a$nor, under$andand$or.nqlhas no source change.What changes, by filter
key:-null+key:-von a columnkey:-null+key:-von a one-to-one relationkey:[null,v]on a column or a one-to-one relationkey:-[null,v]on a columnkey:-[null,v]on a one-to-one relationtags.description:[null,'x'],tags.description:-[null,'x']or inside an$elemMatchtag:[null,news],tag:-null+tag:-news,primary_tag:-null+primary_tag:-newskey:nullandkey:-nullon their ownEach 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
combineNeFiltersis left as it is. An earlier version of this PR also kept$ne: nullout 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, becausekey:-nullon 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:-newsandprimary_author:-null+primary_author:-joewent from the posts with another public primary tag or author to no posts,tag:-null+tag:-newswent from the posts without news to only the untagged posts,tags.description:-null+tags.description:-xundid this fix, andtag:news+posts_tags.sort_order:-null+posts_tags.sort_order:-1became an SQL syntax error. That commit is dropped.What it changes for Ghost
Once Ghost bumps both packages:
{{#get}}and in the SQL side ofroutes.yamlcollection filters. Ghost Admin builds none of these shapes, so only hand-written filters reach them.ghost/core/server/lib/filters, whosecombineNeFilterskeeps$ne: nullout 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), andlabel:-null+label:-vipkeeps selecting only the members with no label, whilelabel:-[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$inand$ninon a column, inside$and,$orand$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$ninwith OR, and splitting a list inside an$orwith 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:-von a column and a one-to-one relation,label:-null+label:-beta, and a list inside an OR.$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:
key:-nullon its own selects the rows with no related row, andkey:nullselects nothing:tag:-nullreturns 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:-nullandprimary_author:-nullselect no post at all. Ghost's members filter matrix pinslabel:-nullas the members with no label.toQuery()on a query forposts_meta.meta_title:-nulland then running it returns the posts without a meta title.$or, the one-to-one null flip doesn't fliporWhereNotNull, and the subquery is joined with AND:featured:true,posts_meta.meta_title:-nullselects no post.tag:news+posts_tags.sort_order:-nullis an SQL syntax error,posts_tags.sort_order:-[null,1]there is rejected by knex, andtag:news+posts_tags.sort_order:-null+posts_tags.sort_order:-1drops its sort order condition. That code is being reworked for DYA-308.