Exemplo n.º 1
0
    def __init__(self, validator, default=_undefined, show=True):
        """Argument constructor

        Parameters
        ----------
        validator : Union[Callable[[arg], coerced], Type, Tuple[Type]]
            Function which handles validation and/or coercion of the given
            argument.
        default : Union[Any, Callable[[], str]]
            In case of missing (None) value for validation this will be used.
            Note, that default value (except for None) must also pass the inner
            validator.
            If callable is passed, it will be executed just before the inner,
            and itsreturn value will be treaded as default.
        show : bool
            Whether to show this argument in an :class:`~ibis.expr.types.Expr`
            that contains it.
        """
        self.default = default
        self.show = show
        if isinstance(validator, type):
            self.validator = rlz.instance_of(validator)
        elif isinstance(validator, tuple):
            assert util.all_of(validator, type)
            self.validator = rlz.instance_of(validator)
        elif callable(validator):
            self.validator = validator
        else:
            raise TypeError(
                'Argument validator must be a callable, type or '
                'tuple of types, given: {}'.format(validator)
            )
Exemplo n.º 2
0
    def _walk_join_tree(self, op):
        left = op.left.op()
        right = op.right.op()

        if util.all_of([left, right], ops.Join):
            raise NotImplementedError('Do not support joins between '
                                      'joins yet')

        self._validate_join_predicates(op.predicates)

        jname = self._get_join_type(op)

        # Read off tables and join predicates left-to-right in
        # depth-first order
        if isinstance(left, ops.Join):
            self._walk_join_tree(left)
            self.join_tables.append(self._format_table(op.right))
            self.join_types.append(jname)
            self.join_predicates.append(op.predicates)
        elif isinstance(right, ops.Join):
            # When rewrites are possible at the expression IR stage, we should
            # do them. Otherwise subqueries might be necessary in some cases
            # here
            raise NotImplementedError('not allowing joins on right '
                                      'side yet')
        else:
            # Both tables
            self.join_tables.append(self._format_table(op.left))
            self.join_tables.append(self._format_table(op.right))
            self.join_types.append(jname)
            self.join_predicates.append(op.predicates)
Exemplo n.º 3
0
    def _walk_join_tree(self, op):
        left = op.left.op()
        right = op.right.op()

        if util.all_of([left, right], ops.Join):
            raise NotImplementedError('Do not support joins between '
                                      'joins yet')

        self._validate_join_predicates(op.predicates)

        jname = self._get_join_type(op)

        # Read off tables and join predicates left-to-right in
        # depth-first order
        if isinstance(left, ops.Join):
            self._walk_join_tree(left)
            self.join_tables.append(self._format_table(op.right))
            self.join_types.append(jname)
            self.join_predicates.append(op.predicates)
        elif isinstance(right, ops.Join):
            # When rewrites are possible at the expression IR stage, we should
            # do them. Otherwise subqueries might be necessary in some cases
            # here
            raise NotImplementedError('not allowing joins on right '
                                      'side yet')
        else:
            # Both tables
            self.join_tables.append(self._format_table(op.left))
            self.join_tables.append(self._format_table(op.right))
            self.join_types.append(jname)
            self.join_predicates.append(op.predicates)
Exemplo n.º 4
0
def Argument(validator, default=EMPTY):
    """Argument constructor
    Parameters
    ----------
    validator : Union[Callable[[arg], coerced], Type, Tuple[Type]]
        Function which handles validation and/or coercion of the given
        argument.
    default : Union[Any, Callable[[], str]]
        In case of missing (None) value for validation this will be used.
        Note, that default value (except for None) must also pass the inner
        validator.
        If callable is passed, it will be executed just before the inner,
        and itsreturn value will be treaded as default.
    """
    if isinstance(validator, Validator):
        pass
    elif isinstance(validator, type):
        validator = _InstanceOf(validator)
    elif isinstance(validator, tuple):
        assert all_of(validator, type)
        validator = _InstanceOf(validator)
    elif isinstance(validator, Validator):
        validator = validator
    elif callable(validator):
        validator = _ValidatorFunction(validator)
    else:
        raise TypeError(
            'Argument validator must be a callable, type or '
            'tuple of types, given: {}'.format(validator)
        )

    if default is EMPTY:
        return validator
    else:
        return Optional(validator, default=default)
Exemplo n.º 5
0
    def __init__(self, validator, default=_undefined, show=True):
        """Argument constructor

        Parameters
        ----------
        validator : Union[Callable[[arg], coerced], Type, Tuple[Type]]
            Function which handles validation and/or coercion of the given
            argument.
        default : Union[Any, Callable[[], str]]
            In case of missing (None) value for validation this will be used.
            Note, that default value (except for None) must also pass the inner
            validator.
            If callable is passed, it will be executed just before the inner,
            and itsreturn value will be treaded as default.
        show : bool
            Whether to show this argument in an :class:`~ibis.expr.types.Expr`
            that contains it.
        """
        self.default = default
        self.show = show
        if isinstance(validator, type):
            self.validator = rlz.instance_of(validator)
        elif isinstance(validator, tuple):
            assert util.all_of(validator, type)
            self.validator = rlz.instance_of(validator)
        elif callable(validator):
            self.validator = validator
        else:
            raise TypeError('Argument validator must be a callable, type or '
                            'tuple of types, given: {}'.format(validator))
