mirror of
https://github.com/apache/superset.git
synced 2024-09-12 08:39:45 -04:00
Make select_star work with SQL Lab views (#8598)
* Make select_star work with SQL Lab views * Fix mypy
This commit is contained in:
parent
ceec53da17
commit
964e6db800
@ -530,7 +530,11 @@ class SqlaTable(Model, BaseDatasource):
|
|||||||
# show_cols and latest_partition set to false to avoid
|
# show_cols and latest_partition set to false to avoid
|
||||||
# the expensive cost of inspecting the DB
|
# the expensive cost of inspecting the DB
|
||||||
return self.database.select_star(
|
return self.database.select_star(
|
||||||
self.table_name, schema=self.schema, show_cols=False, latest_partition=False
|
self.table_name,
|
||||||
|
sql=self.sql,
|
||||||
|
schema=self.schema,
|
||||||
|
show_cols=False,
|
||||||
|
latest_partition=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_col(self, col_name: str) -> Optional[Column]:
|
def get_col(self, col_name: str) -> Optional[Column]:
|
||||||
|
@ -617,6 +617,7 @@ class BaseEngineSpec: # pylint: disable=too-many-public-methods
|
|||||||
database,
|
database,
|
||||||
table_name: str,
|
table_name: str,
|
||||||
engine: Engine,
|
engine: Engine,
|
||||||
|
sql: Optional[str] = None,
|
||||||
schema: Optional[str] = None,
|
schema: Optional[str] = None,
|
||||||
limit: int = 100,
|
limit: int = 100,
|
||||||
show_cols: bool = False,
|
show_cols: bool = False,
|
||||||
@ -629,6 +630,7 @@ class BaseEngineSpec: # pylint: disable=too-many-public-methods
|
|||||||
|
|
||||||
:param database: Database instance
|
:param database: Database instance
|
||||||
:param table_name: Table name
|
:param table_name: Table name
|
||||||
|
:param sql: SQL defining a subselect
|
||||||
:param engine: SqlALchemy Engine instance
|
:param engine: SqlALchemy Engine instance
|
||||||
:param schema: Schema
|
:param schema: Schema
|
||||||
:param limit: limit to impose on query
|
:param limit: limit to impose on query
|
||||||
@ -638,20 +640,23 @@ class BaseEngineSpec: # pylint: disable=too-many-public-methods
|
|||||||
:param cols: Columns to include in query
|
:param cols: Columns to include in query
|
||||||
:return: SQL query
|
:return: SQL query
|
||||||
"""
|
"""
|
||||||
fields = "*"
|
|
||||||
cols = cols or []
|
|
||||||
if (show_cols or latest_partition) and not cols:
|
|
||||||
cols = database.get_columns(table_name, schema)
|
|
||||||
|
|
||||||
if show_cols:
|
|
||||||
fields = cls._get_fields(cols)
|
|
||||||
quote = engine.dialect.identifier_preparer.quote
|
quote = engine.dialect.identifier_preparer.quote
|
||||||
if schema:
|
if schema:
|
||||||
full_table_name = quote(schema) + "." + quote(table_name)
|
full_table_name = quote(schema) + "." + quote(table_name)
|
||||||
else:
|
else:
|
||||||
full_table_name = quote(table_name)
|
full_table_name = quote(table_name)
|
||||||
|
|
||||||
qry = select(fields).select_from(text(full_table_name))
|
if sql is not None:
|
||||||
|
subselect = f"(\n{sql}\n) AS {quote(table_name)}"
|
||||||
|
qry = select("*").select_from(text(subselect))
|
||||||
|
else:
|
||||||
|
fields = "*"
|
||||||
|
cols = cols or []
|
||||||
|
if (show_cols or latest_partition) and not cols:
|
||||||
|
cols = database.get_columns(table_name, schema)
|
||||||
|
if show_cols:
|
||||||
|
fields = cls._get_fields(cols)
|
||||||
|
qry = select(fields).select_from(text(full_table_name))
|
||||||
|
|
||||||
if limit:
|
if limit:
|
||||||
qry = qry.limit(limit)
|
qry = qry.limit(limit)
|
||||||
@ -661,10 +666,10 @@ class BaseEngineSpec: # pylint: disable=too-many-public-methods
|
|||||||
)
|
)
|
||||||
if partition_query is not None:
|
if partition_query is not None:
|
||||||
qry = partition_query
|
qry = partition_query
|
||||||
sql = database.compile_sqla_query(qry)
|
select_star_query = database.compile_sqla_query(qry)
|
||||||
if indent:
|
if indent:
|
||||||
sql = sqlparse.format(sql, reindent=True)
|
select_star_query = sqlparse.format(select_star_query, reindent=True)
|
||||||
return sql
|
return select_star_query
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def estimate_statement_cost(
|
def estimate_statement_cost(
|
||||||
|
@ -354,6 +354,7 @@ class HiveEngineSpec(PrestoEngineSpec):
|
|||||||
database,
|
database,
|
||||||
table_name: str,
|
table_name: str,
|
||||||
engine: Engine,
|
engine: Engine,
|
||||||
|
sql: Optional[str] = None,
|
||||||
schema: str = None,
|
schema: str = None,
|
||||||
limit: int = 100,
|
limit: int = 100,
|
||||||
show_cols: bool = False,
|
show_cols: bool = False,
|
||||||
@ -367,6 +368,7 @@ class HiveEngineSpec(PrestoEngineSpec):
|
|||||||
database,
|
database,
|
||||||
table_name,
|
table_name,
|
||||||
engine,
|
engine,
|
||||||
|
sql,
|
||||||
schema,
|
schema,
|
||||||
limit,
|
limit,
|
||||||
show_cols,
|
show_cols,
|
||||||
|
@ -409,6 +409,7 @@ class PrestoEngineSpec(BaseEngineSpec):
|
|||||||
database,
|
database,
|
||||||
table_name: str,
|
table_name: str,
|
||||||
engine: Engine,
|
engine: Engine,
|
||||||
|
sql: Optional[str] = None,
|
||||||
schema: str = None,
|
schema: str = None,
|
||||||
limit: int = 100,
|
limit: int = 100,
|
||||||
show_cols: bool = False,
|
show_cols: bool = False,
|
||||||
@ -432,6 +433,7 @@ class PrestoEngineSpec(BaseEngineSpec):
|
|||||||
database,
|
database,
|
||||||
table_name,
|
table_name,
|
||||||
engine,
|
engine,
|
||||||
|
sql,
|
||||||
schema,
|
schema,
|
||||||
limit,
|
limit,
|
||||||
show_cols,
|
show_cols,
|
||||||
|
@ -1006,6 +1006,7 @@ class Database(Model, AuditMixinNullable, ImportMixin):
|
|||||||
def select_star(
|
def select_star(
|
||||||
self,
|
self,
|
||||||
table_name: str,
|
table_name: str,
|
||||||
|
sql: Optional[str] = None,
|
||||||
schema: Optional[str] = None,
|
schema: Optional[str] = None,
|
||||||
limit: int = 100,
|
limit: int = 100,
|
||||||
show_cols: bool = False,
|
show_cols: bool = False,
|
||||||
@ -1020,6 +1021,7 @@ class Database(Model, AuditMixinNullable, ImportMixin):
|
|||||||
return self.db_engine_spec.select_star(
|
return self.db_engine_spec.select_star(
|
||||||
self,
|
self,
|
||||||
table_name,
|
table_name,
|
||||||
|
sql=sql,
|
||||||
schema=schema,
|
schema=schema,
|
||||||
engine=eng,
|
engine=eng,
|
||||||
limit=limit,
|
limit=limit,
|
||||||
|
Loading…
Reference in New Issue
Block a user