Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arel: Implemented DB-aware NULL-safe comparison #34451

Merged
merged 3 commits into from Nov 15, 2018

Conversation

Projects
None yet
4 participants
@kpumuk
Copy link
Contributor

commented Nov 14, 2018

Summary

This PR introduces NULL-safe comparison to Arel. The problem is that normal comparison operator = treats NULL values is a special way when anything compared to NULL results in NULL (false). When we want to select results based on comparing two nullable columns, the results are usually unpredictable:

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

MySQL provides <=> operator, SQlite has IS, in Postgresql you can do IS NOT DISTINCT FROM.

With this change, Arel will provide two methods: null_safe_eq and null_safe_not_eq, which can be used to construct a valid database-optimized query:

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"

Other Information

Not every database can perform NULL-safe comparison efficiently. I think it is reasonable to abstract this functionality and provide the best performance possible.

References

@rails-bot

This comment has been minimized.

Copy link

commented Nov 14, 2018

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.

@rafaelfranca

This comment has been minimized.

Copy link
Member

commented Nov 14, 2018

Maybe a better name for those methods would be null_aware_eq? Safe is too vague and to me it relates to security related things.

@kpumuk

This comment has been minimized.

Copy link
Contributor Author

commented Nov 15, 2018

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 is_distinct_from and is_not_distinct_from after SQL construction used in some databases.

PS. It seems like IBM DB2 also supports it...

Update: Wikipedia also mentions it

@rafaelfranca

This comment has been minimized.

Copy link
Member

commented Nov 15, 2018

👍 to is_distinct_from

@kpumuk

This comment has been minimized.

Copy link
Contributor Author

commented Nov 15, 2018

It does sound better indeed. Pushed the change.

@rafaelfranca rafaelfranca merged commit b5302d5 into rails:master Nov 15, 2018

2 checks passed

codeclimate All good!
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@kpumuk

This comment has been minimized.

Copy link
Contributor Author

commented Nov 15, 2018

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.