forked from getredash/redash
-
Notifications
You must be signed in to change notification settings - Fork 21
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
Big Query Data Samples #948
Merged
+184
−7
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
from mock import patch | ||
from tests import BaseTestCase | ||
|
||
from redash.query_runner.big_query import BigQuery | ||
|
||
|
||
class TestBigQuery(BaseTestCase): | ||
|
||
def test_get_table_sample_returns_expected_result(self): | ||
SAMPLES_RESPONSE = { | ||
'rows': [ | ||
{'f': [ | ||
{ | ||
'v': '2017-10-28' | ||
}, { | ||
'v': '2019-03-28T18:57:04.485091' | ||
}, { | ||
'v': '3341' | ||
}, { | ||
'v': '2451' | ||
}, { | ||
'v': 'Iran' | ||
} | ||
]} | ||
] | ||
} | ||
|
||
SCHEMA_RESPONSE = { | ||
'id': 'project:dataset.table', | ||
'schema': { | ||
'fields': [{ | ||
'type': 'DATE', | ||
'name': 'submission_date', | ||
'mode': 'NULLABLE' | ||
}, { | ||
'type': 'DATETIME', | ||
'name': 'generated_time', | ||
'mode': 'NULLABLE' | ||
}, { | ||
'type': 'INTEGER', | ||
'name': 'mau', | ||
'mode': 'NULLABLE' | ||
}, { | ||
'type': 'INTEGER', | ||
'name': 'wau', | ||
'mode': 'NULLABLE' | ||
}, { | ||
'type': 'STRING', | ||
'name': 'country', | ||
'mode': 'NULLABLE' | ||
}] | ||
} | ||
} | ||
|
||
EXPECTED_SAMPLES_DICT = { | ||
'submission_date': '2017-10-28', | ||
'country': 'Iran', | ||
'wau': '2451', | ||
'mau': '3341', | ||
'generated_time': '2019-03-28T18:57:04.485091' | ||
} | ||
|
||
with patch.object(BigQuery, '_get_bigquery_service') as get_bq_service: | ||
tabledata_list = get_bq_service.return_value.tabledata.return_value.list | ||
tabledata_list.return_value.execute.return_value = SAMPLES_RESPONSE | ||
|
||
tables_get = get_bq_service.return_value.tables.return_value.get | ||
tables_get.return_value.execute.return_value = SCHEMA_RESPONSE | ||
|
||
query_runner = BigQuery({ | ||
'loadSchema': True, | ||
'projectId': 'test_project' | ||
}) | ||
table_sample = query_runner.get_table_sample("dataset.table") | ||
|
||
self.assertEqual(table_sample, EXPECTED_SAMPLES_DICT) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to use the
fields
parameter for partial responses and pagination to be a functioning query call, like I did in https://github.com/getredash/redash/pull/3673/files#diff-79a49f870dc6fe9bd78c6c81e5d3b200R267.The
while
loop is the pagination, you must request thenextPageToken
to get it to work.Docs for the "partial response": https://developers.google.com/api-client-library/python/guide/performance#partial-response-fields-parameter
API docs for tabledata listing: https://developers.google.com/apis-explorer/#p/bigquery/v2/bigquery.tabledata.list
API docs for table listing: https://developers.google.com/apis-explorer/#p/bigquery/v2/bigquery.tables.list
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, I've looked into this a bit and I have a few comments on stuff that works and stuff that I think doesn't quite work:
fields
to both these API calls seems to work well and makes sense. Thanks for pointing this out!tables().list()
buttables().get()
(https://developers.google.com/apis-explorer/#p/bigquery/v2/bigquery.tables.get) which doesn't return or use anextPageToken
I think since it's fetching only 1 table it doesn't need to paginatetabledata().list()
, I've also added a parameter,maxResults=1
since I'm only requesting 1 example for a given table. For this reason, I didn't paginate the result. We could potentially add the pagination as a precaution but as far as I can tell, it's not necessary.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, that makes sense, apologies for misreading the API that is used here, my fault.
About pagination, the only risk I see is if for some reason we'd remove the maxResult parameter in the future and would not see that pagination is missing, since the code is not expressive enough to show that. So either just add the while loop or add a comment maybe? Not sure what makes more sense 😬
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, I'll leave a comment on this.