Skip to content

Commit

Permalink
fix(dataset): handle missing python_type gracefully (#19553)
Browse files Browse the repository at this point in the history
* fix(dataset): handle missing python_type gracefully

* refactor TEMPORAL_TYPES

(cherry picked from commit d9343a4)
  • Loading branch information
villebro committed Apr 8, 2022
1 parent 44eb81e commit 5ca1266
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
13 changes: 10 additions & 3 deletions superset/connectors/sqla/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# specific language governing permissions and limitations
# under the License.
from contextlib import closing
from datetime import date, datetime, time, timedelta
from typing import Callable, Dict, List, Optional, Set, TYPE_CHECKING

import sqlparse
Expand All @@ -41,7 +42,7 @@
from superset.connectors.sqla.models import SqlaTable


TEMPORAL_TYPES = {"DATETIME", "DATE", "TIME", "TIMEDELTA"}
TEMPORAL_TYPES = {date, datetime, time, timedelta}


def get_physical_table_metadata(
Expand Down Expand Up @@ -172,6 +173,13 @@ def validate_adhoc_subquery(
return ";\n".join(str(statement) for statement in statements)


def is_column_type_temporal(column_type: TypeEngine) -> bool:
try:
return column_type.python_type in TEMPORAL_TYPES
except NotImplementedError:
return False


def load_or_create_tables( # pylint: disable=too-many-arguments
session: Session,
database_id: int,
Expand Down Expand Up @@ -223,8 +231,7 @@ def load_or_create_tables( # pylint: disable=too-many-arguments
name=column["name"],
type=str(column["type"]),
expression=conditional_quote(column["name"]),
is_temporal=column["type"].python_type.__name__.upper()
in TEMPORAL_TYPES,
is_temporal=is_column_type_temporal(column["type"]),
is_aggregation=False,
is_physical=True,
is_spatial=False,
Expand Down
14 changes: 11 additions & 3 deletions superset/migrations/versions/b8d3a24d9131_new_dataset_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"""

import json
from datetime import date, datetime, time, timedelta
from typing import Callable, List, Optional, Set
from uuid import uuid4

Expand All @@ -35,6 +36,7 @@
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import backref, relationship, Session
from sqlalchemy.schema import UniqueConstraint
from sqlalchemy.sql.type_api import TypeEngine
from sqlalchemy_utils import UUIDType

from superset import app, db
Expand Down Expand Up @@ -230,7 +232,14 @@ class NewDataset(Base):
external_url = sa.Column(sa.Text, nullable=True)


TEMPORAL_TYPES = {"DATETIME", "DATE", "TIME", "TIMEDELTA"}
TEMPORAL_TYPES = {date, datetime, time, timedelta}


def is_column_type_temporal(column_type: TypeEngine) -> bool:
try:
return column_type.python_type in TEMPORAL_TYPES
except NotImplementedError:
return False


def load_or_create_tables(
Expand Down Expand Up @@ -285,8 +294,7 @@ def load_or_create_tables(
name=column["name"],
type=str(column["type"]),
expression=conditional_quote(column["name"]),
is_temporal=column["type"].python_type.__name__.upper()
in TEMPORAL_TYPES,
is_temporal=is_column_type_temporal(column["type"]),
is_aggregation=False,
is_physical=True,
is_spatial=False,
Expand Down

0 comments on commit 5ca1266

Please sign in to comment.