Skip to content

Incorrect SQL in Rails 4.0.1 when using Includes, Order and Limit #306

@annaswims

Description

@annaswims

FinderTest#test_coerced_find_with_order_on_included_associations_with_construct_finder_sql_for_association_limiting_and_is_distinct does not pass

It generates different SQL in Rails 4.0.0 (good) and Rails 4.0.1-2 (failing).

Post.all.merge!(
      :includes => { :authors => :author_address }, 
      :order => 'author_addresses.id DESC ', 
      :limit => 2)

The SQL generated in Rails 4.0.0:

EXEC sp_executesql N'SELECT TOP (2) [posts].id FROM [posts] LEFT OUTER JOIN [categorizations] ON [categorizations].[category_id] = [posts].[id] LEFT OUTER JOIN [authors] ON [authors].[id] = [categorizations].[author_id] LEFT OUTER JOIN [author_addresses] ON [author_addresses].[id] = [authors].[author_address_id] GROUP BY [posts].id ORDER BY MAX(author_addresses.id)'

EXEC sp_executesql N'SELECT [posts].[id] AS t0_r0, [posts].[author_id] AS t0_r1, [posts].[title] AS t0_r2, [posts].[body] AS t0_r3, [posts].[type] AS t0_r4, [posts].[comments_count] AS t0_r5, [posts].[taggings_count] AS t0_r6, [posts].[taggings_with_delete_all_count] AS t0_r7, [posts].[taggings_with_destroy_count] AS t0_r8, [posts].[tags_count] AS t0_r9, [posts].[tags_with_destroy_count] AS t0_r10, [posts].[tags_with_nullify_count] AS t0_r11, [authors].[id] AS t1_r0, [authors].[name] AS t1_r1, [authors].[author_address_id] AS t1_r2, [authors].[author_address_extra_id] AS t1_r3, [authors].[organization_id] AS t1_r4, [authors].[owned_essay_id] AS t1_r5, [author_addresses].[id] AS t2_r0 FROM [posts] LEFT OUTER JOIN [categorizations] ON [categorizations].[category_id] = [posts].[id] LEFT OUTER JOIN [authors] ON [authors].[id] = [categorizations].[author_id] LEFT OUTER JOIN [author_addresses] ON [author_addresses].[id] = [authors].[author_address_id] WHERE [posts].[id] IN (3, 2) ORDER BY author_addresses.id DESC'

The SQL generated in Rails 4.0.1:

EXEC sp_executesql N'SELECT DISTINCT TOP (2) [posts].[id], author_addresses.id AS alias_0 FROM [posts] LEFT OUTER JOIN [categorizations] ON [categorizations].[category_id] = [posts].[id] LEFT OUTER JOIN [authors] ON [authors].[id] = [categorizations].[author_id] LEFT OUTER JOIN [author_addresses] ON [author_addresses].[id] = [authors].[author_address_id] ORDER BY author_addresses.id DESC'

EXEC sp_executesql N'SELECT [posts].[id] AS t0_r0, [posts].[author_id] AS t0_r1, [posts].[title] AS t0_r2, [posts].[body] AS t0_r3, [posts].[type] AS t0_r4, [posts].[comments_count] AS t0_r5, [posts].[taggings_count] AS t0_r6, [posts].[taggings_with_delete_all_count] AS t0_r7, [posts].[taggings_with_destroy_count] AS t0_r8, [posts].[tags_count] AS t0_r9, [posts].[tags_with_destroy_count] AS t0_r10, [posts].[tags_with_nullify_count] AS t0_r11, [authors].[id] AS t1_r0, [authors].[name] AS t1_r1, [authors].[author_address_id] AS t1_r2, [authors].[author_address_extra_id] AS t1_r3, [authors].[organization_id] AS t1_r4, [authors].[owned_essay_id] AS t1_r5, [author_addresses].[id] AS t2_r0 FROM [posts] LEFT OUTER JOIN [categorizations] ON [categorizations].[category_id] = [posts].[id] LEFT OUTER JOIN [authors] ON [authors].[id] = [categorizations].[author_id] LEFT OUTER JOIN [author_addresses] ON [author_addresses].[id] = [authors].[author_address_id] WHERE [posts].[id] IN (1, 1) ORDER BY author_addresses.id DESC'

It looks like this Arel pull request is where the problem originates.
rails/arel#211

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions