Skip to content

Add in GROUP BY querying#234

Merged
jwoertink merged 7 commits intomasterfrom
features/193
Sep 14, 2019
Merged

Add in GROUP BY querying#234
jwoertink merged 7 commits intomasterfrom
features/193

Conversation

@jwoertink
Copy link
Member

@jwoertink jwoertink commented Sep 13, 2019

Fixes #193

  • - Low level group_by SQL generation
  • - High level Query access to low level group
  • - type-safe group_by access
  • - A simple way to get counts from a group like rails User.group(:status).count #=> {"active" => 100, "inactive" => 4}

We have select_count currently, but maybe that needs to become aware if there's some grouping?

@jwoertink jwoertink marked this pull request as ready for review September 13, 2019 19:38
@jwoertink jwoertink changed the title WIP: Add in GROUP BY querying Add in GROUP BY querying Sep 13, 2019
@paulcsmith
Copy link
Member

Very small comments. Otherwise looks awesome!

For the count I think we should have a different method otherwise the return type will be weird. How about “select_counts_by_group” or “select_group_counts”?

@paulcsmith
Copy link
Member

Can be added in a separate PR though. This is useful even without a count method!

Copy link
Member

@paulcsmith paulcsmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specs look good and overall LGTM. Just retying to understand the result of group_by because it looks the same as without group.

users = UserQuery.new.group(&.age).group(&.id)
users.query.statement.should eq "SELECT #{User::COLUMN_SQL} FROM users GROUP BY users.age, users.id"
users.map(&.name).should contain "Dwight"
users.map(&.name).should eq ["Dwight", "Michael", "Jim"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh so does it just return an array? What does group by do in this case. I thought it'd be a hash or something

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It returns an array here of .map(), but UserQuery.new.group(&.age).group(&.id) just returns the BaseQuery object like normal. It is confusing though because technically in this query, the group is useless.

I see this more as building blocks for the next parts to come as well as the underlying access. But if you have suggestions, or whatever, I'm all ears. Postgres grouping hurts my head lol

@paulcsmith
Copy link
Member

Yeah let’s merge it! I think this is a good starting point

@jwoertink jwoertink merged commit bbb127c into master Sep 14, 2019
@jwoertink jwoertink deleted the features/193 branch September 14, 2019 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add missing GROUP BY stuff

2 participants