Exemplo n.º 6
0
def assert_equal(left, right):
    if util.all_of([left, right], Schema):
        assert left.equals(right),\
            'Comparing schemas: \n%s !=\n%s' % (repr(left), repr(right))
    else:
        assert left.equals(right), ('Objects unequal: {0}\nvs\n{1}'.format(
            repr(left), repr(right)))
Exemplo n.º 7
0
def assert_equal(left, right):
    if util.all_of([left, right], Schema):
        assert left.equals(right),\
            'Comparing schemas: \n%s !=\n%s' % (repr(left), repr(right))
    else:
        assert left.equals(right), ('Objects unequal: {0}\nvs\n{1}'
                                    .format(repr(left), repr(right)))
Exemplo n.º 8
0
 def root_tables(self):
     if util.all_of([self.left.op(), self.right.op()],
                    (Join, Projection)):
         # Unraveling is not possible
         return [self.left.op(), self.right.op()]
     else:
         return ir.distinct_roots(self.left, self.right)
Exemplo n.º 9
0
    def _walk_join_tree(self, op):
        left = op.left.op()
        right = op.right.op()

        if util.all_of([left, right], ops.Join):
            raise NotImplementedError('Do not support joins between '
                                      'joins yet')

        self._validate_join_predicates(op.predicates)

        jname = self._get_join_type(op)

        # Read off tables and join predicates left-to-right in
        # depth-first order
        if isinstance(left, ops.Join):
            self._walk_join_tree(left)
            self.join_tables.append(self._format_table(op.right))
        elif isinstance(right, ops.Join):
            self.join_tables.append(self._format_table(op.left))
            self._walk_join_tree(right)
        else:
            # Both tables
            self.join_tables.append(self._format_table(op.left))
            self.join_tables.append(self._format_table(op.right))

        self.join_types.append(jname)
        self.join_predicates.append(op.predicates)
Exemplo n.º 10
0
    def output_dtype(self):
        args = getattr(self, name)
        if util.all_of(args, ir.IntegerValue):
            result = _promote_numeric_binop(args, op)
        else:
            result = highest_precedence_dtype(args)

        return result
Exemplo n.º 11
0
def _true_divide(t, expr):
    op = expr.op()
    left, right = args = op.args

    if util.all_of(args, ir.IntegerValue):
        return t.translate(left.div(right.cast('double')))

    return fixed_arity(lambda x, y: x / y, 2)(t, expr)
Exemplo n.º 12
0
def _true_divide(t, expr):
    op = expr.op()
    left, right = op.args

    if util.all_of(op.args, ir.IntegerValue):
        new_expr = left.div(right.cast('double'))
        return t.translate(new_expr)

    return fixed_arity(lambda x, y: x / y, 2)(t, expr)
Exemplo n.º 13
0
def assert_equal(left, right):
    """Assert that two ibis objects are equal."""

    if util.all_of([left, right], ibis.Schema):
        assert left.equals(right), 'Comparing schemas: \n{!r} !=\n{!r}'.format(
            left, right)
    else:
        assert left.equals(right), 'Objects unequal: \n{}\nvs\n{}'.format(
            repr(left), repr(right))
Exemplo n.º 14
0
 def _get_type(self):
     if util.any_of(self.args, ir.FloatingValue):
         if util.any_of(self.args, ir.DoubleValue):
             return 'double'
         else:
             return 'float'
     elif util.all_of(self.args, ir.IntegerValue):
         return self._get_int_type()
     elif util.any_of(self.args, ir.DecimalValue):
         return _decimal_promoted_type(self.args)
     else:
         raise NotImplementedError
Exemplo n.º 15
0
 def _get_type(self):
     if util.any_of(self.args, ir.FloatingValue):
         if util.any_of(self.args, ir.DoubleValue):
             return 'double'
         else:
             return 'float'
     elif util.all_of(self.args, ir.IntegerValue):
         return self._get_int_type()
     elif util.any_of(self.args, ir.DecimalValue):
         return _decimal_promoted_type(self.args)
     else:
         raise NotImplementedError
Exemplo n.º 16
0
 def _get_type(self):
     if util.any_of(self.args, ir.FloatingValue):
         if util.any_of(self.args, ir.DoubleValue):
             return 'double'
         else:
             return 'float'
     elif util.any_of(self.args, ir.DecimalValue):
         return _decimal_promoted_type(self.args)
     elif util.all_of(self.args, ir.IntegerValue):
         return 'double'
     else:
         raise NotImplementedError(
             'Operands {}, {} not supported for binary operation {}'.format(
                 type(self.left).__name__,
                 type(self.right).__name__, self.op.__name__))
