Ejemplo n.º 1
0
def test_string_lenght(con):
    assert con.execute(L('FOO').length()) == 3
Ejemplo n.º 2
0
def _log10(translator, expr):
    op = expr.op()
    arg = op.args
    return _log_common(translator, arg, L(10))
Ejemplo n.º 3
0
    (TIMESTAMP_CONSTANT.minute(), 48),
    (TIMESTAMP_CONSTANT.second(), 5),
    (TIMESTAMP_CONSTANT.millisecond(), 359),
])
def test_timestamp_functions(con, expr, expected):
    assert con.execute(expr) == expected


def test_now(con):
    expr = ibis.now().strftime('%Y%m%d %H')
    result = con.execute(expr)
    expected = datetime.datetime.utcnow().strftime('%Y%m%d %H')
    assert result == expected


@pytest.mark.parametrize(('expr', 'expected'), [(L(3) + L(4), 7),
                                                (L(3) - L(4), -1),
                                                (L(3) * L(4), 12),
                                                (L(12) / L(4), 3),
                                                (L(12)**L(2), 144),
                                                (L(12) % L(5), 2)])
def test_binary_arithmetic(con, expr, expected):
    assert con.execute(expr) == expected


@pytest.mark.parametrize(('expr', 'expected'), [
    (L(7) / L(2), 3.5),
    (L(7) // L(2), 3),
    (L(7).floordiv(2), 3),
    (L(2).rfloordiv(7), 3),
])
Ejemplo n.º 4
0
 def _check_literals(self, cases):
     for value, expected in cases:
         lit_expr = L(value)
         result = self._translate(lit_expr)
         assert result == expected
Ejemplo n.º 5
0
import numpy as np
import pandas as pd
import pandas.testing as tm
import pytest

import ibis
import ibis.expr.datatypes as dt
from ibis import literal as L

pytestmark = pytest.mark.clickhouse


@pytest.mark.parametrize(
    ('left', 'right', 'type'),
    [
        (L('2017-04-01'), date(2017, 4, 2), dt.date),
        (date(2017, 4, 2), L('2017-04-01'), dt.date),
        (
            L('2017-04-01 01:02:33'),
            datetime(2017, 4, 1, 1, 3, 34),
            dt.timestamp,
        ),
        (
            datetime(2017, 4, 1, 1, 3, 34),
            L('2017-04-01 01:02:33'),
            dt.timestamp,
        ),
    ],
)
@pytest.mark.parametrize(
    'op',
Ejemplo n.º 6
0
 def test_math_functions(self):
     cases = [
         (L(-5).abs(), 5),
         (L(5).abs(), 5),
         (ibis.least(L(5), L(10), L(1)), 1),
         (ibis.greatest(L(5), L(10), L(1)), 10),
         (L(5.5).round(), 6.0),
         (L(5.556).round(2), 5.56),
         (L(5.556).ceil(), 6.0),
         (L(5.556).floor(), 5.0),
         (L(5.556).exp(), math.exp(5.556)),
         (L(5.556).sign(), 1),
         (L(-5.556).sign(), -1),
         (L(0).sign(), 0),
         (L(5.556).sqrt(), math.sqrt(5.556)),
         (L(5.556).log(2), math.log(5.556, 2)),
         (L(5.556).ln(), math.log(5.556)),
         (L(5.556).log2(), math.log(5.556, 2)),
         (L(5.556).log10(), math.log10(5.556)),
     ]
     self._check_e2e_cases(cases)
Ejemplo n.º 7
0
def test_embedded_identifier_quoting(alltypes):
    t = alltypes

    expr = t[[(t.double_col * 2).name('double(fun)')]]['double(fun)'].sum()
    expr.execute()


def test_table_info(alltypes):
    buf = StringIO()
    alltypes.info(buf=buf)

    assert buf.getvalue() is not None


@pytest.mark.parametrize(('expr', 'expected'), [(L(1) + L(2), 3)])
def test_execute_exprs_no_table_ref(con, expr, expected):
    result = con.execute(expr)
    assert result == expected

    # ExprList
    exlist = ibis.api.expr_list([L(1).name('a'),
                                 ibis.now().name('b'),
                                 L(2).log().name('c')])
    con.execute(exlist)


def test_summary_execute(alltypes):
    table = alltypes

    # also test set_column while we're at it
Ejemplo n.º 8
0
 def test_nullifzero(self):
     cases = [
         (L(0).nullifzero(), None),
         (L(5.5).nullifzero(), 5.5),
     ]
     self._check_e2e_cases(cases)
Ejemplo n.º 9
0
 def test_string_length(self):
     cases = [
         (L('foo_bar').length(), 7),
         (L('').length(), 0),
     ]
     self._check_e2e_cases(cases)
Ejemplo n.º 10
0
    result = translate(expr)
    assert result == f"CAST(`{column}` AS {expected_type})"


def test_misc_conditionals(table):
    expr = table.a.nullif(0)
    expected = 'nullif(`a`, 0)'
    result = translate(expr)
    assert result == expected


@pytest.mark.parametrize(
    ("expr_fn", "expected"),
    [
        pytest.param(
            lambda _: L('9.9999999').cast('decimal(38, 5)'),
            "CAST('9.9999999' AS decimal(38, 5))",
            id="literal",
        ),
        pytest.param(
            lambda t: t.f.cast('decimal(12, 2)'),
            "CAST(`f` AS decimal(12, 2))",
            id="column",
        ),
    ],
)
def test_decimal_casts(table, expr_fn, expected):
    expr = expr_fn(table)
    result = translate(expr)
    assert result == expected
Ejemplo n.º 11
0
def test_literals(value, expected):
    expr = L(value)
    result = translate(expr)
    assert result == expected
Ejemplo n.º 12
0
 def test_literal_in_list(self):
     cases = [(L(2).isin([self.table.a, self.table.b,
                          self.table.c]), '2 IN (`a`, `b`, `c`)'),
              (L(2).notin([self.table.a, self.table.b,
                           self.table.c]), '2 NOT IN (`a`, `b`, `c`)')]
     self._check_expr_cases(cases)
Ejemplo n.º 13
0
 def test_decimal_casts(self):
     cases = [(L('9.9999999').cast('decimal(38,5)'),
               "CAST('9.9999999' AS decimal(38,5))"),
              (self.table.f.cast('decimal(12,2)'),
               "CAST(`f` AS decimal(12,2))")]
     self._check_expr_cases(cases)
Ejemplo n.º 14
0
def test_re_replace(con, translate):
    expr1 = L('Hello, World!').re_replace('.', '\\\\0\\\\0')
    expr2 = L('Hello, World!').re_replace('^', 'here: ')

    assert con.execute(expr1) == 'HHeelllloo,,  WWoorrlldd!!'
    assert con.execute(expr2) == 'here: Hello, World!'
Ejemplo n.º 15
0
 def test_find_in_set(self):
     cases = [
         (L('a').find_in_set(list('abc')), 0),
         (L('b').find_in_set(list('abc')), 1),
     ]
     self._check_e2e_cases(cases)
Ejemplo n.º 16
0
 def test_string_pad(self):
     cases = [
         (L('foo').lpad(6, ' '), '   foo'),
         (L('foo').rpad(6, ' '), 'foo   '),
     ]
     self._check_e2e_cases(cases)
Ejemplo n.º 17
0
 def test_find_in_set_null_scalar(self):
     cases = [
         (L(None).cast('string').find_in_set(['a', 'b', None]), 2),
     ]
     self._check_e2e_cases(cases)
Ejemplo n.º 18
0
 def test_string_reverse(self):
     cases = [
         (L('foo').reverse(), 'oof'),
     ]
     self._check_e2e_cases(cases)
Ejemplo n.º 19
0
        ),
    ],
)
def test_field_in_literals(table, expr_fn, expected_fn):
    values = {'foo', 'bar', 'baz'}
    expr = expr_fn(table, values)
    expected = expected_fn(values)
    result = translate(expr)
    assert result == expected


