Skip to content

Commit

Permalink
Load custom encoder only when runner enabled (#6748)
Browse files Browse the repository at this point in the history
* Snapshot: 24.02.0-dev

* load encoders only for enabled runners

* try importing within init

---------

Co-authored-by: github-actions <github-actions@github.com>
Co-authored-by: Andrew Chubatiuk <andrew.chubatiuk@motional.com>
Co-authored-by: Guido Petri <18634426+guidopetri@users.noreply.github.com>
  • Loading branch information
4 people committed Feb 10, 2024
1 parent 320fddf commit 939bec2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
12 changes: 6 additions & 6 deletions redash/query_runner/cass.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ def generate_ssl_options_dict(protocol, cert_path=None):
return ssl_options


def custom_json_encoder(dec, o):
if isinstance(o, sortedset):
return list(o)
return None


class Cassandra(BaseQueryRunner):
noop_query = "SELECT dateof(now()) FROM system.local"

@classmethod
def enabled(cls):
return enabled

@classmethod
def custom_json_encoder(cls, dec, o):
if isinstance(o, sortedset):
return list(o)
return None

@classmethod
def configuration_schema(cls):
return {
Expand Down
20 changes: 10 additions & 10 deletions redash/query_runner/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,6 @@
}


def custom_json_encoder(dec, o):
if isinstance(o, ObjectId):
return str(o)
elif isinstance(o, Timestamp):
return dec.default(o.as_datetime())
elif isinstance(o, Decimal128):
return o.to_decimal()
return None


date_regex = re.compile(r'ISODate\("(.*)"\)', re.IGNORECASE)


Expand Down Expand Up @@ -170,6 +160,16 @@ def __init__(self, configuration):
True if "replicaSetName" in self.configuration and self.configuration["replicaSetName"] else False
)

@classmethod
def custom_json_encoder(cls, dec, o):
if isinstance(o, ObjectId):
return str(o)
elif isinstance(o, Timestamp):
return dec.default(o.as_datetime())
elif isinstance(o, Decimal128):
return o.to_decimal()
return None

def _get_db(self):
kwargs = {}
if self.is_replica_set:
Expand Down
24 changes: 12 additions & 12 deletions redash/query_runner/pg.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,6 @@
}


def custom_json_encoder(dec, o):
if isinstance(o, Range):
# From: https://github.com/psycopg/psycopg2/pull/779
if o._bounds is None:
return ""

items = [o._bounds[0], str(o._lower), ", ", str(o._upper), o._bounds[1]]

return "".join(items)
return None


def _wait(conn, timeout=None):
while 1:
try:
Expand Down Expand Up @@ -195,6 +183,18 @@ def configuration_schema(cls):
def type(cls):
return "pg"

@classmethod
def custom_json_encoder(cls, dec, o):
if isinstance(o, Range):
# From: https://github.com/psycopg/psycopg2/pull/779
if o._bounds is None:
return ""

items = [o._bounds[0], str(o._lower), ", ", str(o._upper), o._bounds[1]]

return "".join(items)
return None

def _get_definitions(self, schema, query):
results, error = self.run_query(query, None)

Expand Down
4 changes: 3 additions & 1 deletion redash/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ class JSONEncoder(json.JSONEncoder):
"""Adapter for `json.dumps`."""

def __init__(self, **kwargs):
self.encoders = [m.custom_json_encoder for m in sys.modules.values() if hasattr(m, "custom_json_encoder")]
from redash.query_runner import query_runners

self.encoders = [r.custom_json_encoder for r in query_runners.values() if hasattr(r, "custom_json_encoder")]
super().__init__(**kwargs)

def default(self, o):
Expand Down

0 comments on commit 939bec2

Please sign in to comment.