|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +"""Python-surface tests for the MLIR backend strings typing module. |
| 4 | +
|
| 5 | +These exercise the host-side surface added by |
| 6 | +``cudf.core.udf.mlir_backend.strings_typing`` without compiling a kernel: |
| 7 | +
|
| 8 | +* ``MLIRStringType`` identity / ABI size / dtype / repr / return_as. |
| 9 | +* ``typeof()`` of the wrapper used to marshal ``mlir_string`` arrays. |
| 10 | +* ``MLIRStringArgHandler.prepare_args`` for the bare ``CPointer`` case and |
| 11 | + the ``Tuple(CPointer, mask)`` case used for nullable string columns. |
| 12 | +
|
| 13 | +Kernel-level (lowering) tests live in ``test_strings_lowering.py``. |
| 14 | +""" |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import numpy as np |
| 19 | +from numba_cuda_mlir import types |
| 20 | +from numba_cuda_mlir.numba_cuda.typing.typeof import typeof |
| 21 | + |
| 22 | +from cudf.core.udf.mlir_backend.strings_typing import ( |
| 23 | + ManagedStrArrayWrapper, |
| 24 | + MLIRStringArgHandler, |
| 25 | + MLIRStringType, |
| 26 | + mlir_string, |
| 27 | + mlir_string_arg_handler, |
| 28 | +) |
| 29 | + |
| 30 | + |
| 31 | +class _FakeBuffer: |
| 32 | + """Minimal stand-in for a Buffer-like object exposing ``ptr``.""" |
| 33 | + |
| 34 | + def __init__(self, ptr): |
| 35 | + self.ptr = ptr |
| 36 | + |
| 37 | + |
| 38 | +def test_extensionty_size_constant(): |
| 39 | + # 8 (meminfo) + 8 (data) + 8 (nbytes) |
| 40 | + assert MLIRStringType._extensionty_size == 24 |
| 41 | + |
| 42 | + |
| 43 | +def test_mlir_string_repr_and_return_as(): |
| 44 | + assert repr(mlir_string) == "mlir_string" |
| 45 | + assert mlir_string.return_as is mlir_string |
| 46 | + |
| 47 | + |
| 48 | +def test_mlir_string_np_dtype_object(): |
| 49 | + assert MLIRStringType.np_dtype == np.dtype("O") |
| 50 | + |
| 51 | + |
| 52 | +def test_mlir_string_is_not_internal(): |
| 53 | + # Passed to kernels as a CPointer, never as an Array/memref. |
| 54 | + assert mlir_string.is_internal is False |
| 55 | + |
| 56 | + |
| 57 | +def test_typeof_managed_str_array_wrapper(): |
| 58 | + """``typeof(ManagedStrArrayWrapper(...))`` -> ``CPointer(mlir_string)``.""" |
| 59 | + wrapper = ManagedStrArrayWrapper(_FakeBuffer(0xDEAD_BEEF)) |
| 60 | + ty = typeof(wrapper) |
| 61 | + assert isinstance(ty, types.CPointer) |
| 62 | + assert ty.dtype is mlir_string |
| 63 | + |
| 64 | + |
| 65 | +def test_arg_handler_cpointer_mlir_string(): |
| 66 | + """Bare ``CPointer(mlir_string)`` is rewritten to ``(uint64, ptr)``.""" |
| 67 | + handler = MLIRStringArgHandler() |
| 68 | + wrapper = ManagedStrArrayWrapper(_FakeBuffer(0xCAFE_F00D)) |
| 69 | + ty = types.CPointer(mlir_string) |
| 70 | + new_ty, new_val = handler.prepare_args(ty, wrapper) |
| 71 | + assert new_ty is types.uint64 |
| 72 | + assert new_val == 0xCAFE_F00D |
| 73 | + |
| 74 | + |
| 75 | +def test_arg_handler_tuple_with_mask(): |
| 76 | + """``Tuple(CPointer(mlir_string), mask)`` -> tuple with the ptr extracted. |
| 77 | +
|
| 78 | + This is the shape produced for nullable string columns: the string-view |
| 79 | + pointer alongside its validity mask array. |
| 80 | + """ |
| 81 | + handler = MLIRStringArgHandler() |
| 82 | + wrapper = ManagedStrArrayWrapper(_FakeBuffer(0xAA)) |
| 83 | + mask_value = "<opaque-mask-array>" |
| 84 | + tuple_ty = types.Tuple((types.CPointer(mlir_string), types.boolean[::1])) |
| 85 | + new_ty, new_val = handler.prepare_args(tuple_ty, (wrapper, mask_value)) |
| 86 | + assert new_ty is tuple_ty |
| 87 | + assert new_val == (0xAA, mask_value) |
| 88 | + |
| 89 | + |
| 90 | +def test_arg_handler_passthrough_for_non_string_types(): |
| 91 | + """Types unrelated to ``mlir_string`` pass through unchanged.""" |
| 92 | + handler = MLIRStringArgHandler() |
| 93 | + plain_ty = types.float64[::1] |
| 94 | + sentinel = object() |
| 95 | + new_ty, new_val = handler.prepare_args(plain_ty, sentinel) |
| 96 | + assert new_ty is plain_ty |
| 97 | + assert new_val is sentinel |
| 98 | + |
| 99 | + |
| 100 | +def test_module_exposes_arg_handler_singleton(): |
| 101 | + assert isinstance(mlir_string_arg_handler, MLIRStringArgHandler) |
0 commit comments