@pytest.mark.parametrize(
    ("expr_fn", "expected"),
    [
        pytest.param(
            lambda t: L(2).isin([t.a, t.b, t.c]),
            '2 IN (`a`, `b`, `c`)',
            id="in",
        ),
        pytest.param(
            lambda t: L(2).notin([t.a, t.b, t.c]),
            '2 NOT IN (`a`, `b`, `c`)',
            id="not_in",
        ),
    ],
)
def test_literal_in_fields(table, expr_fn, expected):
    expr = expr_fn(table)
    result = translate(expr)
    assert result == expected
Ejemplo n.º 20
0
 def test_string_upper_lower(self):
     cases = [
         (L('foo').upper(), 'FOO'),
         (L('FOO').lower(), 'foo'),
     ]
     self._check_e2e_cases(cases)
Ejemplo n.º 21
0
 def test_reduction_invalid_where(self):
     condbad_literal = L('T')
     c = self.table.double_col
     for reduction in [c.sum, c.count, c.mean, c.max, c.min]:
         with self.assertRaises(TypeError):
             reduction(where=condbad_literal)
Ejemplo n.º 22
0
 def test_capitalize(self):
     cases = [
         (L('foo bar foo').capitalize(), 'Foo Bar Foo'),
         (L('foobar Foo').capitalize(), 'Foobar Foo'),
     ]
     self._check_e2e_cases(cases)
