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 BulkResponse wrapper for improved decoding of HTTP bulk responses #649

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

amotl
Copy link
Member

@amotl amotl commented Oct 2, 2024

About

CrateDB HTTP bulk responses include rowcount= items, either signalling if a bulk operation succeeded or failed.

  • success means rowcount=1
  • failure means rowcount=-2

https://cratedb.com/docs/crate/reference/en/latest/interfaces/http.html#error-handling

References

The code is coming from CrateDB Toolkit, but is generally usable beyond there.

@cla-bot cla-bot bot added the cla-signed label Oct 2, 2024
src/crate/client/test_result.py Outdated Show resolved Hide resolved
Copy link
Member Author

Choose a reason for hiding this comment

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

tests.py is the entrypoint for zope.testing to discover test cases. Many utility functions had to be refactored away from here, in order to avoid circular imports.

@amotl amotl force-pushed the bulk-response-wrapper branch 3 times, most recently from 7ef36cd to ccbffd2 Compare October 2, 2024 21:06
@amotl amotl requested review from seut and matriv October 2, 2024 21:13
@amotl amotl marked this pull request as ready for review October 2, 2024 21:13
Copy link
Contributor

@matriv matriv left a comment

Choose a reason for hiding this comment

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

Thx for improving the bulk response handling. Left some comments.


https://cratedb.com/docs/crate/reference/en/latest/interfaces/http.html#error-handling
"""
if self.records is None or self.results is None:
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd rather raise errors (even separate errors in this case. (meaning if self.records is "None" one error, if self.results is "None", a separate error.

Copy link
Member Author

Choose a reason for hiding this comment

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

The callsite at 1 likes to have the situation handled gracefully, but maybe that is just because I wanted to have it "convenient". Please poke me another time if you think the component should really raise an exception in this situation, and I will be happy to adjust.

Footnotes

  1. https://github.com/crate/cratedb-toolkit/blob/v0.0.27/cratedb_toolkit/io/core.py#L147-L149

Copy link
Contributor

Choose a reason for hiding this comment

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

imho, and if I understand it correctly those 2 shouldn't be none unless something has gone really wrong. So why not handle it properly, catching the exceptions in the toolkit if needed?

Copy link
Member Author

Choose a reason for hiding this comment

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

Improved with 4da893a, thanks!

src/crate/client/test_result.py Outdated Show resolved Hide resolved
src/crate/client/test_result.py Outdated Show resolved Hide resolved
@amotl amotl requested a review from matriv October 3, 2024 12:47
@amotl amotl force-pushed the bulk-response-wrapper branch 2 times, most recently from 50347d4 to 433dfdf Compare October 3, 2024 13:03
Comment on lines 61 to 67
# Run a batch insert that is empty.
with self.assertRaises(ProgrammingError) as cm:
cursor.executemany("INSERT INTO foobar (id, name) VALUES (?, ?)", [])
self.assertEqual(
cm.exception.message,
"SQLParseException[The query contains a parameter placeholder $1, "
"but there are only 0 parameter values]")
Copy link
Member Author

Choose a reason for hiding this comment

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

I see. Running an empty batch into executemany() will already raise an exception here. In this spirit, it is probably sane to also let BulkResponse raise an exception?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd say so, because it's the case that the batch is failing as a whole before the execution is started.

CrateDB HTTP bulk responses include `rowcount=` items, either signalling
if a bulk operation succeeded or failed.

- success means `rowcount=1`
- failure means `rowcount=-2`

https://cratedb.com/docs/crate/reference/en/latest/interfaces/http.html#error-handling
self.assertEqual(result, [{"rowcount": 1}, {"rowcount": -2}])

# Verify decoded response.
bulk_response = BulkResponse(invalid_records, result)
Copy link
Contributor

Choose a reason for hiding this comment

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

Looking at this more carefully, I don't like that BulkResponse is something you need con construct manually. Couldn't we directly return it from the insert execution, instead of a list of BulkResultItem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants