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(metadb): handle decimals #25921

Merged
merged 2 commits into from
Nov 21, 2023
Merged
Changes from all commits
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
30 changes: 26 additions & 4 deletions superset/extensions/metadb.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from __future__ import annotations

import datetime
import decimal
import operator
import urllib.parse
from collections.abc import Iterator
Expand All @@ -49,7 +50,6 @@
from shillelagh.backends.apsw.dialects.base import APSWDialect
from shillelagh.exceptions import ProgrammingError
from shillelagh.fields import (
Blob,
Boolean,
Date,
DateTime,
Expand Down Expand Up @@ -86,7 +86,7 @@

Queries can also join data across different Superset databases.

The dialect is built in top of the shillelagh library, leveraging SQLite to
The dialect is built in top of the Shillelagh library, leveraging SQLite to
create virtual tables on-the-fly proxying Superset tables. The
`SupersetShillelaghAdapter` adapter is responsible for returning data when a
Superset table is accessed.
Expand Down Expand Up @@ -164,11 +164,32 @@
db_api_type = "DATETIME"


class Decimal(Field[decimal.Decimal, decimal.Decimal]):
"""
Shillelagh field used for representing decimals.
"""

type = "DECIMAL"
db_api_type = "NUMBER"


class FallbackField(Field[Any, str]):
"""
Fallback field for unknown types; converts to string.
"""

type = "TEXT"
db_api_type = "STRING"

def parse(self, value: Any) -> str | None:
return value if value is None else str(value)

Check warning on line 185 in superset/extensions/metadb.py

View check run for this annotation

Codecov / codecov/patch

superset/extensions/metadb.py#L185

Added line #L185 was not covered by tests


# pylint: disable=too-many-instance-attributes
class SupersetShillelaghAdapter(Adapter):

"""
A shillelagh adapter for Superset tables.
A Shillelagh adapter for Superset tables.

Shillelagh adapters are responsible for fetching data from a given resource,
allowing it to be represented as a virtual table in SQLite. This one works
Expand All @@ -190,6 +211,7 @@
datetime.datetime: DateTime,
datetime.time: Time,
datetime.timedelta: Duration,
decimal.Decimal: Decimal,
}

@staticmethod
Expand Down Expand Up @@ -268,7 +290,7 @@
"""
Convert a Python type into a Shillelagh field.
"""
class_ = cls.type_map.get(python_type, Blob)
class_ = cls.type_map.get(python_type, FallbackField)
return class_(filters=[Equal, Range], order=Order.ANY, exact=True)

def _set_columns(self) -> None:
Expand Down
Loading