-
Notifications
You must be signed in to change notification settings - Fork 930
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a slice expression to polars IR (#18050)
Closes #18051 Authors: - Matthew Murray (https://github.com/Matt711) Approvers: - Matthew Roeschke (https://github.com/mroeschke) URL: #18050
- Loading branch information
Showing
5 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# TODO: remove need for this | ||
# ruff: noqa: D101 | ||
"""Slicing DSL nodes.""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from cudf_polars.dsl.expressions.base import ( | ||
ExecutionContext, | ||
Expr, | ||
) | ||
|
||
if TYPE_CHECKING: | ||
from collections.abc import Mapping | ||
|
||
import pylibcudf as plc | ||
|
||
from cudf_polars.containers import Column, DataFrame | ||
|
||
|
||
__all__ = ["Slice"] | ||
|
||
|
||
class Slice(Expr): | ||
__slots__ = ("length", "offset") | ||
_non_child = ("dtype", "offset", "length") | ||
|
||
def __init__( | ||
self, | ||
dtype: plc.DataType, | ||
offset: int, | ||
length: int, | ||
column: Expr, | ||
) -> None: | ||
self.dtype = dtype | ||
self.offset = offset | ||
self.length = length | ||
self.children = (column,) | ||
|
||
def do_evaluate( | ||
self, | ||
df: DataFrame, | ||
*, | ||
context: ExecutionContext = ExecutionContext.FRAME, | ||
mapping: Mapping[Expr, Column] | None = None, | ||
) -> Column: | ||
"""Evaluate this expression given a dataframe for context.""" | ||
return df.slice((self.offset, self.length)).columns[0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
from __future__ import annotations | ||
|
||
import pytest | ||
|
||
import polars as pl | ||
|
||
from cudf_polars.testing.asserts import assert_gpu_result_equal | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"zlice", | ||
[ | ||
(1,), | ||
(1, 3), | ||
(-1,), | ||
], | ||
) | ||
def test_slice(zlice): | ||
df = pl.LazyFrame({"a": [0, 1, 2, 3], "b": [1, 2, 3, 4]}) | ||
q = df.select(pl.col("a").slice(*zlice)) | ||
|
||
assert_gpu_result_equal(q) |