Paul Nikitochkin
2013-Jul-31 20:40 UTC
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? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/groups/opt_out.
Will Bryant
2013-Jul-31 23:07 UTC
Re: Does Rails need support of `NULLS FIRST|LAST` for `ORDER BY` expression?
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. On 1/08/2013, at 08:40 , Paul Nikitochkin <paul.nikitochkin@gmail.com> wrote:> 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? > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. > To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. > To post to this group, send email to rubyonrails-core@googlegroups.com. > Visit this group at http://groups.google.com/group/rubyonrails-core. > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/groups/opt_out.
Aaron Patterson
2013-Aug-01 05:21 UTC
Re: Does Rails need support of `NULLS FIRST|LAST` for `ORDER BY` expression?
On Thu, Aug 01, 2013 at 11:07:57AM +1200, Will Bryant wrote:> 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. -- Aaron Patterson http://tenderlovemaking.com/ -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/groups/opt_out.
Paul Nikitochkin
2013-Aug-01 10:34 UTC
Re: Does Rails need support of `NULLS FIRST|LAST` for `ORDER BY` expression?
Thanks, cool! I''m also agree with that (this option saw in sequel gem), will create PR for it. On Thursday, August 1, 2013 8:21:31 AM UTC+3, Aaron Patterson wrote:> > On Thu, Aug 01, 2013 at 11:07:57AM +1200, Will Bryant wrote: > > 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. > > -- > Aaron Patterson > http://tenderlovemaking.com/ >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/groups/opt_out.