Skip to content

Commit

Permalink
feat: add function list to auto-complete to Clickhouse datasource (#1…
Browse files Browse the repository at this point in the history
…6234)

* add function list to auto-complete to Clickhouse datasource, fix #15477

Signed-off-by: Slach <bloodjazman@gmail.com>

* add function list to auto-complete to Clickhouse datasource, fix #15477
fix review comments #16234 (comment)

Signed-off-by: Slach <bloodjazman@gmail.com>

* fix CI/CD https://github.com/apache/superset/pull/16234/checks?check_run_id=3396235776 and https://github.com/apache/superset/pull/16234/checks?check_run_id=3396235707

Signed-off-by: Slach <bloodjazman@gmail.com>

* fix black formatting

Signed-off-by: Slach <bloodjazman@gmail.com>

* fix isort pre-commit hook

Signed-off-by: Slach <bloodjazman@gmail.com>
  • Loading branch information
Slach authored Aug 26, 2021
1 parent ec08750 commit 1badcae
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion superset/db_engine_specs/clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,23 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import logging
from datetime import datetime
from typing import Dict, Optional, Type
from typing import Dict, List, Optional, Type, 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.extensions import cache_manager
from superset.utils import core as utils

if TYPE_CHECKING:
# prevent circular imports
from superset.models.core import Database

logger = logging.getLogger(__name__)


class ClickHouseEngineSpec(BaseEngineSpec): # pylint: disable=abstract-method
"""Dialect for ClickHouse analytical DB."""
Expand All @@ -48,6 +56,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 +79,42 @@ 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
"""
system_functions_sql = "SELECT name FROM system.functions"
try:
df = database.get_df(system_functions_sql)
if cls._show_functions_column in df:
return df[cls._show_functions_column].tolist()
columns = df.columns.values.tolist()
logger.error(
"Payload from `%s` has the incorrect format. "
"Expected column `%s`, found: %s.",
system_functions_sql,
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()
except Exception as ex: # pylint: disable=broad-except
logger.error(
"Query `%s` fire error %s. ",
system_functions_sql,
str(ex),
exc_info=True,
)
return []

# otherwise, return no function names to prevent errors
return []

0 comments on commit 1badcae

Please sign in to comment.