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

feat: add aggregate example #187

Merged
merged 4 commits into from
Sep 8, 2021
Merged

feat: add aggregate example #187

merged 4 commits into from
Sep 8, 2021

Conversation

Hoverbear
Copy link
Contributor

Adds a aggregate example as demonstrated by @laysakura in #102 . Thanks!!!

@Hoverbear Hoverbear self-assigned this Sep 3, 2021
@Hoverbear
Copy link
Contributor Author

Based on some feedback from @eeeebbbbrrrr I migrated this to using PgVarlena<T>, this should yield better performance.

@Hoverbear Hoverbear changed the base branch from master to develop September 8, 2021 15:47
Copy link
Contributor

@timClicks timClicks 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 really great Ana. I have a couple of questions and a suggestion or two, for my own understanding mostly, but nothing that would block merging. Well done.

.DS_Store
.idea/
/target
*.iml
Copy link
Contributor

Choose a reason for hiding this comment

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

out of interest, what's an .iml file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I dunno!

Copy link
Contributor

Choose a reason for hiding this comment

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

It’s a CLion project file

pgx-examples/aggregate/.gitignore Show resolved Hide resolved
@@ -0,0 +1,5 @@
comment = 'aggregate: Created by pgx'
Copy link
Contributor

Choose a reason for hiding this comment

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

Thought: perhaps we should have a URL that people can visit so they know what pgx is i they encounter it in their logs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I suggest opening an issue if you feel passionate about this in a general case! I think most users would set this to what their Cargo.toml description is!

pg_module_magic!();


use serde::{Deserialize, Serialize};
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this where we want this import?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I keep forgetting we don't run rustfmt on CI...


let mut split = input.to_bytes().split(|b| *b == b',');
let sum = split.next().map(|value|
i32::from_str(unsafe { std::str::from_utf8_unchecked(value) }).expect("invalid i32")
Copy link
Contributor

Choose a reason for hiding this comment

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

In the error message, should we match the Postgres type name?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure this is important, it's an fairly small example. :)

Comment on lines 38 to 40
buffer.push_str(&self.sum.to_string());
buffer.push(',');
buffer.push_str(&self.n.to_string());
Copy link
Contributor

Choose a reason for hiding this comment

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

Personal preference only. Slightly easier to skim read.

Suggested change
buffer.push_str(&self.sum.to_string());
buffer.push(',');
buffer.push_str(&self.n.to_string());
buffer.push_str(&format!("{},{}"), self.sum, self.n);

Copy link
Contributor

Choose a reason for hiding this comment

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

That would be doing allocations in what might be a hot path for Postgres. The idea of StringBuffer is to save that.

Copy link
Contributor

Choose a reason for hiding this comment

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

One of the reasons I considered using format was that it would only involve 1 allocation rather than 2 (each call to .to_string()).

Copy link
Contributor

Choose a reason for hiding this comment

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

Fair. Push it

Signed-off-by: Ana Hobden <[email protected]>
Signed-off-by: Ana Hobden <[email protected]>
@Hoverbear Hoverbear merged commit 882343b into develop Sep 8, 2021
@Hoverbear Hoverbear added this to the 0.2.0 milestone Sep 23, 2021
This was referenced Sep 24, 2021
@Hoverbear Hoverbear deleted the feat-example-aggregate branch February 25, 2022 22:00
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.

3 participants