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 nulls first/last implementation Mysql #50079

Merged
merged 1 commit into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,8 @@

*Jason Meller*

* Add `nulls_last` and working `desc.nulls_first` for MySQL.

*Tristan Fellows*

Please check [7-1-stable](https://github.com/rails/rails/blob/7-1-stable/activerecord/CHANGELOG.md) for previous changes.
9 changes: 7 additions & 2 deletions activerecord/lib/arel/visitors/mysql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,14 @@ def visit_Arel_Nodes_NotRegexp(o, collector)
infix_value o, collector, " NOT REGEXP "
end

# no-op
def visit_Arel_Nodes_NullsFirst(o, collector)
visit o.expr, collector
visit(o.expr.expr, collector) << " IS NOT NULL, "
visit(o.expr, collector)
end

def visit_Arel_Nodes_NullsLast(o, collector)
visit(o.expr.expr, collector) << " IS NULL, "
visit(o.expr, collector)
end

def visit_Arel_Nodes_Cte(o, collector)
Expand Down
25 changes: 23 additions & 2 deletions activerecord/test/cases/arel/visitors/mysql_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,31 @@ def compile(node)
end

describe "Nodes::Ordering" do
it "should no-op ascending nulls first" do
it "should handle nulls first" do
test = Table.new(:users)[:first_name].asc.nulls_first
_(compile(test)).must_be_like %{
"users"."first_name" ASC
"users"."first_name" IS NOT NULL, "users"."first_name" ASC
}
end

it "should handle nulls last" do
test = Table.new(:users)[:first_name].asc.nulls_last
_(compile(test)).must_be_like %{
"users"."first_name" IS NULL, "users"."first_name" ASC
}
end

it "should handle nulls first reversed" do
test = Table.new(:users)[:first_name].asc.nulls_first.reverse
_(compile(test)).must_be_like %{
"users"."first_name" IS NULL, "users"."first_name" DESC
}
end

it "should handle nulls last reversed" do
test = Table.new(:users)[:first_name].asc.nulls_last.reverse
_(compile(test)).must_be_like %{
"users"."first_name" IS NOT NULL, "users"."first_name" DESC
}
end
end
Expand Down