Skip to content

Commit

Permalink
add function list to auto-complete to Clickhouse datasource, fix #15477
Browse files Browse the repository at this point in the history
Signed-off-by: Slach <bloodjazman@gmail.com>
  • Loading branch information
Slach committed Aug 20, 2021
1 parent 575ee24 commit 327ccdb
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion superset/db_engine_specs/clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}
Expand All @@ -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 []

0 comments on commit 327ccdb

Please sign in to comment.