Skip to content

🐛 Fixed "is not" filters leaving out rows that have no value - #321

Open
rob-ghost wants to merge 1 commit into
mainfrom
fix/dya-361-not-keeps-empty-values
Open

rob-ghost wants to merge 1 commit into
mainfrom
fix/dya-361-not-keeps-empty-values

Conversation

@rob-ghost

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

Copy link
Copy Markdown
Contributor

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

Problem

A row with no value in a column isn't any value, so "is not X" should select it. SQL's !=, NOT IN and NOT LIKE are unknown for NULL, so "is not", "is none of", "does not contain", "does not start with" and "does not end with" on a column left out every row where the column is empty. In Ghost, the members screen's "Name is not" and "Name does not contain" drop every member without a name from the list, the export, saved views and bulk actions.

Solution

A negation of values on a column also selects the rows where the column is NULL:

{name: {$ne: 'Jo'}}           // (members.name is null or members.name != 'Jo')
{name: {$nin: ['Jo', 'Al']}}  // (members.name is null or members.name not in ('Jo', 'Al'))
{name: {$not: /jo/i}}         // (members.name is null or lower(members.name) not like '%jo%' ESCAPE '*')

These now select the same rows as -(name:'Jo'), and the same rows nql's queryJSON matches in memory.

Unchanged, on purpose:

  • A negation that names null: name:-null stays IS NOT NULL, and name:-[null,'Jo'] compiles exactly as before. It says the column has a value, so it isn't widened to rows without one. It is still wrong: its NOT IN (NULL, 'Jo') selects nothing, where it should select the members with a name other than Jo. That fix was dropped.
  • $not with a value that isn't a pattern: only a direct caller can write it, not NQL text, and it still throws, as before.
  • A relation key: labels.slug:-vip already selects the complement through NOT IN (subquery).
  • Conditions inside a relation's subquery, such as tags.description:-~'x': they apply to each related row in an existence check, so adding IS NULL there would not make the filter the complement of its positive form. Left as they are.
  • Conditions inside an $elemMatch: read literally on one related row. Ghost's custom field filters use these and have their own "is set" and "is not set". The new tests prove they select the same rows before and after.

What changes, for which filters

Filter Before After Exposure
Members name:-'Jo', name:-~'jo' ("Name is not", "does not contain") members with no name left out included Admin-built, stored (saved views). Bulk actions select more members: a bulk delete, unsubscribe or label change by one of these now includes members without a name
Comments html:-~'x' ("Text does not contain"), member_id:-<id> ("Author is not") comments with no text, or whose member was deleted, left out included Admin-built (comments list only)
k:-v, k:-[a,b], k:-~v, -~^, -~$ on any column that can be NULL, any model (posts custom_excerpt, feature_image, tags description…) rows with no value left out included Hand-written private and public (Admin and Content API, themes, routes.yaml). No public example of the shape found
A negation on a column that is never NULL (status:-free, id:-{{id}}, email:-'x') same rows, SQL gains col IS NULL OR Admin-built, server-built (email sends use status:-free), themes
A negation inside a negated group: -(name:-'Jo') members named Jo, and members with no name members named Jo only: fewer rows Hand-written private and public
A negated segment whose filter is such a negation: segment:-<slug> where the segment is "Name is not Jo" also members with no name not them: fewer rows Stored. Ghost expands segment:-x to a negated group of the segment's filter. Behind the private memberSegments flag
k:-null, k:-[null,a], negations on relation keys, conditions inside a subquery or an $elemMatch unchanged

The compiler doesn't know which columns can be NULL, so the SQL text changes for every negated column. For a column declared NOT NULL, MySQL optimises col IS NULL away ("IS NULL Optimization" in its manual), so status:-free in an email send plans as before. A negation can't narrow an index search by itself, and SQLite plans (name IS NULL OR name != ?) and status = ? AND (name IS NULL OR name != ?) the same as the old forms (covering-index scan, and an index search on status).

Ghost

  • Members get this change through Ghost's direct mongo-knex dependency.
  • Posts and comments get it only through nql's own mongo-knex. So nql must be released with the new range, or Ghost runs two copies that disagree.
  • Tests: the bump should add an Admin API test that "Name does not contain" lists a member without a name.
  • Nothing else changes in Ghost. The free-subscriber SQL that member.js compares with a literal has no negated column. No email send selects more members: Ghost's own recipient filters negate only status, which is never NULL.

Release

Each of the three filter fixes needs a minor version, since rows change for existing filters:

Package Version Pull request
@tryghost/mongo-knex 0.14.0 #321
@tryghost/nql-lang 0.9.0 #322
@tryghost/nql 0.15.0 #323

Ship them in one run, after all three merge, so nql's ranges move to the new mongo-knex and nql-lang:

pnpm ship:minor --projects=@tryghost/mongo-knex,@tryghost/nql-lang,@tryghost/nql

Leave out --projects and the run also takes @tryghost/mongo-utils to 0.8.0, which has no changes, and moves nql's range to ^0.8.0. Ghost pins mongo-utils 0.7.0 exactly, so it would then load two copies.

A dry run of the command above (pnpm exec nx release version minor --projects=… --dry-run) bumps exactly the three packages and moves both of nql's ranges.

Ghost then bumps mongo-knex, nql and nql-lang in one pull request. Its members pipeline calls mongo-knex directly, while posts and comments compile through nql's own mongo-knex. Bumping one without the other runs two copies that disagree. That pull request also updates the two SQL strings that ghost/test/unit/server/models/post.test.js pins: posts.id != ? becomes (posts.id is null or posts.id != ?).

Testing

New integration tests on posts with a column that can be NULL, a many-to-many and a one-to-one relation, and a custom-field-shaped relation filtered with $elemMatch. The column cases fail before this change; the relation, null and $elemMatch cases pass before and after. A negation ANDed after another condition is tested too, so attaching or bracketing the new group wrongly changes rows. Unit tests pin that a list naming null and a $not without a pattern compile as before, and SQL strings pinned in mongo-knex's and nql's unit tests are updated.

@coderabbitai

coderabbitai Bot commented Nov 30, 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: 39501486-4de1-4ca0-88b3-243475042b05
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@codecov-commenter

codecov-commenter commented Nov 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.67%. Comparing base (e0689bd) to head (51f0390).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #321      +/-   ##
==========================================
+ Coverage   85.56%   85.67%   +0.10%     
==========================================
  Files           9        9              
  Lines        2418     2436      +18     
  Branches      510      519       +9     
==========================================
+ Hits         2069     2087      +18     
  Misses        343      343              
  Partials        6        6              

☔ 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 30, 2026 11:45
ref https://linear.app/ghost/issue/DYA-361

A row with no value in a column isn't any value, so a filter that says the column is not something should select it. It didn't: SQL's `!=`, `NOT IN` and `NOT LIKE` are unknown for NULL, so "is not", "is none of", "does not contain", "does not start with" and "does not end with" on a column all left out the rows where the column is empty. In Ghost, the members screen's "Name is not" and "Name does not contain" dropped every member without a name from the list, the export, saved views and bulk actions. A negation of values on a column now also selects the rows where it is NULL, so `name:-'Jo'` is `(name IS NULL OR name != 'Jo')`, the same rows `-(name:'Jo')` already selected and the same rows matching a JSON object in memory selects. A negation that names null, such as `name:-null` or `name:-[null,'Jo']`, says the column has a value, so it compiles as before. Negations on a relation key already select the complement through `NOT IN` and are unchanged, and so are the conditions inside a relation's subquery and inside an $elemMatch, which apply to each related row, so Ghost's custom field filters select the same members as before.
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