Skip to content

Commit

Permalink
Merge pull request #63 from xmnlab/master
Browse files Browse the repository at this point in the history
Changes from the review
  • Loading branch information
xmnlab authored Jun 18, 2018
2 parents 088f49d + f9f017d commit 7043581
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 6 deletions.
13 changes: 13 additions & 0 deletions ibis/expr/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2299,6 +2299,9 @@ def _timestamp_sub(left, right):

__radd__=_timestamp_radd,
radd=_timestamp_radd,

__rsub__=_timestamp_sub,
rsub=_timestamp_sub,
day_of_week=property(lambda self: ops.DayOfWeekNode(self).to_expr()),
)

Expand Down Expand Up @@ -2355,6 +2358,9 @@ def _date_sub(left, right):
__sub__=_date_sub,
sub=_date_sub,

__rsub__=_date_sub,
rsub=_date_sub,

__add__=_date_add,
add=_date_add,

Expand All @@ -2378,6 +2384,7 @@ def _interval_property(target_unit):

_interval_add = _binop_expr('__add__', ops.IntervalAdd)
_interval_radd = _binop_expr('__radd__', ops.IntervalAdd)
_interval_sub = _binop_expr('__sub__', ops.IntervalSubtract)
_interval_mul = _binop_expr('__mul__', ops.IntervalMultiply)
_interval_rmul = _binop_expr('__rmul__', ops.IntervalMultiply)
_interval_floordiv = _binop_expr('__floordiv__', ops.IntervalFloorDivide)
Expand All @@ -2399,6 +2406,9 @@ def _interval_property(target_unit):
__add__=_interval_add,
add=_interval_add,

__sub__=_interval_sub,
sub=_interval_sub,

__radd__=_interval_radd,
radd=_interval_radd,

Expand Down Expand Up @@ -2501,6 +2511,9 @@ def _time_sub(left, right):
__sub__=_time_sub,
sub=_time_sub,

__rsub__=_time_sub,
rsub=_time_sub,

__add__=_time_add,
add=_time_add,

Expand Down
6 changes: 6 additions & 0 deletions ibis/expr/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2581,6 +2581,12 @@ class IntervalAdd(IntervalBinaryOp):
op = operator.add


class IntervalSubtract(IntervalBinaryOp):
left = Arg(rlz.interval)
right = Arg(rlz.interval)
op = operator.sub


class IntervalMultiply(IntervalBinaryOp):
left = Arg(rlz.interval)
right = Arg(rlz.numeric)
Expand Down
45 changes: 45 additions & 0 deletions ibis/expr/tests/test_temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,24 @@ def test_multiply(expr):
assert expr.type().unit == 'D'


@pytest.mark.parametrize('expr', [
api.day() + api.day(),
api.day(2) + api.hour(4)
])
def test_add(expr):
assert isinstance(expr, ir.IntervalScalar)
assert expr.type().unit == 'D'


@pytest.mark.parametrize('expr', [
api.day(3) - api.day(),
api.day(2) - api.hour(4)
])
def test_subtract(expr):
assert isinstance(expr, ir.IntervalScalar)
assert expr.type().unit == 'D'


