Path: blob/main/py-polars/tests/unit/sql/test_bitwise.py
8375 views
from __future__ import annotations12import pytest34import polars as pl567@pytest.fixture8def df() -> pl.DataFrame:9return pl.DataFrame(10{11"x": [20, 32, 50, 88, 128],12"y": [-128, 0, 10, -1, None],13}14)151617def test_bitwise_and(df: pl.DataFrame) -> None:18res = df.sql(19"""20SELECT21x & y AS x_bitand_op_y,22BITAND(y, x) AS y_bitand_x,23BIT_AND(x, y) AS x_bitand_y,24FROM self25"""26)27assert res.to_dict(as_series=False) == {28"x_bitand_op_y": [0, 0, 2, 88, None],29"y_bitand_x": [0, 0, 2, 88, None],30"x_bitand_y": [0, 0, 2, 88, None],31}323334def test_bitwise_count(df: pl.DataFrame) -> None:35res = df.sql(36"""37SELECT38BITCOUNT(x) AS x_bits_set,39BIT_COUNT(y) AS y_bits_set,40FROM self41"""42)43assert res.to_dict(as_series=False) == {44"x_bits_set": [2, 1, 3, 3, 1],45"y_bits_set": [57, 0, 2, 64, None],46}474849def test_bitwise_not(df: pl.DataFrame) -> None:50res = df.sql(51"""52SELECT53-- note: operator support pending...54-- https://github.com/apache/datafusion-sqlparser-rs/pull/208155-- ~x AS bit_not_op_x,56BIT_NOT(-x) AS bit_not_minus_x,57BITNOT(y) AS bitnot_y,58FROM self59"""60)61assert res.to_dict(as_series=False) == {62"bit_not_minus_x": [19, 31, 49, 87, 127],63"bitnot_y": [127, -1, -11, 0, None],64}656667def test_bitwise_or(df: pl.DataFrame) -> None:68res = df.sql(69"""70SELECT71x | y AS x_bitor_op_y,72BITOR(y, x) AS y_bitor_x,73BIT_OR(x, y) AS x_bitor_y,74FROM self75"""76)77assert res.to_dict(as_series=False) == {78"x_bitor_op_y": [-108, 32, 58, -1, None],79"y_bitor_x": [-108, 32, 58, -1, None],80"x_bitor_y": [-108, 32, 58, -1, None],81}828384def test_bitwise_xor(df: pl.DataFrame) -> None:85res = df.sql(86"""87SELECT88x XOR y AS x_bitxor_op_y,89BITXOR(y, x) AS y_bitxor_x,90BIT_XOR(x, y) AS x_bitxor_y,91FROM self92"""93)94assert res.to_dict(as_series=False) == {95"x_bitxor_op_y": [-108, 32, 56, -89, None],96"y_bitxor_x": [-108, 32, 56, -89, None],97"x_bitxor_y": [-108, 32, 56, -89, None],98}99100101