Skip to content

Commit

Permalink
Alternate fix for apache#2665
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch committed Apr 24, 2017
1 parent cdfc4a3 commit 47fb14a
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 @@ -976,9 +976,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 @@ -462,9 +463,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:
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
>>> '5'
5
>>> '5.2'
5.2
>>> 10
10
>>> 10.1
10.1
>>> 'this is not a string'
None
"""
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 47fb14a

Please sign in to comment.