Skip to content

Commit

Permalink
fix(presto): default unknown types to string type (#10753)
Browse files Browse the repository at this point in the history
* fix(presto): default unknown types to string type

* lint
  • Loading branch information
villebro authored Sep 22, 2020
1 parent 448a41a commit bd140e0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
6 changes: 4 additions & 2 deletions superset/db_engine_specs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,16 +902,18 @@ def make_label_compatible(cls, label: str) -> Union[str, quoted_name]:
return label_mutated

@classmethod
def get_sqla_column_type(cls, type_: str) -> Optional[TypeEngine]:
def get_sqla_column_type(cls, type_: Optional[str]) -> Optional[TypeEngine]:
"""
Return a sqlalchemy native column type that corresponds to the column type
defined in the data source (return None to use default type inferred by
SQLAlchemy). Override `_column_type_mappings` for specific needs
SQLAlchemy). Override `column_type_mappings` for specific needs
(see MSSQL for example of NCHAR/NVARCHAR handling).
:param type_: Column type returned by inspector
:return: SqlAlchemy column type
"""
if not type_:
return None
for regex, sqla_type in cls.column_type_mappings:
match = regex.match(type_)
if match:
Expand Down
18 changes: 13 additions & 5 deletions superset/db_engine_specs/presto.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ def get_view_names(
return [row[0] for row in results]

@classmethod
def _create_column_info(cls, name: str, data_type: str) -> Dict[str, Any]:
def _create_column_info(
cls, name: str, data_type: types.TypeEngine
) -> Dict[str, Any]:
"""
Create column info object
:param name: column name
Expand Down Expand Up @@ -265,8 +267,11 @@ def _parse_structural_column( # pylint: disable=too-many-locals,too-many-branch
# overall structural data type
column_type = cls.get_sqla_column_type(field_info[1])
if column_type is None:
raise NotImplementedError(
_("Unknown column type: %(col)s", col=field_info[1])
column_type = types.String()
logger.info(
"Did not recognize type %s of column %s",
field_info[1],
field_info[0],
)
if field_info[1] == "array" or field_info[1] == "row":
stack.append((field_info[0], field_info[1]))
Expand Down Expand Up @@ -381,8 +386,11 @@ def get_columns(
# otherwise column is a basic data type
column_type = cls.get_sqla_column_type(column.Type)
if column_type is None:
raise NotImplementedError(
_("Unknown column type: %(col)s", col=column_type)
column_type = types.String()
logger.info(
"Did not recognize type %s of column %s",
str(column.Type),
str(column.Column),
)
column_info = cls._create_column_info(column.Column, column_type)
column_info["nullable"] = getattr(column, "Null", True)
Expand Down
3 changes: 3 additions & 0 deletions tests/db_engine_specs/presto_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,3 +511,6 @@ def test_get_sqla_column_type(self):

sqla_type = PrestoEngineSpec.get_sqla_column_type("integer")
assert isinstance(sqla_type, types.Integer)

sqla_type = PrestoEngineSpec.get_sqla_column_type(None)
assert sqla_type is None

0 comments on commit bd140e0

Please sign in to comment.