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 Queryable#group_count functionality #778

Merged
merged 1 commit into from
Jan 10, 2022

Conversation

grepsedawk
Copy link
Contributor

No description provided.

queryable: schema_class.name,
) do |rs|
k = query.groups.map do
rs.read PG::PGValue | Int64 # TODO: Why isn't Int64 part of PGValue?
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jwoertink @robcole and I were talking about this, not sure why it's not a part of PG::PGValue. Maybe @will knows?

Copy link
Member

Choose a reason for hiding this comment

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

I think it may just be an oversight. I'd open up a PR adding that, plus the other supported Ints.

Copy link
Member

@jwoertink jwoertink left a comment

Choose a reason for hiding this comment

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

This is definitely a great start. We can get some more eyes on this, and then some specs and stuff, but good work!

src/avram/queryable.cr Outdated Show resolved Hide resolved
Comment on lines 246 to 250
def group_count
database.query_all(
Copy link
Member

Choose a reason for hiding this comment

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

We will want to make sure we raise an error here if you try to call it when there's no groups. Would be even better if we could make it a compile-time error somehow...

Copy link

Choose a reason for hiding this comment

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

It still works if there aren't groups. It will return a count of all records with an empty array as the grouping key.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep! It becomes 1 group of [], which is canonically correct!

src/avram/query_builder.cr Show resolved Hide resolved
src/avram/queryable.cr Outdated Show resolved Hide resolved
src/avram/query_builder.cr Outdated Show resolved Hide resolved
Copy link
Member

@matthewmcgarvey matthewmcgarvey left a comment

Choose a reason for hiding this comment

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

Nice! Looks like you made something pretty great!

spec/avram/queryable_spec.cr Outdated Show resolved Hide resolved
Copy link
Member

@matthewmcgarvey matthewmcgarvey left a comment

Choose a reason for hiding this comment

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

I'm happy with this 🎉

@grepsedawk
Copy link
Contributor Author

@matthewmcgarvey Still a draft but thanks for your support!
Where would I go about adding docs? (or is that seperately, on the lucky website repo?)

@grepsedawk grepsedawk changed the title WIP: Add Queryable#group_count functionality Add Queryable#group_count functionality Jan 1, 2022
@grepsedawk grepsedawk marked this pull request as ready for review January 1, 2022 04:56
Copy link
Member

@jwoertink jwoertink left a comment

Choose a reason for hiding this comment

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

Overall I think this is great. Really great work on this.

My only concern is the issue when there's no grouping. It seems that when there's no group, you get an empty array. Then ([] + ["COUNT(*)"]).statement would just end up being the same as the select_count method. If you feel this is a case that may be wanted, we could have an escape hatch method group_count? that would just give the value. Then group_count would do the raise. I say group_count for raising instead of group_count! because I would want it to be the default use. What are your thoughts?

spec/avram/queryable_spec.cr Outdated Show resolved Hide resolved
spec/avram/queryable_spec.cr Outdated Show resolved Hide resolved
spec/avram/queryable_spec.cr Outdated Show resolved Hide resolved
src/avram/queryable.cr Outdated Show resolved Hide resolved
src/avram/queryable.cr Show resolved Hide resolved
@matthewmcgarvey
Copy link
Member

@grepsedawk just need to fix the formatting issue

 By using `Model.group(&.column).group_count`, it allows for a Hash of
 mapped values to their respective counts

 Resolves luckyframework#777
@jwoertink jwoertink merged commit 52c2dd9 into luckyframework:master Jan 10, 2022
@jwoertink
Copy link
Member

🎉

@grepsedawk grepsedawk deleted the do-the-impossible branch January 10, 2022 23:05
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.

5 participants