Ejemplo n.º 23
0
 def test_string_join(self):
     cases = [
         (L(',').join(['a', 'b']), "concat_ws(',', 'a', 'b')")
     ]
     self._check_expr_cases(cases)
Ejemplo n.º 24
0
 def test_repeat(self):
     cases = [
         (L('bar ').repeat(3), 'bar bar bar '),
     ]
     self._check_e2e_cases(cases)
Ejemplo n.º 25
0
def test_negate_literal(con):
    expr = -L(5.245)
    assert round(con.execute(expr), 3) == -5.245
Ejemplo n.º 26
0
 def test_re_replace(self):
     cases = [(L('fudge|||chocolate||candy').re_replace('\\|{2,3}', ', '),
               'fudge, chocolate, candy')]
     self._check_e2e_cases(cases)
Ejemplo n.º 27
0
    expr = expr_fn(operand_fn(alltypes))
    expected = expected_expr_fn(expected_operand_fn(df))

    result = con.execute(expr)

    if isinstance(expected, pd.Series):
        expected = backend.default_series_rename(expected)
        backend.assert_series_equal(result, expected)
    else:
        assert result == expected


@pytest.mark.parametrize(
    ('expr', 'expected'),
    [
        param(L(-5).abs(), 5, id='abs-neg'),
        param(L(5).abs(), 5, id='abs'),
        param(ibis.least(L(10), L(1)), 1, id='least'),
        param(ibis.greatest(L(10), L(1)), 10, id='greatest'),
        param(L(5.5).round(), 6.0, id='round'),
        param(L(5.556).round(2), 5.56, id='round-digits'),
        param(L(5.556).ceil(), 6.0, id='ceil'),
        param(L(5.556).floor(), 5.0, id='floor'),
        param(L(5.556).exp(), math.exp(5.556), id='expr'),
        param(L(5.556).sign(), 1, id='sign-pos'),
        param(L(-5.556).sign(), -1, id='sign-neg'),
        param(L(0).sign(), 0, id='sign-zero'),
        param(L(5.556).sqrt(), math.sqrt(5.556), id='sqrt'),
        param(L(5.556).log(2), math.log(5.556, 2), id='log-base'),
        param(L(5.556).ln(), math.log(5.556), id='ln'),
        param(L(5.556).log2(), math.log(5.556, 2), id='log2'),
Ejemplo n.º 28
0
 def test_translate(self):
     cases = [
         (L('faab').translate('a', 'b'), 'fbbb'),
     ]
     self._check_e2e_cases(cases)
Ejemplo n.º 29
0
def test_str_replace(con):
    expr = L('foobarfoo').replace('foo', 'H')
    expected = 'HbarH'
    assert con.execute(expr) == expected
Ejemplo n.º 30
0
def test_string_lower(con):
    assert con.execute(L('FOO').lower()) == 'foo'