Ejemplo n.º 1
0
Archivo: udf.py Proyecto: cloudera/ibis
def _ibis_signature(inputs):
    if isinstance(inputs, sig.TypeSignature):
        return inputs

    arguments = [
        ('_{}'.format(i), sig.Argument(rlz.value(dtype)))
        for i, dtype in enumerate(inputs)
    ]
    return sig.TypeSignature(arguments)
Ejemplo n.º 2
0
 def from_dtypes(cls, dtypes):
     return cls(
         ('_{}'.format(i), Argument(rlz.value(dtype)))
         for i, dtype in enumerate(dtypes)
     )
Ejemplo n.º 3
0
def test_valid_value(dtype, value, expected):
    result = rlz.value(dtype, value)
    assert result.equals(expected)
Ejemplo n.º 4
0
def test_invalid_value(dtype, value, expected):
    with pytest.raises(expected):
        rlz.value(dtype, value)
Ejemplo n.º 5
0
def test_invalid_value(dtype, value, expected):
    with pytest.raises(expected):
        rlz.value(dtype, value)
Ejemplo n.º 6
0
    class FooNode(ops.ValueOp):
        value = Arg(rlz.value(dt.Array(dt.int64)))

        def output_type(self):
            return Foo
Ejemplo n.º 7
0
def test_valid_value(dtype, value, expected):
    result = rlz.value(dtype, value)
    assert result.equals(expected)
Ejemplo n.º 8
0
 class MyOp(ops.ValueOp):
     value = Arg(rlz.value(dt.Array(dt.double)))
     output_type = rlz.typeof('value')
Ejemplo n.º 9
0
 def from_dtypes(cls, dtypes):
     return cls(('_{}'.format(i), Argument(rlz.value(dtype)))
                for i, dtype in enumerate(dtypes))
Ejemplo n.º 10
0
 class MyOp(ops.Value):
     value = rlz.value(dt.Array(dt.double))
     output_dtype = rlz.dtype_like('value')
     output_shape = rlz.shape_like('value')
Ejemplo n.º 11
0
    def wrapper(f):
        if not callable(f):
            raise TypeError('f must be callable, got {}'.format(f))

        signature = inspect.signature(f)
        parameter_names = signature.parameters.keys()

        udf_node_fields = collections.OrderedDict(
            [(name, Arg(rlz.value(type)))
             for name, type in zip(parameter_names, input_type)] + [
                 (
                     'output_type',
                     lambda self, output_type=output_type: rlz.shape_like(
                         self.args, dtype=output_type),
                 ),
                 ('__slots__', ('js', )),
             ])

        udf_node = create_udf_node(f.__name__, udf_node_fields)

        @compiles(udf_node)
        def compiles_udf_node(t, expr):
            return '{}({})'.format(udf_node.__name__,
                                   ', '.join(map(t.translate,
                                                 expr.op().args)))

        type_translation_context = UDFContext()
        return_type = ibis_type_to_bigquery_type(dt.dtype(output_type),
                                                 type_translation_context)
        bigquery_signature = ', '.join('{name} {type}'.format(
            name=name,
            type=ibis_type_to_bigquery_type(dt.dtype(type),
                                            type_translation_context),
        ) for name, type in zip(parameter_names, input_type))
        source = PythonToJavaScriptTranslator(f).compile()
        js = '''\
CREATE TEMPORARY FUNCTION {external_name}({signature})
RETURNS {return_type}
LANGUAGE js AS """
{strict}{source}
return {internal_name}({args});
"""{libraries};'''.format(
            external_name=udf_node.__name__,
            internal_name=f.__name__,
            return_type=return_type,
            source=source,
            signature=bigquery_signature,
            strict=repr('use strict') + ';\n' if strict else '',
            args=', '.join(parameter_names),
            libraries=('\nOPTIONS (\n    library={}\n)'.format(
                repr(list(libraries))) if libraries else ''),
        )

        @functools.wraps(f)
        def wrapped(*args, **kwargs):
            node = udf_node(*args, **kwargs)
            node.js = js
            return node.to_expr()

        wrapped.__signature__ = signature
        wrapped.js = js
        return wrapped
Ejemplo n.º 12
0
Archivo: api.py Proyecto: cloudera/ibis
    def wrapper(f):
        if not callable(f):
            raise TypeError('f must be callable, got {}'.format(f))

        signature = inspect.signature(f)
        parameter_names = signature.parameters.keys()

        udf_node_fields = collections.OrderedDict(
            [
                (name, Arg(rlz.value(type)))
                for name, type in zip(parameter_names, input_type)
            ]
            + [
                (
                    'output_type',
                    lambda self, output_type=output_type: rlz.shape_like(
                        self.args, dtype=output_type
                    ),
                ),
                ('__slots__', ('js',)),
            ]
        )

        udf_node = create_udf_node(f.__name__, udf_node_fields)

        @compiles(udf_node)
        def compiles_udf_node(t, expr):
            return '{}({})'.format(
                udf_node.__name__, ', '.join(map(t.translate, expr.op().args))
            )

        type_translation_context = UDFContext()
        return_type = ibis_type_to_bigquery_type(
            dt.dtype(output_type), type_translation_context
        )
        bigquery_signature = ', '.join(
            '{name} {type}'.format(
                name=name,
                type=ibis_type_to_bigquery_type(
                    dt.dtype(type), type_translation_context
                ),
            )
            for name, type in zip(parameter_names, input_type)
        )
        source = PythonToJavaScriptTranslator(f).compile()
        js = '''\
CREATE TEMPORARY FUNCTION {external_name}({signature})
RETURNS {return_type}
LANGUAGE js AS """
{strict}{source}
return {internal_name}({args});
"""{libraries};'''.format(
            external_name=udf_node.__name__,
            internal_name=f.__name__,
            return_type=return_type,
            source=source,
            signature=bigquery_signature,
            strict=repr('use strict') + ';\n' if strict else '',
            args=', '.join(parameter_names),
            libraries=(
                '\nOPTIONS (\n    library={}\n)'.format(repr(list(libraries)))
                if libraries
                else ''
            ),
        )

        @functools.wraps(f)
        def wrapped(*args, **kwargs):
            node = udf_node(*args, **kwargs)
            node.js = js
            return node.to_expr()

        wrapped.__signature__ = signature
        wrapped.js = js
        return wrapped