Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement cudf-polars chunked parquet reading #16944

Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
146ac45
access and config chunked parquet reader
brandon-b-miller Sep 10, 2024
0242495
do not early return df
brandon-b-miller Sep 16, 2024
e257242
Merge branch 'branch-24.12' into cudf-polars-chunked-parquet-reader
brandon-b-miller Oct 9, 2024
95ebf4d
fix nrows
brandon-b-miller Oct 9, 2024
7533ed3
Merge branch 'branch-24.12' into cudf-polars-chunked-parquet-reader
brandon-b-miller Oct 22, 2024
43acc47
Merge branch 'branch-24.12' into cudf-polars-chunked-parquet-reader
brandon-b-miller Oct 28, 2024
6ddf128
updates, set defaults
brandon-b-miller Oct 29, 2024
fea77d7
pass config through evaluate
brandon-b-miller Oct 30, 2024
53b0b2a
a trial commit to test a different concatenation strategy
brandon-b-miller Oct 31, 2024
ec298d3
merge/resolve
brandon-b-miller Nov 5, 2024
310f8c2
adjust for IR changes / pass tests
brandon-b-miller Nov 6, 2024
62c277b
address reviews
brandon-b-miller Nov 7, 2024
13df5aa
revert translate.py changes
brandon-b-miller Nov 7, 2024
4aee59f
Revert "a trial commit to test a different concatenation strategy"
brandon-b-miller Nov 7, 2024
50add3a
Merge branch 'branch-24.12' into cudf-polars-chunked-parquet-reader
brandon-b-miller Nov 8, 2024
a113737
add docs
brandon-b-miller Nov 8, 2024
828a3bb
merge/resolve
brandon-b-miller Nov 12, 2024
48d3edc
add test coverage
brandon-b-miller Nov 12, 2024
eacc73d
merge/resolve
brandon-b-miller Nov 13, 2024
9c4c1bf
raise on fail true for default testing engine
brandon-b-miller Nov 13, 2024
d6aa668
Apply suggestions from code review
brandon-b-miller Nov 13, 2024
a06f0ae
reword Parquet Reader Options
brandon-b-miller Nov 13, 2024
9930d2e
partially address reviews
brandon-b-miller Nov 13, 2024
b2530a4
Apply suggestions from code review
brandon-b-miller Nov 13, 2024
9958fe9
chunk on by default
brandon-b-miller Nov 13, 2024
d33ec5e
turn OFF chunking in existing parquet tests
brandon-b-miller Nov 13, 2024
b69eaa6
Merge branch 'branch-24.12' into cudf-polars-chunked-parquet-reader
galipremsagar Nov 13, 2024
2be2847
disable slice pushdown with parquet
brandon-b-miller Nov 13, 2024
e72215b
Merge branch 'branch-24.12' into HEAD
wence- Nov 14, 2024
c23afd9
Test parquet filters with chunking off and on
wence- Nov 14, 2024
df341ea
Implement workaround for #16186
wence- Nov 14, 2024
beb2462
xfail a polars test
wence- Nov 14, 2024
b398172
Apply suggestions from code review
brandon-b-miller Nov 15, 2024
2116d94
Merge branch 'branch-24.12' into cudf-polars-chunked-parquet-reader
galipremsagar Nov 15, 2024
e67614a
Remove commented code
wence- Nov 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions python/cudf_polars/cudf_polars/callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def execute_with_cudf(
device = config.device
memory_resource = config.memory_resource
raise_on_fail = config.config.get("raise_on_fail", False)
if unsupported := (config.config.keys() - {"raise_on_fail"}):
if unsupported := (config.config.keys() - {"raise_on_fail", "parquet_options"}):
raise ValueError(
f"Engine configuration contains unsupported settings {unsupported}"
)
Expand All @@ -183,7 +183,7 @@ def execute_with_cudf(
nt.set_udf(
partial(
_callback,
translate_ir(nt),
translate_ir(nt, config),
device=device,
memory_resource=memory_resource,
)
Expand Down
7 changes: 6 additions & 1 deletion python/cudf_polars/cudf_polars/dsl/expressions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
if TYPE_CHECKING:
from collections.abc import Mapping

from polars import GPUEngine

from cudf_polars.containers import Column, DataFrame

__all__ = ["Expr", "NamedExpr", "Col", "AggInfo", "ExecutionContext"]
Expand All @@ -36,13 +38,16 @@ class ExecutionContext(IntEnum):
class Expr(Node["Expr"]):
"""An abstract expression object."""

__slots__ = ("dtype",)
__slots__ = ("dtype", "_config")
dtype: plc.DataType
"""Data type of the expression."""
# This annotation is needed because of https://github.com/python/mypy/issues/17981
_non_child: ClassVar[tuple[str, ...]] = ("dtype",)
"""Names of non-child data (not Exprs) for reconstruction."""

_config: GPUEngine
"""GPU engine configuration."""

def do_evaluate(
self,
df: DataFrame,
Expand Down
65 changes: 53 additions & 12 deletions python/cudf_polars/cudf_polars/dsl/ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@
from collections.abc import Callable, Hashable, MutableMapping, Sequence
from typing import Literal

from polars import GPUEngine

from cudf_polars.typing import Schema

PARQUET_DEFAULT_CHUNK_SIZE = 0
PARQUET_DEFAULT_PASS_LIMIT = 17179869184 # 16GiB

brandon-b-miller marked this conversation as resolved.
Show resolved Hide resolved
__all__ = [
"IR",
Expand Down Expand Up @@ -125,12 +129,15 @@ def broadcast(*columns: Column, target_length: int | None = None) -> list[Column
class IR(Node["IR"]):
"""Abstract plan node, representing an unevaluated dataframe."""

__slots__ = ("schema",)
__slots__ = ("schema", "_config")
# This annotation is needed because of https://github.com/python/mypy/issues/17981
_non_child: ClassVar[tuple[str, ...]] = ("schema",)
schema: Schema
"""Mapping from column names to their data types."""

_config: GPUEngine
"""GPU engine configuration."""

def get_hashable(self) -> Hashable:
"""
Hashable representation of node, treating schema dictionary.
Expand Down Expand Up @@ -418,17 +425,51 @@ def evaluate(self, *, cache: MutableMapping[int, DataFrame]) -> DataFrame:
colnames[0],
)
elif self.typ == "parquet":
tbl_w_meta = plc.io.parquet.read_parquet(
plc.io.SourceInfo(self.paths),
columns=with_columns,
nrows=n_rows,
skip_rows=self.skip_rows,
)
df = DataFrame.from_table(
tbl_w_meta.tbl,
# TODO: consider nested column names?
tbl_w_meta.column_names(include_children=False),
)
parquet_options = self._config.config.get("parquet_options", {})
if parquet_options.get("chunked", False):
reader = plc.io.parquet.ChunkedParquetReader(
plc.io.SourceInfo(self.paths),
columns=with_columns,
nrows=n_rows,
skip_rows=self.skip_rows,
chunk_read_limit=parquet_options.get(
"chunk_read_limit", PARQUET_DEFAULT_CHUNK_SIZE
),
pass_read_limit=parquet_options.get(
"pass_read_limit", PARQUET_DEFAULT_PASS_LIMIT
),
)
chk = reader.read_chunk()
tbl = chk.tbl
names = chk.column_names()
concatenated_columns = tbl.columns()
while reader.has_next():
tbl = reader.read_chunk().tbl

for i in range(tbl.num_columns()):
concatenated_columns[i] = plc.concatenate.concatenate(
[concatenated_columns[i], tbl._columns[i]]
)
# Drop residual columns to save memory
tbl._columns[i] = None
vyasr marked this conversation as resolved.
Show resolved Hide resolved

df = DataFrame.from_table(
brandon-b-miller marked this conversation as resolved.
Show resolved Hide resolved
plc.Table(concatenated_columns),
names=names,
)
else:
tbl_w_meta = plc.io.parquet.read_parquet(
plc.io.SourceInfo(self.paths),
columns=with_columns,
nrows=n_rows,
skip_rows=self.skip_rows,
)
df = DataFrame.from_table(
tbl_w_meta.tbl,
# TODO: consider nested column names?
tbl_w_meta.column_names(include_children=False),
)

elif self.typ == "ndjson":
json_schema: list[tuple[str, str, list]] = [
(name, typ, []) for name, typ in self.schema.items()
Expand Down
16 changes: 15 additions & 1 deletion python/cudf_polars/cudf_polars/dsl/translate.py
brandon-b-miller marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,20 @@

import polars as pl
import polars.polars as plrs
from polars import GPUEngine
from polars.polars import _expr_nodes as pl_expr, _ir_nodes as pl_ir

from cudf_polars.dsl import expr, ir
from cudf_polars.dsl.expr import Expr
from cudf_polars.dsl.ir import IR
from cudf_polars.dsl.traversal import make_recursive, reuse_if_unchanged
from cudf_polars.typing import NodeTraverser
from cudf_polars.utils import dtypes, sorting

if TYPE_CHECKING:
from cudf_polars.typing import ExprTransformer


__all__ = ["translate_ir", "translate_named_expr"]


Expand Down Expand Up @@ -361,14 +365,18 @@ def _(
return ir.HConcat(schema, *(translate_ir(visitor, n=n) for n in node.inputs))


def translate_ir(visitor: NodeTraverser, *, n: int | None = None) -> ir.IR:
def translate_ir(
visitor: NodeTraverser, config: GPUEngine = None, *, n: int | None = None
) -> ir.IR:
"""
Translate a polars-internal IR node to our representation.

Parameters
----------
visitor
Polars NodeTraverser object
config
GPUEngine configuration object
n
Optional node to start traversing from, if not provided uses
current polars-internal node.
Expand All @@ -382,6 +390,9 @@ def translate_ir(visitor: NodeTraverser, *, n: int | None = None) -> ir.IR:
NotImplementedError
If we can't translate the nodes due to unsupported functionality.
"""
if not config:
config = GPUEngine()

ctx: AbstractContextManager[None] = (
set_node(visitor, n) if n is not None else noop_context
)
Expand All @@ -393,6 +404,9 @@ def translate_ir(visitor: NodeTraverser, *, n: int | None = None) -> ir.IR:
f"No support for polars IR {version=}"
) # pragma: no cover; no such version for now.

IR._config = config
Expr._config = config

brandon-b-miller marked this conversation as resolved.
Show resolved Hide resolved
with ctx:
polars_schema = visitor.get_schema()
node = visitor.view_current_node()
Expand Down
Loading