@pytest.mark.parametrize(('case', 'expected'), [
(api.second(2).to_unit('s'), api.second(2)),
(api.second(2).to_unit('ms'), api.millisecond(2 * 1000)),
Expand Down Expand Up @@ -175,6 +193,15 @@ def test_interval_time_expr(table):
assert isinstance(expr, ir.TimeColumn)
assert isinstance(expr.op(), ops.TimeAdd)

expr = x - c
assert isinstance(expr, ir.TimeColumn)
assert isinstance(expr.op(), ops.TimeSub)

# test radd
expr = c - x
assert isinstance(expr, ir.TimeColumn)
assert isinstance(expr.op(), ops.TimeSub)


def test_interval_date_expr(table):
c = table.j
Expand All @@ -189,6 +216,15 @@ def test_interval_date_expr(table):
assert isinstance(expr, ir.DateColumn)
assert isinstance(expr.op(), ops.DateAdd)

expr = x - c
assert isinstance(expr, ir.DateColumn)
assert isinstance(expr.op(), ops.DateSub)

# test radd
expr = c - x
assert isinstance(expr, ir.DateColumn)
assert isinstance(expr.op(), ops.DateSub)


def test_interval_timestamp_expr(table):
c = table.i
Expand All @@ -203,6 +239,15 @@ def test_interval_timestamp_expr(table):
assert isinstance(expr, ir.TimestampColumn)
assert isinstance(expr.op(), ops.TimestampAdd)

expr = x - c
assert isinstance(expr, ir.TimestampColumn)
assert isinstance(expr.op(), ops.TimestampSub)

# test radd
expr = c - x
assert isinstance(expr, ir.TimestampColumn)
assert isinstance(expr.op(), ops.TimestampSub)


@pytest.mark.xfail(raises=AssertionError, reason='NYI')
def test_compound_offset():
Expand Down
2 changes: 1 addition & 1 deletion ibis/mapd/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ def alter_user(
self, name, password=None, is_super=None, insert_access=None
):
"""
Create a new MapD database
Alter MapD user parameters
Parameters
----------
Expand Down
18 changes: 18 additions & 0 deletions ibis/mapd/tests/test_operations.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ibis
import numpy as np
import pandas as pd
import pytest

from pytest import param
Expand Down Expand Up @@ -33,3 +34,20 @@
def test_operations_scalar(alltypes, result_fn, expected):
result = result_fn(alltypes).execute()
np.testing.assert_allclose(result, expected)


@pytest.mark.parametrize(('result_fn', 'check_result'), [
param(
lambda t: (
t[t.date_string_col][t.date_string_col.ilike('10/%')].limit(1)
),
lambda v: v.startswith('10/'),
id='string_ilike'
)
])
def test_string_operations(alltypes, result_fn, check_result):
result = result_fn(alltypes).execute()

if isinstance(result, pd.DataFrame):
result = result.values[0][0]
assert check_result(result)
2 changes: 1 addition & 1 deletion ibis/tests/all/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
pytest.param(Impala, marks=pytest.mark.impala)
]

if sys.version_info.major == 3:
if sys.version_info.major > 2:
params_backend.append(pytest.param(MapD, marks=pytest.mark.mapd))


Expand Down
10 changes: 6 additions & 4 deletions ibis/tests/all/test_temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ def test_date_truncate(backend, alltypes, df, unit):

@pytest.mark.parametrize(
'unit',
['Y', pytest.mark.xfail('Q'), 'M', 'W', 'D', 'h', 'm', 's', 'ms', 'us'])
['Y', pytest.mark.xfail('Q'), 'M', 'W', 'D', 'h', 'm', 's',
pytest.mark.xfail('ms'), pytest.mark.xfail('us')
])
@tu.skipif_unsupported
@tu.skipif_backend('MapD')
def test_integer_to_interval_timestamp(backend, con, alltypes, df, unit):
interval = alltypes.int_col.to_interval(unit=unit)
expr = alltypes.timestamp_col + interval
Expand Down Expand Up @@ -192,9 +193,10 @@ def test_strftime(backend, con, alltypes, df, ibis_pattern, pandas_pattern):
}


@pytest.mark.parametrize('unit', ['D', 's', 'ms', 'us', 'ns'])
@pytest.mark.parametrize(
'unit', ['D', 's', 'ms', pytest.mark.xfail('us'), pytest.mark.xfail('ns')]
)
@tu.skipif_unsupported
@tu.skipif_backend('MapD')
def test_to_timestamp(backend, con, alltypes, df, unit):
if unit not in backend.supported_to_timestamp_units:
pytest.skip(
Expand Down

0 comments on commit 7043581

Please sign in to comment.