Beispiel #1
0
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)
Beispiel #2
0
 def from_dtypes(cls, dtypes):
     return cls(
         ('_{}'.format(i), Argument(rlz.value(dtype)))
         for i, dtype in enumerate(dtypes)
     )
Beispiel #3
0
def test_valid_value(dtype, value, expected):
    result = rlz.value(dtype, value)
    assert result.equals(expected)
Beispiel #4
0
def test_invalid_value(dtype, value, expected):
    with pytest.raises(expected):
        rlz.value(dtype, value)
Beispiel #5
0
def test_invalid_value(dtype, value, expected):
    with pytest.raises(expected):
        rlz.value(dtype, value)
Beispiel #6
0
    class FooNode(ops.ValueOp):
        value = Arg(rlz.value(dt.Array(dt.int64)))

        def output_type(self):
            return Foo
Beispiel #7
0
def test_valid_value(dtype, value, expected):
    result = rlz.value(dtype, value)
    assert result.equals(expected)
Beispiel #8
0
 class MyOp(ops.ValueOp):
     value = Arg(rlz.value(dt.Array(dt.double)))
     output_type = rlz.typeof('value')
Beispiel #9
0
 def from_dtypes(cls, dtypes):
     return cls(('_{}'.format(i), Argument(rlz.value(dtype)))
                for i, dtype in enumerate(dtypes))
Beispiel #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')
Beispiel #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
Beispiel #12
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