I'd like to know the difference between query()'s return value and query().result()s'.
In BigQuery Python Client Library,
bigquery_client = bigquery.Client()
myQuery = "SELECT * FROM `mytable`"
## NOTE: This query result has just 1 row.
job = bigquery_client.query(myQuery)
for row in job:
val1 = row
result = job.result()
for row in result:
val2 = row
print(job == result) # False. I know QueryJob object is different to RowIterator object.
print(val1 == val2) # True
Why are val1 and val2 equivalent? Can the values be different for a very large query?
This is a yearlong self-answer.
Basically, 'job' and 'result' are different in my code.
bigquery_client.query() returns QueryJob instance. ( See https://googleapis.dev/python/bigquery/latest/generated/google.cloud.bigquery.client.Client.html#google.cloud.bigquery.client.Client.query )
But the QueryJob class has own _iter_ method and it returns iter(self.result()) ( See https://github.com/googleapis/python-bigquery/blob/main/google/cloud/bigquery/job/query.py#L1778 )
So 'job' becomes an iterator of result() for the for-in loop.
Thus, job != result but val1 == val2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With