alf-tool / alf-sequel

An adapter for using Alf with common SQL DBMSs, thanks to Sequel
MIT License
4 stars 1 forks source link

SQL compilation bug on intersect #1

Closed blambeau closed 10 years ago

blambeau commented 10 years ago

There is a parenthesizing bug in INTERSECT compilation when using Alf::Sequel::Compiler (works fine with Alf::Sql::Compiler).

The following script:

require 'alf-core'
require 'alf-sql'
require 'alf-sequel'
require 'logger'

SDB = ::Sequel.connect('postgres://localhost/bug', loggers: [])
DB = Alf.connect(SDB)

expr = DB.parse{
  intersect(union(nl, sapl), nl)
}
cog = Alf::Sql::Compiler.new.call(expr)

puts "--- SQL AST"
puts cog.to_sexpr.inspect

puts "\n--- Alf::Sql compilation result"
puts cog.to_sql

puts "\n--- Alf::Sequel compilation result"
puts expr.to_sql

Outputs:

--- SQL AST
[:intersect, [:set_quantifier, "distinct"], [:union, [:set_quantifier, "distinct"], [:select_exp, [:set_quantifier, "all"], [:select_list, [:select_item, [:qualified_name, [:range_var_name, "t1"], [:column_name, "mac"]], [:column_name, "mac"]], [:select_item, [:qualified_name, [:range_var_name, "t1"], [:column_name, "building"]], [:column_name, "building"]], [:select_item, [:qualified_name, [:range_var_name, "t1"], [:column_name, "local"]], [:column_name, "local"]]], [:from_clause, [:table_as, [:table_name, :nl], [:range_var_name, "t1"]]]], [:select_exp, [:set_quantifier, "all"], [:select_list, [:select_item, [:qualified_name, [:range_var_name, "t2"], [:column_name, "mac"]], [:column_name, "mac"]], [:select_item, [:qualified_name, [:range_var_name, "t2"], [:column_name, "building"]], [:column_name, "building"]], [:select_item, [:qualified_name, [:range_var_name, "t2"], [:column_name, "local"]], [:column_name, "local"]]], [:from_clause, [:table_as, [:table_name, :sapl], [:range_var_name, "t2"]]]]], [:select_exp, [:set_quantifier, "all"], [:select_list, [:select_item, [:qualified_name, [:range_var_name, "t3"], [:column_name, "mac"]], [:column_name, "mac"]], [:select_item, [:qualified_name, [:range_var_name, "t3"], [:column_name, "building"]], [:column_name, "building"]], [:select_item, [:qualified_name, [:range_var_name, "t3"], [:column_name, "local"]], [:column_name, "local"]]], [:from_clause, [:table_as, [:table_name, :nl], [:range_var_name, "t3"]]]]]

--- Alf::Sql compilation result
((SELECT t1.mac, t1.building, t1.local FROM nl AS t1) UNION (SELECT t2.mac, t2.building, t2.local FROM sapl AS t2)) INTERSECT (SELECT t3.mac, t3.building, t3.local FROM nl AS t3)

--- Alf::Sequel compilation result
SELECT "t1"."mac" AS "mac", "t1"."building" AS "building", "t1"."local" AS "local" FROM "nl" AS "t1" UNION (SELECT "t2"."mac" AS "mac", "t2"."building" AS "building", "t2"."local" AS "local" FROM "sapl" AS "t2") INTERSECT (SELECT "t3"."mac" AS "mac", "t3"."building" AS "building", "t3"."local" AS "local" FROM "nl" AS "t3")
blambeau commented 10 years ago

Fixed by 840beecb9ffb11037ef7fa6cde4ef867170dad4d