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(spanner): add initial PostgreSQL samples #8617

Merged
merged 3 commits into from
Mar 30, 2022

Conversation

devbww
Copy link
Contributor

@devbww devbww commented Mar 29, 2022

These samples are sufficiently different from the "Google Standard"
dialect ones that they deserve their own source file (not to mention
that samples.cc is becoming unmanageable). If there ends up being
any significant commonality between the samples implementations, we
can factor it out later.


This change is Reviewable

@product-auto-label product-auto-label bot added api: spanner Issues related to the Spanner API. samples Issues that are directly related to samples. labels Mar 29, 2022
@snippet-bot
Copy link

snippet-bot bot commented Mar 29, 2022

Here is the summary of changes.

You are about to add 5 region tags.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@google-cloud-cpp-bot
Copy link
Collaborator

Google Cloud Build Logs
For commit: fc4fc84a7f47228f768d1e5dfeb0804d65506c8d

ℹ️ NOTE: Kokoro logs are linked from "Details" below.

@codecov
Copy link

codecov bot commented Mar 29, 2022

Codecov Report

Merging #8617 (cc31d17) into main (b2dd9b6) will decrease coverage by 0.14%.
The diff coverage is 2.01%.

@@            Coverage Diff             @@
##             main    #8617      +/-   ##
==========================================
- Coverage   93.79%   93.64%   -0.15%     
==========================================
  Files        1449     1450       +1     
  Lines      123769   123968     +199     
==========================================
+ Hits       116087   116092       +5     
- Misses       7682     7876     +194     
Impacted Files Coverage Δ
google/cloud/spanner/samples/postgresql_samples.cc 2.01% <2.01%> (ø)
...cloud/pubsub/internal/subscription_session_test.cc 97.75% <0.00%> (-0.25%) ⬇️
google/cloud/completion_queue_test.cc 96.94% <0.00%> (-0.20%) ⬇️
google/cloud/pubsub/samples/samples.cc 92.10% <0.00%> (+0.07%) ⬆️
...le/cloud/internal/default_completion_queue_impl.cc 97.72% <0.00%> (+0.56%) ⬆️
google/cloud/bigtable/internal/common_client.cc 97.14% <0.00%> (+1.42%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b2dd9b6...cc31d17. Read the comment docs.

@devbww devbww marked this pull request as ready for review March 29, 2022 22:09
@devbww devbww requested a review from a team as a code owner March 29, 2022 22:09
@@ -16,7 +16,7 @@

function (spanner_client_define_samples)
set(spanner_client_integration_samples # cmake-format: sort
samples.cc)
pg_samples.cc samples.cc)
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 the file should have a more obvious name like postgresql_samples.cc or postgres_samples.cc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Meh. OK, postgresql_samples.cc it is.

