-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
still some bugs with precedence that need to be worked out
- Loading branch information
1 parent
728d20f
commit f6b8a7a
Showing
9 changed files
with
293 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module SqlTools | ||
InnerJoin = Data.define(:object, :predicate) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module SqlTools | ||
LeftJoin = Data.define(:object, :predicate) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module SqlTools | ||
class PredicateFilter | ||
def initialize(query) | ||
@query = query | ||
end | ||
|
||
def filter(object) | ||
@stack = [] | ||
filter_recursive(object, @query.predicate) | ||
|
||
right = @stack.pop | ||
|
||
while left = @stack.pop | ||
predicate = Predicate::Binary.new(left, "AND", right) | ||
right = predicate | ||
end | ||
|
||
right | ||
end | ||
|
||
private | ||
|
||
def filter_recursive(object, predicate) | ||
case predicate | ||
when Predicate::Binary | ||
@stack << predicate if filter_recursive(object, predicate.left) || filter_recursive(object, predicate.right) | ||
false | ||
when SqlTools::Column | ||
predicate.table == object | ||
else | ||
raise "Unknown predicate type: #{predicate}" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
module SqlTools | ||
class JoinTest < Minitest::Test | ||
def test_join_predicates | ||
query = query_from_sql(<<~SQL) | ||
SELECT * | ||
FROM table_a a | ||
JOIN table_b b | ||
ON a.id = b.a_id | ||
SQL | ||
|
||
expected = Predicate::Binary.new( | ||
left: Column.new(table: Table.new("table_a", "a"), name: "id"), | ||
operator: "=", | ||
right: Column.new(table: Table.new("table_b", "b"), name: "a_id"), | ||
) | ||
|
||
assert_equal(1, query.joins.size) | ||
assert_equal(expected, query.predicate) | ||
assert_equal(expected, query.joins.first.predicate) | ||
end | ||
|
||
def test_join_predicates_in_the_where | ||
skip("bug in the parser on empty join") | ||
query = query_from_sql(<<~SQL) | ||
SELECT * | ||
FROM table_a a | ||
JOIN table_b b | ||
WHERE a.id = b.a_id | ||
SQL | ||
|
||
expected = Predicate::Binary.new( | ||
left: Column.new(table: Table.new("table_a", "a"), name: "id"), | ||
operator: "=", | ||
right: Column.new(table: Table.new("table_b", "b"), name: "a_id"), | ||
) | ||
|
||
assert_equal(1, query.joins.size) | ||
assert_equal(expected, query.predicate) | ||
assert_equal(expected, query.joins.first.predicate) | ||
end | ||
|
||
def test_join_predicate_spread_out | ||
query = query_from_sql(<<~SQL) | ||
SELECT * | ||
FROM table_a a | ||
JOIN table_b b | ||
ON a.id = b.a_id | ||
WHERE a.shard_id = b.shard_id | ||
SQL | ||
|
||
expected = Predicate::Binary.new( | ||
left: Predicate::Binary.new( | ||
left: Column.new(table: Table.new("table_a", "a"), name: "id"), | ||
operator: "=", | ||
right: Column.new(table: Table.new("table_b", "b"), name: "a_id"), | ||
), | ||
operator: "AND", | ||
right: Predicate::Binary.new( | ||
left: Column.new(table: Table.new("table_a", "a"), name: "shard_id"), | ||
operator: "=", | ||
right: Column.new(table: Table.new("table_b", "b"), name: "shard_id"), | ||
), | ||
) | ||
|
||
assert_equal(1, query.joins.size) | ||
assert_equal(expected, query.predicate) | ||
assert_equal(expected, query.joins.first.predicate) | ||
end | ||
|
||
def test_many_join_predicates_spread_out | ||
skip("predicate precedence is messed up here") | ||
query = query_from_sql(<<~SQL) | ||
SELECT * | ||
FROM table_a a | ||
JOIN table_b b | ||
ON a.id = b.a_id | ||
AND a.shard_id = b.shard_id | ||
WHERE a.user_id = b.user_id | ||
AND a.id = 4 | ||
SQL | ||
|
||
assert_equal( | ||
Predicate::Binary.new( | ||
left: Predicate::Binary.new( | ||
left: Predicate::Binary.new( | ||
left: Predicate::Binary.new( | ||
left: Column.new(table: Table.new("table_a", "a"), name: "id"), | ||
operator: "=", | ||
right: Column.new(table: Table.new("table_b", "b"), name: "a_id"), | ||
), | ||
operator: "AND", | ||
right: Predicate::Binary.new( | ||
left: Column.new(table: Table.new("table_a", "a"), name: "shard_id"), | ||
operator: "=", | ||
right: Column.new(table: Table.new("table_b", "b"), name: "shard_id"), | ||
), | ||
), | ||
operator: "AND", | ||
right: Predicate::Binary.new( | ||
left: Column.new(table: Table.new("table_a", "a"), name: "user_id"), | ||
operator: "=", | ||
right: Column.new(table: Table.new("table_b", "b"), name: "user_id"), | ||
), | ||
), | ||
operator: "AND", | ||
right: Predicate::Binary.new( | ||
left: Column.new(table: Table.new("table_a", "a"), name: "id"), | ||
operator: "=", | ||
right: "4", | ||
), | ||
), | ||
query.predicate, | ||
) | ||
|
||
assert_equal(1, query.joins.size) | ||
assert_equal( | ||
Predicate::Binary.new( | ||
left: Predicate::Binary.new( | ||
left: Predicate::Binary.new( | ||
left: Column.new(table: Table.new("table_a", "a"), name: "id"), | ||
operator: "=", | ||
right: Column.new(table: Table.new("table_b", "b"), name: "a_id"), | ||
), | ||
operator: "AND", | ||
right: Predicate::Binary.new( | ||
left: Column.new(table: Table.new("table_a", "a"), name: "shard_id"), | ||
operator: "=", | ||
right: Column.new(table: Table.new("table_b", "b"), name: "shard_id"), | ||
), | ||
), | ||
operator: "AND", | ||
right: Predicate::Binary.new( | ||
left: Column.new(table: Table.new("table_a", "a"), name: "user_id"), | ||
operator: "=", | ||
right: Column.new(table: Table.new("table_b", "b"), name: "user_id"), | ||
), | ||
), | ||
query.joins.first.predicate, | ||
) | ||
end | ||
end | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters