Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/py-polars/tests/unit/lazyframe/test_engine_selection.py
6939 views
1
from __future__ import annotations
2
3
from typing import TYPE_CHECKING
4
5
import pytest
6
7
import polars as pl
8
from polars.testing import assert_frame_equal
9
10
if TYPE_CHECKING:
11
from polars._typing import EngineType
12
13
14
@pytest.fixture
15
def df() -> pl.LazyFrame:
16
return pl.LazyFrame({"a": [1, 2, 3]})
17
18
19
@pytest.fixture(params=["gpu", pl.GPUEngine()])
20
def engine(request: pytest.FixtureRequest) -> EngineType:
21
value: EngineType = request.param
22
return value
23
24
25
def test_engine_selection_invalid_raises(df: pl.LazyFrame) -> None:
26
with pytest.raises(ValueError):
27
df.collect(engine="unknown") # type: ignore[call-overload]
28
29
30
def test_engine_selection_background_warns(
31
df: pl.LazyFrame, engine: EngineType
32
) -> None:
33
expect = df.collect()
34
with pytest.warns(
35
UserWarning, match="GPU engine does not support streaming or background"
36
):
37
got = df.collect(engine=engine, background=True)
38
assert_frame_equal(expect, got.fetch_blocking())
39
40
41
def test_engine_selection_eager_quiet(df: pl.LazyFrame, engine: EngineType) -> None:
42
expect = df.collect()
43
# _eager collection turns off GPU engine quietly
44
got = df.collect(engine=engine, optimizations=pl.QueryOptFlags._eager())
45
assert_frame_equal(expect, got)
46
47
48
def test_engine_import_error_raises(df: pl.LazyFrame, engine: EngineType) -> None:
49
with pytest.raises(ImportError, match="GPU engine requested"):
50
df.collect(engine=engine)
51
52