Skip to content

Instantly share code, notes, and snippets.

@mshibuya
Created May 21, 2013 05:35
Show Gist options
  • Save mshibuya/5617676 to your computer and use it in GitHub Desktop.
Save mshibuya/5617676 to your computer and use it in GitHub Desktop.
source 'https://rubygems.org'
gem 'activerecord'
gem 'postgres'
require "rubygems"
require "active_record"
ActiveRecord::Base.establish_connection(
adapter: "postgresql",
host: "localhost",
username: "shibuya",
password: "",
database: "sql_test",
)
class T1 < ActiveRecord::Base
self.table_name = 't1'
self.primary_key = :pk
end
class T2 < ActiveRecord::Base
self.table_name = 't2'
self.primary_key = :pk
end
T1.connection.execute('TRUNCATE t1;');
T2.connection.execute('TRUNCATE t2;');
id = 1;
[
[true, true],
[true, false],
[false, true],
[false, false]
].each do |pk1, pk2|
[nil, 1, 2].each do |c1|
[nil, 1, 2].each do |c2|
T1.create! do |r|
r.pk = id
r.c1 = c1
end if pk1
T2.create! do |r|
r.pk = id
r.c2 = c2
end if pk2
id += 1
end
end
end
results = {}
{
:a => 'SELECT C1, C2 FROM T1 NATURAL JOIN T2;',
:b => 'SELECT C1, C2 FROM T1 JOIN T2 ON T1.PK = T2.PK;',
:c => 'SELECT C1, C2 FROM T1 INNER JOIN T2 ON T1.PK = T2.PK;',
:d => 'SELECT C1, C2 FROM T1 LEFT JOIN T2 ON T1.PK = T2.PK;',
:e => 'SELECT C1, C2 FROM T1 RIGHT JOIN T2 ON T1.PK = T2.PK;',
:f => 'SELECT C1, C2 FROM T1 FULL JOIN T2 ON T1.PK = T2.PK;',
:g => 'SELECT C1, C2 FROM T1 LEFT OUTER JOIN T2 ON T1.PK = T2.PK;',
:h => 'SELECT C1, C2 FROM T1 RIGHT OUTER JOIN T2 ON T1.PK = T2.PK;',
:i => 'SELECT C1, C2 FROM T1 FULL OUTER JOIN T2 ON T1.PK = T2.PK;',
:j => 'SELECT C1, C2 FROM T2 NATURAL JOIN T1;',
:k => 'SELECT C1, C2 FROM T2 JOIN T1 ON T1.PK = T2.PK;',
:l => 'SELECT C1, C2 FROM T2 INNER JOIN T1 ON T1.PK = T2.PK;',
:m => 'SELECT C1, C2 FROM T2 LEFT JOIN T1 ON T1.PK = T2.PK;',
:n => 'SELECT C1, C2 FROM T2 RIGHT JOIN T1 ON T1.PK = T2.PK;',
:o => 'SELECT C1, C2 FROM T2 FULL JOIN T1 ON T1.PK = T2.PK;',
:p => 'SELECT C1, C2 FROM T2 LEFT OUTER JOIN T1 ON T1.PK = T2.PK;',
:q => 'SELECT C1, C2 FROM T2 RIGHT OUTER JOIN T1 ON T1.PK = T2.PK;',
:r => 'SELECT C1, C2 FROM T2 FULL OUTER JOIN T1 ON T1.PK = T2.PK;',
:s => 'SELECT C1, C2 FROM T1 INNER JOIN T2 ON T1.PK = T2.PK WHERE C2 = 1;',
:t => 'SELECT C1, C2 FROM T1 LEFT JOIN T2 ON T1.PK = T2.PK WHERE C2 = 1;',
:u => 'SELECT C1, C2 FROM T1 RIGHT JOIN T2 ON T1.PK = T2.PK WHERE C2 = 1;',
:v => 'SELECT C1, C2 FROM T1 FULL JOIN T2 ON T1.PK = T2.PK WHERE C2 = 1;',
:w => 'SELECT C1, C2 FROM T1 INNER JOIN T2 ON T1.PK = T2.PK WHERE C2 IS NULL;',
:x => 'SELECT C1, C2 FROM T1 LEFT JOIN T2 ON T1.PK = T2.PK WHERE C2 IS NULL;',
:y => 'SELECT C1, C2 FROM T1 RIGHT JOIN T2 ON T1.PK = T2.PK WHERE C2 IS NULL;',
:z => 'SELECT C1, C2 FROM T1 FULL JOIN T2 ON T1.PK = T2.PK WHERE C2 IS NULL;'
}.each do |letter, sql|
rows = T1.find_by_sql(sql)
results[letter] = rows.
map{|row| [(row.c1.to_i if row.c1), (row.c2.to_i if row.c2)]}.
sort{|a, b| a.map{|i| i.to_i} <=> b.map{|i| i.to_i}}
end
results.
group_by{|letter, result| result}.
map{|r, identicals| p identicals.map{|letter, r| letter}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment