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

Commit 035a4a5

Browse files
authored
Fix #301: %bq query table view is broke. (#304)
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.
1 parent 84ccb95 commit 035a4a5

File tree

5 files changed

+30
-9
lines changed

5 files changed

+30
-9
lines changed

datalab/bigquery/commands/_bigquery.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,7 @@ def _table_viewer(table, rows_per_page=25, fields=None):
944944
rowNumberCell: 'gchart-table-rownumcell'
945945
}}
946946
}},
947-
{{source_index: {source_index}, fields: '{fields}'}},
947+
{{source_index: {source_index}, fields: '{fields}', legacy: 'true'}},
948948
0,
949949
{total_rows});
950950
}}

datalab/utils/commands/_chart_data.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
from . import _utils
3131

3232

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

3638
refresh = 0

google/datalab/bigquery/_table.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ def name(self):
139139
"""The TableName named tuple (project_id, dataset_id, table_id, decorator) for the table."""
140140
return self._name_parts
141141

142+
@property
143+
def full_name(self):
144+
"""The full name of the table in the form of project.dataset.table."""
145+
return self._full_name
146+
142147
@property
143148
def job(self):
144149
""" For tables resulting from executing queries, the job that created the table.

google/datalab/bigquery/commands/_bigquery.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ def _table_cell(args, cell_body):
736736

737737
tables = []
738738
for dataset in datasets:
739-
tables.extend([str(table) for table in dataset if fnmatch.fnmatch(str(table), filter_)])
739+
tables.extend([table.full_name for table in dataset if fnmatch.fnmatch(table.full_name, filter_)])
740740

741741
return _render_list(tables)
742742

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

994994
if not table.exists():
995-
raise Exception('Table %s does not exist' % str(table))
995+
raise Exception('Table %s does not exist' % table.full_name)
996996

997997
_HTML_TEMPLATE = u"""
998998
<div class="bqtv" id="{div_id}">{static_table}</div>
@@ -1047,7 +1047,7 @@ def _table_viewer(table, rows_per_page=25, fields=None):
10471047
fields = google.datalab.utils.commands.get_field_list(fields, table.schema)
10481048
div_id = google.datalab.utils.commands.Html.next_id()
10491049
meta_count = ('rows: %d' % table.length) if table.length >= 0 else ''
1050-
meta_name = str(table) if table.job is None else ('job: %s' % table.job.id)
1050+
meta_name = table.full_name if table.job is None else ('job: %s' % table.job.id)
10511051
if table.job:
10521052
if table.job.cache_hit:
10531053
meta_cost = 'cached'
@@ -1076,7 +1076,7 @@ def _table_viewer(table, rows_per_page=25, fields=None):
10761076
static_table=google.datalab.utils.commands.HtmlBuilder.render_chart_data(data),
10771077
meta_data=meta_data,
10781078
chart_style=chart,
1079-
source_index=google.datalab.utils.commands.get_data_source_index(str(table)),
1079+
source_index=google.datalab.utils.commands.get_data_source_index(table.full_name),
10801080
fields=','.join(fields),
10811081
total_rows=total_count,
10821082
rows_per_page=rows_per_page,

google/datalab/utils/commands/_chart_data.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import json
2626

27+
import datalab.utils.commands
2728
import google.datalab.data
2829
import google.datalab.utils
2930

@@ -41,17 +42,30 @@ def _get_chart_data(line, cell_body=''):
4142
fields = metadata.get('fields', '*')
4243
first_row = int(metadata.get('first', 0))
4344
count = int(metadata.get('count', -1))
45+
legacy = metadata.get('legacy', None)
46+
47+
# Both legacy and non-legacy table viewer calls this magic for new pages of data.
48+
# Need to find their own data source --- one under datalab.utils.commands._utils
49+
# and the other under google.datalab.utils.commands._utils.
50+
if legacy is not None:
51+
data_source = datalab.utils.commands._utils._data_sources
52+
else:
53+
data_source = _utils._data_sources
4454

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

5363
controls = metadata['controls'] if 'controls' in metadata else {}
54-
data, _ = _utils.get_data(source, fields, controls, first_row, count, schema)
64+
if legacy is not None:
65+
data, _ = datalab.utils.commands.get_data(
66+
source, fields, controls, first_row, count, schema)
67+
else:
68+
data, _ = _utils.get_data(source, fields, controls, first_row, count, schema)
5569
except Exception as e:
5670
google.datalab.utils.print_exception_with_last_stack(e)
5771
print('Failed with exception %s' % e)

0 commit comments

Comments
 (0)