diff --git a/superset/db_engine_specs/clickhouse.py b/superset/db_engine_specs/clickhouse.py index a425a719ba364..f7cb9a6ccdff1 100644 --- a/superset/db_engine_specs/clickhouse.py +++ b/superset/db_engine_specs/clickhouse.py @@ -15,13 +15,18 @@ # specific language governing permissions and limitations # under the License. from datetime import datetime -from typing import Dict, Optional, Type +from typing import Dict, Optional, Type, List, TYPE_CHECKING from urllib3.exceptions import NewConnectionError from superset.db_engine_specs.base import BaseEngineSpec from superset.db_engine_specs.exceptions import SupersetDBAPIDatabaseError from superset.utils import core as utils +from superset.extensions import cache_manager + +if TYPE_CHECKING: + # prevent circular imports + from superset.models.core import Database class ClickHouseEngineSpec(BaseEngineSpec): # pylint: disable=abstract-method @@ -48,6 +53,8 @@ class ClickHouseEngineSpec(BaseEngineSpec): # pylint: disable=abstract-method "P1Y": "toStartOfYear(toDateTime({col}))", } + _show_functions_column = "name" + @classmethod def get_dbapi_exception_mapping(cls) -> Dict[Type[Exception], Type[Exception]]: return {NewConnectionError: SupersetDBAPIDatabaseError} @@ -69,3 +76,33 @@ def convert_dttm(cls, target_type: str, dttm: datetime) -> Optional[str]: if tt == utils.TemporalType.DATETIME: return f"""toDateTime('{dttm.isoformat(sep=" ", timespec="seconds")}')""" return None + + + @classmethod + @cache_manager.cache.memoize() + def get_function_names(cls, database: "Database") -> List[str]: + """ + Get a list of function names that are able to be called on the database. + Used for SQL Lab autocomplete. + + :param database: The database to get functions for + :return: A list of function names usable in the database + """ + df = database.get_df("SELECT name FROM system.functions") + if cls._show_functions_column in df: + return df[cls._show_functions_column].tolist() + + columns = df.columns.values.tolist() + logger.error( + "Payload from `SELECT name FROM system.functions` has the incorrect format. " + "Expected column `%s`, found: %s.", + cls._show_functions_column, + ", ".join(columns), + exc_info=True, + ) + # if the results have a single column, use that + if len(columns) == 1: + return df[columns[0]].tolist() + + # otherwise, return no function names to prevent errors + return []