Skip to content

Commit

Permalink
[sql lab] fix CREATE TABLE AS (#2719)
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch authored May 7, 2017
1 parent 46d7a92 commit a6e1e18
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
23 changes: 12 additions & 11 deletions superset/db_engine_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,24 +162,25 @@ def where_latest_partition(

@classmethod
def select_star(cls, my_db, table_name, schema=None, limit=100,
show_cols=False, indent=True):
show_cols=False, indent=True, latest_partition=True):
fields = '*'
table = my_db.get_table(table_name, schema=schema)
cols = []
if show_cols or latest_partition:
cols = my_db.get_table(table_name, schema=schema).columns

if show_cols:
fields = [my_db.get_quoter()(c.name) for c in table.columns]
fields = [my_db.get_quoter()(c.name) for c in cols]
full_table_name = table_name
if schema:
full_table_name = schema + '.' + table_name
qry = select(fields)
qry = select(fields).select_from(text(full_table_name))
if limit:
qry = qry.limit(limit)
partition_query = cls.where_latest_partition(
table_name, schema, my_db, qry, columns=table.columns)
# if not partition_query condition fails.
if partition_query == False: # noqa
qry = qry.select_from(text(full_table_name))
else:
qry = partition_query
if latest_partition:
partition_query = cls.where_latest_partition(
table_name, schema, my_db, qry, columns=cols)
if partition_query != False: # noqa
qry = partition_query
sql = my_db.compile_sqla_query(qry)
if indent:
sql = sqlparse.format(sql, reindent=True)
Expand Down
4 changes: 2 additions & 2 deletions superset/models/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,11 +602,11 @@ def compile_sqla_query(self, qry, schema=None):

def select_star(
self, table_name, schema=None, limit=100, show_cols=False,
indent=True):
indent=True, latest_partition=True):
"""Generates a ``select *`` statement in the proper dialect"""
return self.db_engine_spec.select_star(
self, table_name, schema=schema, limit=limit, show_cols=show_cols,
indent=indent)
indent=indent, latest_partition=latest_partition)

def wrap_sql_limit(self, sql, limit=1000):
qry = (
Expand Down
4 changes: 3 additions & 1 deletion superset/sql_lab.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ def handle_error(msg):
query.select_sql = '{}'.format(database.select_star(
query.tmp_table_name,
limit=query.limit,
schema=database.force_ctas_schema
schema=database.force_ctas_schema,
show_cols=False,
latest_partition=False,
))
query.end_time = utils.now_as_float()
session.merge(query)
Expand Down

0 comments on commit a6e1e18

Please sign in to comment.