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
8420 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,
36
match="GPU engine does not support streaming or background",
37
):
38
got = df.collect(engine=engine, background=True)
39
assert_frame_equal(expect, got.fetch_blocking())
40
41
42
def test_engine_selection_eager_quiet(df: pl.LazyFrame, engine: EngineType) -> None:
43
expect = df.collect()
44
# _eager collection turns off GPU engine quietly
45
got = df.collect(engine=engine, optimizations=pl.QueryOptFlags._eager())
46
assert_frame_equal(expect, got)
47
48
49
def test_engine_import_error_raises(df: pl.LazyFrame, engine: EngineType) -> None:
50
with pytest.raises(
51
ImportError,
52
match="GPU engine requested",
53
):
54
df.collect(engine=engine)
55
56