Does Rails need support of `NULLS FIRST|LAST` for `ORDER BY` expression?

Hi,

after I investigated https://github.com/rails/rails/issues/11571 issue,

found that ORDER BY … NULLS FIRST|LAST expression is supported (documented) by ANSI,

and implemented in PostreSQL (MySQL has another options for this feature and does not support for now).

To fix in full that issue reverse_order should generate for “ORDER BY time ASC NULLS FIRST” => "ORDER BY time DESC NULLS LAST".

But adding NULLS FIRST|LAST which are supported only by Postgresql, will add more complexity for reverse_sql_order (https://github.com/rails/rails/blob/master/activerecord/lib/active_record/relation/query_methods.rb#L978)

Another way is remain current implementation and developer should use custom order expression by reorder

topics = Topics.order(‘time ASC NULLS FIRST’)

reversed_topics = topics.reorder(’‘time DESC NULLS LAST’)

What do you think?

There is a simple way to do customization, and does Rails need upgraded reverse_order to generate valid SQL for custom ORDER BY expressions?

It’s not hard to implement an equivalent for mysql, to reverse where nulls sort you just use ORDER by -x DESC instead of ORDER BY x ASC. If there’s a proper API for it in AR then it should be easy enough to support both ANSI and Mysql implementations.

Dunno what the best API is, but I’d suggest something like order(“x”, :nulls => :last). Better than more and more string fudging IMHO.

I'd much rather it's an option than do string parsing.

Thanks, cool!

I’m also agree with that (this option saw in sequel gem), will create PR for it.