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

BQ: Empty array when no results #184

Merged
merged 2 commits into from
Feb 16, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bigquery/api/sync_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def main(project_id, query, timeout, num_retries):
pageToken=page_token,
**query_job['jobReference']).execute(num_retries=2)

print(json.dumps(page['rows']))
print(json.dumps(page.get('rows', [])))
Copy link
Contributor

Choose a reason for hiding this comment

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

I would prefer None over an empty list. How does json.dumps() respond when passed 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.

$ python3 -c 'import json ; print(json.dumps(None))'
null

I went with [] because the return value is an array containing all the result rows, which feels like it should be an empty array when there are no rows. But I am almost apathetic towards this :P as long as it doesn't KeyError, I'm fine.

Copy link
Contributor

Choose a reason for hiding this comment

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

Heh, fair enough... How about we do even better and do this:

rows = page.get('rows')

if rows:
    print json.dumps(rows)
else:
    print 'No results found.'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not a fan. The appeal of this script is its composability through valid JSON output on stdout; you can pipe it to jq or other JSON tools. Incidentally this is also why I chose []; by always returning an array you can just loop over the result no matter what.

Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting, I don't think we considered that a primary use case here. How about:

    # [START paging]
    # Page through the result set and print all results.
    results = []
    page_token = None

    while True:
        page = bigquery.jobs().getQueryResults(
            pageToken=page_token,
            **query_job['jobReference']).execute(num_retries=2)

        results.extend(page.get('rows', []))

        page_token = page.get('pageToken')
        if not page_token:
            break

    print(json.dumps(results))
    # [END paging]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah! so you're saying paging would break the original script's JSON output?

This is a nice solution, good idea.

The valid JSON output is very nice in combination with jq because the raw output can be a bit.. raw :)

+1 !

Copy link
Contributor

Choose a reason for hiding this comment

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

Cool, if you'll update your PR I'll get it merged. Thank you for your contribution!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.


page_token = page.get('pageToken')
if not page_token:
Expand Down