Skip to content

Commit

Permalink
Resolves ibis #1418 and resolves ibis #893
Browse files Browse the repository at this point in the history
  • Loading branch information
xmnlab committed Apr 13, 2018
1 parent 4061ea0 commit c52a0bb
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 65 deletions.
33 changes: 33 additions & 0 deletions ibis/expr/api.py
Expand Up @@ -76,6 +76,7 @@
'negate', 'ifelse',
'Expr', 'Schema',
'window', 'trailing_window', 'cumulative_window',
'pi'
]


Expand Down Expand Up @@ -388,6 +389,8 @@ def row_number():

e = ops.E().to_expr()

pi = ops.Pi().to_expr().name('pi')


def _add_methods(klass, method_table):
for k, v in method_table.items():
Expand All @@ -405,6 +408,17 @@ def f(arg):
return f


def _generic_op(name, klass, doc=None):
def f(*args):
return klass(*args).to_expr()
f.__name__ = name
if doc is not None:
f.__doc__ = doc
else:
f.__doc__ = klass.__doc__
return f


def negate(arg):
"""
Negate a numeric expression
Expand Down Expand Up @@ -1300,6 +1314,16 @@ def _integer_to_interval(arg, unit='s'):
sign = _unary_op('sign', ops.Sign)
sqrt = _unary_op('sqrt', ops.Sqrt)

# TRIGONOMETRIC OPERATIONS
acos = _unary_op('acos', ops.Acos)
asin = _unary_op('asin', ops.Asin)
atan = _unary_op('atan', ops.Atan)
atan2 = _generic_op('atan2', ops.Atan2)
cos = _unary_op('cos', ops.Cos)
cot = _unary_op('cot', ops.Cot)
sin = _unary_op('sin', ops.Sin)
tan = _unary_op('tan', ops.Tan)


_numeric_value_methods = dict(
__neg__=negate,
Expand Down Expand Up @@ -1353,6 +1377,15 @@ def _integer_to_interval(arg, unit='s'):

__mod__=mod,
__rmod__=_rbinop_expr('__rmod__', ops.Modulus),
# trigonometric operations
acos=acos,
asin=asin,
atan=atan,
atan2=atan2,
cos=cos,
cot=cot,
sin=sin,
tan=tan,
)


Expand Down
60 changes: 60 additions & 0 deletions ibis/expr/operations.py
Expand Up @@ -284,6 +284,11 @@ class UnaryOp(ValueOp):
arg = Arg(rlz.any)


class BinaryOp(ValueOp):
left = Arg(rlz.any)
right = Arg(rlz.any)


class Cast(ValueOp):
arg = Arg(rlz.any)
to = Arg(dt.dtype)
Expand Down Expand Up @@ -516,6 +521,53 @@ class Log10(Logarithm):
"""Logarithm base 10"""


# TRIGONOMETRIC OPERATIONS

class TrigonometryUnary(UnaryOp):
"""Trigonometry base unary"""
arg = Arg(rlz.numeric)
output_type = rlz.shape_like('arg', 'float')


class TrigonometryBinary(BinaryOp):
"""Trigonometry base binary"""
left = Arg(rlz.numeric)
right = Arg(rlz.numeric)
output_type = rlz.shape_like('left', 'float')


class Acos(TrigonometryUnary):
"""Returns the arc cosine of x"""


class Asin(TrigonometryUnary):
"""Returns the arc sine of x"""


class Atan(TrigonometryUnary):
"""Returns the arc tangent of x"""


class Atan2(TrigonometryBinary):
"""Returns the arc tangent of x and y"""


class Cos(TrigonometryUnary):
"""Returns the cosine of x"""


class Cot(TrigonometryUnary):
"""Returns the cotangent of x"""


class Sin(TrigonometryUnary):
"""Returns the sine of x"""


class Tan(TrigonometryUnary):
"""Returns the tangent of x"""


class StringUnaryOp(UnaryOp):
arg = Arg(rlz.string)
output_type = rlz.shape_like('arg', dt.string)
Expand Down Expand Up @@ -2183,6 +2235,14 @@ def output_type(self):
return partial(ir.FloatingScalar, dtype=dt.float64)


class Pi(Constant):
"""
"""
def output_type(self):
return partial(ir.FloatingScalar, dtype=dt.float64)


class TemporalUnaryOp(UnaryOp):
arg = Arg(rlz.temporal)

Expand Down
1 change: 0 additions & 1 deletion ibis/mapd/api.py
@@ -1,7 +1,6 @@
from ibis.config import options
from ibis.mapd.compiler import dialect, compiles, rewrites
from ibis.mapd.client import MapDClient
from ibis.mapd.operations import pi

import ibis.common as com

Expand Down
17 changes: 17 additions & 0 deletions ibis/mapd/compiler.py
Expand Up @@ -252,3 +252,20 @@ def compile_corr(translator, expr):
compiled_y = translator.translate(y)

return 'CORR%s(%s, %s)' % (f_type, compiled_x, compiled_y)


@compiles(ops.StringLength)
def compile_char_length(translator, expr):
# pull out the arguments to the expression
arg = expr.op().args[0]

# compile the argument
compiled_arg = translator.translate(arg)

return 'CHAR_LENGTH(%s)' % compiled_arg


@compiles(ops.Pi)
def compile_pi(*args, **kwargs):
# pull out the arguments to the expression
return 'PI()'
72 changes: 8 additions & 64 deletions ibis/mapd/operations.py
Expand Up @@ -12,10 +12,6 @@
from ibis.expr.types import NumericValue, StringValue


def pi_compile():
return 'pi()'


def _cast(translator, expr):
from ibis.mapd.client import MapDDataType

Expand Down Expand Up @@ -511,15 +507,6 @@ class Degrees(ops.UnaryOp):
output_type = rlz.shape_like('arg', ops.dt.float)


class PI(ops.ValueOp):
"""Converts radians to degrees"""
def output_type(self):
return ops.dt.float64.scalar_type()


pi = PI()


class Log(ops.Ln):
"""
Expand Down Expand Up @@ -569,49 +556,6 @@ class Corr(ops.BinaryOp):
output_type = rlz.shape_like('x', ops.dt.float)


