Skip to content

Querying from multiple tables #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions README_TEST.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
New file for testing CI/CD
42 changes: 36 additions & 6 deletions bigquery/query_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,18 @@ def _render_sources(dataset, tables):
dataset : str
The data set to fetch log data from.
tables : Union[dict, list]
The tables to fetch log data from
The tables to fetch log data from; detailed formats are described below:
list:
tables = [table1, table2, table3]
dict:
tables = {
'date_range': True,
'table': 'ga_sessions_',
# OR
# 'table': ['ga_sessions_', 'ga_sessions_intraday_'],
'from_date': '2016-08-01',
'to_date': '2016-08-24'
}

Returns
-------
Expand All @@ -149,11 +160,20 @@ def _render_sources(dataset, tables):
if isinstance(tables, dict):
if tables.get('date_range', False):
try:
dataset_table = '.'.join([dataset, tables['table']])
return "FROM (TABLE_DATE_RANGE([{}], TIMESTAMP('{}'),"\
" TIMESTAMP('{}'))) ".format(dataset_table,
tables['from_date'],
tables['to_date'])
table_list = tables['table']
if isinstance(table_list, str):
table_list = [table_list]

from_list = []
for single_table in table_list:
from_string = _render_single_source(
dataset, single_table,
tables['from_date'], tables['to_date'])
from_list.append(from_string)
from_strings = ', '.join(from_list)

return "FROM {}".format(from_strings)

except KeyError as exp:
logger.warn(
'Missing parameter %s in selecting sources' % (exp))
Expand All @@ -163,6 +183,16 @@ def _render_sources(dataset, tables):
["[%s.%s]" % (dataset, table) for table in tables])


def _render_single_source(dataset, table, from_date, to_date):
dataset_table = '.'.join([dataset, table])
from_string = (
"(TABLE_DATE_RANGE([{}], "
"TIMESTAMP('{}'), TIMESTAMP('{}'))) ".format(
dataset_table, from_date, to_date)
)
return from_string


def _render_conditions(conditions):
"""Render the conditions part of a query.

Expand Down
2 changes: 1 addition & 1 deletion bigquery/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.8.0'
__version__ = '1.8.1'
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
google-api-python-client
httplib2
python-dateutil