Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[[source]]
url = "https://pypi.python.org/simple"
verify_ssl = true
name = "pypi"

[packages]


[dev-packages]
google-api-python-client = "*"
python-dateutil = "*"
nose = "*"
rednose = "*"
mock = "==1.0.1"
coverage = "*"
nose-exclude = "*"
tox = "*"

[requires]
python_version = "3.6"
14 changes: 10 additions & 4 deletions bigquery/query_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


def render_query(dataset, tables, select=None, conditions=None,
groupings=None, having=None, order_by=None, limit=None):
groupings=None, having=None, order_by=None, limit=None, use_legacy_sql=None):
"""Render a query that will run over the given tables using the specified
parameters.

Expand Down Expand Up @@ -36,7 +36,8 @@ def render_query(dataset, tables, select=None, conditions=None,
{'field':'TimeStamp, 'direction':'desc'} or similar
limit : int, optional
Limit the amount of data needed to be returned.

use_legacy_sql: bool, optional
Use legacy SQL syntax instead of Standard SQL.
Returns
-------
str
Expand Down Expand Up @@ -131,7 +132,7 @@ def _format_select(formatter, name):
return name


def _render_sources(dataset, tables):
def _render_sources(dataset, tables, use_legacy_sql=None ):
"""Render the source part of a query.

Parameters
Expand Down Expand Up @@ -160,9 +161,14 @@ def _render_sources(dataset, tables):
'Missing parameter %s in selecting sources' % (exp))