# TRIGONOMETRY

class TrigonometryUnary(ops.UnaryOp):
"""Trigonometry base unary"""
output_type = rlz.shape_like('arg', 'float')


class TrigonometryBinary(ops.BinaryOp):
"""Trigonometry base binary"""


class Acos(TrigonometryUnary):
"""Returns the arc cosine of x"""


class Asin(TrigonometryUnary):
"""Returns the arc sine of x"""


class Atan(TrigonometryUnary):
"""Returns the arc tangent of x"""


class Atan2(TrigonometryBinary):
"""Returns the arc tangent of x and y"""


class Cos(TrigonometryUnary):
"""Returns the cosine of x"""


class Cot(TrigonometryUnary):
"""Returns the cotangent of x"""


class Sin(TrigonometryUnary):
"""Returns the sine of x"""


class Tan(TrigonometryUnary):
"""Returns the tangent of x"""


# GEOMETRIC

class Distance_In_Meters(ops.ValueOp):
Expand Down Expand Up @@ -721,14 +665,14 @@ class StringLengthBytes(ops.UnaryOp):


_trigonometric_ops = {
Acos: unary('acos'),
Asin: unary('asin'),
Atan: unary('atan'),
Atan2: fixed_arity('atan2', 2),
Cos: unary('cos'),
Cot: unary('cot'),
Sin: unary('sin'),
Tan: unary('tan')
ops.Acos: unary('acos'),
ops.Asin: unary('asin'),
ops.Atan: unary('atan'),
ops.Atan2: fixed_arity('atan2', 2),
ops.Cos: unary('cos'),
ops.Cot: unary('cot'),
ops.Sin: unary('sin'),
ops.Tan: unary('tan')
}

_geometric_ops = {
Expand Down

0 comments on commit c52a0bb

Please sign in to comment.