oxidecomputer / oxidecomputer/omicron

Diesel queries: should we prefer `is_not_distinct_from()` over separate `eq()` / `is_null()` branches?

Open
#9,834 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
572
Forks
97
Avg merge
2d 12h
Merged PRs (30d)
96

Description

(This is followup from #9570 where we added additional branching queries.)

When we compare a nullable column against a Rust value with the type Option<T>, we often box the query and branch based on its value to either compare with eq or check for is_null; e.g. from main:

https://github.com/oxidecomputer/omicron/blob/4c9d4a0df27588128da233f02004d0b7356ef44d/nexus/db-queries/src/db/datastore/bgp.rs#L109-L122

This is because eq() faithfully reproduces the SQL behavior of some_column = NULL returning false even if the value of some_column is itself NULL.

However, I believe we could replace these branches by using https://docs.rs/diesel/latest/diesel/expression_methods/trait.PgExpressionMethods.html#method.is_not_distinct_from, which behaves more like we'd expect from Option comparisons in Rust:

This behaves identically to the = operator, except that NULL is treated as a normal value.

The value of this is pretty small on examples like the above (although we potentially get to drop the boxing of the query to type-erase it); it becomes more of a win if the branching contains multiple other filters that have to be manually kept in sync. An example from #9570, where these branches are identical except for addr.eq(addr) vs addr.is_null():

https://github.com/oxidecomputer/omicron/blob/2a5ffb0cdfd523f4cf080afa92b48b0be217e1bc/nexus/db-queries/src/db/datastore/bgp.rs#L891-L910

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read the nullable-value branches in nexus/db-queries/src/db/datastore/bgp.rs at lines 109-122 and 891-910, along with follow-up issue #9570. Compare the existing eq()/is_null() behavior with Diesel's PgExpressionMethods::is_not_distinct_from, then determine whether the approach should replace these patterns and document the affected scope and validation needed.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, rust
Domain
databases
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.