Exemplo n.º 17
0
    def _get_int_type(self):
        deps = [x.op() for x in self.args]

        if util.all_of(deps, ir.Literal):
            return _smallest_int_containing([self.op(deps[0].value, deps[1].value)])
        elif util.any_of(deps, ir.Literal):
            if isinstance(deps[0], ir.Literal):
                val = deps[0].value
                atype = self.args[1].type()
            else:
                val = deps[1].value
                atype = self.args[0].type()
            return _int_one_literal_promotion(atype, val, self.op)
        else:
            return _int_bounds_promotion(self.left.type(), self.right.type(), self.op)
Exemplo n.º 18
0
    def _get_type(self):
        rval = self.args[1].op()

        if util.any_of(self.args, ir.FloatingValue):
            if util.any_of(self.args, ir.DoubleValue):
                return 'double'
            else:
                return 'float'
        elif util.any_of(self.args, ir.DecimalValue):
            return _decimal_promoted_type(self.args)
        elif isinstance(rval, ir.Literal) and rval.value < 0:
            return 'double'
        elif util.all_of(self.args, ir.IntegerValue):
            return self._get_int_type()
        else:
            raise NotImplementedError
Exemplo n.º 19
0
    def _get_type(self):
        rval = self.args[1].op()

        if util.any_of(self.args, ir.FloatingValue):
            if util.any_of(self.args, ir.DoubleValue):
                return 'double'
            else:
                return 'float'
        elif util.any_of(self.args, ir.DecimalValue):
            return _decimal_promoted_type(self.args)
        elif isinstance(rval, ir.Literal) and rval.value < 0:
            return 'double'
        elif util.all_of(self.args, ir.IntegerValue):
            return self._get_int_type()
        else:
            raise NotImplementedError
Exemplo n.º 20
0
    def _get_int_type(self):
        deps = [x.op() for x in self.args]

        if util.all_of(deps, ir.Literal):
            return _smallest_int_containing(
                [self.op(deps[0].value, deps[1].value)])
        elif util.any_of(deps, ir.Literal):
            if isinstance(deps[0], ir.Literal):
                val = deps[0].value
                atype = self.args[1].type()
            else:
                val = deps[1].value
                atype = self.args[0].type()
            return _int_one_literal_promotion(atype, val, self.op)
        else:
            return _int_bounds_promotion(self.left.type(), self.right.type(),
                                         self.op)
Exemplo n.º 21
0
 def _check_compatibility(self):
     if (util.any_of(self.args, ir.StringValue)
             and not util.all_of(self.args, ir.StringValue)):
         raise TypeError('String and non-string incompatible')
Exemplo n.º 22
0
 def output_dtype(self):
     if util.all_of(self.args, ir.IntegerValue):
         return dt.float64
     else:
         return rlz.highest_precedence_dtype(self.args)
Exemplo n.º 23
0
def numeric_like(args, op):
    if util.all_of(args, ir.IntegerValue):
        dtype = _promote_numeric_binop(args, op)
        return shape_like(args, dtype=dtype)
    else:
        return shape_like(args)
Exemplo n.º 24
0
def numeric_like(args, op):
    if util.all_of(args, ir.IntegerValue):
        dtype = _promote_numeric_binop(args, op)
        return shape_like(args, dtype=dtype)
    else:
        return shape_like(args)
Exemplo n.º 25
0
 def root_tables(self):
     if util.all_of([self.left.op(), self.right.op()], (Join, Projection)):
         # Unraveling is not possible
         return [self.left.op(), self.right.op()]
     else:
         return ir.distinct_roots(self.left, self.right)
Exemplo n.º 26
0
    def output_type(self):
        if not util.all_of(self.args, ir.NumericValue):
            raise TypeError('One argument was non-numeric')

        return rules.shape_like_args(self.args, 'double')
Exemplo n.º 27
0
 def output_type(self):
     if not util.all_of(self.args, ir.BooleanValue):
         raise TypeError('Only valid with boolean data')
     return rules.shape_like_args(self.args, 'boolean')
Exemplo n.º 28
0
 def output_type(self):
     if not util.all_of(self.args, ir.BooleanValue):
         raise TypeError('Only valid with boolean data')
     return rules.shape_like_args(self.args, 'boolean')
Exemplo n.º 29
0
    def output_type(self):
        if not util.all_of(self.args, ir.NumericValue):
            raise TypeError('One argument was non-numeric')

        return rules.shape_like_args(self.args, 'double')
Exemplo n.º 30
0
 def _check_compatibility(self):
     if (util.any_of(self.args, ir.StringValue) and
             not util.all_of(self.args, ir.StringValue)):
         raise TypeError('String and non-string incompatible')