Skip to content
This repository has been archived by the owner on Nov 2, 2023. It is now read-only.

Commit

Permalink
Make patito compatible with DuckDB version 0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobGM committed Jul 5, 2022
1 parent bab0d03 commit 2eef479
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 44 deletions.
78 changes: 39 additions & 39 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 21 additions & 5 deletions src/patito/duckdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,24 @@ def _enum_type_name(field_properties: dict) -> str:
return f"enum__{value_hash}"


def _is_missing_enum_type_exception(exception: BaseException) -> bool:
"""
Return True if the given exception might be caused by missing enum type definitions.
Args:
exception: Exception raised by DuckDB.
Returns:
True if the exception might be caused by a missing SQL enum type definition.
"""
description = str(exception)
# DuckDB version <= 0.3.4
old_exception = description.startswith("Not implemented Error: DataType")
# DuckDB version >= 0.4.0
new_exception = description.startswith("Catalog Error: Type with name enum_")
return old_exception or new_exception


class Relation(Generic[ModelType]):
# Can be set by subclasses in order to specify the serialization class for rows.
# Must accept column names as keyword arguments.
Expand Down Expand Up @@ -597,9 +615,7 @@ def project(
# We might get a RunTime error if the enum type has not
# been created yet. If so, we create all enum types for
# this model.
if self.model is not None and str(exc).startswith(
"Not implemented Error: DataType"
):
if self.model is not None and _is_missing_enum_type_exception(exc):
self.database.create_enum_types(model=self.model)
relation = self._relation.project(projection)
else:
Expand Down Expand Up @@ -799,7 +815,7 @@ def with_missing_defaultable_columns(
# We might get a RunTime error if the enum type has not
# been created yet. If so, we create all enum types for
# this model.
if str(exc).startswith("Not implemented Error: DataType"):
if _is_missing_enum_type_exception(exc):
self.database.create_enum_types(model=self.model)
relation = self._relation.project(projection)
else:
Expand Down Expand Up @@ -846,7 +862,7 @@ def with_missing_nullable_columns(
# We might get a RunTime error if the enum type has not
# been created yet. If so, we create all enum types for
# this model.
if str(exc).startswith("Not implemented Error: DataType"):
if _is_missing_enum_type_exception(exc):
self.database.create_enum_types(model=self.model)
relation = self._relation.project(projection)
else:
Expand Down

0 comments on commit 2eef479

Please sign in to comment.