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

Make total_rows available on RowIterator before iteration #7622

Merged
merged 3 commits into from
Apr 16, 2019

Conversation

tswast
Copy link
Contributor

@tswast tswast commented Mar 30, 2019

After running a query, the total number of rows is available from the
call to the getQueryResults API. This commit plumbs the total rows
through to the faux Table created in QueryJob.results and then on
through to the RowIterator created by list_rows.

Fixes #6117.

Since this feature is more useful when list_rows has a full Table resource, this PR is based on #7621.

@tswast tswast added type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. api: bigquery Issues related to the BigQuery API. do not merge Indicates a pull request not ready for merge, due to either quality or timing. labels Mar 30, 2019
@tswast tswast requested a review from crwilcox as a code owner March 30, 2019 00:36
@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Mar 30, 2019
self._total_rows = None
if table is not None and hasattr(table, "num_rows"):
self._total_rows = table.num_rows
Copy link
Contributor

Choose a reason for hiding this comment

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

This could simplify down to:

        self._total_rows = getattr(table, "num_rows", None)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. Done in 99441d7.

rows = list(result)
self.assertEqual(len(rows), 1)
self.assertEqual(rows[0].col1, "abc")
self.assertEqual(result.total_rows, 1)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you mean to test explicitly the case where the initial query result resource has a different number of rows than the count got by iteration? If so, then a comment above stating that would be helpful.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that's intentional. You're right that it looks wrong. I suspect it'll be quite rare in practice, but it can happen, especially when append query jobs are involved. Added comments in 99441d7.

After running a query, the total number of rows is available from the
call to the getQueryResults API. This commit plumbs the total rows
through to the faux Table created in QueryJob.results and then on
through to the RowIterator created by list_rows.
Use getattr instead of protecting with hasattr in the RowIterator
constructor.

Add comments about intentionally conflicting values for total_rows.
@tswast tswast added kokoro:force-run Add this label to force Kokoro to re-run the tests. and removed do not merge Indicates a pull request not ready for merge, due to either quality or timing. labels Apr 1, 2019
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Apr 1, 2019
@tswast tswast requested review from a team and tseaver April 2, 2019 22:53
@yoshi-automation yoshi-automation added the 🚨 This issue needs some love. label Apr 6, 2019
@tswast tswast merged commit 0bccf6c into googleapis:master Apr 16, 2019
@tswast tswast deleted the issue6117-total_rows branch April 16, 2019 18:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: bigquery Issues related to the BigQuery API. cla: yes This human has signed the Contributor License Agreement. 🚨 This issue needs some love. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BigQuery: populate RowIterator.total_rows after running a query job
6 participants