std::int64_t budget) {
auto sql = google::cloud::spanner::SqlStatement(
"UPDATE Albums SET MarketingBudget = $1"
" WHERE SingerId = $3 AND AlbumId = $2",
Copy link
Member

Choose a reason for hiding this comment

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

nit: can we swap 2 and 3 here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It doesn't look like there is a definitive source of what these samples are supposed to look like (just their names), but this one says it should be similar to the non-pg version, where these were ordered this way, although the non-pg versions uses names to bind instead of indices. I could imagine that explicitly using out-of-order indices is a teaching point, but probably not worth it. So, yes, swapped.

return 0;
}

void SampleBanner(std::string const& name) {
Copy link
Member

Choose a reason for hiding this comment

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

We could factor this out as it appears in samples.cc as well. (We could also refactor the Command/make_command_entry stuff too). Probably not something we want to do in this PR. Maybe not something we ever want to do.

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 mentioned possible refactorings down the line in the original PR description, but, yes, we should certainly postpone that decision until we see where things fall. (At the moment it looks like it would be a net complication.)

if (google::cloud::internal::GetEnv(auto_run).value_or("") == "yes") {
return RunAll();
}
std::string program(ac ? (ac--, *av++) : "pg_samples");
Copy link
Member

Choose a reason for hiding this comment

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

TIL argc can be 0

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah. It can be instructive to try executing common programs with no arguments and seeing how many survive the ordeal.

};
auto it = commands.find(argv[0]);
if (it == commands.end()) {
throw std::runtime_error(argv[0] + ": Unknown command");
Copy link
Member

Choose a reason for hiding this comment

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

So how can the user learn which commands are available? Well they can look in the code, but I don't think they should have to. Can we print all commands here?

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 added a "help" command to list all the available commands, and mention using it in this error message.

Comment on lines 362 to 365
if (ac == 0) {
throw std::runtime_error("Usage: " + program +
" <command> [<argument> ...]");
}
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 we also want to print the available commands in this failure case.

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 added a "help" command to list all the available commands, and mention using it in this error message.

These samples are sufficiently different from the "Google Standard"
dialect ones that they deserve their own source file (not to mention
that `samples.cc` is becoming unmanageable).  If there ends up being
any significant commonality between the samples implementations, we
can factor it out later.
@devbww devbww force-pushed the database-dialect-samples branch from fc4fc84 to 5ac6fd3 Compare March 30, 2022 02:57
@google-cloud-cpp-bot
Copy link
Collaborator

Google Cloud Build Logs
For commit: 5ac6fd3fd601379ddc7c6b580a09c78a064b873c

ℹ️ NOTE: Kokoro logs are linked from "Details" below.

@google-cloud-cpp-bot
Copy link
Collaborator

Google Cloud Build Logs
For commit: b33cce7e0d7b850e7a418996ddfd36739478c17f

ℹ️ NOTE: Kokoro logs are linked from "Details" below.

Copy link
Member

@dbolduc dbolduc left a comment

Choose a reason for hiding this comment

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

Reviewable status: 0 of 3 files reviewed, 6 unresolved discussions (waiting on @dbolduc and @devbww)


google/cloud/spanner/samples/pg_samples.cc, line 177 at r1 (raw file):

Previously, devbww (Bradley White) wrote…

It doesn't look like there is a definitive source of what these samples are supposed to look like (just their names), but this one says it should be similar to the non-pg version, where these were ordered this way, although the non-pg versions uses names to bind instead of indices. I could imagine that explicitly using out-of-order indices is a teaching point, but probably not worth it. So, yes, swapped.

Hm, for the record I was thinking:

    auto sql = google::cloud::spanner::SqlStatement(
        "UPDATE Albums SET MarketingBudget = $1"
        "  WHERE SingerId = $2 AND AlbumId = $3",
        {{"p1", google::cloud::spanner::Value(budget)},
         {"p2", google::cloud::spanner::Value(singer_id)},
         {"p3", google::cloud::spanner::Value(album_id)}});

But this is all a nit anyway.


google/cloud/spanner/samples/pg_samples.cc, line 263 at r1 (raw file):

Previously, devbww (Bradley White) wrote…

I added a "help" command to list all the available commands, and mention using it in this error message.

works for me

Copy link
Contributor Author

@devbww devbww left a comment

Choose a reason for hiding this comment

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

Reviewable status: 0 of 3 files reviewed, 5 unresolved discussions (waiting on @dbolduc)


google/cloud/spanner/samples/pg_samples.cc, line 177 at r1 (raw file):

Previously, dbolduc (Darren Bolduc) wrote…

Hm, for the record I was thinking:

    auto sql = google::cloud::spanner::SqlStatement(
        "UPDATE Albums SET MarketingBudget = $1"
        "  WHERE SingerId = $2 AND AlbumId = $3",
        {{"p1", google::cloud::spanner::Value(budget)},
         {"p2", google::cloud::spanner::Value(singer_id)},
         {"p3", google::cloud::spanner::Value(album_id)}});

But this is all a nit anyway.

I was going to say that it makes a little more sense this way because the Albums key is (AlbumId, SingerId) (for better or worse), but it looks like its the other way around. I'm going to change that.

@google-cloud-cpp-bot
Copy link
Collaborator

Google Cloud Build Logs
For commit: cc31d17837cad7c26ac475c6b03001eb395845ca

ℹ️ NOTE: Kokoro logs are linked from "Details" below.

@devbww devbww merged commit 07b9fe0 into googleapis:main Mar 30, 2022
@devbww devbww deleted the database-dialect-samples branch March 30, 2022 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: spanner Issues related to the Spanner API. samples Issues that are directly related to samples.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants