Skip to content
This repository

[PostgreSQL] Remove NULLS FIRST/LAST. Closes #5152 #5168

Merged
merged 1 commit into from about 2 years ago

2 participants

Toshinori Kajihara Aaron Patterson
Toshinori Kajihara
Collaborator

When we use the postgresql adapter and construct distinct select clause, we have a problem with NULLS first/last.

without fix
=> posts.updater_idnulls first AS alias_0
with fix
=> posts.updater_id AS alias_0

closes #5152

This original issue was reported for 3-2-stable.

Aaron Patterson tenderlove merged commit 699e460 into from February 27, 2012
Aaron Patterson tenderlove closed this February 27, 2012
Toshinori Kajihara kennyj referenced this pull request from a commit August 01, 2012
Commit has since been removed from the repository and is no longer available.
Toshinori Kajihara kennyj referenced this pull request from a commit in kennyj/rails August 02, 2012
Toshinori Kajihara Backport #5168 to 3-2-stable. Fix a problem that NULLS is ignored by …
…postgresql_adapter.rb while creating distincts.
f137c2b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Feb 25, 2012
Toshinori Kajihara Remove NULLS FIRST/LAST. closes #5152 404b73b
This page is out of date. Refresh to see the latest.
2  activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -1189,7 +1189,7 @@ def distinct(columns, orders) #:nodoc:
1189 1189
 
1190 1190
         # Construct a clean list of column names from the ORDER BY clause, removing
1191 1191
         # any ASC/DESC modifiers
1192  
-        order_columns = orders.collect { |s| s.gsub(/\s+(ASC|DESC)\s*/i, '') }
  1192
+        order_columns = orders.collect { |s| s.gsub(/\s+(ASC|DESC)\s*(NULLS\s+(FIRST|LAST)\s*)?/i, '') }
1193 1193
         order_columns.delete_if { |c| c.blank? }
1194 1194
         order_columns = order_columns.zip((0...order_columns.size).to_a).map { |s,i| "#{s} AS alias_#{i}" }
1195 1195
 
5  activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb
@@ -185,6 +185,11 @@ def test_partial_index
185 185
         assert_equal "(number > 100)", index.where
186 186
       end
187 187
 
  188
+      def test_distinct_with_nulls
  189
+        assert_equal "DISTINCT posts.title, posts.updater_id AS alias_0", @connection.distinct("posts.title", ["posts.updater_id desc nulls first"])
  190
+        assert_equal "DISTINCT posts.title, posts.updater_id AS alias_0", @connection.distinct("posts.title", ["posts.updater_id desc nulls last"])
  191
+      end
  192
+
188 193
       private
189 194
       def insert(ctx, data)
190 195
         binds   = data.map { |name, value|
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.