zhaoyongjie commented on code in PR #21163:
URL: https://github.com/apache/superset/pull/21163#discussion_r959726973
##########
superset/connectors/sqla/utils.py:
##########
@@ -147,6 +149,24 @@ def get_virtual_table_metadata(dataset: "SqlaTable") ->
List[ResultSetColumnType
return cols
+def get_columns_description(
+ database: Database,
+ query: str,
+) -> List[ResultSetColumnType]:
+ db_engine_spec = database.db_engine_spec
+ try:
+ with closing(database.get_sqla_engine().raw_connection()) as conn:
+ cursor = conn.cursor()
+ query = database.apply_limit_to_sql(query, limit=1)
+ cursor.execute(query)
+ db_engine_spec.execute(cursor, query)
+ result = db_engine_spec.fetch_data(cursor, limit=1)
+ result_set = SupersetResultSet(result, cursor.description,
db_engine_spec)
+ return result_set.columns
+ except Exception as ex:
+ raise SupersetGenericDBErrorException(message=str(ex)) from ex
Review Comment:
nice point, I will add this detector API in the separated PR.
##########
superset/connectors/sqla/models.py:
##########
@@ -1124,7 +1125,25 @@ def adhoc_column_to_sqla(
schema=self.schema,
template_processor=template_processor,
)
- sqla_column = literal_column(expression)
+ col_in_metadata = self.get_column(expression)
+ if col_in_metadata:
+ sqla_column = col_in_metadata.get_sqla_col()
+ is_dttm = col_in_metadata.is_temporal
+ else:
+ sqla_column = literal_column(expression)
+ # probe adhoc column type
+ tbl, _ = self.get_from_clause(template_processor)
+ qry = sa.select([sqla_column]).limit(1).select_from(tbl)
+ sql = self.database.compile_sqla_query(qry)
+ col_desc = get_columns_description(self.database, sql)
+ is_dttm = col_desc[0]["is_dttm"]
+
+ if col.get("is_axis") and col.get("time_grain") and is_dttm:
+ sqla_column = self.db_engine_spec.get_timestamp_expr(
+ sqla_column,
+ None,
+ col.get("time_grain"),
+ )
Review Comment:
The `timeGrian` means, this column can use some DB expression (I actually
started with a `config` field), but Superset doesn't support other database
function mappings now. So I just use the old naming style - 'timeGrain`.
I have changed `is_axis` to the `columnType`. It is easy to extend the type
of column using this approach.
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]