c16b99a49c
In this particular case the use of UNION ALL leads to a better query plan compared to using 1 big query that uses an OR statement to combine different data sources. See https://gitlab.com/gitlab-org/gitlab-ce/issues/38508 for more information.
33 lines
1 KiB
Ruby
33 lines
1 KiB
Ruby
require 'spec_helper'
|
|
|
|
describe Gitlab::SQL::Union do
|
|
let(:relation_1) { User.where(email: 'alice@example.com').select(:id) }
|
|
let(:relation_2) { User.where(email: 'bob@example.com').select(:id) }
|
|
|
|
def to_sql(relation)
|
|
relation.reorder(nil).to_sql
|
|
end
|
|
|
|
describe '#to_sql' do
|
|
it 'returns a String joining relations together using a UNION' do
|
|
union = described_class.new([relation_1, relation_2])
|
|
|
|
expect(union.to_sql).to eq("#{to_sql(relation_1)}\nUNION\n#{to_sql(relation_2)}")
|
|
end
|
|
|
|
it 'skips Model.none segements' do
|
|
empty_relation = User.none
|
|
union = described_class.new([empty_relation, relation_1, relation_2])
|
|
|
|
expect {User.where("users.id IN (#{union.to_sql})").to_a}.not_to raise_error
|
|
expect(union.to_sql).to eq("#{to_sql(relation_1)}\nUNION\n#{to_sql(relation_2)}")
|
|
end
|
|
|
|
it 'uses UNION ALL when removing duplicates is disabled' do
|
|
union = described_class
|
|
.new([relation_1, relation_2], remove_duplicates: false)
|
|
|
|
expect(union.to_sql).to include('UNION ALL')
|
|
end
|
|
end
|
|
end
|