@@ -150,7 +150,7 @@ if Code.ensure_loaded?(Tds) do
150150 group_by = group_by ( query , sources )
151151 having = having ( query , sources )
152152 _window = window ( query , sources )
153- combinations = combinations ( query )
153+ combinations = combinations ( query , as_prefix )
154154 order_by = order_by ( query , sources )
155155 # limit = is handled in select (TOP X)
156156 offset = offset ( query , sources )
@@ -645,14 +645,14 @@ if Code.ensure_loaded?(Tds) do
645645 defp lock ( % { lock: binary } , _sources ) when is_binary ( binary ) , do: [ " OPTION (" , binary , ?) ]
646646 defp lock ( % { lock: expr } = query , sources ) , do: [ " OPTION (" , expr ( expr , sources , query ) , ?) ]
647647
648- defp combinations ( % { combinations: combinations } ) do
648+ defp combinations ( % { combinations: combinations } , as_prefix ) do
649649 Enum . map ( combinations , fn
650- { :union , query } -> [ " UNION (" , all ( query ) , ")" ]
651- { :union_all , query } -> [ " UNION ALL (" , all ( query ) , ")" ]
652- { :except , query } -> [ " EXCEPT (" , all ( query ) , ")" ]
653- { :except_all , query } -> [ " EXCEPT ALL (" , all ( query ) , ")" ]
654- { :intersect , query } -> [ " INTERSECT (" , all ( query ) , ")" ]
655- { :intersect_all , query } -> [ " INTERSECT ALL (" , all ( query ) , ")" ]
650+ { :union , query } -> [ " UNION (" , all ( query , as_prefix ) , ")" ]
651+ { :union_all , query } -> [ " UNION ALL (" , all ( query , as_prefix ) , ")" ]
652+ { :except , query } -> [ " EXCEPT (" , all ( query , as_prefix ) , ")" ]
653+ { :except_all , query } -> [ " EXCEPT ALL (" , all ( query , as_prefix ) , ")" ]
654+ { :intersect , query } -> [ " INTERSECT (" , all ( query , as_prefix ) , ")" ]
655+ { :intersect_all , query } -> [ " INTERSECT ALL (" , all ( query , as_prefix ) , ")" ]
656656 end )
657657 end
658658
@@ -775,6 +775,12 @@ if Code.ensure_loaded?(Tds) do
775775 end
776776
777777 defp expr ( % Ecto.SubQuery { query: query } , sources , parent_query ) do
778+ combinations =
779+ Enum . map ( query . combinations , fn { type , combination_query } ->
780+ { type , put_in ( combination_query . aliases [ @ parent_as ] , { parent_query , sources } ) }
781+ end )
782+
783+ query = put_in ( query . combinations , combinations )
778784 query = put_in ( query . aliases [ @ parent_as ] , { parent_query , sources } )
779785 [ ?( , all ( query , subquery_as_prefix ( sources ) ) , ?) ]
780786 end
0 commit comments