Skip to content

Commit

Permalink
Alternate fix for #2665 (#2671)
Browse files Browse the repository at this point in the history
* Alternate fix for #2665

* Addressing comments

* Fix doctest
  • Loading branch information
mistercrunch authored Apr 26, 2017
1 parent 0bdc301 commit 1922225
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
4 changes: 2 additions & 2 deletions superset/connectors/druid/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -980,9 +980,9 @@ def get_filters(self, raw_filters): # noqa
eq = eq[0] if len(eq) > 0 else ''
if col in self.num_cols:
if op in ('in', 'not in'):
eq = [utils.js_string_to_num(v) for v in eq]
eq = [utils.string_to_num(v) for v in eq]
else:
eq = utils.js_string_to_num(eq)
eq = utils.string_to_num(eq)
if op == '==':
cond = Dimension(col) == eq
elif op == '!=':
Expand Down
17 changes: 14 additions & 3 deletions superset/connectors/sqla/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import datetime
import logging
import sqlparse
from past.builtins import basestring

import pandas as pd

Expand Down Expand Up @@ -467,9 +468,19 @@ def visit_column(element, compiler, **kw):
col_obj = cols.get(col)
if col_obj:
if op in ('in', 'not in'):
values = [types.strip("'").strip('"') for types in eq]
if col_obj.is_num:
values = [utils.js_string_to_num(s) for s in values]
values = []
for v in eq:
# For backwards compatibility and edge cases
# where a column data type might have changed
if isinstance(v, basestring):
v = v.strip("'").strip('"')
if col_obj.is_num:
v = utils.string_to_num(v)

# Removing empty strings and non numeric values
# targeting numeric columns
if v is not None:
values.append(v)
cond = col_obj.sqla_col.in_(values)
if op == 'not in':
cond = ~cond
Expand Down
30 changes: 24 additions & 6 deletions superset/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,32 @@ def __get__(self, obj, objtype):
def js_string_to_python(item):
return None if item in ('null', 'undefined') else item

def js_string_to_num(item):
if item.isdigit():
return int(item)
s = item

def string_to_num(s):
"""Converts a string to an int/float
Returns ``None`` if it can't be converted
>>> string_to_num('5')
5
>>> string_to_num('5.2')
5.2
>>> string_to_num(10)
10
>>> string_to_num(10.1)
10.1
>>> string_to_num('this is not a string') is None
True
"""
if isinstance(s, (int, float)):
return s
if s.isdigit():
return int(s)
try:
s = float(item)
return float(s)
except ValueError:
return s
return None


class DimSelector(Having):
def __init__(self, **args):
Expand Down

0 comments on commit 1922225

Please sign in to comment.