else:
return "FROM " + ", ".join(
if use_legacy_sql is True:
return "FROM " + ", ".join(
["[%s.%s]" % (dataset, table) for table in tables])
else:
return "FROM " + ", ".join(
["`%s.%s`" % (dataset, table) for table in tables])



def _render_conditions(conditions):
"""Render the conditions part of a query.
Expand Down
36 changes: 18 additions & 18 deletions bigquery/tests/test_query_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_multi_tables(self):
result = _render_sources('spider', ['man', 'pig', 'bro'])

self.assertEqual(
result, 'FROM [spider.man], [spider.pig], [spider.bro]')
result, 'FROM `spider.man`, `spider.pig`, `spider.bro`')

def test_no_tables(self):
"""Ensure that render sources can handle no tables."""
Expand All @@ -78,7 +78,7 @@ def test_no_dataset(self):

result = _render_sources('', ['man', 'pig', 'bro'])

self.assertEqual(result, 'FROM [.man], [.pig], [.bro]')
self.assertEqual(result, 'FROM `.man`, `.pig`, `.bro`')

def test_tables_in_date_range(self):
"""Ensure that render sources can handle tables in DATE RANGE."""
Expand Down Expand Up @@ -417,7 +417,7 @@ def test_full_query(self):
limit=10)

expected_query = ("SELECT status as status, start_time as timestamp, "
"resource as url FROM [dataset.2013_06_appspot_1]"
"resource as url FROM `dataset.2013_06_appspot_1`"
" WHERE (start_time <= INTEGER('1371566954')) AND "
"(start_time >= INTEGER('1371556954')) GROUP BY "
"timestamp, status HAVING (status == INTEGER('1')) "
Expand Down Expand Up @@ -448,7 +448,7 @@ def test_empty_conditions(self):

expected_query = ("SELECT status as status, start_time as timestamp, "
"resource as url FROM "
"[dataset.2013_06_appspot_1] ORDER BY "
"`dataset.2013_06_appspot_1` ORDER BY "
"timestamp desc ")
expected_select = (expected_query[len('SELECT '):]
.split('FROM')[0].strip().split(', '))
Expand Down Expand Up @@ -485,7 +485,7 @@ def test_incorrect_conditions(self):

expected_query = ("SELECT status as status, start_time as timestamp, "
"resource as url FROM "
"[dataset.2013_06_appspot_1] ORDER BY "
"`dataset.2013_06_appspot_1` ORDER BY "
"timestamp desc ")
expected_select = (expected_query[len('SELECT '):]
.split('FROM')[0].strip().split(', '))
Expand Down Expand Up @@ -533,7 +533,7 @@ def test_multiple_condition_values(self):

expected_query = ("SELECT status as status, start_time as timestamp, "
"resource as url FROM "
"[dataset.2013_06_appspot_1] WHERE (start_time "
"`dataset.2013_06_appspot_1` WHERE (start_time "
"<= INTEGER('1371566954')) AND (start_time >= "
"INTEGER('1371556954')) AND "
"((resource CONTAINS STRING('foo') AND resource "
Expand Down Expand Up @@ -571,7 +571,7 @@ def test_negated_condition_value(self):

expected_query = ("SELECT status as status, start_time as timestamp, "
"resource as url FROM "
"[dataset.2013_06_appspot_1] WHERE (NOT resource "
"`dataset.2013_06_appspot_1` WHERE (NOT resource "
"CONTAINS STRING('foo')) ORDER BY timestamp desc ")
expected_select = (expected_query[len('SELECT '):]
.split('FROM')[0].strip().split(', '))
Expand Down Expand Up @@ -612,7 +612,7 @@ def test_multiple_negated_condition_values(self):

expected_query = ("SELECT status as status, start_time as timestamp, "
"resource as url FROM "
"[dataset.2013_06_appspot_1] WHERE (NOT resource "
"`dataset.2013_06_appspot_1` WHERE (NOT resource "
"CONTAINS STRING('foo') AND NOT resource CONTAINS "
"STRING('baz') AND NOT resource CONTAINS "
"STRING('bar')) ORDER BY timestamp desc ")
Expand Down Expand Up @@ -651,7 +651,7 @@ def test_empty_order(self):

expected_query = ("SELECT status as status, start_time as timestamp, "
"resource as url FROM "
"[dataset.2013_06_appspot_1] WHERE (start_time "
"`dataset.2013_06_appspot_1` WHERE (start_time "
"<= INTEGER('1371566954')) AND (start_time >= "
"INTEGER('1371556954')) ")
expected_select = (expected_query[len('SELECT '):]
Expand Down Expand Up @@ -689,7 +689,7 @@ def test_incorrect_order(self):

expected_query = ("SELECT status as status, start_time as timestamp, "
"resource as url FROM "
"[dataset.2013_06_appspot_1] WHERE (start_time "
"`dataset.2013_06_appspot_1` WHERE (start_time "
"<= INTEGER('1371566954')) AND (start_time >= "
"INTEGER('1371556954')) ")
expected_select = (expected_query[len('SELECT '):]
Expand Down Expand Up @@ -721,7 +721,7 @@ def test_empty_select(self):
],
order_by={'fields': ['timestamp'], 'direction': 'desc'})

expected_query = ("SELECT * FROM [dataset.2013_06_appspot_1] "
expected_query = ("SELECT * FROM `dataset.2013_06_appspot_1` "
"WHERE (start_time <= INTEGER('1371566954')) AND "
"(start_time >= INTEGER('1371556954')) ORDER BY "
"timestamp desc ")
Expand Down Expand Up @@ -752,7 +752,7 @@ def test_no_alias(self):
order_by={'fields': ['start_time'], 'direction': 'desc'})

expected_query = ("SELECT status , start_time , resource FROM "
"[dataset.2013_06_appspot_1] WHERE (start_time "
"`dataset.2013_06_appspot_1` WHERE (start_time "
"<= INTEGER('1371566954')) AND (start_time >= "
"INTEGER('1371556954')) ORDER BY start_time desc ")
expected_select = (field.strip() for field in
Expand Down Expand Up @@ -797,7 +797,7 @@ def test_formatting(self):
expected_query = ("SELECT status as status, "
"FORMAT_UTC_USEC(INTEGER(start_time)) as timestamp, "
"resource as url FROM "
"[dataset.2013_06_appspot_1] WHERE (start_time "
"`dataset.2013_06_appspot_1` WHERE (start_time "
"<= INTEGER('1371566954')) AND (start_time >= "
"INTEGER('1371556954')) ORDER BY timestamp desc ")
expected_select = (expected_query[len('SELECT '):]
Expand Down Expand Up @@ -849,7 +849,7 @@ def test_formatting_duplicate_columns(self):
"FORMAT_UTC_USEC(INTEGER(start_time)) as timestamp, "
"LEFT(FORMAT_UTC_USEC(INTEGER(start_time*1000000)),"
"10) as day, resource as url FROM "
"[dataset.2013_06_appspot_1] WHERE "
"`dataset.2013_06_appspot_1` WHERE "
"(start_time <= INTEGER('1371566954')) AND "
"(start_time >= INTEGER('1371556954')) ORDER BY "
"timestamp desc ")
Expand Down Expand Up @@ -894,7 +894,7 @@ def test_sec_to_micro_formatting(self):
expected_query = ("SELECT status as status, "
"SEC_TO_TIMESTAMP(INTEGER(start_time*1000000)) as "
"timestamp, resource as url FROM "
"[dataset.2013_06_appspot_1] WHERE (start_time "
"`dataset.2013_06_appspot_1` WHERE (start_time "
"<= INTEGER('1371566954')) AND (start_time >= "
"INTEGER('1371556954')) ORDER BY timestamp desc ")
expected_select = (expected_query[len('SELECT '):]
Expand Down Expand Up @@ -952,7 +952,7 @@ def test_empty_groupings(self):

expected_query = ("SELECT status as status, start_time as timestamp, "
"resource as url FROM "
"[dataset.2013_06_appspot_1] ORDER BY "
"`dataset.2013_06_appspot_1` ORDER BY "
"timestamp desc ")
expected_select = (expected_query[len('SELECT '):]
.split('FROM')[0].strip().split(', '))
Expand Down Expand Up @@ -990,8 +990,8 @@ def test_multi_tables(self):

expected_query = ("SELECT status as status, start_time as timestamp, "
"resource as url FROM "
"[dataset.2013_06_appspot_1], "
"[dataset.2013_07_appspot_1] WHERE (start_time "
"`dataset.2013_06_appspot_1`, "
"`dataset.2013_07_appspot_1` WHERE (start_time "
"<= INTEGER('1371566954')) AND (start_time >= "
"INTEGER('1371556954')) GROUP BY timestamp, status "
"ORDER BY timestamp desc ")
Expand Down