Skip to content

Commit c21ee4b

Browse files
committed
Add support for specifying location
1 parent 0aecbbd commit c21ee4b

File tree

6 files changed

+33
-11
lines changed

6 files changed

+33
-11
lines changed

dev_requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
sqlalchemy>=1.1.9
2-
google-cloud-bigquery>=0.30.0
2+
google-cloud-bigquery>=1.5.0
33
future==0.16.0
44

55
pytest==3.2.2

pybigquery/api.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77

88

99
class ApiClient(object):
10-
def __init__(self, credentials_path=None):
10+
def __init__(self, credentials_path=None, location=None):
1111
self.credentials_path = credentials_path
12+
self.location = location
1213
if self.credentials_path:
1314
self.client = Client.from_service_account_json(
14-
self.credentials_path)
15+
self.credentials_path, location=self.location)
1516
else:
16-
self.client = Client()
17+
self.client = Client(location=self.location)
1718

1819
def dry_run_query(self, query):
1920
job_config = QueryJobConfig()

pybigquery/sqlalchemy_bigquery.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -193,20 +193,22 @@ class BigQueryDialect(DefaultDialect):
193193
supports_simple_order_by_label = True
194194
postfetch_lastrowid = False
195195

196-
def __init__(self, arraysize=5000, credentials_path=None, *args, **kwargs):
196+
def __init__(self, arraysize=5000, credentials_path=None, location=None, *args, **kwargs):
197197
super(BigQueryDialect, self).__init__(*args, **kwargs)
198198
self.arraysize = arraysize
199199
self.credentials_path = credentials_path
200+
self.location = location
200201

201202
@classmethod
202203
def dbapi(cls):
203204
return dbapi
204205

205206
def create_connect_args(self, url):
206207
if self.credentials_path:
207-
client = bigquery.Client.from_service_account_json(self.credentials_path)
208+
client = bigquery.Client.from_service_account_json(
209+
self.credentials_path, location=self.location)
208210
else:
209-
client = bigquery.Client(url.host)
211+
client = bigquery.Client(url.host, location=self.location)
210212
return ([client], {})
211213

212214
def _json_deserializer(self, row):

scripts/load_test_data.sh

+5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
bq mk test_pybigquery
2+
bq mk --data_location=asia-northeast1 test_pybigquery_location
3+
24
bq rm -f -t test_pybigquery.sample
35
bq rm -f -t test_pybigquery.sample_one_row
46
bq rm -f -t test_pybigquery.sample_dml
7+
bq rm -f -t test_pybigquery_location.sample_one_row
8+
59
bq load --source_format=NEWLINE_DELIMITED_JSON --schema=$(dirname $0)/schema.json test_pybigquery.sample $(dirname $0)/sample.json
610
bq load --source_format=NEWLINE_DELIMITED_JSON --schema=$(dirname $0)/schema.json test_pybigquery.sample_one_row $(dirname $0)/sample_one_row.json
11+
bq --location=asia-northeast1 load --source_format=NEWLINE_DELIMITED_JSON --schema=$(dirname $0)/schema.json test_pybigquery_location.sample_one_row $(dirname $0)/sample_one_row.json
712
bq mk --schema=$(dirname $0)/schema.json -t test_pybigquery.sample_dml

setup.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@ def readme():
88

99
setup(
1010
name="pybigquery",
11-
version='0.4.1',
11+
version='0.4.2',
1212
description="SQLAlchemy dialect for BigQuery",
1313
long_description=readme(),
1414
long_description_content_type="text/x-rst",
1515
author="Maxim Zudilov",
1616
author_email="maxim.zudilov@gmail.com",
1717
packages=['pybigquery'],
1818
url="https://github.com/mxmzdlv/pybigquery",
19-
download_url='https://github.com/mxmzdlv/pybigquery/archive/v0.4.1.tar.gz',
19+
download_url='https://github.com/mxmzdlv/pybigquery/archive/v0.4.2.tar.gz',
2020
keywords=['bigquery', 'sqlalchemy'],
2121
classifiers=[
2222
"Intended Audience :: Developers",
2323
"Topic :: Database :: Front-Ends"
2424
],
2525
install_requires=[
2626
'sqlalchemy>=1.1.9',
27-
'google-cloud-bigquery>=0.30.0',
27+
'google-cloud-bigquery>=1.5.0',
2828
'future',
2929
],
3030
tests_require=[

test/test_sqlalchemy_bigquery.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ def engine():
9191
return engine
9292

9393

94+
@pytest.fixture(scope='session')
95+
def engine_with_location():
96+
engine = create_engine('bigquery://', echo=True, location="asia-northeast1")
97+
return engine
98+
99+
94100
@pytest.fixture(scope='session')
95101
def table(engine):
96102
return Table('test_pybigquery.sample', MetaData(bind=engine), autoload=True)
@@ -133,20 +139,28 @@ def query(table):
133139
)
134140
return query
135141

142+
136143
@pytest.fixture(scope='session')
137144
def api_client():
138145
return ApiClient()
139146

147+
140148
def test_dry_run(engine, api_client):
141149
sql = 'SELECT * FROM test_pybigquery.sample_one_row'
142-
assert api_client.dry_run_query(sql).total_bytes_processed == 112
150+
assert api_client.dry_run_query(sql).total_bytes_processed == 132
143151

144152
sql = 'SELECT * FROM sample_one_row'
145153
with pytest.raises(BadRequest) as excinfo:
146154
api_client.dry_run_query(sql)
147155

148156
assert 'Table name "sample_one_row" cannot be resolved: dataset name is missing.' in str(excinfo.value.message)
149157

158+
159+
def test_dataset_location(engine_with_location):
160+
rows = engine_with_location.execute('SELECT * FROM test_pybigquery_location.sample_one_row').fetchall()
161+
assert list(rows[0]) == ONE_ROW_CONTENTS
162+
163+
150164
def test_reflect_select(engine, table):
151165
assert len(table.c) == 14
152166

0 commit comments

Comments
 (0)