villebro commented on a change in pull request #10645:
URL:
https://github.com/apache/incubator-superset/pull/10645#discussion_r474546416
##########
File path: superset/connectors/sqla/models.py
##########
@@ -629,12 +636,38 @@ def sql_url(self) -> str:
return self.database.sql_url + "?table_name=" + str(self.table_name)
def external_metadata(self) -> List[Dict[str, str]]:
- cols = self.database.get_columns(self.table_name, schema=self.schema)
- for col in cols:
- try:
- col["type"] = str(col["type"])
- except CompileError:
- col["type"] = "UNKNOWN"
+ db_engine_spec = self.database.db_engine_spec
+ if self.sql:
+ engine = self.database.get_sqla_engine()
+ parsed_query = ParsedQuery(self.sql)
+ if not parsed_query.is_readonly():
+ raise SupersetSecurityException(
+ _("Only `SELECT` statements are allowed")
+ )
+ statements = parsed_query.get_statements()
+ if len(statements) > 1:
+ raise SupersetSecurityException(_("Only single queries
supported"))
+ with closing(engine.raw_connection()) as conn:
+ with closing(conn.cursor()) as cursor:
+ query = statements[0]
+ db_engine_spec.execute(cursor, query)
+ result = db_engine_spec.fetch_data(cursor, limit=1)
+ result_set = SupersetResultSet(
+ result, cursor.description, db_engine_spec
+ )
+ cols = result_set.columns
+ else:
+ db_dialect = self.database.get_dialect()
+ cols = self.database.get_columns(
+ self.table_name, schema=self.schema or None
Review comment:
I had add checking for empty strings here (`schema=self.schema or None`)
to get CI to pass, as I wasn't able to track down why CI was always creating
empty schema names instead of `None` on some of the examples databases. I later
noticed that similar logic is being applied elsewhere (e.g.
[here](https://github.com/apache/incubator-superset/blob/878f06d1339bb32f74a70e3c6c5d338c86a6f5c6/superset/models/core.py#L611)
and
[here](https://github.com/apache/incubator-superset/blob/878f06d1339bb32f74a70e3c6c5d338c86a6f5c6/superset/models/core.py#L675)),
so I figured it's ok to solve the symptoms here with this simple workaround
instead of ensuring undefined schemas are always `None`.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]