|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +import functools |
| 19 | +import os |
| 20 | + |
| 21 | +from . import cdata |
| 22 | +from .tester import Tester, CDataExporter, CDataImporter |
| 23 | +from ..utils.source import ARROW_ROOT_DEFAULT |
| 24 | + |
| 25 | + |
| 26 | +_NANOARROW_PATH = os.environ.get( |
| 27 | + "ARROW_NANOARROW_PATH", |
| 28 | + os.path.join(ARROW_ROOT_DEFAULT, "nanoarrow/cdata"), |
| 29 | +) |
| 30 | + |
| 31 | +_INTEGRATION_DLL = os.path.join( |
| 32 | + _NANOARROW_PATH, "libnanoarrow_c_data_integration" + cdata.dll_suffix |
| 33 | +) |
| 34 | + |
| 35 | + |
| 36 | +class NanoarrowTester(Tester): |
| 37 | + PRODUCER = False |
| 38 | + CONSUMER = False |
| 39 | + FLIGHT_SERVER = False |
| 40 | + FLIGHT_CLIENT = False |
| 41 | + C_DATA_SCHEMA_EXPORTER = True |
| 42 | + C_DATA_ARRAY_EXPORTER = True |
| 43 | + C_DATA_SCHEMA_IMPORTER = True |
| 44 | + C_DATA_ARRAY_IMPORTER = True |
| 45 | + |
| 46 | + name = "nanoarrow" |
| 47 | + |
| 48 | + def validate(self, json_path, arrow_path, quirks=None): |
| 49 | + raise NotImplementedError() |
| 50 | + |
| 51 | + def json_to_file(self, json_path, arrow_path): |
| 52 | + raise NotImplementedError() |
| 53 | + |
| 54 | + def stream_to_file(self, stream_path, file_path): |
| 55 | + raise NotImplementedError() |
| 56 | + |
| 57 | + def file_to_stream(self, file_path, stream_path): |
| 58 | + raise NotImplementedError() |
| 59 | + |
| 60 | + def make_c_data_exporter(self): |
| 61 | + return NanoarrowCDataExporter(self.debug, self.args) |
| 62 | + |
| 63 | + def make_c_data_importer(self): |
| 64 | + return NanoarrowCDataImporter(self.debug, self.args) |
| 65 | + |
| 66 | + |
| 67 | +_nanoarrow_c_data_entrypoints = """ |
| 68 | + const char* nanoarrow_CDataIntegration_ExportSchemaFromJson( |
| 69 | + const char* json_path, struct ArrowSchema* out); |
| 70 | +
|
| 71 | + const char* nanoarrow_CDataIntegration_ImportSchemaAndCompareToJson( |
| 72 | + const char* json_path, struct ArrowSchema* schema); |
| 73 | +
|
| 74 | + const char* nanoarrow_CDataIntegration_ExportBatchFromJson( |
| 75 | + const char* json_path, int num_batch, struct ArrowArray* out); |
| 76 | +
|
| 77 | + const char* nanoarrow_CDataIntegration_ImportBatchAndCompareToJson( |
| 78 | + const char* json_path, int num_batch, struct ArrowArray* batch); |
| 79 | +
|
| 80 | + int64_t nanoarrow_BytesAllocated(void); |
| 81 | + """ |
| 82 | + |
| 83 | + |
| 84 | +@functools.lru_cache |
| 85 | +def _load_ffi(ffi, lib_path=_INTEGRATION_DLL): |
| 86 | + ffi.cdef(_nanoarrow_c_data_entrypoints) |
| 87 | + dll = ffi.dlopen(lib_path) |
| 88 | + return dll |
| 89 | + |
| 90 | + |
| 91 | +class _CDataBase: |
| 92 | + def __init__(self, debug, args): |
| 93 | + self.debug = debug |
| 94 | + self.args = args |
| 95 | + self.ffi = cdata.ffi() |
| 96 | + self.dll = _load_ffi(self.ffi) |
| 97 | + |
| 98 | + def _check_nanoarrow_error(self, na_error): |
| 99 | + """ |
| 100 | + Check a `const char*` error return from an integration entrypoint. |
| 101 | +
|
| 102 | + A null means success, a non-empty string is an error message. |
| 103 | + The string is statically allocated on the nanoarrow side and does not |
| 104 | + need to be released. |
| 105 | + """ |
| 106 | + assert self.ffi.typeof(na_error) is self.ffi.typeof("const char*") |
| 107 | + if na_error != self.ffi.NULL: |
| 108 | + error = self.ffi.string(na_error).decode("utf8", errors="replace") |
| 109 | + raise RuntimeError(f"nanoarrow C Data Integration call failed: {error}") |
| 110 | + |
| 111 | + |
| 112 | +class NanoarrowCDataExporter(CDataExporter, _CDataBase): |
| 113 | + def export_schema_from_json(self, json_path, c_schema_ptr): |
| 114 | + na_error = self.dll.nanoarrow_CDataIntegration_ExportSchemaFromJson( |
| 115 | + str(json_path).encode(), c_schema_ptr |
| 116 | + ) |
| 117 | + self._check_nanoarrow_error(na_error) |
| 118 | + |
| 119 | + def export_batch_from_json(self, json_path, num_batch, c_array_ptr): |
| 120 | + na_error = self.dll.nanoarrow_CDataIntegration_ExportBatchFromJson( |
| 121 | + str(json_path).encode(), num_batch, c_array_ptr |
| 122 | + ) |
| 123 | + self._check_nanoarrow_error(na_error) |
| 124 | + |
| 125 | + @property |
| 126 | + def supports_releasing_memory(self): |
| 127 | + return True |
| 128 | + |
| 129 | + def record_allocation_state(self): |
| 130 | + return self.dll.nanoarrow_BytesAllocated() |
| 131 | + |
| 132 | + |
| 133 | +class NanoarrowCDataImporter(CDataImporter, _CDataBase): |
| 134 | + def import_schema_and_compare_to_json(self, json_path, c_schema_ptr): |
| 135 | + na_error = self.dll.nanoarrow_CDataIntegration_ImportSchemaAndCompareToJson( |
| 136 | + str(json_path).encode(), c_schema_ptr |
| 137 | + ) |
| 138 | + self._check_nanoarrow_error(na_error) |
| 139 | + |
| 140 | + def import_batch_and_compare_to_json(self, json_path, num_batch, c_array_ptr): |
| 141 | + na_error = self.dll.nanoarrow_CDataIntegration_ImportBatchAndCompareToJson( |
| 142 | + str(json_path).encode(), num_batch, c_array_ptr |
| 143 | + ) |
| 144 | + self._check_nanoarrow_error(na_error) |
| 145 | + |
| 146 | + @property |
| 147 | + def supports_releasing_memory(self): |
| 148 | + return True |
0 commit comments