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) )
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)
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)
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))
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)))
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)))
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)
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)
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
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)
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)
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))
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
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__))
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)
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
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)
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')
def output_dtype(self): if util.all_of(self.args, ir.IntegerValue): return dt.float64 else: return rlz.highest_precedence_dtype(self.args)
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)
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')
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')