Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ids as a function alias #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ It's not so smart right now, but here's a cheatsheet:
| `:count` | `COUNT(*)`
| `:count_column1`, `:column1_count` | `COUNT(column1)` (doesn't count NULL's in that column)
| `:count_distinct_column1`, `:column1_distinct_count` | `COUNT(DISTINCT column1)`
| `:ids` | `ARRAY_AGG(id)` (only works on Postgres)
| `:max_column1`, `:column1_max`, `:maximum_column1`, `:column1_maximum` | `MAX(column1)`
| `:min_column1`, `:column1_min`, `:minimum_column1`, `:column1_minimum` | `MIN(column1)`
| `:avg_column1`, `:column1_avg`, `:average_column1`, `:column1_average` | `AVG(column1)`
Expand Down
2 changes: 2 additions & 0 deletions lib/calculate-all/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def decode_function_aliases(aliases)
"MAX(#{$1})"
when /^(.*)_minimum$/, /^minimum_(.*)$/
"MIN(#{$1})"
when :ids
'ARRAY_AGG(id)'
else
raise ArgumentError, "Can't recognize function alias #{key}"
end
Expand Down
9 changes: 9 additions & 0 deletions test/calculate_all_postgresql_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,13 @@ def test_returns_array_on_grouped_array_aggregate
assert_equal expected, Order.group(:kind).calculate_all('ARRAY_AGG(currency ORDER BY id)')
end

def test_ids_aggregation
create_orders
expected = {
'RUB' => Order.where(currency: 'RUB').ids,
'USD' => Order.where(currency: 'USD').ids
}
assert_equal(expected, Order.group(:currency).calculate_all(:ids))
end

end