-
Notifications
You must be signed in to change notification settings - Fork 912
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pylibcudf.Scalar that interoperates with Arrow scalars (#14133)
This PR adds a new Scalar object to pylibcudf that will function as the pylibcudf equivalent of cudf::scalar. Unlike columns, which are typically operated on in the form of views rather than owning types by libcudf, owning scalars are accepted by (const) ref by libcudf APIs and no corresponding view type exists. Therefore, pylibcudf.Scalar differs from pylibcudf.Column by actually owning an instance of the underlying libcudf type (cudf::scalar). Construction of pylibcudf Scalars is expected to be done from an Arrow scalar. This PR relies on #14124 and should not be merged until after that one. Authors: - Vyas Ramasubramani (https://github.com/vyasr) Approvers: - Lawrence Mitchell (https://github.com/wence-) URL: #14133
- Loading branch information
Showing
18 changed files
with
378 additions
and
138 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
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 |
---|---|---|
@@ -1,16 +1,19 @@ | ||
# Copyright (c) 2023, NVIDIA CORPORATION. | ||
|
||
from . import copying | ||
from . import copying, interop | ||
from .column import Column | ||
from .gpumemoryview import gpumemoryview | ||
from .scalar import Scalar | ||
from .table import Table | ||
from .types import DataType, TypeId | ||
|
||
__all__ = [ | ||
"Column", | ||
"DataType", | ||
"Scalar", | ||
"Table", | ||
"TypeId", | ||
"copying", | ||
"gpumemoryview", | ||
"interop", | ||
] |
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,9 @@ | ||
# Copyright (c) 2023, NVIDIA CORPORATION. | ||
|
||
from cudf._lib.cpp.interop cimport column_metadata | ||
|
||
|
||
cdef class ColumnMetadata: | ||
cdef public object name | ||
cdef public object children_meta | ||
cdef column_metadata to_libcudf(self) |
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,23 @@ | ||
# Copyright (c) 2023, NVIDIA CORPORATION. | ||
|
||
from cudf._lib.cpp.interop cimport column_metadata | ||
|
||
|
||
cdef class ColumnMetadata: | ||
def __init__(self, name): | ||
self.name = name | ||
self.children_meta = [] | ||
|
||
cdef column_metadata to_libcudf(self): | ||
"""Convert to C++ column_metadata. | ||
Since this class is mutable and cheap, it is easier to create the C++ | ||
object on the fly rather than have it directly backing the storage for | ||
the Cython class. | ||
""" | ||
cdef column_metadata c_metadata | ||
cdef ColumnMetadata child_meta | ||
c_metadata.name = self.name.encode() | ||
for child_meta in self.children_meta: | ||
c_metadata.children_meta.push_back(child_meta.to_libcudf()) | ||
return c_metadata |
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,32 @@ | ||
# Copyright (c) 2023, NVIDIA CORPORATION. | ||
|
||
from libcpp cimport bool | ||
from libcpp.memory cimport unique_ptr | ||
from pyarrow cimport lib as pa | ||
|
||
from rmm._lib.memory_resource cimport DeviceMemoryResource | ||
|
||
from cudf._lib.cpp.scalar.scalar cimport scalar | ||
|
||
from .interop cimport ColumnMetadata | ||
from .types cimport DataType | ||
|
||
|
||
cdef class Scalar: | ||
cdef unique_ptr[scalar] c_obj | ||
cdef DataType _data_type | ||
|
||
# Holds a reference to the DeviceMemoryResource used for allocation. | ||
# Ensures the MR does not get destroyed before this DeviceBuffer. `mr` is | ||
# needed for deallocation | ||
cdef DeviceMemoryResource mr | ||
|
||
cdef const scalar* get(self) except * | ||
|
||
cpdef DataType type(self) | ||
cpdef bool is_valid(self) | ||
|
||
@staticmethod | ||
cdef Scalar from_libcudf(unique_ptr[scalar] libcudf_scalar, dtype=*) | ||
|
||
cpdef pa.Scalar to_arrow(self, ColumnMetadata metadata) |
Oops, something went wrong.