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

Commit

Permalink
Fix #301: %bq query table view is broke. (#304)
Browse files Browse the repository at this point in the history
There are two issues:

- Like old library, the new library still uses "str(table)" to get table full name, but __str__() method is removed from new library's Table class.
- Old library and new library shares the same in-memory object for storing opened tables, but due to different naming convention between old table and new table full name (mainly project:dataset.table vs project.dataset.table), it no longer works. Either old or new library breaks without separating the two.

The fix takes care of both issues above to make both "%%sql" and "%%bq query" working --- the output is a paged table that can go next and previous.
  • Loading branch information
qimingj committed Mar 10, 2017
1 parent 84ccb95 commit 035a4a5
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
2 changes: 1 addition & 1 deletion datalab/bigquery/commands/_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ def _table_viewer(table, rows_per_page=25, fields=None):
rowNumberCell: 'gchart-table-rownumcell'
}}
}},
{{source_index: {source_index}, fields: '{fields}'}},
{{source_index: {source_index}, fields: '{fields}', legacy: 'true'}},
0,
{total_rows});
}}
Expand Down
4 changes: 3 additions & 1 deletion datalab/utils/commands/_chart_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
from . import _utils


@IPython.core.magic.register_cell_magic
# Disable the magic here because another one with same name is available under
# google.datalab namespace.
# @IPython.core.magic.register_cell_magic
def _get_chart_data(line, cell_body=''):

refresh = 0
Expand Down
5 changes: 5 additions & 0 deletions google/datalab/bigquery/_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ def name(self):
"""The TableName named tuple (project_id, dataset_id, table_id, decorator) for the table."""
return self._name_parts

@property
def full_name(self):
"""The full name of the table in the form of project.dataset.table."""
return self._full_name

@property
def job(self):
""" For tables resulting from executing queries, the job that created the table.
Expand Down
8 changes: 4 additions & 4 deletions google/datalab/bigquery/commands/_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ def _table_cell(args, cell_body):

tables = []
for dataset in datasets:
tables.extend([str(table) for table in dataset if fnmatch.fnmatch(str(table), filter_)])
tables.extend([table.full_name for table in dataset if fnmatch.fnmatch(table.full_name, filter_)])

return _render_list(tables)

Expand Down Expand Up @@ -992,7 +992,7 @@ def _table_viewer(table, rows_per_page=25, fields=None):
# TODO(gram): rework this to use google.datalab.utils.commands.chart_html

if not table.exists():
raise Exception('Table %s does not exist' % str(table))
raise Exception('Table %s does not exist' % table.full_name)

_HTML_TEMPLATE = u"""
<div class="bqtv" id="{div_id}">{static_table}</div>
Expand Down Expand Up @@ -1047,7 +1047,7 @@ def _table_viewer(table, rows_per_page=25, fields=None):
fields = google.datalab.utils.commands.get_field_list(fields, table.schema)
div_id = google.datalab.utils.commands.Html.next_id()
meta_count = ('rows: %d' % table.length) if table.length >= 0 else ''
meta_name = str(table) if table.job is None else ('job: %s' % table.job.id)
meta_name = table.full_name if table.job is None else ('job: %s' % table.job.id)
if table.job:
if table.job.cache_hit:
meta_cost = 'cached'
Expand Down Expand Up @@ -1076,7 +1076,7 @@ def _table_viewer(table, rows_per_page=25, fields=None):
static_table=google.datalab.utils.commands.HtmlBuilder.render_chart_data(data),
meta_data=meta_data,
chart_style=chart,
source_index=google.datalab.utils.commands.get_data_source_index(str(table)),
source_index=google.datalab.utils.commands.get_data_source_index(table.full_name),
fields=','.join(fields),
total_rows=total_count,
rows_per_page=rows_per_page,
Expand Down
20 changes: 17 additions & 3 deletions google/datalab/utils/commands/_chart_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import json

import datalab.utils.commands
import google.datalab.data
import google.datalab.utils

Expand All @@ -41,17 +42,30 @@ def _get_chart_data(line, cell_body=''):
fields = metadata.get('fields', '*')
first_row = int(metadata.get('first', 0))
count = int(metadata.get('count', -1))
legacy = metadata.get('legacy', None)

# Both legacy and non-legacy table viewer calls this magic for new pages of data.
# Need to find their own data source --- one under datalab.utils.commands._utils
# and the other under google.datalab.utils.commands._utils.
if legacy is not None:
data_source = datalab.utils.commands._utils._data_sources
else:
data_source = _utils._data_sources

source_index = int(source_index)
if source_index >= len(_utils._data_sources): # Can happen after e.g. kernel restart
if source_index >= len(data_source): # Can happen after e.g. kernel restart
# TODO(gram): get kernel restart events in charting.js and disable any refresh timers.
print('No source %d' % source_index)
return IPython.core.display.JSON({'data': {}})
source = _utils._data_sources[source_index]
source = data_source[source_index]
schema = None

controls = metadata['controls'] if 'controls' in metadata else {}
data, _ = _utils.get_data(source, fields, controls, first_row, count, schema)
if legacy is not None:
data, _ = datalab.utils.commands.get_data(
source, fields, controls, first_row, count, schema)
else:
data, _ = _utils.get_data(source, fields, controls, first_row, count, schema)
except Exception as e:
google.datalab.utils.print_exception_with_last_stack(e)
print('Failed with exception %s' % e)
Expand Down

0 comments on commit 035a4a5

Please sign in to comment.