Skip to content
This repository was archived by the owner on Sep 3, 2022. It is now read-only.

Commit 9d79da7

Browse files
author
Yasser Elsayed
authored
Fix Query.data_sources method to return dict instead of list of tuples (#308)
* Fix Query.data_sources method to return dict instead of list of tuples * added regression test
1 parent 035a4a5 commit 9d79da7

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

google/datalab/bigquery/_query.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def subqueries(self):
197197
@property
198198
def data_sources(self):
199199
""" Get a dictionary of external data sources referenced by the query."""
200-
return self._data_sources
200+
return dict(self._data_sources)
201201

202202
def dry_run(self, context=None, query_params=None):
203203
"""Dry run a query, to check the validity of the query and return some useful statistics.
@@ -218,7 +218,7 @@ def dry_run(self, context=None, query_params=None):
218218
api = _api.Api(context)
219219
try:
220220
query_result = api.jobs_insert_query(self.sql, dry_run=True,
221-
table_definitions=self._data_sources,
221+
table_definitions=self.data_sources,
222222
query_params=query_params)
223223
except Exception as e:
224224
raise e
@@ -263,7 +263,7 @@ def execute_async(self, output_options=None, sampling=None, context=None, query_
263263
append=append, overwrite=overwrite, batch=batch,
264264
use_cache=output_options.use_cache,
265265
allow_large_results=output_options.allow_large_results,
266-
table_definitions=self._data_sources,
266+
table_definitions=self.data_sources,
267267
query_params=query_params)
268268
except Exception as e:
269269
raise e

tests/bigquery/query_tests.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ def test_parameter_validation(self):
4343
self.assertEqual(q.udfs, {'testudf': udf})
4444
self.assertEqual(q._sql, sql)
4545

46+
with self.assertRaises(Exception) as error:
47+
q = TestCases._create_query(sql, data_sources=['test_datasource'])
48+
test_datasource = TestCases._create_data_source('gs://test/path')
49+
env = {'test_datasource': test_datasource}
50+
q = TestCases._create_query(sql, env=env, data_sources=['test_datasource'])
51+
self.assertIsNotNone(q)
52+
self.assertEqual(q.data_sources, {'test_datasource': test_datasource})
53+
self.assertEqual(q._sql, sql)
54+
4655
@mock.patch('google.datalab.bigquery._api.Api.tabledata_list')
4756
@mock.patch('google.datalab.bigquery._api.Api.jobs_insert_query')
4857
@mock.patch('google.datalab.bigquery._api.Api.jobs_query_results')
@@ -200,6 +209,10 @@ def _create_query(sql='SELECT * ...', name=None, env=None, udfs=None, data_sourc
200209
def _create_udf(name, code, return_type):
201210
return google.datalab.bigquery.UDF(name, code, return_type)
202211

212+
@staticmethod
213+
def _create_data_source(source):
214+
return google.datalab.bigquery.ExternalDataSource(source=source)
215+
203216
@staticmethod
204217
def _create_context():
205218
project_id = 'test'

0 commit comments

Comments
 (0)