Skip to content

Commit

Permalink
Casting bucket category to int32 is a noop
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed May 30, 2015
1 parent 9b3b971 commit c3609b3
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 17 deletions.
7 changes: 5 additions & 2 deletions ibis/sql/exprs.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@
def _cast(translator, expr):
op = expr.op()
arg = translator.translate(op.arg)
sql_type = _type_to_sql_string(op.target_type)
return 'CAST({!s} AS {!s})'.format(arg, sql_type)
if isinstance(op.arg, ir.CategoryValue) and op.target_type == 'int32':
return arg
else:
sql_type = _type_to_sql_string(op.target_type)
return 'CAST({!s} AS {!s})'.format(arg, sql_type)


def _type_to_sql_string(tval):
Expand Down
65 changes: 50 additions & 15 deletions ibis/sql/tests/test_exprs.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,33 @@ def test_search_case(self):
END"""
assert result == expected

def test_where_use_if(self):
expr = api.where(self.table.f > 0, self.table.e, self.table.a)
assert isinstance(expr, ir.FloatValue)

result = self._translate(expr)
expected = "if(f > 0, e, a)"
assert result == expected

def test_nullif_ifnull(self):
table = self.con.table('tpch_lineitem')

f = table.l_quantity

cases = [
(f.nullif(f == 0),
'nullif(l_quantity, l_quantity = 0)'),
(f.fillna(0), 'isnull(l_quantity, 0)'),
]
self._check_expr_cases(cases)


class TestBucketHistogram(unittest.TestCase, ExprSQLTest):

def setUp(self):
self.con = MockConnection()
self.table = self.con.table('alltypes')

def test_bucket_to_case(self):
buckets = [0, 10, 25, 50]

Expand Down Expand Up @@ -499,25 +526,33 @@ def test_bucket_to_case(self):
]
self._check_expr_cases(cases)

def test_where_use_if(self):
expr = api.where(self.table.f > 0, self.table.e, self.table.a)
assert isinstance(expr, ir.FloatValue)
def test_cast_category_to_int_noop(self):
# Because the bucket result is an integer, no explicit cast is
# necessary
expr = (self.table.f.bucket([10], include_over=True,
include_under=True)
.cast('int32'))

result = self._translate(expr)
expected = "if(f > 0, e, a)"
assert result == expected
expected = """\
CASE
WHEN f < 10 THEN 0
WHEN f >= 10 THEN 1
ELSE NULL
END"""

def test_nullif_ifnull(self):
table = self.con.table('tpch_lineitem')
expr2 = (self.table.f.bucket([10], include_over=True,
include_under=True)
.cast('double'))

f = table.l_quantity
expected2 = """\
CAST(CASE
WHEN f < 10 THEN 0
WHEN f >= 10 THEN 1
ELSE NULL
END AS double)"""

cases = [
(f.nullif(f == 0),
'nullif(l_quantity, l_quantity = 0)'),
(f.fillna(0), 'isnull(l_quantity, 0)'),
]
self._check_expr_cases(cases)
self._check_expr_cases([(expr, expected),
(expr2, expected2)])


class TestInNotIn(unittest.TestCase, ExprSQLTest):
Expand Down

0 comments on commit c3609b3

Please sign in to comment.