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

Alternate fix for #2665 #2671

Merged
merged 3 commits into from
Apr 26, 2017
Merged
Show file tree
Hide file tree
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
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 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