Skip to content

Commit 21c6c22

Browse files
authored
Merge PR rails#40491
2 parents ea1ca5d + a738295 commit 21c6c22

File tree

8 files changed

+72
-0
lines changed

8 files changed

+72
-0
lines changed

activerecord/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* Add support for FILTER clause (SQL:2003) to Arel.
2+
3+
Currently supported by PostgreSQL 9.4+ and SQLite 3.30+.
4+
5+
*Andrey Novikov*
6+
17
* Automatically set timestamps on record creation during bulk insert/upsert
28

39
Prior to this change, only updates during an upsert operation (e.g. `upsert_all`) would touch timestamps (`updated_{at,on}`). Now, record creations also touch timestamp columns (`{created,updated}_{at,on}`).

activerecord/lib/arel.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
require "arel/expressions"
99
require "arel/predications"
10+
require "arel/filter_predications"
1011
require "arel/window_predications"
1112
require "arel/math"
1213
require "arel/alias_predication"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
module Arel
4+
module FilterPredications
5+
def filter(expr)
6+
Nodes::Filter.new(self, expr)
7+
end
8+
end
9+
end

activerecord/lib/arel/nodes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
# binary
2929
require "arel/nodes/binary"
3030
require "arel/nodes/equality"
31+
require "arel/nodes/filter"
3132
require "arel/nodes/in"
3233
require "arel/nodes/join_source"
3334
require "arel/nodes/delete_statement"

activerecord/lib/arel/nodes/filter.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
3+
module Arel
4+
module Nodes
5+
class Filter < Binary
6+
include Arel::WindowPredications
7+
include Arel::AliasPredication
8+
end
9+
end
10+
end

activerecord/lib/arel/nodes/function.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Arel # :nodoc: all
44
module Nodes
55
class Function < Arel::Nodes::NodeExpression
66
include Arel::WindowPredications
7+
include Arel::FilterPredications
78
attr_accessor :expressions, :alias, :distinct
89

910
def initialize(expr, aliaz = nil)

activerecord/lib/arel/visitors/to_sql.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,13 @@ def visit_Arel_Nodes_Window(o, collector)
245245
collector << ")"
246246
end
247247

248+
def visit_Arel_Nodes_Filter(o, collector)
249+
visit o.left, collector
250+
collector << " FILTER (WHERE "
251+
visit o.right, collector
252+
collector << ")"
253+
end
254+
248255
def visit_Arel_Nodes_Rows(o, collector)
249256
if o.expr
250257
collector << "ROWS "
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "../helper"
4+
5+
module Arel
6+
module Nodes
7+
class ::FilterTest < Arel::Spec
8+
describe "Filter" do
9+
it "should add filter to expression" do
10+
table = Arel::Table.new :users
11+
_(table[:id].count.filter(table[:income].gteq(40_000)).to_sql).must_be_like %{
12+
COUNT("users"."id") FILTER (WHERE "users"."income" >= 40000)
13+
}
14+
end
15+
16+
describe "as" do
17+
it "should alias the expression" do
18+
table = Arel::Table.new :users
19+
_(table[:id].count.filter(table[:income].gteq(40_000)).as("rich_users_count").to_sql).must_be_like %{
20+
COUNT("users"."id") FILTER (WHERE "users"."income" >= 40000) AS rich_users_count
21+
}
22+
end
23+
end
24+
25+
describe "over" do
26+
it "should reference the window definition by name" do
27+
table = Arel::Table.new :users
28+
window = Arel::Nodes::Window.new.partition(table[:year])
29+
_(table[:id].count.filter(table[:income].gteq(40_000)).over(window).to_sql).must_be_like %{
30+
COUNT("users"."id") FILTER (WHERE "users"."income" >= 40000) OVER (PARTITION BY "users"."year")
31+
}
32+
end
33+
end
34+
end
35+
end
36+
end
37+
end

0 commit comments

Comments
 (0)