Skip to content

Commit

Permalink
Merge branch 'bigquery-udf-resources'
Browse files Browse the repository at this point in the history
  • Loading branch information
necnec committed Dec 5, 2016
2 parents 834a2ff + 028c8be commit ce8ebe4
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 34 deletions.
1 change: 0 additions & 1 deletion doc/source/whatsnew/v0.19.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,6 @@ Google BigQuery Enhancements

- The :func:`read_gbq` method has gained the ``dialect`` argument to allow users to specify whether to use BigQuery's legacy SQL or BigQuery's standard SQL. See the :ref:`docs <io.bigquery_reader>` for more details (:issue:`13615`).
- The :func:`~DataFrame.to_gbq` method now allows the DataFrame column order to differ from the destination table schema (:issue:`11359`).
- The :func:`read_gbq` method now allows query configuration preferences

.. _whatsnew_0190.errstate:

Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Other enhancements
^^^^^^^^^^^^^^^^^^

- ``pd.read_excel`` now preserves sheet order when using ``sheetname=None`` (:issue:`9930`)
- ``pd.io.gbq.read_gbq`` method now allows query configuration preferences


.. _whatsnew_0200.api_breaking:
Expand Down
14 changes: 9 additions & 5 deletions pandas/io/gbq.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,13 @@ def run_query(self, query, **kwargs):
}
}
}
query_config = kwargs.get('query_config')
if query_config is not None:
job_data['configuration']['query'].update(query_config)
configuration = kwargs.get('configuration')
if configuration is not None:
if 'query' in configuration:
job_data['configuration']['query']\
.update(configuration['query'])
else:
job_data['configuration'] = configuration

self._start_timer()
try:
Expand Down Expand Up @@ -687,11 +691,11 @@ def read_gbq(query, project_id=None, index_col=None, col_order=None,
.. versionadded:: 0.19.0
**kwargs: Arbitrary keyword arguments
query_config (dict): query configuration parameters for job processing.
configuration (dict): query config parameters for job processing.
For more information see `BigQuery SQL Reference
<https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs#configuration.query>`
.. versionadded:: 0.19.0
.. versionadded:: 0.20.0
Returns
-------
Expand Down
62 changes: 34 additions & 28 deletions pandas/io/tests/test_gbq.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,49 +713,55 @@ def test_invalid_option_for_sql_dialect(self):

def test_query_with_parameters(self):
sql_statement = "SELECT @param1 + @param2 as VALID_RESULT"
query_config = {
"useLegacySql": False,
"parameterMode": "named",
"queryParameters": [
{
"name": "param1",
"parameterType": {
"type": "INTEGER"
config = {
'query': {
"useLegacySql": False,
"parameterMode": "named",
"queryParameters": [
{
"name": "param1",
"parameterType": {
"type": "INTEGER"
},
"parameterValue": {
"value": 1
}
},
"parameterValue": {
"value": 1
{
"name": "param2",
"parameterType": {
"type": "INTEGER"
},
"parameterValue": {
"value": 2
}
}
},
{
"name": "param2",
"parameterType": {
"type": "INTEGER"
},
"parameterValue": {
"value": 2
}
}
]
]
}
}
# Test that an invalid query without query_config
# Test that an invalid query without query config
with tm.assertRaises(ValueError):
gbq.read_gbq(sql_statement, project_id=_get_project_id(),
private_key=_get_private_key_path())

# Test that a correct query with query config
df = gbq.read_gbq(sql_statement, project_id=_get_project_id(),
private_key=_get_private_key_path(),
query_config=query_config)
configuration=config)
tm.assert_frame_equal(df, DataFrame({'VALID_RESULT': [3]}))

def test_query_no_cache(self):
def test_query_inside_configuration(self):
query_no_use = 'SELECT "PI_WRONG" as VALID_STRING'
query = 'SELECT "PI" as VALID_STRING'
query_config = {
"useQueryCache": False,
config = {
'query': {
"query": query,
"useQueryCache": False,
}
}
df = gbq.read_gbq(query, project_id=_get_project_id(),
df = gbq.read_gbq(query_no_use, project_id=_get_project_id(),
private_key=_get_private_key_path(),
query_config=query_config)
configuration=config)
tm.assert_frame_equal(df, DataFrame({'VALID_STRING': ['PI']}))


Expand Down

0 comments on commit ce8ebe4

Please sign in to comment.