-
Notifications
You must be signed in to change notification settings - Fork 888
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
192 additions
and
12 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
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,8 @@ | ||
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
"""Utilities.""" | ||
|
||
from __future__ import annotations | ||
|
||
__all__: list[str] = [] |
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,89 @@ | ||
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
"""Datatype utilities.""" | ||
|
||
from __future__ import annotations | ||
|
||
from functools import cache | ||
|
||
from typing_extensions import assert_never | ||
|
||
import polars as pl | ||
|
||
import cudf._lib.pylibcudf as plc | ||
|
||
|
||
@cache | ||
def from_polars(dtype: pl.DataType) -> plc.DataType: | ||
""" | ||
Convert a polars datatype to a pylibcudf one. | ||
Parameters | ||
---------- | ||
dtype | ||
Polars dtype to convert | ||
Returns | ||
------- | ||
Matching pylibcudf DataType object. | ||
Raises | ||
------ | ||
NotImplementedError for unsupported conversions. | ||
""" | ||
if isinstance(dtype, pl.Int8): | ||
return plc.DataType(plc.TypeId.BOOL8) | ||
elif isinstance(dtype, pl.Int8): | ||
return plc.DataType(plc.TypeId.INT8) | ||
elif isinstance(dtype, pl.Int16): | ||
return plc.DataType(plc.TypeId.INT16) | ||
elif isinstance(dtype, pl.Int32): | ||
return plc.DataType(plc.TypeId.INT32) | ||
elif isinstance(dtype, pl.Int64): | ||
return plc.DataType(plc.TypeId.INT64) | ||
if isinstance(dtype, pl.UInt8): | ||
return plc.DataType(plc.TypeId.UINT8) | ||
elif isinstance(dtype, pl.UInt16): | ||
return plc.DataType(plc.TypeId.UINT16) | ||
elif isinstance(dtype, pl.UInt32): | ||
return plc.DataType(plc.TypeId.UINT32) | ||
elif isinstance(dtype, pl.UInt64): | ||
return plc.DataType(plc.TypeId.UINT64) | ||
elif isinstance(dtype, pl.Float32): | ||
return plc.DataType(plc.TypeId.FLOAT32) | ||
elif isinstance(dtype, pl.Float64): | ||
return plc.DataType(plc.TypeId.FLOAT64) | ||
elif isinstance(dtype, pl.Date): | ||
return plc.DataType(plc.TypeId.TIMESTAMP_DAYS) | ||
elif isinstance(dtype, pl.Time): | ||
raise NotImplementedError("Time of day dtype not implemented") | ||
elif isinstance(dtype, pl.Datetime): | ||
if dtype.time_zone is not None: | ||
raise NotImplementedError("Time zone support") | ||
if dtype.time_unit == "ms": | ||
return plc.DataType(plc.TypeId.TIMESTAMP_MILLISECONDS) | ||
elif dtype.time_unit == "us": | ||
return plc.DataType(plc.TypeId.TIMESTAMP_MICROSECONDS) | ||
elif dtype.time_unit == "ns": | ||
return plc.DataType(plc.TypeId.TIMESTAMP_NANOSECONDS) | ||
else: | ||
assert dtype.time_unit is not None | ||
assert_never(dtype.time_unit) | ||
elif isinstance(dtype, pl.Duration): | ||
if dtype.time_unit == "ms": | ||
return plc.DataType(plc.TypeId.DURATION_MILLISECONDS) | ||
elif dtype.time_unit == "us": | ||
return plc.DataType(plc.TypeId.DURATION_MICROSECONDS) | ||
elif dtype.time_unit == "ns": | ||
return plc.DataType(plc.TypeId.DURATION_NANOSECONDS) | ||
else: | ||
assert dtype.time_unit is not None | ||
assert_never(dtype.time_unit) | ||
elif isinstance(dtype, pl.String): | ||
return plc.DataType(plc.TypeId.STRING) | ||
elif isinstance(dtype, pl.Null): | ||
# TODO: Hopefully | ||
return plc.DataType(plc.TypeId.EMPTY) | ||
else: | ||
raise NotImplementedError(f"{dtype=} conversion not supported") |