Skip to content

Commit

Permalink
Merge pull request #71 from factorialco/outer-join-for-arel
Browse files Browse the repository at this point in the history
Change to outer join
  • Loading branch information
txus authored Dec 13, 2022
2 parents 4e10d27 + 0fff99f commit de83165
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 15 deletions.
14 changes: 12 additions & 2 deletions .github/workflows/gem-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,19 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Set up Ruby 2.7
uses: actions/setup-ruby@v1
uses: ruby/setup-ruby@v1
with:
ruby-version: 2.7
bundler-cache: true # runs 'bundle install' and caches installed gems automatically

- name: Run tests
run: bundle exec rspec -f j -o tmp/rspec_results.json -f p

- name: RSpec Report
uses: SonicGarden/rspec-report-action@v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
json-path: tmp/rspec_results.json
if: always()

- name: Publish to GPR
run: |
Expand Down
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.7.4
8 changes: 4 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
fql (0.2.6)
fql (0.2.11)
activerecord (~> 6.0)
i18n (~> 1.8)
sorbet-rails (~> 0.7.3)
Expand Down Expand Up @@ -168,12 +168,12 @@ GEM
polyfill (~> 1.8)
safe_type (~> 1.1, >= 1.1.1)
sorbet-runtime (>= 0.4.4704)
sorbet-rails (0.7.32)
sorbet-rails (0.7.34)
method_source (>= 0.9.2)
parlour (>= 4.0.1)
parser (>= 2.7)
sorbet-coerce (>= 0.2.6)
sorbet-runtime (>= 0.5)
sorbet-runtime (>= 0.5.9892)
sorbet-runtime (0.5.10481)
sorbet-static (0.5.9531-universal-darwin-14)
sorbet-struct-comparable (1.3.0)
Expand Down Expand Up @@ -206,7 +206,7 @@ GEM
yard-sorbet (0.7.0)
sorbet-runtime (>= 0.5)
yard (>= 0.9)
zeitwerk (2.6.1)
zeitwerk (2.6.6)

PLATFORMS
ruby
Expand Down
2 changes: 1 addition & 1 deletion lib/fql/backend/arel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def compile_expression(expr)
{
joins: state[:joins] + [
arel_table
.join(new_aliased_relation)
.join(new_aliased_relation, ::Arel::Nodes::OuterJoin)
.on(
(state[:aliased_relation] || arel_table)[assoc.join_foreign_key]
.eq(new_aliased_relation[assoc.join_primary_key])
Expand Down
2 changes: 1 addition & 1 deletion lib/fql/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# typed: false
module FQL
VERSION = "0.2.10".freeze
VERSION = "0.2.11".freeze
end
14 changes: 7 additions & 7 deletions spec/fql/backend/arel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@

context "when the name does not refer to self" do
it "compiles to a join query" do
expect(F.eq(F.attr(F.rel(:address), :country), "es")).to compile_to('SELECT DISTINCT "users".* FROM "users" INNER JOIN addresses "address" ON "users"."id" = "address"."tenant_id" WHERE "address"."country" = \'es\'')
expect(F.eq(F.attr(F.rel(:address), :country), "es")).to compile_to('SELECT DISTINCT "users".* FROM "users" LEFT OUTER JOIN addresses "address" ON "users"."id" = "address"."tenant_id" WHERE "address"."country" = \'es\'')
end
end

Expand All @@ -138,8 +138,8 @@
).to compile_to(
[
'SELECT DISTINCT "users".* FROM "users"',
'INNER JOIN addresses "address" ON "users"."id" = "address"."tenant_id"',
'INNER JOIN cities "city" ON "address"."city_id" = "city"."id"',
'LEFT OUTER JOIN addresses "address" ON "users"."id" = "address"."tenant_id"',
'LEFT OUTER JOIN cities "city" ON "address"."city_id" = "city"."id"',
'WHERE "city"."name" = \'Barcelona\''
].join(" ")
)
Expand All @@ -151,7 +151,7 @@
subject { described_class.new(User, vars: { country: "fr" }, library: library) }

it "lookups up a variable at compile time" do
expect(F.eq(F.attr(F.rel(:address), :country), F.var(:country))).to compile_to('SELECT DISTINCT "users".* FROM "users" INNER JOIN addresses "address" ON "users"."id" = "address"."tenant_id" WHERE "address"."country" = \'fr\'')
expect(F.eq(F.attr(F.rel(:address), :country), F.var(:country))).to compile_to('SELECT DISTINCT "users".* FROM "users" LEFT OUTER JOIN addresses "address" ON "users"."id" = "address"."tenant_id" WHERE "address"."country" = \'fr\'')
end

context "when the variable does not exist at compile time" do
Expand All @@ -167,15 +167,15 @@
subject { described_class.new(User, vars: {}, library: library) }

it "can call a simple function from the library" do
expect(F.eq(F.call(:country), "fr")).to compile_to('SELECT DISTINCT "users".* FROM "users" INNER JOIN addresses "address" ON "users"."id" = "address"."tenant_id" WHERE "address"."country" = \'fr\'')
expect(F.eq(F.call(:country), "fr")).to compile_to('SELECT DISTINCT "users".* FROM "users" LEFT OUTER JOIN addresses "address" ON "users"."id" = "address"."tenant_id" WHERE "address"."country" = \'fr\'')
end

it "can call a simple function with array" do
expect(F.eq(F.call(:country), F.call(:echo, ["fr", "EN"]))).to compile_to('SELECT DISTINCT "users".* FROM "users" INNER JOIN addresses "address" ON "users"."id" = "address"."tenant_id" WHERE "address"."country" IN (\'fr\', \'EN\')')
expect(F.eq(F.call(:country), F.call(:echo, ["fr", "EN"]))).to compile_to('SELECT DISTINCT "users".* FROM "users" LEFT OUTER JOIN addresses "address" ON "users"."id" = "address"."tenant_id" WHERE "address"."country" IN (\'fr\', \'EN\')')
end

it "can call a parameterized function from the library" do
expect(F.eq(F.call(:country), F.call(:echo, "fr"))).to compile_to('SELECT DISTINCT "users".* FROM "users" INNER JOIN addresses "address" ON "users"."id" = "address"."tenant_id" WHERE "address"."country" = \'fr\'')
expect(F.eq(F.call(:country), F.call(:echo, "fr"))).to compile_to('SELECT DISTINCT "users".* FROM "users" LEFT OUTER JOIN addresses "address" ON "users"."id" = "address"."tenant_id" WHERE "address"."country" = \'fr\'')
end

it "can call a parameterized boolean function from the library" do
Expand Down

0 comments on commit de83165

Please sign in to comment.