Skip to content

Commit

Permalink
[select-star] Adding optional schema to view (#6051)
Browse files Browse the repository at this point in the history
  • Loading branch information
john-bodley committed Oct 8, 2018
1 parent 9dcf8e1 commit 1ee08fc
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
18 changes: 16 additions & 2 deletions superset/db_engine_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import pandas
from past.builtins import basestring
import sqlalchemy as sqla
from sqlalchemy import select
from sqlalchemy import Column, select
from sqlalchemy.engine import create_engine
from sqlalchemy.engine.url import make_url
from sqlalchemy.sql import quoted_name, text
Expand Down Expand Up @@ -852,6 +852,20 @@ def _partition_query(
""").format(**locals())
return sql

@classmethod
def where_latest_partition(
cls, table_name, schema, database, qry, columns=None):
try:
col_name, value = cls.latest_partition(
table_name, schema, database, show_first=True)
except Exception:
# table is not partitioned
return False
for c in columns:
if c.get('name') == col_name:
return qry.where(Column(col_name) == value)
return False

@classmethod
def _latest_partition_from_df(cls, df):
recs = df.to_records(index=False)
Expand Down Expand Up @@ -1180,7 +1194,7 @@ def where_latest_partition(
cls, table_name, schema, database, qry, columns=None):
try:
col_name, value = cls.latest_partition(
table_name, schema, database)
table_name, schema, database, show_first=True)
except Exception:
# table is not partitioned
return False
Expand Down
1 change: 0 additions & 1 deletion superset/templates/superset/ajah.html

This file was deleted.

17 changes: 11 additions & 6 deletions superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2332,7 +2332,7 @@ def table(self, database_id, table_name, schema):
'columns': payload_columns,
'selectStar': mydb.select_star(
table_name, schema=schema, show_cols=True, indent=True,
cols=columns, latest_partition=False),
cols=columns, latest_partition=True),
'primaryKey': primary_key,
'foreignKeys': foreign_keys,
'indexes': keys,
Expand All @@ -2350,14 +2350,19 @@ def extra_table_metadata(self, database_id, table_name, schema):
return json_success(json.dumps(payload))

@has_access
@expose('/select_star/<database_id>/<table_name>/')
@expose('/select_star/<database_id>/<table_name>')
@expose('/select_star/<database_id>/<table_name>/<schema>')
@log_this
def select_star(self, database_id, table_name):
def select_star(self, database_id, table_name, schema=None):
mydb = db.session.query(
models.Database).filter_by(id=database_id).first()
return self.render_template(
'superset/ajah.html',
content=mydb.select_star(table_name, show_cols=True),
return json_success(
mydb.select_star(
table_name,
schema,
latest_partition=True,
show_cols=True,
),
)

@expose('/theme/')
Expand Down
5 changes: 5 additions & 0 deletions tests/core_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,11 @@ def test_schemas_access_for_csv_upload_endpoint(self,
.format(db_id=dbobj.id))
assert data == ['this_schema_is_allowed_too']

def test_select_star(self):
self.login(username='admin')
resp = self.get_resp('/superset/select_star/1/birth_names')
self.assertIn('gender', resp)


if __name__ == '__main__':
unittest.main()

0 comments on commit 1ee08fc

Please sign in to comment.