Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.Sign up
Arel: Implemented DB-aware NULL-safe comparison #34451
This PR introduces NULL-safe comparison to Arel. The problem is that normal comparison operator
SELECT * FROM `users` WHERE `first_name` = `last_name`
Different databases have different mechanisms to overcome this. A generic (and inefficient) way to solve this with SQL is:
CASE WHEN (a = b) or (a IS NULL AND b IS NULL) THEN 1 ELSE 0 END = 1
With this change, Arel will provide two methods:
Table.new(:users)[:first_name].null_safe_eq Table.new(:users)[:last_name] # (psql) => "users"."first_name" IS NOT DISTINCT FROM "users"."last_name" # (mysql) => "users"."first_name" <=> "users"."last_name" # (sqlite) => "users"."first_name" IS "users"."last_name"
Not every database can perform NULL-safe comparison efficiently. I think it is reasonable to abstract this functionality and provide the best performance possible.
Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @pixeltrix (or someone else) soon.
If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.
This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.
Please see the contribution instructions for more information.
I am not really attached to the name :) This feature was in use internally for a while, and we use MySQL, which calls it "NULL-safe equal to operator" in the documentation.
Another option is to rename it to
PS. It seems like IBM DB2 also supports it...
Update: Wikipedia also mentions it