Path: blob/main/py-polars/tests/unit/sql/test_bitwise.py
6939 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_or(df: pl.DataFrame) -> None:50res = df.sql(51"""52SELECT53x | y AS x_bitor_op_y,54BITOR(y, x) AS y_bitor_x,55BIT_OR(x, y) AS x_bitor_y,56FROM self57"""58)59assert res.to_dict(as_series=False) == {60"x_bitor_op_y": [-108, 32, 58, -1, None],61"y_bitor_x": [-108, 32, 58, -1, None],62"x_bitor_y": [-108, 32, 58, -1, None],63}646566def test_bitwise_xor(df: pl.DataFrame) -> None:67res = df.sql(68"""69SELECT70x XOR y AS x_bitxor_op_y,71BITXOR(y, x) AS y_bitxor_x,72BIT_XOR(x, y) AS x_bitxor_y,73FROM self74"""75)76assert res.to_dict(as_series=False) == {77"x_bitxor_op_y": [-108, 32, 56, -89, None],78"y_bitxor_x": [-108, 32, 56, -89, None],79"x_bitxor_y": [-108, 32, 56, -89, None],80}818283