rails / arel

A Relational Algebra
2.06k stars 390 forks source link

union operation broken in 9.0.0 #532

Closed aqeelvn closed 6 years ago

aqeelvn commented 6 years ago

I was updating our rails app to 5.2.0 and hence arel from 8.0.0 to 9.0.0

The union operation seems to be broken( even though its only deprecated in 9.0.0 ).

Here is an example of what the issue is:

left and right are two ActiveRecord query objects. Individually they work, but left.union(right) is breaking now in 9.0.0.

left.to_sql
#=> "SELECT *, 1 as priority FROM `notifications` WHERE `notifications`.`receiver_id` = <masked> AND `notifications`.`read_at` IS NULL AND `notifications`.`target_type` = 'photo_type' AND `notifications`.`created_at` > '2018-04-09 10:11:12.732444'"
right.to_sql
#=> "SELECT *, 2 as priority FROM `notifications` WHERE `notifications`.`receiver_id` = <masked> AND `notifications`.`target_type` IN ('type1', 'type2', 'type3', 'type4') AND 1=1"
left.union(right).to_sql
#DEPRECATION WARNING: Delegating union to arel is deprecated and will be removed in Rails 6.0. (called from build_union_query at (pry):20)
#DEPRECATION WARNING: Delegating ast to arel is deprecated and will be removed in Rails 6.0. (called from build_union_query at (pry):20)

#=> "( SELECT *, 1 as priority FROM `notifications` WHERE `notifications`.`receiver_id` = <masked> AND `notifications`.`read_at` IS NULL AND `notifications`.`target_type` = 'photo_type' AND `notifications`.`created_at` > '2018-04-09 10:11:12.732444' UNION SELECT *, 2 as priority FROM `notifications` WHERE `notifications`.`receiver_id` = <masked> AND `notifications`.`target_type` IN (?, ?, ?, ?) AND 1=1 )"

As can be seen the generated SQL is invalid at this point:

 `notifications`.`target_type` IN (?, ?, ?, ?) 
matthewd commented 6 years ago

Per #523, Arel development is moving to rails/rails.

If this issue is still relevant, please consider reopening it over there. (Note that the Rails repository does not accept feature request issues, and requires reproduction steps that rely on Active Record's documented API.)