Skip to content

Commit 617d7af

Browse files
authored
Merge pull request #20 from sumedhsakdeo/dry_run
Google BigQuery API Client
2 parents f134f6b + 1876f6d commit 617d7af

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

packages/sqlalchemy-bigquery/README.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
1-
SQLAlchemy dialect for BigQuery.
1+
SQLAlchemy dialect and API client for BigQuery.
22

33

44
Usage
55
=====
66

77
.. code-block:: python
88
9+
SQLAchemy
10+
_________
11+
912
from sqlalchemy import *
1013
from sqlalchemy.engine import create_engine
1114
from sqlalchemy.schema import *
1215
engine = create_engine('bigquery://project')
1316
table = Table('dataset.table', MetaData(bind=engine), autoload=True)
1417
print(select([func.count('*')], from_obj=table).scalar())
1518

19+
API Client
20+
_________
21+
from pybigquery.api import ApiClient
22+
api_client = ApiClient()
23+
print(api_client.dry_run_query(query=sqlstr).total_bytes_processed)
1624

1725
Project
1826
_______
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""Integration with BigQuery API."""
2+
3+
from __future__ import absolute_import
4+
from __future__ import unicode_literals
5+
6+
from google.cloud.bigquery import Client, QueryJobConfig
7+
8+
9+
class ApiClient(object):
10+
def __init__(self, credentials_path=None):
11+
self.credentials_path = credentials_path
12+
if self.credentials_path:
13+
self.client = Client.from_service_account_json(
14+
self.credentials_path)
15+
else:
16+
self.client = Client()
17+
18+
def dry_run_query(self, query):
19+
job_config = QueryJobConfig()
20+
job_config.dry_run = True
21+
job_config.use_query_cache = False
22+
return self.client.query(query=(query), job_config=job_config)

packages/sqlalchemy-bigquery/test/test_sqlalchemy_bigquery.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import unicode_literals
33

4+
from google.api_core.exceptions import BadRequest
5+
from pybigquery.api import ApiClient
46
from sqlalchemy.engine import create_engine
57
from sqlalchemy.schema import Table, MetaData, Column
68
from sqlalchemy import types, func, case, inspect
@@ -131,6 +133,19 @@ def query(table):
131133
)
132134
return query
133135

136+
@pytest.fixture(scope='session')
137+
def api_client():
138+
return ApiClient()
139+
140+
def test_dry_run(engine, api_client):
141+
sql = 'SELECT * FROM test_pybigquery.sample_one_row'
142+
assert api_client.dry_run_query(sql).total_bytes_processed == 112
143+
144+
sql = 'SELECT * FROM sample_one_row'
145+
with pytest.raises(BadRequest) as excinfo:
146+
api_client.dry_run_query(sql)
147+
148+
assert 'Table name "sample_one_row" cannot be resolved: dataset name is missing.' in str(excinfo.value.message)
134149

135150
def test_reflect_select(engine, table):
136151
assert len(table.c) == 14

0 commit comments

Comments
 (0)