Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(explore): Druid data type conversion #13595

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion superset/db_engine_specs/druid.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,26 @@
# under the License.
import json
import logging
import re
from datetime import datetime
from typing import Any, Dict, Optional, TYPE_CHECKING
from typing import (
Any,
Callable,
Dict,
Match,
Optional,
Pattern,
Tuple,
TYPE_CHECKING,
Union,
)

from sqlalchemy.types import TIMESTAMP, TypeEngine

from superset.db_engine_specs.base import BaseEngineSpec
from superset.exceptions import SupersetException
from superset.utils import core as utils
from superset.utils.core import ColumnSpec, GenericDataType

if TYPE_CHECKING:
from superset.connectors.sqla.models import TableColumn
Expand All @@ -38,6 +52,14 @@ class DruidEngineSpec(BaseEngineSpec): # pylint: disable=abstract-method
allows_joins = False
allows_subqueries = True

column_type_mappings = (
(
re.compile(r"^__time", re.IGNORECASE),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's actually the column name that is __type, not the type. So this needs to be picked up in get_column_spec, probably by adding the column name to the signature.

TIMESTAMP(),
utils.GenericDataType.TEMPORAL,
),
)

_time_grain_expressions = {
None: "{col}",
"PT1S": "FLOOR({col} TO SECOND)",
Expand Down Expand Up @@ -91,3 +113,26 @@ def convert_dttm(cls, target_type: str, dttm: datetime) -> Optional[str]:
if tt in (utils.TemporalType.DATETIME, utils.TemporalType.TIMESTAMP):
return f"""TIME_PARSE('{dttm.isoformat(timespec="seconds")}')"""
return None

@classmethod
def get_column_spec( # type: ignore
cls,
native_type: Optional[str],
source: utils.ColumnTypeSource = utils.ColumnTypeSource.GET_TABLE,
column_type_mappings: Tuple[
Tuple[
Pattern[str],
Union[TypeEngine, Callable[[Match[str]], TypeEngine]],
GenericDataType,
],
...,
] = column_type_mappings,
) -> Union[ColumnSpec, None]:

column_spec = super().get_column_spec(native_type)
if column_spec:
return column_spec

return super().get_column_spec(
native_type, column_type_mappings=column_type_mappings
)