Skip to content
Merged
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
19 changes: 11 additions & 8 deletions superset/connectors/sqla/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,9 @@ def values_for_column(self, column_name, limit=10000):
cols = {col.column_name: col for col in self.columns}
target_col = cols[column_name]

tbl = self.get_sqla_table()
qry = (
select([target_col.sqla_col])
.select_from(tbl)
.select_from(self.get_from_clause())
.distinct(column_name)
)
if limit:
Expand Down Expand Up @@ -338,6 +337,15 @@ def get_sqla_table(self):
tbl.schema = self.schema
return tbl

def get_from_clause(self):
# Supporting arbitrary SQL statements in place of tables
if self.sql:
tp = self.get_template_processor()
from_sql = tp.process_template(self.sql)
return TextAsFrom(sa.text(from_sql), []).alias('expr_qry')

return self.get_sqla_table()

def get_sqla_query( # sqla
self,
groupby, metrics,
Expand Down Expand Up @@ -436,12 +444,7 @@ def get_sqla_query( # sqla
select_exprs += metrics_exprs
qry = sa.select(select_exprs)

# Supporting arbitrary SQL statements in place of tables
if self.sql:
from_sql = template_processor.process_template(self.sql)
tbl = TextAsFrom(sa.text(from_sql), []).alias('expr_qry')
else:
tbl = self.get_sqla_table()
tbl = self.get_from_clause()

if not columns:
qry = qry.group_by(*groupby_exprs)
Expand Down