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

Add custom JSON encoder for PostgreSQL #3442

Merged
merged 2 commits into from
Feb 25, 2019
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
25 changes: 23 additions & 2 deletions redash/query_runner/pg.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import select

import psycopg2
from psycopg2.extras import Range

from redash.query_runner import *
from redash.utils import json_dumps, json_loads
from redash.utils import JSONEncoder, json_dumps, json_loads

logger = logging.getLogger(__name__)

Expand All @@ -28,6 +29,26 @@
}


class PostgreSQLJSONEncoder(JSONEncoder):
def default(self, 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)
arikfr marked this conversation as resolved.
Show resolved Hide resolved

return super(PostgreSQLJSONEncoder, self).default(o)


def _wait(conn, timeout=None):
while 1:
try:
Expand Down Expand Up @@ -165,7 +186,7 @@ def run_query(self, query, user):

data = {'columns': columns, 'rows': rows}
error = None
json_data = json_dumps(data, ignore_nan=True)
json_data = json_dumps(data, ignore_nan=True, cls=PostgreSQLJSONEncoder)
else:
error = 'Query completed but it returned no data.'
json_data = None
Expand Down