snowflake-connector-python 3.16.0__cp313-cp313-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (246) hide show
  1. snowflake/connector/__init__.py +96 -0
  2. snowflake/connector/_query_context_cache.py +276 -0
  3. snowflake/connector/_sql_util.py +48 -0
  4. snowflake/connector/_utils.py +70 -0
  5. snowflake/connector/arrow_context.py +203 -0
  6. snowflake/connector/auth/__init__.py +53 -0
  7. snowflake/connector/auth/_auth.py +586 -0
  8. snowflake/connector/auth/_http_server.py +220 -0
  9. snowflake/connector/auth/_oauth_base.py +400 -0
  10. snowflake/connector/auth/by_plugin.py +219 -0
  11. snowflake/connector/auth/default.py +36 -0
  12. snowflake/connector/auth/idtoken.py +72 -0
  13. snowflake/connector/auth/keypair.py +222 -0
  14. snowflake/connector/auth/no_auth.py +39 -0
  15. snowflake/connector/auth/oauth.py +49 -0
  16. snowflake/connector/auth/oauth_code.py +479 -0
  17. snowflake/connector/auth/oauth_credentials.py +66 -0
  18. snowflake/connector/auth/okta.py +335 -0
  19. snowflake/connector/auth/pat.py +39 -0
  20. snowflake/connector/auth/usrpwdmfa.py +69 -0
  21. snowflake/connector/auth/webbrowser.py +502 -0
  22. snowflake/connector/auth/workload_identity.py +94 -0
  23. snowflake/connector/azure_storage_client.py +276 -0
  24. snowflake/connector/backoff_policies.py +141 -0
  25. snowflake/connector/bind_upload_agent.py +89 -0
  26. snowflake/connector/cache.py +696 -0
  27. snowflake/connector/compat.py +127 -0
  28. snowflake/connector/config_manager.py +496 -0
  29. snowflake/connector/connection.py +2273 -0
  30. snowflake/connector/connection_diagnostic.py +776 -0
  31. snowflake/connector/constants.py +442 -0
  32. snowflake/connector/converter.py +785 -0
  33. snowflake/connector/converter_issue23517.py +87 -0
  34. snowflake/connector/converter_null.py +14 -0
  35. snowflake/connector/converter_snowsql.py +205 -0
  36. snowflake/connector/cursor.py +1951 -0
  37. snowflake/connector/dbapi.py +53 -0
  38. snowflake/connector/description.py +19 -0
  39. snowflake/connector/direct_file_operation_utils.py +88 -0
  40. snowflake/connector/encryption_util.py +220 -0
  41. snowflake/connector/errorcode.py +91 -0
  42. snowflake/connector/errors.py +615 -0
  43. snowflake/connector/externals_utils/__init__.py +0 -0
  44. snowflake/connector/externals_utils/externals_setup.py +27 -0
  45. snowflake/connector/feature.py +4 -0
  46. snowflake/connector/file_compression_type.py +118 -0
  47. snowflake/connector/file_lock.py +72 -0
  48. snowflake/connector/file_transfer_agent.py +1215 -0
  49. snowflake/connector/file_util.py +153 -0
  50. snowflake/connector/gcs_storage_client.py +474 -0
  51. snowflake/connector/gzip_decoder.py +85 -0
  52. snowflake/connector/local_storage_client.py +90 -0
  53. snowflake/connector/log_configuration.py +60 -0
  54. snowflake/connector/logging_utils/__init__.py +0 -0
  55. snowflake/connector/logging_utils/filters.py +72 -0
  56. snowflake/connector/nanoarrow_arrow_iterator.cp313-win_amd64.pyd +0 -0
  57. snowflake/connector/nanoarrow_cpp/ArrowIterator/ArrayConverter.cpp +60 -0
  58. snowflake/connector/nanoarrow_cpp/ArrowIterator/ArrayConverter.hpp +29 -0
  59. snowflake/connector/nanoarrow_cpp/ArrowIterator/BinaryConverter.cpp +19 -0
  60. snowflake/connector/nanoarrow_cpp/ArrowIterator/BinaryConverter.hpp +26 -0
  61. snowflake/connector/nanoarrow_cpp/ArrowIterator/BooleanConverter.cpp +21 -0
  62. snowflake/connector/nanoarrow_cpp/ArrowIterator/BooleanConverter.hpp +23 -0
  63. snowflake/connector/nanoarrow_cpp/ArrowIterator/CArrowChunkIterator.cpp +557 -0
  64. snowflake/connector/nanoarrow_cpp/ArrowIterator/CArrowChunkIterator.hpp +98 -0
  65. snowflake/connector/nanoarrow_cpp/ArrowIterator/CArrowIterator.cpp +125 -0
  66. snowflake/connector/nanoarrow_cpp/ArrowIterator/CArrowIterator.hpp +115 -0
  67. snowflake/connector/nanoarrow_cpp/ArrowIterator/CArrowTableIterator.cpp +1005 -0
  68. snowflake/connector/nanoarrow_cpp/ArrowIterator/CArrowTableIterator.hpp +127 -0
  69. snowflake/connector/nanoarrow_cpp/ArrowIterator/DateConverter.cpp +47 -0
  70. snowflake/connector/nanoarrow_cpp/ArrowIterator/DateConverter.hpp +46 -0
  71. snowflake/connector/nanoarrow_cpp/ArrowIterator/DecFloatConverter.cpp +83 -0
  72. snowflake/connector/nanoarrow_cpp/ArrowIterator/DecFloatConverter.hpp +35 -0
  73. snowflake/connector/nanoarrow_cpp/ArrowIterator/DecimalConverter.cpp +97 -0
  74. snowflake/connector/nanoarrow_cpp/ArrowIterator/DecimalConverter.hpp +72 -0
  75. snowflake/connector/nanoarrow_cpp/ArrowIterator/FixedSizeListConverter.cpp +73 -0
  76. snowflake/connector/nanoarrow_cpp/ArrowIterator/FixedSizeListConverter.hpp +28 -0
  77. snowflake/connector/nanoarrow_cpp/ArrowIterator/FloatConverter.cpp +30 -0
  78. snowflake/connector/nanoarrow_cpp/ArrowIterator/FloatConverter.hpp +35 -0
  79. snowflake/connector/nanoarrow_cpp/ArrowIterator/IColumnConverter.hpp +17 -0
  80. snowflake/connector/nanoarrow_cpp/ArrowIterator/IntConverter.cpp +23 -0
  81. snowflake/connector/nanoarrow_cpp/ArrowIterator/IntConverter.hpp +45 -0
  82. snowflake/connector/nanoarrow_cpp/ArrowIterator/IntervalConverter.cpp +71 -0
  83. snowflake/connector/nanoarrow_cpp/ArrowIterator/IntervalConverter.hpp +56 -0
  84. snowflake/connector/nanoarrow_cpp/ArrowIterator/LICENSE.txt +209 -0
  85. snowflake/connector/nanoarrow_cpp/ArrowIterator/MapConverter.cpp +75 -0
  86. snowflake/connector/nanoarrow_cpp/ArrowIterator/MapConverter.hpp +30 -0
  87. snowflake/connector/nanoarrow_cpp/ArrowIterator/ObjectConverter.cpp +46 -0
  88. snowflake/connector/nanoarrow_cpp/ArrowIterator/ObjectConverter.hpp +29 -0
  89. snowflake/connector/nanoarrow_cpp/ArrowIterator/Python/Common.cpp +8 -0
  90. snowflake/connector/nanoarrow_cpp/ArrowIterator/Python/Common.hpp +95 -0
  91. snowflake/connector/nanoarrow_cpp/ArrowIterator/Python/Helpers.cpp +57 -0
  92. snowflake/connector/nanoarrow_cpp/ArrowIterator/Python/Helpers.hpp +36 -0
  93. snowflake/connector/nanoarrow_cpp/ArrowIterator/SnowflakeType.cpp +34 -0
  94. snowflake/connector/nanoarrow_cpp/ArrowIterator/SnowflakeType.hpp +48 -0
  95. snowflake/connector/nanoarrow_cpp/ArrowIterator/StringConverter.cpp +19 -0
  96. snowflake/connector/nanoarrow_cpp/ArrowIterator/StringConverter.hpp +26 -0
  97. snowflake/connector/nanoarrow_cpp/ArrowIterator/TimeConverter.cpp +36 -0
  98. snowflake/connector/nanoarrow_cpp/ArrowIterator/TimeConverter.hpp +31 -0
  99. snowflake/connector/nanoarrow_cpp/ArrowIterator/TimeStampConverter.cpp +346 -0
  100. snowflake/connector/nanoarrow_cpp/ArrowIterator/TimeStampConverter.hpp +145 -0
  101. snowflake/connector/nanoarrow_cpp/ArrowIterator/Util/macros.hpp +14 -0
  102. snowflake/connector/nanoarrow_cpp/ArrowIterator/Util/time.cpp +65 -0
  103. snowflake/connector/nanoarrow_cpp/ArrowIterator/Util/time.hpp +68 -0
  104. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_accessors.h +101 -0
  105. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_alloc.h +127 -0
  106. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_assert.h +45 -0
  107. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_builder.h +1908 -0
  108. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_emitter.h +215 -0
  109. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_endian.h +125 -0
  110. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_epilogue.h +7 -0
  111. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_flatbuffers.h +55 -0
  112. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_identifier.h +148 -0
  113. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_iov.h +31 -0
  114. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_prologue.h +8 -0
  115. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_refmap.h +144 -0
  116. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_rtconfig.h +162 -0
  117. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_types.h +97 -0
  118. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_verifier.h +239 -0
  119. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/flatcc_portable.h +14 -0
  120. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/paligned_alloc.h +210 -0
  121. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pattributes.h +84 -0
  122. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pdiagnostic.h +84 -0
  123. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pdiagnostic_pop.h +20 -0
  124. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pdiagnostic_push.h +51 -0
  125. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pendian.h +206 -0
  126. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pendian_detect.h +118 -0
  127. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pinline.h +19 -0
  128. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pinttypes.h +52 -0
  129. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/portable.h +2 -0
  130. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/portable_basic.h +25 -0
  131. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pstatic_assert.h +67 -0
  132. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pstdalign.h +162 -0
  133. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pstdint.h +898 -0
  134. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/punaligned.h +190 -0
  135. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pversion.h +6 -0
  136. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pwarnings.h +52 -0
  137. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc.c +3204 -0
  138. snowflake/connector/nanoarrow_cpp/ArrowIterator/nanoarrow.c +3217 -0
  139. snowflake/connector/nanoarrow_cpp/ArrowIterator/nanoarrow.h +3618 -0
  140. snowflake/connector/nanoarrow_cpp/ArrowIterator/nanoarrow.hpp +379 -0
  141. snowflake/connector/nanoarrow_cpp/ArrowIterator/nanoarrow_arrow_iterator.pyx +256 -0
  142. snowflake/connector/nanoarrow_cpp/ArrowIterator/nanoarrow_device.c +512 -0
  143. snowflake/connector/nanoarrow_cpp/ArrowIterator/nanoarrow_device.h +350 -0
  144. snowflake/connector/nanoarrow_cpp/ArrowIterator/nanoarrow_ipc.c +33273 -0
  145. snowflake/connector/nanoarrow_cpp/ArrowIterator/nanoarrow_ipc.h +397 -0
  146. snowflake/connector/nanoarrow_cpp/Logging/logging.cpp +100 -0
  147. snowflake/connector/nanoarrow_cpp/Logging/logging.hpp +49 -0
  148. snowflake/connector/network.py +1297 -0
  149. snowflake/connector/ocsp_asn1crypto.py +447 -0
  150. snowflake/connector/ocsp_snowflake.py +1883 -0
  151. snowflake/connector/options.py +131 -0
  152. snowflake/connector/pandas_tools.py +732 -0
  153. snowflake/connector/proxy.py +43 -0
  154. snowflake/connector/py.typed +0 -0
  155. snowflake/connector/result_batch.py +786 -0
  156. snowflake/connector/result_set.py +319 -0
  157. snowflake/connector/s3_storage_client.py +605 -0
  158. snowflake/connector/secret_detector.py +181 -0
  159. snowflake/connector/sf_dirs.py +64 -0
  160. snowflake/connector/sfbinaryformat.py +35 -0
  161. snowflake/connector/sfdatetime.py +345 -0
  162. snowflake/connector/snow_logging.py +144 -0
  163. snowflake/connector/sqlstate.py +9 -0
  164. snowflake/connector/ssd_internal_keys.py +30 -0
  165. snowflake/connector/ssl_wrap_socket.py +134 -0
  166. snowflake/connector/storage_client.py +478 -0
  167. snowflake/connector/telemetry.py +249 -0
  168. snowflake/connector/telemetry_oob.py +543 -0
  169. snowflake/connector/test_util.py +30 -0
  170. snowflake/connector/time_util.py +159 -0
  171. snowflake/connector/token_cache.py +402 -0
  172. snowflake/connector/tool/__init__.py +0 -0
  173. snowflake/connector/tool/dump_certs.py +57 -0
  174. snowflake/connector/tool/dump_ocsp_response.py +139 -0
  175. snowflake/connector/tool/dump_ocsp_response_cache.py +194 -0
  176. snowflake/connector/tool/probe_connection.py +69 -0
  177. snowflake/connector/url_util.py +49 -0
  178. snowflake/connector/util_text.py +301 -0
  179. snowflake/connector/vendored/__init__.py +3 -0
  180. snowflake/connector/vendored/requests/LICENSE +175 -0
  181. snowflake/connector/vendored/requests/__init__.py +169 -0
  182. snowflake/connector/vendored/requests/__version__.py +14 -0
  183. snowflake/connector/vendored/requests/_internal_utils.py +50 -0
  184. snowflake/connector/vendored/requests/adapters.py +537 -0
  185. snowflake/connector/vendored/requests/api.py +157 -0
  186. snowflake/connector/vendored/requests/auth.py +315 -0
  187. snowflake/connector/vendored/requests/certs.py +17 -0
  188. snowflake/connector/vendored/requests/compat.py +79 -0
  189. snowflake/connector/vendored/requests/cookies.py +561 -0
  190. snowflake/connector/vendored/requests/exceptions.py +140 -0
  191. snowflake/connector/vendored/requests/help.py +134 -0
  192. snowflake/connector/vendored/requests/hooks.py +33 -0
  193. snowflake/connector/vendored/requests/models.py +1033 -0
  194. snowflake/connector/vendored/requests/sessions.py +833 -0
  195. snowflake/connector/vendored/requests/status_codes.py +128 -0
  196. snowflake/connector/vendored/requests/structures.py +99 -0
  197. snowflake/connector/vendored/requests/utils.py +1093 -0
  198. snowflake/connector/vendored/urllib3/LICENSE.txt +21 -0
  199. snowflake/connector/vendored/urllib3/__init__.py +85 -0
  200. snowflake/connector/vendored/urllib3/_collections.py +355 -0
  201. snowflake/connector/vendored/urllib3/_version.py +2 -0
  202. snowflake/connector/vendored/urllib3/connection.py +572 -0
  203. snowflake/connector/vendored/urllib3/connectionpool.py +1137 -0
  204. snowflake/connector/vendored/urllib3/contrib/__init__.py +0 -0
  205. snowflake/connector/vendored/urllib3/contrib/_appengine_environ.py +36 -0
  206. snowflake/connector/vendored/urllib3/contrib/_securetransport/__init__.py +0 -0
  207. snowflake/connector/vendored/urllib3/contrib/_securetransport/bindings.py +519 -0
  208. snowflake/connector/vendored/urllib3/contrib/_securetransport/low_level.py +397 -0
  209. snowflake/connector/vendored/urllib3/contrib/appengine.py +314 -0
  210. snowflake/connector/vendored/urllib3/contrib/ntlmpool.py +130 -0
  211. snowflake/connector/vendored/urllib3/contrib/pyopenssl.py +509 -0
  212. snowflake/connector/vendored/urllib3/contrib/securetransport.py +920 -0
  213. snowflake/connector/vendored/urllib3/contrib/socks.py +216 -0
  214. snowflake/connector/vendored/urllib3/exceptions.py +323 -0
  215. snowflake/connector/vendored/urllib3/fields.py +274 -0
  216. snowflake/connector/vendored/urllib3/filepost.py +98 -0
  217. snowflake/connector/vendored/urllib3/packages/__init__.py +0 -0
  218. snowflake/connector/vendored/urllib3/packages/backports/__init__.py +0 -0
  219. snowflake/connector/vendored/urllib3/packages/backports/makefile.py +51 -0
  220. snowflake/connector/vendored/urllib3/packages/backports/weakref_finalize.py +155 -0
  221. snowflake/connector/vendored/urllib3/packages/six.py +1076 -0
  222. snowflake/connector/vendored/urllib3/poolmanager.py +540 -0
  223. snowflake/connector/vendored/urllib3/request.py +191 -0
  224. snowflake/connector/vendored/urllib3/response.py +885 -0
  225. snowflake/connector/vendored/urllib3/util/__init__.py +49 -0
  226. snowflake/connector/vendored/urllib3/util/connection.py +156 -0
  227. snowflake/connector/vendored/urllib3/util/proxy.py +57 -0
  228. snowflake/connector/vendored/urllib3/util/queue.py +22 -0
  229. snowflake/connector/vendored/urllib3/util/request.py +146 -0
  230. snowflake/connector/vendored/urllib3/util/response.py +107 -0
  231. snowflake/connector/vendored/urllib3/util/retry.py +620 -0
  232. snowflake/connector/vendored/urllib3/util/ssl_.py +495 -0
  233. snowflake/connector/vendored/urllib3/util/ssl_match_hostname.py +159 -0
  234. snowflake/connector/vendored/urllib3/util/ssltransport.py +221 -0
  235. snowflake/connector/vendored/urllib3/util/timeout.py +271 -0
  236. snowflake/connector/vendored/urllib3/util/url.py +435 -0
  237. snowflake/connector/vendored/urllib3/util/wait.py +152 -0
  238. snowflake/connector/version.py +3 -0
  239. snowflake/connector/wif_util.py +407 -0
  240. snowflake_connector_python-3.16.0.dist-info/METADATA +1475 -0
  241. snowflake_connector_python-3.16.0.dist-info/RECORD +246 -0
  242. snowflake_connector_python-3.16.0.dist-info/WHEEL +5 -0
  243. snowflake_connector_python-3.16.0.dist-info/entry_points.txt +4 -0
  244. snowflake_connector_python-3.16.0.dist-info/licenses/LICENSE.txt +202 -0
  245. snowflake_connector_python-3.16.0.dist-info/licenses/NOTICE +8 -0
  246. snowflake_connector_python-3.16.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,3618 @@
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
+ #ifndef NANOARROW_BUILD_ID_H_INCLUDED
19
+ #define NANOARROW_BUILD_ID_H_INCLUDED
20
+
21
+ #define NANOARROW_VERSION_MAJOR 0
22
+ #define NANOARROW_VERSION_MINOR 3
23
+ #define NANOARROW_VERSION_PATCH 0
24
+ #define NANOARROW_VERSION "0.3.0"
25
+
26
+ #define NANOARROW_VERSION_INT \
27
+ (NANOARROW_VERSION_MAJOR * 10000 + NANOARROW_VERSION_MINOR * 100 + \
28
+ NANOARROW_VERSION_PATCH)
29
+
30
+ // #define NANOARROW_NAMESPACE YourNamespaceHere
31
+
32
+ #endif
33
+ // Licensed to the Apache Software Foundation (ASF) under one
34
+ // or more contributor license agreements. See the NOTICE file
35
+ // distributed with this work for additional information
36
+ // regarding copyright ownership. The ASF licenses this file
37
+ // to you under the Apache License, Version 2.0 (the
38
+ // "License"); you may not use this file except in compliance
39
+ // with the License. You may obtain a copy of the License at
40
+ //
41
+ // http://www.apache.org/licenses/LICENSE-2.0
42
+ //
43
+ // Unless required by applicable law or agreed to in writing,
44
+ // software distributed under the License is distributed on an
45
+ // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
46
+ // KIND, either express or implied. See the License for the
47
+ // specific language governing permissions and limitations
48
+ // under the License.
49
+
50
+ #ifndef NANOARROW_NANOARROW_TYPES_H_INCLUDED
51
+ #define NANOARROW_NANOARROW_TYPES_H_INCLUDED
52
+
53
+ #include <stdint.h>
54
+ #include <string.h>
55
+
56
+ #if defined(NANOARROW_DEBUG) && !defined(NANOARROW_PRINT_AND_DIE)
57
+ #include <stdio.h>
58
+ #include <stdlib.h>
59
+ #endif
60
+
61
+ #ifdef __cplusplus
62
+ extern "C" {
63
+ #endif
64
+
65
+ // Extra guard for versions of Arrow without the canonical guard
66
+ #ifndef ARROW_FLAG_DICTIONARY_ORDERED
67
+
68
+ /// \defgroup nanoarrow-arrow-cdata Arrow C Data interface
69
+ ///
70
+ /// The Arrow C Data (https://arrow.apache.org/docs/format/CDataInterface.html)
71
+ /// and Arrow C Stream
72
+ /// (https://arrow.apache.org/docs/format/CStreamInterface.html) interfaces are
73
+ /// part of the Arrow Columnar Format specification
74
+ /// (https://arrow.apache.org/docs/format/Columnar.html). See the Arrow
75
+ /// documentation for documentation of these structures.
76
+ ///
77
+ /// @{
78
+
79
+ #ifndef ARROW_C_DATA_INTERFACE
80
+ #define ARROW_C_DATA_INTERFACE
81
+
82
+ #define ARROW_FLAG_DICTIONARY_ORDERED 1
83
+ #define ARROW_FLAG_NULLABLE 2
84
+ #define ARROW_FLAG_MAP_KEYS_SORTED 4
85
+
86
+ struct ArrowSchema {
87
+ // Array type description
88
+ const char* format;
89
+ const char* name;
90
+ const char* metadata;
91
+ int64_t flags;
92
+ int64_t n_children;
93
+ struct ArrowSchema** children;
94
+ struct ArrowSchema* dictionary;
95
+
96
+ // Release callback
97
+ void (*release)(struct ArrowSchema*);
98
+ // Opaque producer-specific data
99
+ void* private_data;
100
+ };
101
+
102
+ struct ArrowArray {
103
+ // Array data description
104
+ int64_t length;
105
+ int64_t null_count;
106
+ int64_t offset;
107
+ int64_t n_buffers;
108
+ int64_t n_children;
109
+ const void** buffers;
110
+ struct ArrowArray** children;
111
+ struct ArrowArray* dictionary;
112
+
113
+ // Release callback
114
+ void (*release)(struct ArrowArray*);
115
+ // Opaque producer-specific data
116
+ void* private_data;
117
+ };
118
+
119
+ #endif // ARROW_C_DATA_INTERFACE
120
+
121
+ #ifndef ARROW_C_STREAM_INTERFACE
122
+ #define ARROW_C_STREAM_INTERFACE
123
+
124
+ struct ArrowArrayStream {
125
+ // Callback to get the stream type
126
+ // (will be the same for all arrays in the stream).
127
+ //
128
+ // Return value: 0 if successful, an `errno`-compatible error code otherwise.
129
+ //
130
+ // If successful, the ArrowSchema must be released independently from the
131
+ // stream.
132
+ int (*get_schema)(struct ArrowArrayStream*, struct ArrowSchema* out);
133
+
134
+ // Callback to get the next array
135
+ // (if no error and the array is released, the stream has ended)
136
+ //
137
+ // Return value: 0 if successful, an `errno`-compatible error code otherwise.
138
+ //
139
+ // If successful, the ArrowArray must be released independently from the
140
+ // stream.
141
+ int (*get_next)(struct ArrowArrayStream*, struct ArrowArray* out);
142
+
143
+ // Callback to get optional detailed error information.
144
+ // This must only be called if the last stream operation failed
145
+ // with a non-0 return code.
146
+ //
147
+ // Return value: pointer to a null-terminated character array describing
148
+ // the last error, or NULL if no description is available.
149
+ //
150
+ // The returned pointer is only valid until the next operation on this stream
151
+ // (including release).
152
+ const char* (*get_last_error)(struct ArrowArrayStream*);
153
+
154
+ // Release callback: release the stream's own resources.
155
+ // Note that arrays returned by `get_next` must be individually released.
156
+ void (*release)(struct ArrowArrayStream*);
157
+
158
+ // Opaque producer-specific data
159
+ void* private_data;
160
+ };
161
+
162
+ #endif // ARROW_C_STREAM_INTERFACE
163
+ #endif // ARROW_FLAG_DICTIONARY_ORDERED
164
+
165
+ /// \brief Move the contents of src into dst and set src->release to NULL
166
+ static inline void ArrowSchemaMove(struct ArrowSchema* src,
167
+ struct ArrowSchema* dst) {
168
+ memcpy(dst, src, sizeof(struct ArrowSchema));
169
+ src->release = NULL;
170
+ }
171
+
172
+ /// \brief Move the contents of src into dst and set src->release to NULL
173
+ static inline void ArrowArrayMove(struct ArrowArray* src,
174
+ struct ArrowArray* dst) {
175
+ memcpy(dst, src, sizeof(struct ArrowArray));
176
+ src->release = NULL;
177
+ }
178
+
179
+ /// \brief Move the contents of src into dst and set src->release to NULL
180
+ static inline void ArrowArrayStreamMove(struct ArrowArrayStream* src,
181
+ struct ArrowArrayStream* dst) {
182
+ memcpy(dst, src, sizeof(struct ArrowArrayStream));
183
+ src->release = NULL;
184
+ }
185
+
186
+ /// @}
187
+
188
+ // Utility macros
189
+ #define _NANOARROW_CONCAT(x, y) x##y
190
+ #define _NANOARROW_MAKE_NAME(x, y) _NANOARROW_CONCAT(x, y)
191
+
192
+ #define _NANOARROW_RETURN_NOT_OK_IMPL(NAME, EXPR) \
193
+ do { \
194
+ const int NAME = (EXPR); \
195
+ if (NAME) return NAME; \
196
+ } while (0)
197
+
198
+ #define _NANOARROW_CHECK_RANGE(x_, min_, max_) \
199
+ NANOARROW_RETURN_NOT_OK((x_ >= min_ && x_ <= max_) ? NANOARROW_OK : EINVAL)
200
+
201
+ #define _NANOARROW_CHECK_UPPER_LIMIT(x_, max_) \
202
+ NANOARROW_RETURN_NOT_OK((x_ <= max_) ? NANOARROW_OK : EINVAL)
203
+
204
+ #if defined(NANOARROW_DEBUG)
205
+ #define _NANOARROW_RETURN_NOT_OK_WITH_ERROR_IMPL(NAME, EXPR, ERROR_PTR_EXPR, \
206
+ EXPR_STR) \
207
+ do { \
208
+ const int NAME = (EXPR); \
209
+ if (NAME) { \
210
+ ArrowErrorSet((ERROR_PTR_EXPR), "%s failed with errno %d\n* %s:%d", \
211
+ EXPR_STR, NAME, __FILE__, __LINE__); \
212
+ return NAME; \
213
+ } \
214
+ } while (0)
215
+ #else
216
+ #define _NANOARROW_RETURN_NOT_OK_WITH_ERROR_IMPL(NAME, EXPR, ERROR_PTR_EXPR, \
217
+ EXPR_STR) \
218
+ do { \
219
+ const int NAME = (EXPR); \
220
+ if (NAME) { \
221
+ ArrowErrorSet((ERROR_PTR_EXPR), "%s failed with errno %d", EXPR_STR, \
222
+ NAME); \
223
+ return NAME; \
224
+ } \
225
+ } while (0)
226
+ #endif
227
+
228
+ /// \brief Return code for success.
229
+ /// \ingroup nanoarrow-errors
230
+ #define NANOARROW_OK 0
231
+
232
+ /// \brief Represents an errno-compatible error code
233
+ /// \ingroup nanoarrow-errors
234
+ typedef int ArrowErrorCode;
235
+
236
+ /// \brief Check the result of an expression and return it if not NANOARROW_OK
237
+ /// \ingroup nanoarrow-errors
238
+ #define NANOARROW_RETURN_NOT_OK(EXPR) \
239
+ _NANOARROW_RETURN_NOT_OK_IMPL( \
240
+ _NANOARROW_MAKE_NAME(errno_status_, __COUNTER__), EXPR)
241
+
242
+ /// \brief Check the result of an expression and return it if not NANOARROW_OK,
243
+ /// adding an auto-generated message to an ArrowError.
244
+ /// \ingroup nanoarrow-errors
245
+ ///
246
+ /// This macro is used to ensure that functions that accept an ArrowError
247
+ /// as input always set its message when returning an error code (e.g., when
248
+ /// calling a nanoarrow function that does *not* accept ArrowError).
249
+ #define NANOARROW_RETURN_NOT_OK_WITH_ERROR(EXPR, ERROR_EXPR) \
250
+ _NANOARROW_RETURN_NOT_OK_WITH_ERROR_IMPL( \
251
+ _NANOARROW_MAKE_NAME(errno_status_, __COUNTER__), EXPR, ERROR_EXPR, \
252
+ #EXPR)
253
+
254
+ #if defined(NANOARROW_DEBUG) && !defined(NANOARROW_PRINT_AND_DIE)
255
+ #define NANOARROW_PRINT_AND_DIE(VALUE, EXPR_STR) \
256
+ do { \
257
+ fprintf(stderr, "%s failed with errno %d\n* %s:%d\n", EXPR_STR, \
258
+ (int)(VALUE), __FILE__, (int)__LINE__); \
259
+ abort(); \
260
+ } while (0)
261
+ #endif
262
+
263
+ #if defined(NANOARROW_DEBUG)
264
+ #define _NANOARROW_ASSERT_OK_IMPL(NAME, EXPR, EXPR_STR) \
265
+ do { \
266
+ const int NAME = (EXPR); \
267
+ if (NAME) NANOARROW_PRINT_AND_DIE(NAME, EXPR_STR); \
268
+ } while (0)
269
+
270
+ /// \brief Assert that an expression's value is NANOARROW_OK
271
+ /// \ingroup nanoarrow-errors
272
+ ///
273
+ /// If nanoarrow was built in debug mode (i.e., defined(NANOARROW_DEBUG) is
274
+ /// true), print a message to stderr and abort. If nanoarrow was built in
275
+ /// release mode, this statement has no effect. You can customize fatal error
276
+ /// behaviour be defining the NANOARROW_PRINT_AND_DIE macro before including
277
+ /// nanoarrow.h This macro is provided as a convenience for users and is not
278
+ /// used internally.
279
+ #define NANOARROW_ASSERT_OK(EXPR) \
280
+ _NANOARROW_ASSERT_OK_IMPL(_NANOARROW_MAKE_NAME(errno_status_, __COUNTER__), \
281
+ EXPR, #EXPR)
282
+ #else
283
+ #define NANOARROW_ASSERT_OK(EXPR) EXPR
284
+ #endif
285
+
286
+ static char _ArrowIsLittleEndian(void) {
287
+ uint32_t check = 1;
288
+ char first_byte;
289
+ memcpy(&first_byte, &check, sizeof(char));
290
+ return first_byte;
291
+ }
292
+
293
+ /// \brief Arrow type enumerator
294
+ /// \ingroup nanoarrow-utils
295
+ ///
296
+ /// These names are intended to map to the corresponding arrow::Type::type
297
+ /// enumerator; however, the numeric values are specifically not equal
298
+ /// (i.e., do not rely on numeric comparison).
299
+ enum ArrowType {
300
+ NANOARROW_TYPE_UNINITIALIZED = 0,
301
+ NANOARROW_TYPE_NA = 1,
302
+ NANOARROW_TYPE_BOOL,
303
+ NANOARROW_TYPE_UINT8,
304
+ NANOARROW_TYPE_INT8,
305
+ NANOARROW_TYPE_UINT16,
306
+ NANOARROW_TYPE_INT16,
307
+ NANOARROW_TYPE_UINT32,
308
+ NANOARROW_TYPE_INT32,
309
+ NANOARROW_TYPE_UINT64,
310
+ NANOARROW_TYPE_INT64,
311
+ NANOARROW_TYPE_HALF_FLOAT,
312
+ NANOARROW_TYPE_FLOAT,
313
+ NANOARROW_TYPE_DOUBLE,
314
+ NANOARROW_TYPE_STRING,
315
+ NANOARROW_TYPE_BINARY,
316
+ NANOARROW_TYPE_FIXED_SIZE_BINARY,
317
+ NANOARROW_TYPE_DATE32,
318
+ NANOARROW_TYPE_DATE64,
319
+ NANOARROW_TYPE_TIMESTAMP,
320
+ NANOARROW_TYPE_TIME32,
321
+ NANOARROW_TYPE_TIME64,
322
+ NANOARROW_TYPE_INTERVAL_MONTHS,
323
+ NANOARROW_TYPE_INTERVAL_DAY_TIME,
324
+ NANOARROW_TYPE_DECIMAL128,
325
+ NANOARROW_TYPE_DECIMAL256,
326
+ NANOARROW_TYPE_LIST,
327
+ NANOARROW_TYPE_STRUCT,
328
+ NANOARROW_TYPE_SPARSE_UNION,
329
+ NANOARROW_TYPE_DENSE_UNION,
330
+ NANOARROW_TYPE_DICTIONARY,
331
+ NANOARROW_TYPE_MAP,
332
+ NANOARROW_TYPE_EXTENSION,
333
+ NANOARROW_TYPE_FIXED_SIZE_LIST,
334
+ NANOARROW_TYPE_DURATION,
335
+ NANOARROW_TYPE_LARGE_STRING,
336
+ NANOARROW_TYPE_LARGE_BINARY,
337
+ NANOARROW_TYPE_LARGE_LIST,
338
+ NANOARROW_TYPE_INTERVAL_MONTH_DAY_NANO
339
+ };
340
+
341
+ /// \brief Get a string value of an enum ArrowType value
342
+ /// \ingroup nanoarrow-utils
343
+ ///
344
+ /// Returns NULL for invalid values for type
345
+ static inline const char* ArrowTypeString(enum ArrowType type);
346
+
347
+ static inline const char* ArrowTypeString(enum ArrowType type) {
348
+ switch (type) {
349
+ case NANOARROW_TYPE_NA:
350
+ return "na";
351
+ case NANOARROW_TYPE_BOOL:
352
+ return "bool";
353
+ case NANOARROW_TYPE_UINT8:
354
+ return "uint8";
355
+ case NANOARROW_TYPE_INT8:
356
+ return "int8";
357
+ case NANOARROW_TYPE_UINT16:
358
+ return "uint16";
359
+ case NANOARROW_TYPE_INT16:
360
+ return "int16";
361
+ case NANOARROW_TYPE_UINT32:
362
+ return "uint32";
363
+ case NANOARROW_TYPE_INT32:
364
+ return "int32";
365
+ case NANOARROW_TYPE_UINT64:
366
+ return "uint64";
367
+ case NANOARROW_TYPE_INT64:
368
+ return "int64";
369
+ case NANOARROW_TYPE_HALF_FLOAT:
370
+ return "half_float";
371
+ case NANOARROW_TYPE_FLOAT:
372
+ return "float";
373
+ case NANOARROW_TYPE_DOUBLE:
374
+ return "double";
375
+ case NANOARROW_TYPE_STRING:
376
+ return "string";
377
+ case NANOARROW_TYPE_BINARY:
378
+ return "binary";
379
+ case NANOARROW_TYPE_FIXED_SIZE_BINARY:
380
+ return "fixed_size_binary";
381
+ case NANOARROW_TYPE_DATE32:
382
+ return "date32";
383
+ case NANOARROW_TYPE_DATE64:
384
+ return "date64";
385
+ case NANOARROW_TYPE_TIMESTAMP:
386
+ return "timestamp";
387
+ case NANOARROW_TYPE_TIME32:
388
+ return "time32";
389
+ case NANOARROW_TYPE_TIME64:
390
+ return "time64";
391
+ case NANOARROW_TYPE_INTERVAL_MONTHS:
392
+ return "interval_months";
393
+ case NANOARROW_TYPE_INTERVAL_DAY_TIME:
394
+ return "interval_day_time";
395
+ case NANOARROW_TYPE_DECIMAL128:
396
+ return "decimal128";
397
+ case NANOARROW_TYPE_DECIMAL256:
398
+ return "decimal256";
399
+ case NANOARROW_TYPE_LIST:
400
+ return "list";
401
+ case NANOARROW_TYPE_STRUCT:
402
+ return "struct";
403
+ case NANOARROW_TYPE_SPARSE_UNION:
404
+ return "sparse_union";
405
+ case NANOARROW_TYPE_DENSE_UNION:
406
+ return "dense_union";
407
+ case NANOARROW_TYPE_DICTIONARY:
408
+ return "dictionary";
409
+ case NANOARROW_TYPE_MAP:
410
+ return "map";
411
+ case NANOARROW_TYPE_EXTENSION:
412
+ return "extension";
413
+ case NANOARROW_TYPE_FIXED_SIZE_LIST:
414
+ return "fixed_size_list";
415
+ case NANOARROW_TYPE_DURATION:
416
+ return "duration";
417
+ case NANOARROW_TYPE_LARGE_STRING:
418
+ return "large_string";
419
+ case NANOARROW_TYPE_LARGE_BINARY:
420
+ return "large_binary";
421
+ case NANOARROW_TYPE_LARGE_LIST:
422
+ return "large_list";
423
+ case NANOARROW_TYPE_INTERVAL_MONTH_DAY_NANO:
424
+ return "interval_month_day_nano";
425
+ default:
426
+ return NULL;
427
+ }
428
+ }
429
+
430
+ /// \brief Arrow time unit enumerator
431
+ /// \ingroup nanoarrow-utils
432
+ ///
433
+ /// These names and values map to the corresponding arrow::TimeUnit::type
434
+ /// enumerator.
435
+ enum ArrowTimeUnit {
436
+ NANOARROW_TIME_UNIT_SECOND = 0,
437
+ NANOARROW_TIME_UNIT_MILLI = 1,
438
+ NANOARROW_TIME_UNIT_MICRO = 2,
439
+ NANOARROW_TIME_UNIT_NANO = 3
440
+ };
441
+
442
+ /// \brief Validation level enumerator
443
+ /// \ingroup nanoarrow-array
444
+ enum ArrowValidationLevel {
445
+ /// \brief Do not validate buffer sizes or content.
446
+ NANOARROW_VALIDATION_LEVEL_NONE = 0,
447
+
448
+ /// \brief Validate buffer sizes that depend on array length but do not
449
+ /// validate buffer
450
+ /// sizes that depend on buffer data access.
451
+ NANOARROW_VALIDATION_LEVEL_MINIMAL = 1,
452
+
453
+ /// \brief Validate all buffer sizes, including those that require buffer data
454
+ /// access,
455
+ /// but do not perform any checks that are O(1) along the length of the
456
+ /// buffers.
457
+ NANOARROW_VALIDATION_LEVEL_DEFAULT = 2,
458
+
459
+ /// \brief Validate all buffer sizes and all buffer content. This is useful in
460
+ /// the
461
+ /// context of untrusted input or input that may have been corrupted in
462
+ /// transit.
463
+ NANOARROW_VALIDATION_LEVEL_FULL = 3
464
+ };
465
+
466
+ /// \brief Get a string value of an enum ArrowTimeUnit value
467
+ /// \ingroup nanoarrow-utils
468
+ ///
469
+ /// Returns NULL for invalid values for time_unit
470
+ static inline const char* ArrowTimeUnitString(enum ArrowTimeUnit time_unit);
471
+
472
+ static inline const char* ArrowTimeUnitString(enum ArrowTimeUnit time_unit) {
473
+ switch (time_unit) {
474
+ case NANOARROW_TIME_UNIT_SECOND:
475
+ return "s";
476
+ case NANOARROW_TIME_UNIT_MILLI:
477
+ return "ms";
478
+ case NANOARROW_TIME_UNIT_MICRO:
479
+ return "us";
480
+ case NANOARROW_TIME_UNIT_NANO:
481
+ return "ns";
482
+ default:
483
+ return NULL;
484
+ }
485
+ }
486
+
487
+ /// \brief Functional types of buffers as described in the Arrow Columnar
488
+ /// Specification \ingroup nanoarrow-array-view
489
+ enum ArrowBufferType {
490
+ NANOARROW_BUFFER_TYPE_NONE,
491
+ NANOARROW_BUFFER_TYPE_VALIDITY,
492
+ NANOARROW_BUFFER_TYPE_TYPE_ID,
493
+ NANOARROW_BUFFER_TYPE_UNION_OFFSET,
494
+ NANOARROW_BUFFER_TYPE_DATA_OFFSET,
495
+ NANOARROW_BUFFER_TYPE_DATA
496
+ };
497
+
498
+ /// \brief An non-owning view of a string
499
+ /// \ingroup nanoarrow-utils
500
+ struct ArrowStringView {
501
+ /// \brief A pointer to the start of the string
502
+ ///
503
+ /// If size_bytes is 0, this value may be NULL.
504
+ const char* data;
505
+
506
+ /// \brief The size of the string in bytes,
507
+ ///
508
+ /// (Not including the null terminator.)
509
+ int64_t size_bytes;
510
+ };
511
+
512
+ /// \brief Return a view of a const C string
513
+ /// \ingroup nanoarrow-utils
514
+ static inline struct ArrowStringView ArrowCharView(const char* value);
515
+
516
+ static inline struct ArrowStringView ArrowCharView(const char* value) {
517
+ struct ArrowStringView out;
518
+
519
+ out.data = value;
520
+ if (value) {
521
+ out.size_bytes = (int64_t)strlen(value);
522
+ } else {
523
+ out.size_bytes = 0;
524
+ }
525
+
526
+ return out;
527
+ }
528
+
529
+ union ArrowBufferViewData {
530
+ const void* data;
531
+ const int8_t* as_int8;
532
+ const uint8_t* as_uint8;
533
+ const int16_t* as_int16;
534
+ const uint16_t* as_uint16;
535
+ const int32_t* as_int32;
536
+ const uint32_t* as_uint32;
537
+ const int64_t* as_int64;
538
+ const uint64_t* as_uint64;
539
+ const double* as_double;
540
+ const float* as_float;
541
+ const char* as_char;
542
+ };
543
+
544
+ /// \brief An non-owning view of a buffer
545
+ /// \ingroup nanoarrow-utils
546
+ struct ArrowBufferView {
547
+ /// \brief A pointer to the start of the buffer
548
+ ///
549
+ /// If size_bytes is 0, this value may be NULL.
550
+ union ArrowBufferViewData data;
551
+
552
+ /// \brief The size of the buffer in bytes
553
+ int64_t size_bytes;
554
+ };
555
+
556
+ /// \brief Array buffer allocation and deallocation
557
+ /// \ingroup nanoarrow-buffer
558
+ ///
559
+ /// Container for allocate, reallocate, and free methods that can be used
560
+ /// to customize allocation and deallocation of buffers when constructing
561
+ /// an ArrowArray.
562
+ struct ArrowBufferAllocator {
563
+ /// \brief Reallocate a buffer or return NULL if it cannot be reallocated
564
+ uint8_t* (*reallocate)(struct ArrowBufferAllocator* allocator, uint8_t* ptr,
565
+ int64_t old_size, int64_t new_size);
566
+
567
+ /// \brief Deallocate a buffer allocated by this allocator
568
+ void (*free)(struct ArrowBufferAllocator* allocator, uint8_t* ptr,
569
+ int64_t size);
570
+
571
+ /// \brief Opaque data specific to the allocator
572
+ void* private_data;
573
+ };
574
+
575
+ /// \brief An owning mutable view of a buffer
576
+ /// \ingroup nanoarrow-buffer
577
+ struct ArrowBuffer {
578
+ /// \brief A pointer to the start of the buffer
579
+ ///
580
+ /// If capacity_bytes is 0, this value may be NULL.
581
+ uint8_t* data;
582
+
583
+ /// \brief The size of the buffer in bytes
584
+ int64_t size_bytes;
585
+
586
+ /// \brief The capacity of the buffer in bytes
587
+ int64_t capacity_bytes;
588
+
589
+ /// \brief The allocator that will be used to reallocate and/or free the
590
+ /// buffer
591
+ struct ArrowBufferAllocator allocator;
592
+ };
593
+
594
+ /// \brief An owning mutable view of a bitmap
595
+ /// \ingroup nanoarrow-bitmap
596
+ struct ArrowBitmap {
597
+ /// \brief An ArrowBuffer to hold the allocated memory
598
+ struct ArrowBuffer buffer;
599
+
600
+ /// \brief The number of bits that have been appended to the bitmap
601
+ int64_t size_bits;
602
+ };
603
+
604
+ /// \brief A description of an arrangement of buffers
605
+ /// \ingroup nanoarrow-utils
606
+ ///
607
+ /// Contains the minimum amount of information required to
608
+ /// calculate the size of each buffer in an ArrowArray knowing only
609
+ /// the length and offset of the array.
610
+ struct ArrowLayout {
611
+ /// \brief The function of each buffer
612
+ enum ArrowBufferType buffer_type[3];
613
+
614
+ /// \brief The data type of each buffer
615
+ enum ArrowType buffer_data_type[3];
616
+
617
+ /// \brief The size of an element each buffer or 0 if this size is variable or
618
+ /// unknown
619
+ int64_t element_size_bits[3];
620
+
621
+ /// \brief The number of elements in the child array per element in this array
622
+ /// for a fixed-size list
623
+ int64_t child_size_elements;
624
+ };
625
+
626
+ /// \brief A non-owning view of an ArrowArray
627
+ /// \ingroup nanoarrow-array-view
628
+ ///
629
+ /// This data structure provides access to the values contained within
630
+ /// an ArrowArray with fields provided in a more readily-extractible
631
+ /// form. You can re-use an ArrowArrayView for multiple ArrowArrays
632
+ /// with the same storage type, use it to represent a hypothetical
633
+ /// ArrowArray that does not exist yet, or use it to validate the buffers
634
+ /// of a future ArrowArray.
635
+ struct ArrowArrayView {
636
+ /// \brief The underlying ArrowArray or NULL if it has not been set or
637
+ /// if the buffers in this ArrowArrayView are not backed by an ArrowArray.
638
+ struct ArrowArray* array;
639
+
640
+ /// \brief The number of elements from the physical start of the buffers.
641
+ int64_t offset;
642
+
643
+ /// \brief The number of elements in this view.
644
+ int64_t length;
645
+
646
+ /// \brief A cached null count or -1 to indicate that this value is unknown.
647
+ int64_t null_count;
648
+
649
+ /// \brief The type used to store values in this array
650
+ ///
651
+ /// This type represents only the minimum required information to
652
+ /// extract values from the array buffers (e.g., for a Date32 array,
653
+ /// this value will be NANOARROW_TYPE_INT32). For dictionary-encoded
654
+ /// arrays, this will be the index type.
655
+ enum ArrowType storage_type;
656
+
657
+ /// \brief The buffer types, strides, and sizes of this Array's buffers
658
+ struct ArrowLayout layout;
659
+
660
+ /// \brief This Array's buffers as ArrowBufferView objects
661
+ struct ArrowBufferView buffer_views[3];
662
+
663
+ /// \brief The number of children of this view
664
+ int64_t n_children;
665
+
666
+ /// \brief Pointers to views of this array's children
667
+ struct ArrowArrayView** children;
668
+
669
+ /// \brief Pointer to a view of this array's dictionary
670
+ struct ArrowArrayView* dictionary;
671
+
672
+ /// \brief Union type id to child index mapping
673
+ ///
674
+ /// If storage_type is a union type, a 256-byte ArrowMalloc()ed buffer
675
+ /// such that child_index == union_type_id_map[type_id] and
676
+ /// type_id == union_type_id_map[128 + child_index]. This value may be
677
+ /// NULL in the case where child_id == type_id.
678
+ int8_t* union_type_id_map;
679
+ };
680
+
681
+ // Used as the private data member for ArrowArrays allocated here and accessed
682
+ // internally within inline ArrowArray* helpers.
683
+ struct ArrowArrayPrivateData {
684
+ // Holder for the validity buffer (or first buffer for union types, which are
685
+ // the only type whose first buffer is not a valdiity buffer)
686
+ struct ArrowBitmap bitmap;
687
+
688
+ // Holder for additional buffers as required
689
+ struct ArrowBuffer buffers[2];
690
+
691
+ // The array of pointers to buffers. This must be updated after a sequence
692
+ // of appends to synchronize its values with the actual buffer addresses
693
+ // (which may have ben reallocated uring that time)
694
+ const void* buffer_data[3];
695
+
696
+ // The storage data type, or NANOARROW_TYPE_UNINITIALIZED if unknown
697
+ enum ArrowType storage_type;
698
+
699
+ // The buffer arrangement for the storage type
700
+ struct ArrowLayout layout;
701
+
702
+ // Flag to indicate if there are non-sequence union type ids.
703
+ // In the future this could be replaced with a type id<->child mapping
704
+ // to support constructing unions in append mode where type_id != child_index
705
+ int8_t union_type_id_is_child_index;
706
+ };
707
+
708
+ /// \brief A representation of an interval.
709
+ /// \ingroup nanoarrow-utils
710
+ struct ArrowInterval {
711
+ /// \brief The type of interval being used
712
+ enum ArrowType type;
713
+ /// \brief The number of months represented by the interval
714
+ int32_t months;
715
+ /// \brief The number of days represented by the interval
716
+ int32_t days;
717
+ /// \brief The number of ms represented by the interval
718
+ int32_t ms;
719
+ /// \brief The number of ns represented by the interval
720
+ int64_t ns;
721
+ };
722
+
723
+ /// \brief Zero initialize an Interval with a given unit
724
+ /// \ingroup nanoarrow-utils
725
+ static inline void ArrowIntervalInit(struct ArrowInterval* interval,
726
+ enum ArrowType type) {
727
+ memset(interval, 0, sizeof(struct ArrowInterval));
728
+ interval->type = type;
729
+ }
730
+
731
+ /// \brief A representation of a fixed-precision decimal number
732
+ /// \ingroup nanoarrow-utils
733
+ ///
734
+ /// This structure should be initialized with ArrowDecimalInit() once and
735
+ /// values set using ArrowDecimalSetInt(), ArrowDecimalSetBytes128(),
736
+ /// or ArrowDecimalSetBytes256().
737
+ struct ArrowDecimal {
738
+ /// \brief An array of 64-bit integers of n_words length defined in
739
+ /// native-endian order
740
+ uint64_t words[4];
741
+
742
+ /// \brief The number of significant digits this decimal number can represent
743
+ int32_t precision;
744
+
745
+ /// \brief The number of digits after the decimal point. This can be negative.
746
+ int32_t scale;
747
+
748
+ /// \brief The number of words in the words array
749
+ int n_words;
750
+
751
+ /// \brief Cached value used by the implementation
752
+ int high_word_index;
753
+
754
+ /// \brief Cached value used by the implementation
755
+ int low_word_index;
756
+ };
757
+
758
+ /// \brief Initialize a decimal with a given set of type parameters
759
+ /// \ingroup nanoarrow-utils
760
+ static inline void ArrowDecimalInit(struct ArrowDecimal* decimal,
761
+ int32_t bitwidth, int32_t precision,
762
+ int32_t scale) {
763
+ memset(decimal->words, 0, sizeof(decimal->words));
764
+ decimal->precision = precision;
765
+ decimal->scale = scale;
766
+ decimal->n_words = bitwidth / 8 / sizeof(uint64_t);
767
+
768
+ if (_ArrowIsLittleEndian()) {
769
+ decimal->low_word_index = 0;
770
+ decimal->high_word_index = decimal->n_words - 1;
771
+ } else {
772
+ decimal->low_word_index = decimal->n_words - 1;
773
+ decimal->high_word_index = 0;
774
+ }
775
+ }
776
+
777
+ /// \brief Get a signed integer value of a sufficiently small ArrowDecimal
778
+ ///
779
+ /// This does not check if the decimal's precision sufficiently small to fit
780
+ /// within the signed 64-bit integer range (A precision less than or equal
781
+ /// to 18 is sufficiently small).
782
+ static inline int64_t ArrowDecimalGetIntUnsafe(struct ArrowDecimal* decimal) {
783
+ return (int64_t)decimal->words[decimal->low_word_index];
784
+ }
785
+
786
+ /// \brief Copy the bytes of this decimal into a sufficiently large buffer
787
+ /// \ingroup nanoarrow-utils
788
+ static inline void ArrowDecimalGetBytes(struct ArrowDecimal* decimal,
789
+ uint8_t* out) {
790
+ memcpy(out, decimal->words, decimal->n_words * sizeof(uint64_t));
791
+ }
792
+
793
+ /// \brief Returns 1 if the value represented by decimal is >= 0 or -1 otherwise
794
+ /// \ingroup nanoarrow-utils
795
+ static inline int64_t ArrowDecimalSign(struct ArrowDecimal* decimal) {
796
+ return 1 | ((int64_t)(decimal->words[decimal->high_word_index]) >> 63);
797
+ }
798
+
799
+ /// \brief Sets the integer value of this decimal
800
+ /// \ingroup nanoarrow-utils
801
+ static inline void ArrowDecimalSetInt(struct ArrowDecimal* decimal,
802
+ int64_t value) {
803
+ if (value < 0) {
804
+ memset(decimal->words, 0xff, decimal->n_words * sizeof(uint64_t));
805
+ } else {
806
+ memset(decimal->words, 0, decimal->n_words * sizeof(uint64_t));
807
+ }
808
+
809
+ decimal->words[decimal->low_word_index] = value;
810
+ }
811
+
812
+ /// \brief Copy bytes from a buffer into this decimal
813
+ /// \ingroup nanoarrow-utils
814
+ static inline void ArrowDecimalSetBytes(struct ArrowDecimal* decimal,
815
+ const uint8_t* value) {
816
+ memcpy(decimal->words, value, decimal->n_words * sizeof(uint64_t));
817
+ }
818
+
819
+ #ifdef __cplusplus
820
+ }
821
+ #endif
822
+
823
+ #endif
824
+ // Licensed to the Apache Software Foundation (ASF) under one
825
+ // or more contributor license agreements. See the NOTICE file
826
+ // distributed with this work for additional information
827
+ // regarding copyright ownership. The ASF licenses this file
828
+ // to you under the Apache License, Version 2.0 (the
829
+ // "License"); you may not use this file except in compliance
830
+ // with the License. You may obtain a copy of the License at
831
+ //
832
+ // http://www.apache.org/licenses/LICENSE-2.0
833
+ //
834
+ // Unless required by applicable law or agreed to in writing,
835
+ // software distributed under the License is distributed on an
836
+ // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
837
+ // KIND, either express or implied. See the License for the
838
+ // specific language governing permissions and limitations
839
+ // under the License.
840
+
841
+ #ifndef NANOARROW_H_INCLUDED
842
+ #define NANOARROW_H_INCLUDED
843
+
844
+ #include <stddef.h>
845
+ #include <stdint.h>
846
+ #include <stdlib.h>
847
+
848
+ // If using CMake, optionally pass -DNANOARROW_NAMESPACE=MyNamespace which will
849
+ // set this define in nanoarrow_config.h. If not, you can optionally #define
850
+ // NANOARROW_NAMESPACE MyNamespace here.
851
+
852
+ // This section remaps the non-prefixed symbols to the prefixed symbols so that
853
+ // code written against this build can be used independent of the value of
854
+ // NANOARROW_NAMESPACE.
855
+ #ifdef NANOARROW_NAMESPACE
856
+ #define NANOARROW_CAT(A, B) A##B
857
+ #define NANOARROW_SYMBOL(A, B) NANOARROW_CAT(A, B)
858
+
859
+ #define ArrowNanoarrowVersion \
860
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowNanoarrowVersion)
861
+ #define ArrowNanoarrowVersionInt \
862
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowNanoarrowVersionInt)
863
+ #define ArrowErrorMessage \
864
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowErrorMessage)
865
+ #define ArrowMalloc NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowMalloc)
866
+ #define ArrowRealloc NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowRealloc)
867
+ #define ArrowFree NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowFree)
868
+ #define ArrowBufferAllocatorDefault \
869
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowBufferAllocatorDefault)
870
+ #define ArrowBufferDeallocator \
871
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowBufferDeallocator)
872
+ #define ArrowErrorSet NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowErrorSet)
873
+ #define ArrowLayoutInit NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowLayoutInit)
874
+ #define ArrowSchemaInit NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowSchemaInit)
875
+ #define ArrowSchemaInitFromType \
876
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowSchemaInitFromType)
877
+ #define ArrowSchemaSetType \
878
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowSchemaSetType)
879
+ #define ArrowSchemaSetTypeStruct \
880
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowSchemaSetTypeStruct)
881
+ #define ArrowSchemaSetTypeFixedSize \
882
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowSchemaSetTypeFixedSize)
883
+ #define ArrowSchemaSetTypeDecimal \
884
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowSchemaSetTypeDecimal)
885
+ #define ArrowSchemaSetTypeDateTime \
886
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowSchemaSetTypeDateTime)
887
+ #define ArrowSchemaSetTypeUnion \
888
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowSchemaSetTypeUnion)
889
+ #define ArrowSchemaDeepCopy \
890
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowSchemaDeepCopy)
891
+ #define ArrowSchemaSetFormat \
892
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowSchemaSetFormat)
893
+ #define ArrowSchemaSetName \
894
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowSchemaSetName)
895
+ #define ArrowSchemaSetMetadata \
896
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowSchemaSetMetadata)
897
+ #define ArrowSchemaAllocateChildren \
898
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowSchemaAllocateChildren)
899
+ #define ArrowSchemaAllocateDictionary \
900
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowSchemaAllocateDictionary)
901
+ #define ArrowMetadataReaderInit \
902
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowMetadataReaderInit)
903
+ #define ArrowMetadataReaderRead \
904
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowMetadataReaderRead)
905
+ #define ArrowMetadataSizeOf \
906
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowMetadataSizeOf)
907
+ #define ArrowMetadataHasKey \
908
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowMetadataHasKey)
909
+ #define ArrowMetadataGetValue \
910
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowMetadataGetValue)
911
+ #define ArrowMetadataBuilderInit \
912
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowMetadataBuilderInit)
913
+ #define ArrowMetadataBuilderAppend \
914
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowMetadataBuilderAppend)
915
+ #define ArrowMetadataBuilderSet \
916
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowMetadataBuilderSet)
917
+ #define ArrowMetadataBuilderRemove \
918
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowMetadataBuilderRemove)
919
+ #define ArrowSchemaViewInit \
920
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowSchemaViewInit)
921
+ #define ArrowSchemaToString \
922
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowSchemaToString)
923
+ #define ArrowArrayInitFromType \
924
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowArrayInitFromType)
925
+ #define ArrowArrayInitFromSchema \
926
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowArrayInitFromSchema)
927
+ #define ArrowArrayInitFromArrayView \
928
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowArrayInitFromArrayView)
929
+ #define ArrowArrayInitFromArrayView \
930
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowArrayInitFromArrayView)
931
+ #define ArrowArrayAllocateChildren \
932
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowArrayAllocateChildren)
933
+ #define ArrowArrayAllocateDictionary \
934
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowArrayAllocateDictionary)
935
+ #define ArrowArraySetValidityBitmap \
936
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowArraySetValidityBitmap)
937
+ #define ArrowArraySetBuffer \
938
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowArraySetBuffer)
939
+ #define ArrowArrayReserve \
940
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowArrayReserve)
941
+ #define ArrowArrayFinishBuilding \
942
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowArrayFinishBuilding)
943
+ #define ArrowArrayFinishBuildingDefault \
944
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowArrayFinishBuildingDefault)
945
+ #define ArrowArrayViewInitFromType \
946
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowArrayViewInitFromType)
947
+ #define ArrowArrayViewInitFromSchema \
948
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowArrayViewInitFromSchema)
949
+ #define ArrowArrayViewAllocateChildren \
950
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowArrayViewAllocateChildren)
951
+ #define ArrowArrayViewAllocateDictionary \
952
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowArrayViewAllocateDictionary)
953
+ #define ArrowArrayViewSetLength \
954
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowArrayViewSetLength)
955
+ #define ArrowArrayViewSetArray \
956
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowArrayViewSetArray)
957
+ #define ArrowArrayViewSetArrayMinimal \
958
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowArrayViewSetArrayMinimal)
959
+ #define ArrowArrayViewValidate \
960
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowArrayViewValidate)
961
+ #define ArrowArrayViewReset \
962
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowArrayViewReset)
963
+ #define ArrowBasicArrayStreamInit \
964
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowBasicArrayStreamInit)
965
+ #define ArrowBasicArrayStreamSetArray \
966
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowBasicArrayStreamSetArray)
967
+ #define ArrowBasicArrayStreamValidate \
968
+ NANOARROW_SYMBOL(NANOARROW_NAMESPACE, ArrowBasicArrayStreamValidate)
969
+
970
+ #endif
971
+
972
+ #ifdef __cplusplus
973
+ extern "C" {
974
+ #endif
975
+
976
+ /// \defgroup nanoarrow Nanoarrow C library
977
+ ///
978
+ /// Except where noted, objects are not thread-safe and clients should
979
+ /// take care to serialize accesses to methods.
980
+ ///
981
+ /// Because this library is intended to be vendored, it provides full type
982
+ /// definitions and encourages clients to stack or statically allocate
983
+ /// where convenient.
984
+
985
+ /// \defgroup nanoarrow-malloc Memory management
986
+ ///
987
+ /// Non-buffer members of a struct ArrowSchema and struct ArrowArray
988
+ /// must be allocated using ArrowMalloc() or ArrowRealloc() and freed
989
+ /// using ArrowFree() for schemas and arrays allocated here. Buffer members
990
+ /// are allocated using an ArrowBufferAllocator.
991
+ ///
992
+ /// @{
993
+
994
+ /// \brief Allocate like malloc()
995
+ void* ArrowMalloc(int64_t size);
996
+
997
+ /// \brief Reallocate like realloc()
998
+ void* ArrowRealloc(void* ptr, int64_t size);
999
+
1000
+ /// \brief Free a pointer allocated using ArrowMalloc() or ArrowRealloc().
1001
+ void ArrowFree(void* ptr);
1002
+
1003
+ /// \brief Return the default allocator
1004
+ ///
1005
+ /// The default allocator uses ArrowMalloc(), ArrowRealloc(), and
1006
+ /// ArrowFree().
1007
+ struct ArrowBufferAllocator ArrowBufferAllocatorDefault(void);
1008
+
1009
+ /// \brief Create a custom deallocator
1010
+ ///
1011
+ /// Creates a buffer allocator with only a free method that can be used to
1012
+ /// attach a custom deallocator to an ArrowBuffer. This may be used to
1013
+ /// avoid copying an existing buffer that was not allocated using the
1014
+ /// infrastructure provided here (e.g., by an R or Python object).
1015
+ struct ArrowBufferAllocator ArrowBufferDeallocator(
1016
+ void (*custom_free)(struct ArrowBufferAllocator* allocator, uint8_t* ptr,
1017
+ int64_t size),
1018
+ void* private_data);
1019
+
1020
+ /// @}
1021
+
1022
+ /// \defgroup nanoarrow-errors Error handling
1023
+ ///
1024
+ /// Functions generally return an errno-compatible error code; functions that
1025
+ /// need to communicate more verbose error information accept a pointer
1026
+ /// to an ArrowError. This can be stack or statically allocated. The
1027
+ /// content of the message is undefined unless an error code has been
1028
+ /// returned. If a nanoarrow function is passed a non-null ArrowError pointer,
1029
+ /// the ArrowError pointed to by the argument will be propagated with a
1030
+ /// null-terminated error message. It is safe to pass a NULL ArrowError anywhere
1031
+ /// in the nanoarrow API.
1032
+ ///
1033
+ /// Except where documented, it is generally not safe to continue after a
1034
+ /// function has returned a non-zero ArrowErrorCode. The NANOARROW_RETURN_NOT_OK
1035
+ /// and NANOARROW_ASSERT_OK macros are provided to help propagate errors. C++
1036
+ /// clients can use the helpers provided in the nanoarrow.hpp header to
1037
+ /// facilitate using C++ idioms for memory management and error propgagtion.
1038
+ ///
1039
+ /// @{
1040
+
1041
+ /// \brief Error type containing a UTF-8 encoded message.
1042
+ struct ArrowError {
1043
+ /// \brief A character buffer with space for an error message.
1044
+ char message[1024];
1045
+ };
1046
+
1047
+ /// \brief Ensure an ArrowError is null-terminated by zeroing the first
1048
+ /// character.
1049
+ ///
1050
+ /// If error is NULL, this function does nothing.
1051
+ static inline void ArrowErrorInit(struct ArrowError* error) {
1052
+ if (error) {
1053
+ error->message[0] = '\0';
1054
+ }
1055
+ }
1056
+
1057
+ /// \brief Set the contents of an error using printf syntax.
1058
+ ///
1059
+ /// If error is NULL, this function does nothing and returns NANOARROW_OK.
1060
+ ArrowErrorCode ArrowErrorSet(struct ArrowError* error, const char* fmt, ...);
1061
+
1062
+ /// \brief Get the contents of an error
1063
+ ///
1064
+ /// If error is NULL, returns "", or returns the contents of the error message
1065
+ /// otherwise.
1066
+ const char* ArrowErrorMessage(struct ArrowError* error);
1067
+
1068
+ /// @}
1069
+
1070
+ /// \defgroup nanoarrow-utils Utility data structures
1071
+ ///
1072
+ /// @{
1073
+
1074
+ /// \brief Return a version string in the form "major.minor.patch"
1075
+ const char* ArrowNanoarrowVersion(void);
1076
+
1077
+ /// \brief Return an integer that can be used to compare versions sequentially
1078
+ int ArrowNanoarrowVersionInt(void);
1079
+
1080
+ /// \brief Initialize a description of buffer arrangements from a storage type
1081
+ void ArrowLayoutInit(struct ArrowLayout* layout, enum ArrowType storage_type);
1082
+
1083
+ /// \brief Create a string view from a null-terminated string
1084
+ static inline struct ArrowStringView ArrowCharView(const char* value);
1085
+
1086
+ /// @}
1087
+
1088
+ /// \defgroup nanoarrow-schema Creating schemas
1089
+ ///
1090
+ /// These functions allocate, copy, and destroy ArrowSchema structures
1091
+ ///
1092
+ /// @{
1093
+
1094
+ /// \brief Initialize an ArrowSchema
1095
+ ///
1096
+ /// Initializes the fields and release callback of schema_out. Caller
1097
+ /// is responsible for calling the schema->release callback if
1098
+ /// NANOARROW_OK is returned.
1099
+ void ArrowSchemaInit(struct ArrowSchema* schema);
1100
+
1101
+ /// \brief Initialize an ArrowSchema from an ArrowType
1102
+ ///
1103
+ /// A convenience constructor for that calls ArrowSchemaInit() and
1104
+ /// ArrowSchemaSetType() for the common case of constructing an
1105
+ /// unparameterized type. The caller is responsible for calling the
1106
+ /// schema->release callback if NANOARROW_OK is returned.
1107
+ ArrowErrorCode ArrowSchemaInitFromType(struct ArrowSchema* schema,
1108
+ enum ArrowType type);
1109
+
1110
+ /// \brief Get a human-readable summary of a Schema
1111
+ ///
1112
+ /// Writes a summary of an ArrowSchema to out (up to n - 1 characters)
1113
+ /// and returns the number of characters required for the output if
1114
+ /// n were sufficiently large. If recursive is non-zero, the result will
1115
+ /// also include children.
1116
+ int64_t ArrowSchemaToString(struct ArrowSchema* schema, char* out, int64_t n,
1117
+ char recursive);
1118
+
1119
+ /// \brief Set the format field of a schema from an ArrowType
1120
+ ///
1121
+ /// Initializes the fields and release callback of schema_out. For
1122
+ /// NANOARROW_TYPE_LIST, NANOARROW_TYPE_LARGE_LIST, and
1123
+ /// NANOARROW_TYPE_MAP, the appropriate number of children are
1124
+ /// allocated, initialized, and named; however, the caller must
1125
+ /// ArrowSchemaSetType() on the preinitialized children. Schema must have been
1126
+ /// initialized using ArrowSchemaInit() or ArrowSchemaDeepCopy().
1127
+ ArrowErrorCode ArrowSchemaSetType(struct ArrowSchema* schema,
1128
+ enum ArrowType type);
1129
+
1130
+ /// \brief Set the format field and initialize children of a struct schema
1131
+ ///
1132
+ /// The specified number of children are initialized; however, the caller is
1133
+ /// responsible for calling ArrowSchemaSetType() and ArrowSchemaSetName() on
1134
+ /// each child. Schema must have been initialized using ArrowSchemaInit() or
1135
+ /// ArrowSchemaDeepCopy().
1136
+ ArrowErrorCode ArrowSchemaSetTypeStruct(struct ArrowSchema* schema,
1137
+ int64_t n_children);
1138
+
1139
+ /// \brief Set the format field of a fixed-size schema
1140
+ ///
1141
+ /// Returns EINVAL for fixed_size <= 0 or for type that is not
1142
+ /// NANOARROW_TYPE_FIXED_SIZE_BINARY or NANOARROW_TYPE_FIXED_SIZE_LIST.
1143
+ /// For NANOARROW_TYPE_FIXED_SIZE_LIST, the appropriate number of children are
1144
+ /// allocated, initialized, and named; however, the caller must
1145
+ /// ArrowSchemaSetType() the first child. Schema must have been initialized
1146
+ /// using ArrowSchemaInit() or ArrowSchemaDeepCopy().
1147
+ ArrowErrorCode ArrowSchemaSetTypeFixedSize(struct ArrowSchema* schema,
1148
+ enum ArrowType type,
1149
+ int32_t fixed_size);
1150
+
1151
+ /// \brief Set the format field of a decimal schema
1152
+ ///
1153
+ /// Returns EINVAL for scale <= 0 or for type that is not
1154
+ /// NANOARROW_TYPE_DECIMAL128 or NANOARROW_TYPE_DECIMAL256. Schema must have
1155
+ /// been initialized using ArrowSchemaInit() or ArrowSchemaDeepCopy().
1156
+ ArrowErrorCode ArrowSchemaSetTypeDecimal(struct ArrowSchema* schema,
1157
+ enum ArrowType type,
1158
+ int32_t decimal_precision,
1159
+ int32_t decimal_scale);
1160
+
1161
+ /// \brief Set the format field of a time, timestamp, or duration schema
1162
+ ///
1163
+ /// Returns EINVAL for type that is not
1164
+ /// NANOARROW_TYPE_TIME32, NANOARROW_TYPE_TIME64,
1165
+ /// NANOARROW_TYPE_TIMESTAMP, or NANOARROW_TYPE_DURATION. The
1166
+ /// timezone parameter must be NULL for a non-timestamp type. Schema must have
1167
+ /// been initialized using ArrowSchemaInit() or ArrowSchemaDeepCopy().
1168
+ ArrowErrorCode ArrowSchemaSetTypeDateTime(struct ArrowSchema* schema,
1169
+ enum ArrowType type,
1170
+ enum ArrowTimeUnit time_unit,
1171
+ const char* timezone);
1172
+
1173
+ /// \brief Seet the format field of a union schema
1174
+ ///
1175
+ /// Returns EINVAL for a type that is not NANOARROW_TYPE_DENSE_UNION
1176
+ /// or NANOARROW_TYPE_SPARSE_UNION. The specified number of children are
1177
+ /// allocated, and initialized.
1178
+ ArrowErrorCode ArrowSchemaSetTypeUnion(struct ArrowSchema* schema,
1179
+ enum ArrowType type, int64_t n_children);
1180
+
1181
+ /// \brief Make a (recursive) copy of a schema
1182
+ ///
1183
+ /// Allocates and copies fields of schema into schema_out.
1184
+ ArrowErrorCode ArrowSchemaDeepCopy(struct ArrowSchema* schema,
1185
+ struct ArrowSchema* schema_out);
1186
+
1187
+ /// \brief Copy format into schema->format
1188
+ ///
1189
+ /// schema must have been allocated using ArrowSchemaInitFromType() or
1190
+ /// ArrowSchemaDeepCopy().
1191
+ ArrowErrorCode ArrowSchemaSetFormat(struct ArrowSchema* schema,
1192
+ const char* format);
1193
+
1194
+ /// \brief Copy name into schema->name
1195
+ ///
1196
+ /// schema must have been allocated using ArrowSchemaInitFromType() or
1197
+ /// ArrowSchemaDeepCopy().
1198
+ ArrowErrorCode ArrowSchemaSetName(struct ArrowSchema* schema, const char* name);
1199
+
1200
+ /// \brief Copy metadata into schema->metadata
1201
+ ///
1202
+ /// schema must have been allocated using ArrowSchemaInitFromType() or
1203
+ /// ArrowSchemaDeepCopy.
1204
+ ArrowErrorCode ArrowSchemaSetMetadata(struct ArrowSchema* schema,
1205
+ const char* metadata);
1206
+
1207
+ /// \brief Allocate the schema->children array
1208
+ ///
1209
+ /// Includes the memory for each child struct ArrowSchema.
1210
+ /// schema must have been allocated using ArrowSchemaInitFromType() or
1211
+ /// ArrowSchemaDeepCopy().
1212
+ ArrowErrorCode ArrowSchemaAllocateChildren(struct ArrowSchema* schema,
1213
+ int64_t n_children);
1214
+
1215
+ /// \brief Allocate the schema->dictionary member
1216
+ ///
1217
+ /// schema must have been allocated using ArrowSchemaInitFromType() or
1218
+ /// ArrowSchemaDeepCopy().
1219
+ ArrowErrorCode ArrowSchemaAllocateDictionary(struct ArrowSchema* schema);
1220
+
1221
+ /// @}
1222
+
1223
+ /// \defgroup nanoarrow-metadata Create, read, and modify schema metadata
1224
+ ///
1225
+ /// @{
1226
+
1227
+ /// \brief Reader for key/value pairs in schema metadata
1228
+ ///
1229
+ /// The ArrowMetadataReader does not own any data and is only valid
1230
+ /// for the lifetime of the underlying metadata pointer.
1231
+ struct ArrowMetadataReader {
1232
+ /// \brief A metadata string from a schema->metadata field.
1233
+ const char* metadata;
1234
+
1235
+ /// \brief The current offset into the metadata string
1236
+ int64_t offset;
1237
+
1238
+ /// \brief The number of remaining keys
1239
+ int32_t remaining_keys;
1240
+ };
1241
+
1242
+ /// \brief Initialize an ArrowMetadataReader
1243
+ ArrowErrorCode ArrowMetadataReaderInit(struct ArrowMetadataReader* reader,
1244
+ const char* metadata);
1245
+
1246
+ /// \brief Read the next key/value pair from an ArrowMetadataReader
1247
+ ArrowErrorCode ArrowMetadataReaderRead(struct ArrowMetadataReader* reader,
1248
+ struct ArrowStringView* key_out,
1249
+ struct ArrowStringView* value_out);
1250
+
1251
+ /// \brief The number of bytes in in a key/value metadata string
1252
+ int64_t ArrowMetadataSizeOf(const char* metadata);
1253
+
1254
+ /// \brief Check for a key in schema metadata
1255
+ char ArrowMetadataHasKey(const char* metadata, struct ArrowStringView key);
1256
+
1257
+ /// \brief Extract a value from schema metadata
1258
+ ///
1259
+ /// If key does not exist in metadata, value_out is unmodified
1260
+ ArrowErrorCode ArrowMetadataGetValue(const char* metadata,
1261
+ struct ArrowStringView key,
1262
+ struct ArrowStringView* value_out);
1263
+
1264
+ /// \brief Initialize a builder for schema metadata from key/value pairs
1265
+ ///
1266
+ /// metadata can be an existing metadata string or NULL to initialize
1267
+ /// an empty metadata string.
1268
+ ArrowErrorCode ArrowMetadataBuilderInit(struct ArrowBuffer* buffer,
1269
+ const char* metadata);
1270
+
1271
+ /// \brief Append a key/value pair to a buffer containing serialized metadata
1272
+ ArrowErrorCode ArrowMetadataBuilderAppend(struct ArrowBuffer* buffer,
1273
+ struct ArrowStringView key,
1274
+ struct ArrowStringView value);
1275
+
1276
+ /// \brief Set a key/value pair to a buffer containing serialized metadata
1277
+ ///
1278
+ /// Ensures that the only entry for key in the metadata is set to value.
1279
+ /// This function maintains the existing position of (the first instance of)
1280
+ /// key if present in the data.
1281
+ ArrowErrorCode ArrowMetadataBuilderSet(struct ArrowBuffer* buffer,
1282
+ struct ArrowStringView key,
1283
+ struct ArrowStringView value);
1284
+
1285
+ /// \brief Remove a key from a buffer containing serialized metadata
1286
+ ArrowErrorCode ArrowMetadataBuilderRemove(struct ArrowBuffer* buffer,
1287
+ struct ArrowStringView key);
1288
+
1289
+ /// @}
1290
+
1291
+ /// \defgroup nanoarrow-schema-view Reading schemas
1292
+ ///
1293
+ /// @{
1294
+
1295
+ /// \brief A non-owning view of a parsed ArrowSchema
1296
+ ///
1297
+ /// Contains more readily extractable values than a raw ArrowSchema.
1298
+ /// Clients can stack or statically allocate this structure but are
1299
+ /// encouraged to use the provided getters to ensure forward
1300
+ /// compatibility.
1301
+ struct ArrowSchemaView {
1302
+ /// \brief A pointer to the schema represented by this view
1303
+ struct ArrowSchema* schema;
1304
+
1305
+ /// \brief The data type represented by the schema
1306
+ ///
1307
+ /// This value may be NANOARROW_TYPE_DICTIONARY if the schema has a
1308
+ /// non-null dictionary member; datetime types are valid values.
1309
+ /// This value will never be NANOARROW_TYPE_EXTENSION (see
1310
+ /// extension_name and/or extension_metadata to check for
1311
+ /// an extension type).
1312
+ enum ArrowType type;
1313
+
1314
+ /// \brief The storage data type represented by the schema
1315
+ ///
1316
+ /// This value will never be NANOARROW_TYPE_DICTIONARY,
1317
+ /// NANOARROW_TYPE_EXTENSION or any datetime type. This value represents only
1318
+ /// the type required to interpret the buffers in the array.
1319
+ enum ArrowType storage_type;
1320
+
1321
+ /// \brief The storage layout represented by the schema
1322
+ struct ArrowLayout layout;
1323
+
1324
+ /// \brief The extension type name if it exists
1325
+ ///
1326
+ /// If the ARROW:extension:name key is present in schema.metadata,
1327
+ /// extension_name.data will be non-NULL.
1328
+ struct ArrowStringView extension_name;
1329
+
1330
+ /// \brief The extension type metadata if it exists
1331
+ ///
1332
+ /// If the ARROW:extension:metadata key is present in schema.metadata,
1333
+ /// extension_metadata.data will be non-NULL.
1334
+ struct ArrowStringView extension_metadata;
1335
+
1336
+ /// \brief Format fixed size parameter
1337
+ ///
1338
+ /// This value is set when parsing a fixed-size binary or fixed-size
1339
+ /// list schema; this value is undefined for other types. For a
1340
+ /// fixed-size binary schema this value is in bytes; for a fixed-size
1341
+ /// list schema this value refers to the number of child elements for
1342
+ /// each element of the parent.
1343
+ int32_t fixed_size;
1344
+
1345
+ /// \brief Decimal bitwidth
1346
+ ///
1347
+ /// This value is set when parsing a decimal type schema;
1348
+ /// this value is undefined for other types.
1349
+ int32_t decimal_bitwidth;
1350
+
1351
+ /// \brief Decimal precision
1352
+ ///
1353
+ /// This value is set when parsing a decimal type schema;
1354
+ /// this value is undefined for other types.
1355
+ int32_t decimal_precision;
1356
+
1357
+ /// \brief Decimal scale
1358
+ ///
1359
+ /// This value is set when parsing a decimal type schema;
1360
+ /// this value is undefined for other types.
1361
+ int32_t decimal_scale;
1362
+
1363
+ /// \brief Format time unit parameter
1364
+ ///
1365
+ /// This value is set when parsing a date/time type. The value is
1366
+ /// undefined for other types.
1367
+ enum ArrowTimeUnit time_unit;
1368
+
1369
+ /// \brief Format timezone parameter
1370
+ ///
1371
+ /// This value is set when parsing a timestamp type and represents
1372
+ /// the timezone format parameter. This value points to
1373
+ /// data within the schema and is undefined for other types.
1374
+ const char* timezone;
1375
+
1376
+ /// \brief Union type ids parameter
1377
+ ///
1378
+ /// This value is set when parsing a union type and represents
1379
+ /// type ids parameter. This value points to
1380
+ /// data within the schema and is undefined for other types.
1381
+ const char* union_type_ids;
1382
+ };
1383
+
1384
+ /// \brief Initialize an ArrowSchemaView
1385
+ ArrowErrorCode ArrowSchemaViewInit(struct ArrowSchemaView* schema_view,
1386
+ struct ArrowSchema* schema,
1387
+ struct ArrowError* error);
1388
+
1389
+ /// @}
1390
+
1391
+ /// \defgroup nanoarrow-buffer Owning, growable buffers
1392
+ ///
1393
+ /// @{
1394
+
1395
+ /// \brief Initialize an ArrowBuffer
1396
+ ///
1397
+ /// Initialize a buffer with a NULL, zero-size buffer using the default
1398
+ /// buffer allocator.
1399
+ static inline void ArrowBufferInit(struct ArrowBuffer* buffer);
1400
+
1401
+ /// \brief Set a newly-initialized buffer's allocator
1402
+ ///
1403
+ /// Returns EINVAL if the buffer has already been allocated.
1404
+ static inline ArrowErrorCode ArrowBufferSetAllocator(
1405
+ struct ArrowBuffer* buffer, struct ArrowBufferAllocator allocator);
1406
+
1407
+ /// \brief Reset an ArrowBuffer
1408
+ ///
1409
+ /// Releases the buffer using the allocator's free method if
1410
+ /// the buffer's data member is non-null, sets the data member
1411
+ /// to NULL, and sets the buffer's size and capacity to 0.
1412
+ static inline void ArrowBufferReset(struct ArrowBuffer* buffer);
1413
+
1414
+ /// \brief Move an ArrowBuffer
1415
+ ///
1416
+ /// Transfers the buffer data and lifecycle management to another
1417
+ /// address and resets buffer.
1418
+ static inline void ArrowBufferMove(struct ArrowBuffer* src,
1419
+ struct ArrowBuffer* dst);
1420
+
1421
+ /// \brief Grow or shrink a buffer to a given capacity
1422
+ ///
1423
+ /// When shrinking the capacity of the buffer, the buffer is only reallocated
1424
+ /// if shrink_to_fit is non-zero. Calling ArrowBufferResize() does not
1425
+ /// adjust the buffer's size member except to ensure that the invariant
1426
+ /// capacity >= size remains true.
1427
+ static inline ArrowErrorCode ArrowBufferResize(struct ArrowBuffer* buffer,
1428
+ int64_t new_capacity_bytes,
1429
+ char shrink_to_fit);
1430
+
1431
+ /// \brief Ensure a buffer has at least a given additional capacity
1432
+ ///
1433
+ /// Ensures that the buffer has space to append at least
1434
+ /// additional_size_bytes, overallocating when required.
1435
+ static inline ArrowErrorCode ArrowBufferReserve(struct ArrowBuffer* buffer,
1436
+ int64_t additional_size_bytes);
1437
+
1438
+ /// \brief Write data to buffer and increment the buffer size
1439
+ ///
1440
+ /// This function does not check that buffer has the required capacity
1441
+ static inline void ArrowBufferAppendUnsafe(struct ArrowBuffer* buffer,
1442
+ const void* data,
1443
+ int64_t size_bytes);
1444
+
1445
+ /// \brief Write data to buffer and increment the buffer size
1446
+ ///
1447
+ /// This function writes and ensures that the buffer has the required capacity,
1448
+ /// possibly by reallocating the buffer. Like ArrowBufferReserve, this will
1449
+ /// overallocate when reallocation is required.
1450
+ static inline ArrowErrorCode ArrowBufferAppend(struct ArrowBuffer* buffer,
1451
+ const void* data,
1452
+ int64_t size_bytes);
1453
+
1454
+ /// \brief Write fill to buffer and increment the buffer size
1455
+ ///
1456
+ /// This function writes the specified number of fill bytes and
1457
+ /// ensures that the buffer has the required capacity,
1458
+ static inline ArrowErrorCode ArrowBufferAppendFill(struct ArrowBuffer* buffer,
1459
+ uint8_t value,
1460
+ int64_t size_bytes);
1461
+
1462
+ /// \brief Write an 8-bit integer to a buffer
1463
+ static inline ArrowErrorCode ArrowBufferAppendInt8(struct ArrowBuffer* buffer,
1464
+ int8_t value);
1465
+
1466
+ /// \brief Write an unsigned 8-bit integer to a buffer
1467
+ static inline ArrowErrorCode ArrowBufferAppendUInt8(struct ArrowBuffer* buffer,
1468
+ uint8_t value);
1469
+
1470
+ /// \brief Write a 16-bit integer to a buffer
1471
+ static inline ArrowErrorCode ArrowBufferAppendInt16(struct ArrowBuffer* buffer,
1472
+ int16_t value);
1473
+
1474
+ /// \brief Write an unsigned 16-bit integer to a buffer
1475
+ static inline ArrowErrorCode ArrowBufferAppendUInt16(struct ArrowBuffer* buffer,
1476
+ uint16_t value);
1477
+
1478
+ /// \brief Write a 32-bit integer to a buffer
1479
+ static inline ArrowErrorCode ArrowBufferAppendInt32(struct ArrowBuffer* buffer,
1480
+ int32_t value);
1481
+
1482
+ /// \brief Write an unsigned 32-bit integer to a buffer
1483
+ static inline ArrowErrorCode ArrowBufferAppendUInt32(struct ArrowBuffer* buffer,
1484
+ uint32_t value);
1485
+
1486
+ /// \brief Write a 64-bit integer to a buffer
1487
+ static inline ArrowErrorCode ArrowBufferAppendInt64(struct ArrowBuffer* buffer,
1488
+ int64_t value);
1489
+
1490
+ /// \brief Write an unsigned 64-bit integer to a buffer
1491
+ static inline ArrowErrorCode ArrowBufferAppendUInt64(struct ArrowBuffer* buffer,
1492
+ uint64_t value);
1493
+
1494
+ /// \brief Write a double to a buffer
1495
+ static inline ArrowErrorCode ArrowBufferAppendDouble(struct ArrowBuffer* buffer,
1496
+ double value);
1497
+
1498
+ /// \brief Write a float to a buffer
1499
+ static inline ArrowErrorCode ArrowBufferAppendFloat(struct ArrowBuffer* buffer,
1500
+ float value);
1501
+
1502
+ /// \brief Write an ArrowStringView to a buffer
1503
+ static inline ArrowErrorCode ArrowBufferAppendStringView(
1504
+ struct ArrowBuffer* buffer, struct ArrowStringView value);
1505
+
1506
+ /// \brief Write an ArrowBufferView to a buffer
1507
+ static inline ArrowErrorCode ArrowBufferAppendBufferView(
1508
+ struct ArrowBuffer* buffer, struct ArrowBufferView value);
1509
+
1510
+ /// @}
1511
+
1512
+ /// \defgroup nanoarrow-bitmap Bitmap utilities
1513
+ ///
1514
+ /// @{
1515
+
1516
+ /// \brief Extract a boolean value from a bitmap
1517
+ static inline int8_t ArrowBitGet(const uint8_t* bits, int64_t i);
1518
+
1519
+ /// \brief Set a boolean value to a bitmap to true
1520
+ static inline void ArrowBitSet(uint8_t* bits, int64_t i);
1521
+
1522
+ /// \brief Set a boolean value to a bitmap to false
1523
+ static inline void ArrowBitClear(uint8_t* bits, int64_t i);
1524
+
1525
+ /// \brief Set a boolean value to a bitmap
1526
+ static inline void ArrowBitSetTo(uint8_t* bits, int64_t i, uint8_t value);
1527
+
1528
+ /// \brief Set a boolean value to a range in a bitmap
1529
+ static inline void ArrowBitsSetTo(uint8_t* bits, int64_t start_offset,
1530
+ int64_t length, uint8_t bits_are_set);
1531
+
1532
+ /// \brief Count true values in a bitmap
1533
+ static inline int64_t ArrowBitCountSet(const uint8_t* bits, int64_t i_from,
1534
+ int64_t i_to);
1535
+
1536
+ /// \brief Extract int8 boolean values from a range in a bitmap
1537
+ static inline void ArrowBitsUnpackInt8(const uint8_t* bits,
1538
+ int64_t start_offset, int64_t length,
1539
+ int8_t* out);
1540
+
1541
+ /// \brief Extract int32 boolean values from a range in a bitmap
1542
+ static inline void ArrowBitsUnpackInt32(const uint8_t* bits,
1543
+ int64_t start_offset, int64_t length,
1544
+ int32_t* out);
1545
+
1546
+ /// \brief Initialize an ArrowBitmap
1547
+ ///
1548
+ /// Initialize the builder's buffer, empty its cache, and reset the size to zero
1549
+ static inline void ArrowBitmapInit(struct ArrowBitmap* bitmap);
1550
+
1551
+ /// \brief Move an ArrowBitmap
1552
+ ///
1553
+ /// Transfers the underlying buffer data and lifecycle management to another
1554
+ /// address and resets the bitmap.
1555
+ static inline void ArrowBitmapMove(struct ArrowBitmap* src,
1556
+ struct ArrowBitmap* dst);
1557
+
1558
+ /// \brief Ensure a bitmap builder has at least a given additional capacity
1559
+ ///
1560
+ /// Ensures that the buffer has space to append at least
1561
+ /// additional_size_bits, overallocating when required.
1562
+ static inline ArrowErrorCode ArrowBitmapReserve(struct ArrowBitmap* bitmap,
1563
+ int64_t additional_size_bits);
1564
+
1565
+ /// \brief Grow or shrink a bitmap to a given capacity
1566
+ ///
1567
+ /// When shrinking the capacity of the bitmap, the bitmap is only reallocated
1568
+ /// if shrink_to_fit is non-zero. Calling ArrowBitmapResize() does not
1569
+ /// adjust the buffer's size member except when shrinking new_capacity_bits
1570
+ /// to a value less than the current number of bits in the bitmap.
1571
+ static inline ArrowErrorCode ArrowBitmapResize(struct ArrowBitmap* bitmap,
1572
+ int64_t new_capacity_bits,
1573
+ char shrink_to_fit);
1574
+
1575
+ /// \brief Reserve space for and append zero or more of the same boolean value
1576
+ /// to a bitmap
1577
+ static inline ArrowErrorCode ArrowBitmapAppend(struct ArrowBitmap* bitmap,
1578
+ uint8_t bits_are_set,
1579
+ int64_t length);
1580
+
1581
+ /// \brief Append zero or more of the same boolean value to a bitmap
1582
+ static inline void ArrowBitmapAppendUnsafe(struct ArrowBitmap* bitmap,
1583
+ uint8_t bits_are_set,
1584
+ int64_t length);
1585
+
1586
+ /// \brief Append boolean values encoded as int8_t to a bitmap
1587
+ ///
1588
+ /// The values must all be 0 or 1.
1589
+ static inline void ArrowBitmapAppendInt8Unsafe(struct ArrowBitmap* bitmap,
1590
+ const int8_t* values,
1591
+ int64_t n_values);
1592
+
1593
+ /// \brief Append boolean values encoded as int32_t to a bitmap
1594
+ ///
1595
+ /// The values must all be 0 or 1.
1596
+ static inline void ArrowBitmapAppendInt32Unsafe(struct ArrowBitmap* bitmap,
1597
+ const int32_t* values,
1598
+ int64_t n_values);
1599
+
1600
+ /// \brief Reset a bitmap builder
1601
+ ///
1602
+ /// Releases any memory held by buffer, empties the cache, and resets the size
1603
+ /// to zero
1604
+ static inline void ArrowBitmapReset(struct ArrowBitmap* bitmap);
1605
+
1606
+ /// @}
1607
+
1608
+ /// \defgroup nanoarrow-array Creating arrays
1609
+ ///
1610
+ /// These functions allocate, copy, and destroy ArrowArray structures.
1611
+ /// Once an ArrowArray has been initialized via ArrowArrayInitFromType()
1612
+ /// or ArrowArrayInitFromSchema(), the caller is responsible for releasing
1613
+ /// it using the embedded release callback.
1614
+ ///
1615
+ /// @{
1616
+
1617
+ /// \brief Initialize the fields of an array
1618
+ ///
1619
+ /// Initializes the fields and release callback of array. Caller
1620
+ /// is responsible for calling the array->release callback if
1621
+ /// NANOARROW_OK is returned.
1622
+ ArrowErrorCode ArrowArrayInitFromType(struct ArrowArray* array,
1623
+ enum ArrowType storage_type);
1624
+
1625
+ /// \brief Initialize the contents of an ArrowArray from an ArrowSchema
1626
+ ///
1627
+ /// Caller is responsible for calling the array->release callback if
1628
+ /// NANOARROW_OK is returned.
1629
+ ArrowErrorCode ArrowArrayInitFromSchema(struct ArrowArray* array,
1630
+ struct ArrowSchema* schema,
1631
+ struct ArrowError* error);
1632
+
1633
+ /// \brief Initialize the contents of an ArrowArray from an ArrowArrayView
1634
+ ///
1635
+ /// Caller is responsible for calling the array->release callback if
1636
+ /// NANOARROW_OK is returned.
1637
+ ArrowErrorCode ArrowArrayInitFromArrayView(struct ArrowArray* array,
1638
+ struct ArrowArrayView* array_view,
1639
+ struct ArrowError* error);
1640
+
1641
+ /// \brief Allocate the array->children array
1642
+ ///
1643
+ /// Includes the memory for each child struct ArrowArray,
1644
+ /// whose members are marked as released and may be subsequently initialized
1645
+ /// with ArrowArrayInitFromType() or moved from an existing ArrowArray.
1646
+ /// schema must have been allocated using ArrowArrayInitFromType().
1647
+ ArrowErrorCode ArrowArrayAllocateChildren(struct ArrowArray* array,
1648
+ int64_t n_children);
1649
+
1650
+ /// \brief Allocate the array->dictionary member
1651
+ ///
1652
+ /// Includes the memory for the struct ArrowArray, whose contents
1653
+ /// is marked as released and may be subsequently initialized
1654
+ /// with ArrowArrayInitFromType() or moved from an existing ArrowArray.
1655
+ /// array must have been allocated using ArrowArrayInitFromType()
1656
+ ArrowErrorCode ArrowArrayAllocateDictionary(struct ArrowArray* array);
1657
+
1658
+ /// \brief Set the validity bitmap of an ArrowArray
1659
+ ///
1660
+ /// array must have been allocated using ArrowArrayInitFromType()
1661
+ void ArrowArraySetValidityBitmap(struct ArrowArray* array,
1662
+ struct ArrowBitmap* bitmap);
1663
+
1664
+ /// \brief Set a buffer of an ArrowArray
1665
+ ///
1666
+ /// array must have been allocated using ArrowArrayInitFromType()
1667
+ ArrowErrorCode ArrowArraySetBuffer(struct ArrowArray* array, int64_t i,
1668
+ struct ArrowBuffer* buffer);
1669
+
1670
+ /// \brief Get the validity bitmap of an ArrowArray
1671
+ ///
1672
+ /// array must have been allocated using ArrowArrayInitFromType()
1673
+ static inline struct ArrowBitmap* ArrowArrayValidityBitmap(
1674
+ struct ArrowArray* array);
1675
+
1676
+ /// \brief Get a buffer of an ArrowArray
1677
+ ///
1678
+ /// array must have been allocated using ArrowArrayInitFromType()
1679
+ static inline struct ArrowBuffer* ArrowArrayBuffer(struct ArrowArray* array,
1680
+ int64_t i);
1681
+
1682
+ /// \brief Start element-wise appending to an ArrowArray
1683
+ ///
1684
+ /// Initializes any values needed to use ArrowArrayAppend*() functions.
1685
+ /// All element-wise appenders append by value and return EINVAL if the exact
1686
+ /// value cannot be represented by the underlying storage type. array must have
1687
+ /// been allocated using ArrowArrayInitFromType()
1688
+ static inline ArrowErrorCode ArrowArrayStartAppending(struct ArrowArray* array);
1689
+
1690
+ /// \brief Reserve space for future appends
1691
+ ///
1692
+ /// For buffer sizes that can be calculated (i.e., not string data buffers or
1693
+ /// child array sizes for non-fixed-size arrays), recursively reserve space for
1694
+ /// additional elements. This is useful for reducing the number of reallocations
1695
+ /// that occur using the item-wise appenders.
1696
+ ArrowErrorCode ArrowArrayReserve(struct ArrowArray* array,
1697
+ int64_t additional_size_elements);
1698
+
1699
+ /// \brief Append a null value to an array
1700
+ static inline ArrowErrorCode ArrowArrayAppendNull(struct ArrowArray* array,
1701
+ int64_t n);
1702
+
1703
+ /// \brief Append an empty, non-null value to an array
1704
+ static inline ArrowErrorCode ArrowArrayAppendEmpty(struct ArrowArray* array,
1705
+ int64_t n);
1706
+
1707
+ /// \brief Append a signed integer value to an array
1708
+ ///
1709
+ /// Returns NANOARROW_OK if value can be exactly represented by
1710
+ /// the underlying storage type or EINVAL otherwise (e.g., value
1711
+ /// is outside the valid array range).
1712
+ static inline ArrowErrorCode ArrowArrayAppendInt(struct ArrowArray* array,
1713
+ int64_t value);
1714
+
1715
+ /// \brief Append an unsigned integer value to an array
1716
+ ///
1717
+ /// Returns NANOARROW_OK if value can be exactly represented by
1718
+ /// the underlying storage type or EINVAL otherwise (e.g., value
1719
+ /// is outside the valid array range).
1720
+ static inline ArrowErrorCode ArrowArrayAppendUInt(struct ArrowArray* array,
1721
+ uint64_t value);
1722
+
1723
+ /// \brief Append a double value to an array
1724
+ ///
1725
+ /// Returns NANOARROW_OK if value can be exactly represented by
1726
+ /// the underlying storage type or EINVAL otherwise (e.g., value
1727
+ /// is outside the valid array range or there is an attempt to append
1728
+ /// a non-integer to an array with an integer storage type).
1729
+ static inline ArrowErrorCode ArrowArrayAppendDouble(struct ArrowArray* array,
1730
+ double value);
1731
+
1732
+ /// \brief Append a string of bytes to an array
1733
+ ///
1734
+ /// Returns NANOARROW_OK if value can be exactly represented by
1735
+ /// the underlying storage type, EOVERFLOW if appending value would overflow
1736
+ /// the offset type (e.g., if the data buffer would be larger than 2 GB for a
1737
+ /// non-large string type), or EINVAL otherwise (e.g., the underlying array is
1738
+ /// not a binary, string, large binary, large string, or fixed-size binary
1739
+ /// array, or value is the wrong size for a fixed-size binary array).
1740
+ static inline ArrowErrorCode ArrowArrayAppendBytes(
1741
+ struct ArrowArray* array, struct ArrowBufferView value);
1742
+
1743
+ /// \brief Append a string value to an array
1744
+ ///
1745
+ /// Returns NANOARROW_OK if value can be exactly represented by
1746
+ /// the underlying storage type, EOVERFLOW if appending value would overflow
1747
+ /// the offset type (e.g., if the data buffer would be larger than 2 GB for a
1748
+ /// non-large string type), or EINVAL otherwise (e.g., the underlying array is
1749
+ /// not a string or large string array).
1750
+ static inline ArrowErrorCode ArrowArrayAppendString(
1751
+ struct ArrowArray* array, struct ArrowStringView value);
1752
+
1753
+ /// \brief Append a Interval to an array
1754
+ ///
1755
+ /// Returns NANOARROW_OK if value can be exactly represented by
1756
+ /// the underlying storage type or EINVAL otherwise.
1757
+ static inline ArrowErrorCode ArrowArrayAppendInterval(
1758
+ struct ArrowArray* array, struct ArrowInterval* value);
1759
+
1760
+ /// \brief Append a decimal value to an array
1761
+ ///
1762
+ /// Returns NANOARROW_OK if array is a decimal array with the appropriate
1763
+ /// bitwidth or EINVAL otherwise.
1764
+ static inline ArrowErrorCode ArrowArrayAppendDecimal(
1765
+ struct ArrowArray* array, struct ArrowDecimal* value);
1766
+
1767
+ /// \brief Finish a nested array element
1768
+ ///
1769
+ /// Appends a non-null element to the array based on the first child's current
1770
+ /// length. Returns NANOARROW_OK if the item was successfully added, EOVERFLOW
1771
+ /// if the child of a list or map array would exceed INT_MAX elements, or EINVAL
1772
+ /// if the underlying storage type is not a struct, list, large list, or
1773
+ /// fixed-size list, or if there was an attempt to add a struct or fixed-size
1774
+ /// list element where the length of the child array(s) did not match the
1775
+ /// expected length.
1776
+ static inline ArrowErrorCode ArrowArrayFinishElement(struct ArrowArray* array);
1777
+
1778
+ /// \brief Finish a union array element
1779
+ ///
1780
+ /// Appends an element to the union type ids buffer and increments
1781
+ /// array->length. For sparse unions, up to one element is added to non type-id
1782
+ /// children. Returns EINVAL if the underlying storage type is not a union, if
1783
+ /// type_id is not valid, or if child sizes after appending are inconsistent.
1784
+ static inline ArrowErrorCode ArrowArrayFinishUnionElement(
1785
+ struct ArrowArray* array, int8_t type_id);
1786
+
1787
+ /// \brief Shrink buffer capacity to the size required
1788
+ ///
1789
+ /// Also applies shrinking to any child arrays. array must have been allocated
1790
+ /// using ArrowArrayInitFromType
1791
+ static inline ArrowErrorCode ArrowArrayShrinkToFit(struct ArrowArray* array);
1792
+
1793
+ /// \brief Finish building an ArrowArray
1794
+ ///
1795
+ /// Flushes any pointers from internal buffers that may have been reallocated
1796
+ /// into array->buffers and checks the actual size of the buffers
1797
+ /// against the expected size based on the final length.
1798
+ /// array must have been allocated using ArrowArrayInitFromType()
1799
+ ArrowErrorCode ArrowArrayFinishBuildingDefault(struct ArrowArray* array,
1800
+ struct ArrowError* error);
1801
+
1802
+ /// \brief Finish building an ArrowArray with explicit validation
1803
+ ///
1804
+ /// Finish building with an explicit validation level. This could perform less
1805
+ /// validation (i.e. NANOARROW_VALIDATION_LEVEL_NONE or
1806
+ /// NANOARROW_VALIDATION_LEVEL_MINIMAL) if CPU buffer data access is not
1807
+ /// possible or more validation (i.e., NANOARROW_VALIDATION_LEVEL_FULL) if
1808
+ /// buffer content was obtained from an untrusted or corruptible source.
1809
+ ArrowErrorCode ArrowArrayFinishBuilding(
1810
+ struct ArrowArray* array, enum ArrowValidationLevel validation_level,
1811
+ struct ArrowError* error);
1812
+
1813
+ /// @}
1814
+
1815
+ /// \defgroup nanoarrow-array-view Reading arrays
1816
+ ///
1817
+ /// These functions read and validate the contents ArrowArray structures.
1818
+ ///
1819
+ /// @{
1820
+
1821
+ /// \brief Initialize the contents of an ArrowArrayView
1822
+ void ArrowArrayViewInitFromType(struct ArrowArrayView* array_view,
1823
+ enum ArrowType storage_type);
1824
+
1825
+ /// \brief Move an ArrowArrayView
1826
+ ///
1827
+ /// Transfers the ArrowArrayView data and lifecycle management to another
1828
+ /// address and resets the contents of src.
1829
+ static inline void ArrowArrayViewMove(struct ArrowArrayView* src,
1830
+ struct ArrowArrayView* dst);
1831
+
1832
+ /// \brief Initialize the contents of an ArrowArrayView from an ArrowSchema
1833
+ ArrowErrorCode ArrowArrayViewInitFromSchema(struct ArrowArrayView* array_view,
1834
+ struct ArrowSchema* schema,
1835
+ struct ArrowError* error);
1836
+
1837
+ /// \brief Allocate the array_view->children array
1838
+ ///
1839
+ /// Includes the memory for each child struct ArrowArrayView
1840
+ ArrowErrorCode ArrowArrayViewAllocateChildren(struct ArrowArrayView* array_view,
1841
+ int64_t n_children);
1842
+
1843
+ /// \brief Allocate array_view->dictionary
1844
+ ArrowErrorCode ArrowArrayViewAllocateDictionary(
1845
+ struct ArrowArrayView* array_view);
1846
+
1847
+ /// \brief Set data-independent buffer sizes from length
1848
+ void ArrowArrayViewSetLength(struct ArrowArrayView* array_view, int64_t length);
1849
+
1850
+ /// \brief Set buffer sizes and data pointers from an ArrowArray
1851
+ ArrowErrorCode ArrowArrayViewSetArray(struct ArrowArrayView* array_view,
1852
+ struct ArrowArray* array,
1853
+ struct ArrowError* error);
1854
+
1855
+ /// \brief Set buffer sizes and data pointers from an ArrowArray except for
1856
+ /// those that require dereferencing buffer content.
1857
+ ArrowErrorCode ArrowArrayViewSetArrayMinimal(struct ArrowArrayView* array_view,
1858
+ struct ArrowArray* array,
1859
+ struct ArrowError* error);
1860
+
1861
+ /// \brief Performs checks on the content of an ArrowArrayView
1862
+ ///
1863
+ /// If using ArrowArrayViewSetArray() to back array_view with an ArrowArray,
1864
+ /// the buffer sizes and some content (fist and last offset) have already
1865
+ /// been validated at the "default" level. If setting the buffer pointers
1866
+ /// and sizes otherwise, you may wish to perform checks at a different level.
1867
+ /// See documentation for ArrowValidationLevel for the details of checks
1868
+ /// performed at each level.
1869
+ ArrowErrorCode ArrowArrayViewValidate(
1870
+ struct ArrowArrayView* array_view,
1871
+ enum ArrowValidationLevel validation_level, struct ArrowError* error);
1872
+
1873
+ /// \brief Reset the contents of an ArrowArrayView and frees resources
1874
+ void ArrowArrayViewReset(struct ArrowArrayView* array_view);
1875
+
1876
+ /// \brief Check for a null element in an ArrowArrayView
1877
+ static inline int8_t ArrowArrayViewIsNull(struct ArrowArrayView* array_view,
1878
+ int64_t i);
1879
+
1880
+ /// \brief Get the type id of a union array element
1881
+ static inline int8_t ArrowArrayViewUnionTypeId(
1882
+ struct ArrowArrayView* array_view, int64_t i);
1883
+
1884
+ /// \brief Get the child index of a union array element
1885
+ static inline int8_t ArrowArrayViewUnionChildIndex(
1886
+ struct ArrowArrayView* array_view, int64_t i);
1887
+
1888
+ /// \brief Get the index to use into the relevant union child array
1889
+ static inline int64_t ArrowArrayViewUnionChildOffset(
1890
+ struct ArrowArrayView* array_view, int64_t i);
1891
+
1892
+ /// \brief Get an element in an ArrowArrayView as an integer
1893
+ ///
1894
+ /// This function does not check for null values, that values are actually
1895
+ /// integers, or that values are within a valid range for an int64.
1896
+ static inline int64_t ArrowArrayViewGetIntUnsafe(
1897
+ struct ArrowArrayView* array_view, int64_t i);
1898
+
1899
+ /// \brief Get an element in an ArrowArrayView as an unsigned integer
1900
+ ///
1901
+ /// This function does not check for null values, that values are actually
1902
+ /// integers, or that values are within a valid range for a uint64.
1903
+ static inline uint64_t ArrowArrayViewGetUIntUnsafe(
1904
+ struct ArrowArrayView* array_view, int64_t i);
1905
+
1906
+ /// \brief Get an element in an ArrowArrayView as a double
1907
+ ///
1908
+ /// This function does not check for null values, or
1909
+ /// that values are within a valid range for a double.
1910
+ static inline double ArrowArrayViewGetDoubleUnsafe(
1911
+ struct ArrowArrayView* array_view, int64_t i);
1912
+
1913
+ /// \brief Get an element in an ArrowArrayView as an ArrowStringView
1914
+ ///
1915
+ /// This function does not check for null values.
1916
+ static inline struct ArrowStringView ArrowArrayViewGetStringUnsafe(
1917
+ struct ArrowArrayView* array_view, int64_t i);
1918
+
1919
+ /// \brief Get an element in an ArrowArrayView as an ArrowBufferView
1920
+ ///
1921
+ /// This function does not check for null values.
1922
+ static inline struct ArrowBufferView ArrowArrayViewGetBytesUnsafe(
1923
+ struct ArrowArrayView* array_view, int64_t i);
1924
+
1925
+ /// \brief Get an element in an ArrowArrayView as an ArrowDecimal
1926
+ ///
1927
+ /// This function does not check for null values. The out parameter must
1928
+ /// be initialized with ArrowDecimalInit() with the proper parameters for this
1929
+ /// type before calling this for the first time.
1930
+ static inline void ArrowArrayViewGetDecimalUnsafe(
1931
+ struct ArrowArrayView* array_view, int64_t i, struct ArrowDecimal* out);
1932
+
1933
+ /// @}
1934
+
1935
+ /// \defgroup nanoarrow-basic-array-stream Basic ArrowArrayStream implementation
1936
+ ///
1937
+ /// An implementation of an ArrowArrayStream based on a collection of
1938
+ /// zero or more previously-existing ArrowArray objects. Users should
1939
+ /// initialize and/or validate the contents before transferring the
1940
+ /// responsibility of the ArrowArrayStream elsewhere.
1941
+ ///
1942
+ /// @{
1943
+
1944
+ /// \brief Initialize an ArrowArrayStream backed by this implementation
1945
+ ///
1946
+ /// This function moves the ownership of schema to the array_stream. If
1947
+ /// this function returns NANOARROW_OK, the caller is responsible for
1948
+ /// releasing the ArrowArrayStream.
1949
+ ArrowErrorCode ArrowBasicArrayStreamInit(struct ArrowArrayStream* array_stream,
1950
+ struct ArrowSchema* schema,
1951
+ int64_t n_arrays);
1952
+
1953
+ /// \brief Set the ith ArrowArray in this ArrowArrayStream.
1954
+ ///
1955
+ /// array_stream must have been initialized with ArrowBasicArrayStreamInit().
1956
+ /// This function move the ownership of array to the array_stream. i must
1957
+ /// be greater than zero and less than the value of n_arrays passed in
1958
+ /// ArrowBasicArrayStreamInit(). Callers are not required to fill all
1959
+ /// n_arrays members (i.e., n_arrays is a maximum bound).
1960
+ void ArrowBasicArrayStreamSetArray(struct ArrowArrayStream* array_stream,
1961
+ int64_t i, struct ArrowArray* array);
1962
+
1963
+ /// \brief Validate the contents of this ArrowArrayStream
1964
+ ///
1965
+ /// array_stream must have been initialized with ArrowBasicArrayStreamInit().
1966
+ /// This function uses ArrowArrayStreamInitFromSchema() and
1967
+ /// ArrowArrayStreamSetArray() to validate the contents of the arrays.
1968
+ ArrowErrorCode ArrowBasicArrayStreamValidate(
1969
+ struct ArrowArrayStream* array_stream, struct ArrowError* error);
1970
+
1971
+ /// @}
1972
+
1973
+ // Inline function definitions
1974
+
1975
+ #ifdef __cplusplus
1976
+ }
1977
+ #endif
1978
+
1979
+ #endif
1980
+ // Licensed to the Apache Software Foundation (ASF) under one
1981
+ // or more contributor license agreements. See the NOTICE file
1982
+ // distributed with this work for additional information
1983
+ // regarding copyright ownership. The ASF licenses this file
1984
+ // to you under the Apache License, Version 2.0 (the
1985
+ // "License"); you may not use this file except in compliance
1986
+ // with the License. You may obtain a copy of the License at
1987
+ //
1988
+ // http://www.apache.org/licenses/LICENSE-2.0
1989
+ //
1990
+ // Unless required by applicable law or agreed to in writing,
1991
+ // software distributed under the License is distributed on an
1992
+ // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1993
+ // KIND, either express or implied. See the License for the
1994
+ // specific language governing permissions and limitations
1995
+ // under the License.
1996
+
1997
+ #ifndef NANOARROW_BUFFER_INLINE_H_INCLUDED
1998
+ #define NANOARROW_BUFFER_INLINE_H_INCLUDED
1999
+
2000
+ #include <errno.h>
2001
+ #include <stdint.h>
2002
+ #include <string.h>
2003
+
2004
+ #ifdef __cplusplus
2005
+ extern "C" {
2006
+ #endif
2007
+
2008
+ static inline int64_t _ArrowGrowByFactor(int64_t current_capacity,
2009
+ int64_t new_capacity) {
2010
+ int64_t doubled_capacity = current_capacity * 2;
2011
+ if (doubled_capacity > new_capacity) {
2012
+ return doubled_capacity;
2013
+ } else {
2014
+ return new_capacity;
2015
+ }
2016
+ }
2017
+
2018
+ static inline void ArrowBufferInit(struct ArrowBuffer* buffer) {
2019
+ buffer->data = NULL;
2020
+ buffer->size_bytes = 0;
2021
+ buffer->capacity_bytes = 0;
2022
+ buffer->allocator = ArrowBufferAllocatorDefault();
2023
+ }
2024
+
2025
+ static inline ArrowErrorCode ArrowBufferSetAllocator(
2026
+ struct ArrowBuffer* buffer, struct ArrowBufferAllocator allocator) {
2027
+ if (buffer->data == NULL) {
2028
+ buffer->allocator = allocator;
2029
+ return NANOARROW_OK;
2030
+ } else {
2031
+ return EINVAL;
2032
+ }
2033
+ }
2034
+
2035
+ static inline void ArrowBufferReset(struct ArrowBuffer* buffer) {
2036
+ if (buffer->data != NULL) {
2037
+ buffer->allocator.free(&buffer->allocator, (uint8_t*)buffer->data,
2038
+ buffer->capacity_bytes);
2039
+ buffer->data = NULL;
2040
+ }
2041
+
2042
+ buffer->capacity_bytes = 0;
2043
+ buffer->size_bytes = 0;
2044
+ }
2045
+
2046
+ static inline void ArrowBufferMove(struct ArrowBuffer* src,
2047
+ struct ArrowBuffer* dst) {
2048
+ memcpy(dst, src, sizeof(struct ArrowBuffer));
2049
+ src->data = NULL;
2050
+ ArrowBufferReset(src);
2051
+ }
2052
+
2053
+ static inline ArrowErrorCode ArrowBufferResize(struct ArrowBuffer* buffer,
2054
+ int64_t new_capacity_bytes,
2055
+ char shrink_to_fit) {
2056
+ if (new_capacity_bytes < 0) {
2057
+ return EINVAL;
2058
+ }
2059
+
2060
+ if (new_capacity_bytes > buffer->capacity_bytes || shrink_to_fit) {
2061
+ buffer->data = buffer->allocator.reallocate(
2062
+ &buffer->allocator, buffer->data, buffer->capacity_bytes,
2063
+ new_capacity_bytes);
2064
+ if (buffer->data == NULL && new_capacity_bytes > 0) {
2065
+ buffer->capacity_bytes = 0;
2066
+ buffer->size_bytes = 0;
2067
+ return ENOMEM;
2068
+ }
2069
+
2070
+ buffer->capacity_bytes = new_capacity_bytes;
2071
+ }
2072
+
2073
+ // Ensures that when shrinking that size <= capacity
2074
+ if (new_capacity_bytes < buffer->size_bytes) {
2075
+ buffer->size_bytes = new_capacity_bytes;
2076
+ }
2077
+
2078
+ return NANOARROW_OK;
2079
+ }
2080
+
2081
+ static inline ArrowErrorCode ArrowBufferReserve(struct ArrowBuffer* buffer,
2082
+ int64_t additional_size_bytes) {
2083
+ int64_t min_capacity_bytes = buffer->size_bytes + additional_size_bytes;
2084
+ if (min_capacity_bytes <= buffer->capacity_bytes) {
2085
+ return NANOARROW_OK;
2086
+ }
2087
+
2088
+ return ArrowBufferResize(
2089
+ buffer, _ArrowGrowByFactor(buffer->capacity_bytes, min_capacity_bytes),
2090
+ 0);
2091
+ }
2092
+
2093
+ static inline void ArrowBufferAppendUnsafe(struct ArrowBuffer* buffer,
2094
+ const void* data,
2095
+ int64_t size_bytes) {
2096
+ if (size_bytes > 0) {
2097
+ memcpy(buffer->data + buffer->size_bytes, data, size_bytes);
2098
+ buffer->size_bytes += size_bytes;
2099
+ }
2100
+ }
2101
+
2102
+ static inline ArrowErrorCode ArrowBufferAppend(struct ArrowBuffer* buffer,
2103
+ const void* data,
2104
+ int64_t size_bytes) {
2105
+ NANOARROW_RETURN_NOT_OK(ArrowBufferReserve(buffer, size_bytes));
2106
+
2107
+ ArrowBufferAppendUnsafe(buffer, data, size_bytes);
2108
+ return NANOARROW_OK;
2109
+ }
2110
+
2111
+ static inline ArrowErrorCode ArrowBufferAppendInt8(struct ArrowBuffer* buffer,
2112
+ int8_t value) {
2113
+ return ArrowBufferAppend(buffer, &value, sizeof(int8_t));
2114
+ }
2115
+
2116
+ static inline ArrowErrorCode ArrowBufferAppendUInt8(struct ArrowBuffer* buffer,
2117
+ uint8_t value) {
2118
+ return ArrowBufferAppend(buffer, &value, sizeof(uint8_t));
2119
+ }
2120
+
2121
+ static inline ArrowErrorCode ArrowBufferAppendInt16(struct ArrowBuffer* buffer,
2122
+ int16_t value) {
2123
+ return ArrowBufferAppend(buffer, &value, sizeof(int16_t));
2124
+ }
2125
+
2126
+ static inline ArrowErrorCode ArrowBufferAppendUInt16(struct ArrowBuffer* buffer,
2127
+ uint16_t value) {
2128
+ return ArrowBufferAppend(buffer, &value, sizeof(uint16_t));
2129
+ }
2130
+
2131
+ static inline ArrowErrorCode ArrowBufferAppendInt32(struct ArrowBuffer* buffer,
2132
+ int32_t value) {
2133
+ return ArrowBufferAppend(buffer, &value, sizeof(int32_t));
2134
+ }
2135
+
2136
+ static inline ArrowErrorCode ArrowBufferAppendUInt32(struct ArrowBuffer* buffer,
2137
+ uint32_t value) {
2138
+ return ArrowBufferAppend(buffer, &value, sizeof(uint32_t));
2139
+ }
2140
+
2141
+ static inline ArrowErrorCode ArrowBufferAppendInt64(struct ArrowBuffer* buffer,
2142
+ int64_t value) {
2143
+ return ArrowBufferAppend(buffer, &value, sizeof(int64_t));
2144
+ }
2145
+
2146
+ static inline ArrowErrorCode ArrowBufferAppendUInt64(struct ArrowBuffer* buffer,
2147
+ uint64_t value) {
2148
+ return ArrowBufferAppend(buffer, &value, sizeof(uint64_t));
2149
+ }
2150
+
2151
+ static inline ArrowErrorCode ArrowBufferAppendDouble(struct ArrowBuffer* buffer,
2152
+ double value) {
2153
+ return ArrowBufferAppend(buffer, &value, sizeof(double));
2154
+ }
2155
+
2156
+ static inline ArrowErrorCode ArrowBufferAppendFloat(struct ArrowBuffer* buffer,
2157
+ float value) {
2158
+ return ArrowBufferAppend(buffer, &value, sizeof(float));
2159
+ }
2160
+
2161
+ static inline ArrowErrorCode ArrowBufferAppendStringView(
2162
+ struct ArrowBuffer* buffer, struct ArrowStringView value) {
2163
+ return ArrowBufferAppend(buffer, value.data, value.size_bytes);
2164
+ }
2165
+
2166
+ static inline ArrowErrorCode ArrowBufferAppendBufferView(
2167
+ struct ArrowBuffer* buffer, struct ArrowBufferView value) {
2168
+ return ArrowBufferAppend(buffer, value.data.data, value.size_bytes);
2169
+ }
2170
+
2171
+ static inline ArrowErrorCode ArrowBufferAppendFill(struct ArrowBuffer* buffer,
2172
+ uint8_t value,
2173
+ int64_t size_bytes) {
2174
+ NANOARROW_RETURN_NOT_OK(ArrowBufferReserve(buffer, size_bytes));
2175
+
2176
+ memset(buffer->data + buffer->size_bytes, value, size_bytes);
2177
+ buffer->size_bytes += size_bytes;
2178
+ return NANOARROW_OK;
2179
+ }
2180
+
2181
+ static const uint8_t _ArrowkBitmask[] = {1, 2, 4, 8, 16, 32, 64, 128};
2182
+ static const uint8_t _ArrowkFlippedBitmask[] = {254, 253, 251, 247,
2183
+ 239, 223, 191, 127};
2184
+ static const uint8_t _ArrowkPrecedingBitmask[] = {0, 1, 3, 7, 15, 31, 63, 127};
2185
+ static const uint8_t _ArrowkTrailingBitmask[] = {255, 254, 252, 248,
2186
+ 240, 224, 192, 128};
2187
+
2188
+ static const uint8_t _ArrowkBytePopcount[] = {
2189
+ 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4,
2190
+ 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2191
+ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4,
2192
+ 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2193
+ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6,
2194
+ 4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2195
+ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5,
2196
+ 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
2197
+ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6,
2198
+ 4, 5, 5, 6, 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
2199
+ 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8};
2200
+
2201
+ static inline int64_t _ArrowRoundUpToMultipleOf8(int64_t value) {
2202
+ return (value + 7) & ~((int64_t)7);
2203
+ }
2204
+
2205
+ static inline int64_t _ArrowRoundDownToMultipleOf8(int64_t value) {
2206
+ return (value / 8) * 8;
2207
+ }
2208
+
2209
+ static inline int64_t _ArrowBytesForBits(int64_t bits) {
2210
+ return (bits >> 3) + ((bits & 7) != 0);
2211
+ }
2212
+
2213
+ static inline void _ArrowBitsUnpackInt8(const uint8_t word, int8_t* out) {
2214
+ out[0] = (word >> 0) & 1;
2215
+ out[1] = (word >> 1) & 1;
2216
+ out[2] = (word >> 2) & 1;
2217
+ out[3] = (word >> 3) & 1;
2218
+ out[4] = (word >> 4) & 1;
2219
+ out[5] = (word >> 5) & 1;
2220
+ out[6] = (word >> 6) & 1;
2221
+ out[7] = (word >> 7) & 1;
2222
+ }
2223
+
2224
+ static inline void _ArrowBitsUnpackInt32(const uint8_t word, int32_t* out) {
2225
+ out[0] = (word >> 0) & 1;
2226
+ out[1] = (word >> 1) & 1;
2227
+ out[2] = (word >> 2) & 1;
2228
+ out[3] = (word >> 3) & 1;
2229
+ out[4] = (word >> 4) & 1;
2230
+ out[5] = (word >> 5) & 1;
2231
+ out[6] = (word >> 6) & 1;
2232
+ out[7] = (word >> 7) & 1;
2233
+ }
2234
+
2235
+ static inline void _ArrowBitmapPackInt8(const int8_t* values, uint8_t* out) {
2236
+ *out = (values[0] | values[1] << 1 | values[2] << 2 | values[3] << 3 |
2237
+ values[4] << 4 | values[5] << 5 | values[6] << 6 | values[7] << 7);
2238
+ }
2239
+
2240
+ static inline void _ArrowBitmapPackInt32(const int32_t* values, uint8_t* out) {
2241
+ *out = (values[0] | values[1] << 1 | values[2] << 2 | values[3] << 3 |
2242
+ values[4] << 4 | values[5] << 5 | values[6] << 6 | values[7] << 7);
2243
+ }
2244
+
2245
+ static inline int8_t ArrowBitGet(const uint8_t* bits, int64_t i) {
2246
+ return (bits[i >> 3] >> (i & 0x07)) & 1;
2247
+ }
2248
+
2249
+ static inline void ArrowBitsUnpackInt8(const uint8_t* bits,
2250
+ int64_t start_offset, int64_t length,
2251
+ int8_t* out) {
2252
+ if (length == 0) {
2253
+ return;
2254
+ }
2255
+
2256
+ const int64_t i_begin = start_offset;
2257
+ const int64_t i_end = start_offset + length;
2258
+ const int64_t i_last_valid = i_end - 1;
2259
+
2260
+ const int64_t bytes_begin = i_begin / 8;
2261
+ const int64_t bytes_last_valid = i_last_valid / 8;
2262
+
2263
+ if (bytes_begin == bytes_last_valid) {
2264
+ for (int i = 0; i < length; i++) {
2265
+ out[i] = ArrowBitGet(&bits[bytes_begin], i + i_begin % 8);
2266
+ }
2267
+
2268
+ return;
2269
+ }
2270
+
2271
+ // first byte
2272
+ for (int i = 0; i < 8 - (i_begin % 8); i++) {
2273
+ *out++ = ArrowBitGet(&bits[bytes_begin], i + i_begin % 8);
2274
+ }
2275
+
2276
+ // middle bytes
2277
+ for (int64_t i = bytes_begin + 1; i < bytes_last_valid; i++) {
2278
+ _ArrowBitsUnpackInt8(bits[i], out);
2279
+ out += 8;
2280
+ }
2281
+
2282
+ // last byte
2283
+ const int bits_remaining = i_end % 8 == 0 ? 8 : i_end % 8;
2284
+ for (int i = 0; i < bits_remaining; i++) {
2285
+ *out++ = ArrowBitGet(&bits[bytes_last_valid], i);
2286
+ }
2287
+ }
2288
+
2289
+ static inline void ArrowBitsUnpackInt32(const uint8_t* bits,
2290
+ int64_t start_offset, int64_t length,
2291
+ int32_t* out) {
2292
+ if (length == 0) {
2293
+ return;
2294
+ }
2295
+
2296
+ const int64_t i_begin = start_offset;
2297
+ const int64_t i_end = start_offset + length;
2298
+ const int64_t i_last_valid = i_end - 1;
2299
+
2300
+ const int64_t bytes_begin = i_begin / 8;
2301
+ const int64_t bytes_last_valid = i_last_valid / 8;
2302
+
2303
+ if (bytes_begin == bytes_last_valid) {
2304
+ for (int i = 0; i < length; i++) {
2305
+ out[i] = ArrowBitGet(&bits[bytes_begin], i + i_begin % 8);
2306
+ }
2307
+
2308
+ return;
2309
+ }
2310
+
2311
+ // first byte
2312
+ for (int i = 0; i < 8 - (i_begin % 8); i++) {
2313
+ *out++ = ArrowBitGet(&bits[bytes_begin], i + i_begin % 8);
2314
+ }
2315
+
2316
+ // middle bytes
2317
+ for (int64_t i = bytes_begin + 1; i < bytes_last_valid; i++) {
2318
+ _ArrowBitsUnpackInt32(bits[i], out);
2319
+ out += 8;
2320
+ }
2321
+
2322
+ // last byte
2323
+ const int bits_remaining = i_end % 8 == 0 ? 8 : i_end % 8;
2324
+ for (int i = 0; i < bits_remaining; i++) {
2325
+ *out++ = ArrowBitGet(&bits[bytes_last_valid], i);
2326
+ }
2327
+ }
2328
+
2329
+ static inline void ArrowBitSet(uint8_t* bits, int64_t i) {
2330
+ bits[i / 8] |= _ArrowkBitmask[i % 8];
2331
+ }
2332
+
2333
+ static inline void ArrowBitClear(uint8_t* bits, int64_t i) {
2334
+ bits[i / 8] &= _ArrowkFlippedBitmask[i % 8];
2335
+ }
2336
+
2337
+ static inline void ArrowBitSetTo(uint8_t* bits, int64_t i, uint8_t bit_is_set) {
2338
+ bits[i / 8] ^= ((uint8_t)(-((uint8_t)(bit_is_set != 0)) ^ bits[i / 8])) &
2339
+ _ArrowkBitmask[i % 8];
2340
+ }
2341
+
2342
+ static inline void ArrowBitsSetTo(uint8_t* bits, int64_t start_offset,
2343
+ int64_t length, uint8_t bits_are_set) {
2344
+ const int64_t i_begin = start_offset;
2345
+ const int64_t i_end = start_offset + length;
2346
+ const uint8_t fill_byte = (uint8_t)(-bits_are_set);
2347
+
2348
+ const int64_t bytes_begin = i_begin / 8;
2349
+ const int64_t bytes_end = i_end / 8 + 1;
2350
+
2351
+ const uint8_t first_byte_mask = _ArrowkPrecedingBitmask[i_begin % 8];
2352
+ const uint8_t last_byte_mask = _ArrowkTrailingBitmask[i_end % 8];
2353
+
2354
+ if (bytes_end == bytes_begin + 1) {
2355
+ // set bits within a single byte
2356
+ const uint8_t only_byte_mask =
2357
+ i_end % 8 == 0 ? first_byte_mask
2358
+ : (uint8_t)(first_byte_mask | last_byte_mask);
2359
+ bits[bytes_begin] &= only_byte_mask;
2360
+ bits[bytes_begin] |= (uint8_t)(fill_byte & ~only_byte_mask);
2361
+ return;
2362
+ }
2363
+
2364
+ // set/clear trailing bits of first byte
2365
+ bits[bytes_begin] &= first_byte_mask;
2366
+ bits[bytes_begin] |= (uint8_t)(fill_byte & ~first_byte_mask);
2367
+
2368
+ if (bytes_end - bytes_begin > 2) {
2369
+ // set/clear whole bytes
2370
+ memset(bits + bytes_begin + 1, fill_byte,
2371
+ (size_t)(bytes_end - bytes_begin - 2));
2372
+ }
2373
+
2374
+ if (i_end % 8 == 0) {
2375
+ return;
2376
+ }
2377
+
2378
+ // set/clear leading bits of last byte
2379
+ bits[bytes_end - 1] &= last_byte_mask;
2380
+ bits[bytes_end - 1] |= (uint8_t)(fill_byte & ~last_byte_mask);
2381
+ }
2382
+
2383
+ static inline int64_t ArrowBitCountSet(const uint8_t* bits,
2384
+ int64_t start_offset, int64_t length) {
2385
+ if (length == 0) {
2386
+ return 0;
2387
+ }
2388
+
2389
+ const int64_t i_begin = start_offset;
2390
+ const int64_t i_end = start_offset + length;
2391
+ const int64_t i_last_valid = i_end - 1;
2392
+
2393
+ const int64_t bytes_begin = i_begin / 8;
2394
+ const int64_t bytes_last_valid = i_last_valid / 8;
2395
+
2396
+ if (bytes_begin == bytes_last_valid) {
2397
+ // count bits within a single byte
2398
+ const uint8_t first_byte_mask = _ArrowkPrecedingBitmask[i_end % 8];
2399
+ const uint8_t last_byte_mask = _ArrowkTrailingBitmask[i_begin % 8];
2400
+
2401
+ const uint8_t only_byte_mask =
2402
+ i_end % 8 == 0 ? last_byte_mask
2403
+ : (uint8_t)(first_byte_mask & last_byte_mask);
2404
+
2405
+ const uint8_t byte_masked = bits[bytes_begin] & only_byte_mask;
2406
+ return _ArrowkBytePopcount[byte_masked];
2407
+ }
2408
+
2409
+ const uint8_t first_byte_mask = _ArrowkPrecedingBitmask[i_begin % 8];
2410
+ const uint8_t last_byte_mask =
2411
+ i_end % 8 == 0 ? 0 : _ArrowkTrailingBitmask[i_end % 8];
2412
+ int64_t count = 0;
2413
+
2414
+ // first byte
2415
+ count += _ArrowkBytePopcount[bits[bytes_begin] & ~first_byte_mask];
2416
+
2417
+ // middle bytes
2418
+ for (int64_t i = bytes_begin + 1; i < bytes_last_valid; i++) {
2419
+ count += _ArrowkBytePopcount[bits[i]];
2420
+ }
2421
+
2422
+ // last byte
2423
+ count += _ArrowkBytePopcount[bits[bytes_last_valid] & ~last_byte_mask];
2424
+
2425
+ return count;
2426
+ }
2427
+
2428
+ static inline void ArrowBitmapInit(struct ArrowBitmap* bitmap) {
2429
+ ArrowBufferInit(&bitmap->buffer);
2430
+ bitmap->size_bits = 0;
2431
+ }
2432
+
2433
+ static inline void ArrowBitmapMove(struct ArrowBitmap* src,
2434
+ struct ArrowBitmap* dst) {
2435
+ ArrowBufferMove(&src->buffer, &dst->buffer);
2436
+ dst->size_bits = src->size_bits;
2437
+ src->size_bits = 0;
2438
+ }
2439
+
2440
+ static inline ArrowErrorCode ArrowBitmapReserve(struct ArrowBitmap* bitmap,
2441
+ int64_t additional_size_bits) {
2442
+ int64_t min_capacity_bits = bitmap->size_bits + additional_size_bits;
2443
+ if (min_capacity_bits <= (bitmap->buffer.capacity_bytes * 8)) {
2444
+ return NANOARROW_OK;
2445
+ }
2446
+
2447
+ NANOARROW_RETURN_NOT_OK(ArrowBufferReserve(
2448
+ &bitmap->buffer, _ArrowBytesForBits(additional_size_bits)));
2449
+
2450
+ bitmap->buffer.data[bitmap->buffer.capacity_bytes - 1] = 0;
2451
+ return NANOARROW_OK;
2452
+ }
2453
+
2454
+ static inline ArrowErrorCode ArrowBitmapResize(struct ArrowBitmap* bitmap,
2455
+ int64_t new_capacity_bits,
2456
+ char shrink_to_fit) {
2457
+ if (new_capacity_bits < 0) {
2458
+ return EINVAL;
2459
+ }
2460
+
2461
+ int64_t new_capacity_bytes = _ArrowBytesForBits(new_capacity_bits);
2462
+ NANOARROW_RETURN_NOT_OK(
2463
+ ArrowBufferResize(&bitmap->buffer, new_capacity_bytes, shrink_to_fit));
2464
+
2465
+ if (new_capacity_bits < bitmap->size_bits) {
2466
+ bitmap->size_bits = new_capacity_bits;
2467
+ }
2468
+
2469
+ return NANOARROW_OK;
2470
+ }
2471
+
2472
+ static inline ArrowErrorCode ArrowBitmapAppend(struct ArrowBitmap* bitmap,
2473
+ uint8_t bits_are_set,
2474
+ int64_t length) {
2475
+ NANOARROW_RETURN_NOT_OK(ArrowBitmapReserve(bitmap, length));
2476
+
2477
+ ArrowBitmapAppendUnsafe(bitmap, bits_are_set, length);
2478
+ return NANOARROW_OK;
2479
+ }
2480
+
2481
+ static inline void ArrowBitmapAppendUnsafe(struct ArrowBitmap* bitmap,
2482
+ uint8_t bits_are_set,
2483
+ int64_t length) {
2484
+ ArrowBitsSetTo(bitmap->buffer.data, bitmap->size_bits, length, bits_are_set);
2485
+ bitmap->size_bits += length;
2486
+ bitmap->buffer.size_bytes = _ArrowBytesForBits(bitmap->size_bits);
2487
+ }
2488
+
2489
+ static inline void ArrowBitmapAppendInt8Unsafe(struct ArrowBitmap* bitmap,
2490
+ const int8_t* values,
2491
+ int64_t n_values) {
2492
+ if (n_values == 0) {
2493
+ return;
2494
+ }
2495
+
2496
+ const int8_t* values_cursor = values;
2497
+ int64_t n_remaining = n_values;
2498
+ int64_t out_i_cursor = bitmap->size_bits;
2499
+ uint8_t* out_cursor = bitmap->buffer.data + bitmap->size_bits / 8;
2500
+
2501
+ // First byte
2502
+ if ((out_i_cursor % 8) != 0) {
2503
+ int64_t n_partial_bits =
2504
+ _ArrowRoundUpToMultipleOf8(out_i_cursor) - out_i_cursor;
2505
+ for (int i = 0; i < n_partial_bits; i++) {
2506
+ ArrowBitSetTo(bitmap->buffer.data, out_i_cursor++, values[i]);
2507
+ }
2508
+
2509
+ out_cursor++;
2510
+ values_cursor += n_partial_bits;
2511
+ n_remaining -= n_partial_bits;
2512
+ }
2513
+
2514
+ // Middle bytes
2515
+ int64_t n_full_bytes = n_remaining / 8;
2516
+ for (int64_t i = 0; i < n_full_bytes; i++) {
2517
+ _ArrowBitmapPackInt8(values_cursor, out_cursor);
2518
+ values_cursor += 8;
2519
+ out_cursor++;
2520
+ }
2521
+
2522
+ // Last byte
2523
+ out_i_cursor += n_full_bytes * 8;
2524
+ n_remaining -= n_full_bytes * 8;
2525
+ if (n_remaining > 0) {
2526
+ // Zero out the last byte
2527
+ *out_cursor = 0x00;
2528
+ for (int i = 0; i < n_remaining; i++) {
2529
+ ArrowBitSetTo(bitmap->buffer.data, out_i_cursor++, values_cursor[i]);
2530
+ }
2531
+ out_cursor++;
2532
+ }
2533
+
2534
+ bitmap->size_bits += n_values;
2535
+ bitmap->buffer.size_bytes = out_cursor - bitmap->buffer.data;
2536
+ }
2537
+
2538
+ static inline void ArrowBitmapAppendInt32Unsafe(struct ArrowBitmap* bitmap,
2539
+ const int32_t* values,
2540
+ int64_t n_values) {
2541
+ if (n_values == 0) {
2542
+ return;
2543
+ }
2544
+
2545
+ const int32_t* values_cursor = values;
2546
+ int64_t n_remaining = n_values;
2547
+ int64_t out_i_cursor = bitmap->size_bits;
2548
+ uint8_t* out_cursor = bitmap->buffer.data + bitmap->size_bits / 8;
2549
+
2550
+ // First byte
2551
+ if ((out_i_cursor % 8) != 0) {
2552
+ int64_t n_partial_bits =
2553
+ _ArrowRoundUpToMultipleOf8(out_i_cursor) - out_i_cursor;
2554
+ for (int i = 0; i < n_partial_bits; i++) {
2555
+ ArrowBitSetTo(bitmap->buffer.data, out_i_cursor++, values[i]);
2556
+ }
2557
+
2558
+ out_cursor++;
2559
+ values_cursor += n_partial_bits;
2560
+ n_remaining -= n_partial_bits;
2561
+ }
2562
+
2563
+ // Middle bytes
2564
+ int64_t n_full_bytes = n_remaining / 8;
2565
+ for (int64_t i = 0; i < n_full_bytes; i++) {
2566
+ _ArrowBitmapPackInt32(values_cursor, out_cursor);
2567
+ values_cursor += 8;
2568
+ out_cursor++;
2569
+ }
2570
+
2571
+ // Last byte
2572
+ out_i_cursor += n_full_bytes * 8;
2573
+ n_remaining -= n_full_bytes * 8;
2574
+ if (n_remaining > 0) {
2575
+ // Zero out the last byte
2576
+ *out_cursor = 0x00;
2577
+ for (int i = 0; i < n_remaining; i++) {
2578
+ ArrowBitSetTo(bitmap->buffer.data, out_i_cursor++, values_cursor[i]);
2579
+ }
2580
+ out_cursor++;
2581
+ }
2582
+
2583
+ bitmap->size_bits += n_values;
2584
+ bitmap->buffer.size_bytes = out_cursor - bitmap->buffer.data;
2585
+ }
2586
+
2587
+ static inline void ArrowBitmapReset(struct ArrowBitmap* bitmap) {
2588
+ ArrowBufferReset(&bitmap->buffer);
2589
+ bitmap->size_bits = 0;
2590
+ }
2591
+
2592
+ #ifdef __cplusplus
2593
+ }
2594
+ #endif
2595
+
2596
+ #endif
2597
+ // Licensed to the Apache Software Foundation (ASF) under one
2598
+ // or more contributor license agreements. See the NOTICE file
2599
+ // distributed with this work for additional information
2600
+ // regarding copyright ownership. The ASF licenses this file
2601
+ // to you under the Apache License, Version 2.0 (the
2602
+ // "License"); you may not use this file except in compliance
2603
+ // with the License. You may obtain a copy of the License at
2604
+ //
2605
+ // http://www.apache.org/licenses/LICENSE-2.0
2606
+ //
2607
+ // Unless required by applicable law or agreed to in writing,
2608
+ // software distributed under the License is distributed on an
2609
+ // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
2610
+ // KIND, either express or implied. See the License for the
2611
+ // specific language governing permissions and limitations
2612
+ // under the License.
2613
+
2614
+ #ifndef NANOARROW_ARRAY_INLINE_H_INCLUDED
2615
+ #define NANOARROW_ARRAY_INLINE_H_INCLUDED
2616
+
2617
+ #include <errno.h>
2618
+ #include <float.h>
2619
+ #include <limits.h>
2620
+ #include <stdint.h>
2621
+ #include <string.h>
2622
+
2623
+ #ifdef __cplusplus
2624
+ extern "C" {
2625
+ #endif
2626
+
2627
+ static inline struct ArrowBitmap* ArrowArrayValidityBitmap(
2628
+ struct ArrowArray* array) {
2629
+ struct ArrowArrayPrivateData* private_data =
2630
+ (struct ArrowArrayPrivateData*)array->private_data;
2631
+ return &private_data->bitmap;
2632
+ }
2633
+
2634
+ static inline struct ArrowBuffer* ArrowArrayBuffer(struct ArrowArray* array,
2635
+ int64_t i) {
2636
+ struct ArrowArrayPrivateData* private_data =
2637
+ (struct ArrowArrayPrivateData*)array->private_data;
2638
+ switch (i) {
2639
+ case 0:
2640
+ return &private_data->bitmap.buffer;
2641
+ default:
2642
+ return private_data->buffers + i - 1;
2643
+ }
2644
+ }
2645
+
2646
+ // We don't currently support the case of unions where type_id != child_index;
2647
+ // however, these functions are used to keep track of where that assumption
2648
+ // is made.
2649
+ static inline int8_t _ArrowArrayUnionChildIndex(struct ArrowArray* array,
2650
+ int8_t type_id) {
2651
+ return type_id;
2652
+ }
2653
+
2654
+ static inline int8_t _ArrowArrayUnionTypeId(struct ArrowArray* array,
2655
+ int8_t child_index) {
2656
+ return child_index;
2657
+ }
2658
+
2659
+ static inline int8_t _ArrowParseUnionTypeIds(const char* type_ids,
2660
+ int8_t* out) {
2661
+ if (*type_ids == '\0') {
2662
+ return 0;
2663
+ }
2664
+
2665
+ int32_t i = 0;
2666
+ long type_id;
2667
+ char* end_ptr;
2668
+ do {
2669
+ type_id = strtol(type_ids, &end_ptr, 10);
2670
+ if (end_ptr == type_ids || type_id < 0 || type_id > 127) {
2671
+ return -1;
2672
+ }
2673
+
2674
+ if (out != NULL) {
2675
+ out[i] = (int8_t)type_id;
2676
+ }
2677
+
2678
+ i++;
2679
+
2680
+ type_ids = end_ptr;
2681
+ if (*type_ids == '\0') {
2682
+ return i;
2683
+ } else if (*type_ids != ',') {
2684
+ return -1;
2685
+ } else {
2686
+ type_ids++;
2687
+ }
2688
+ } while (1);
2689
+
2690
+ return -1;
2691
+ }
2692
+
2693
+ static inline int8_t _ArrowParsedUnionTypeIdsWillEqualChildIndices(
2694
+ const int8_t* type_ids, int64_t n_type_ids, int64_t n_children) {
2695
+ if (n_type_ids != n_children) {
2696
+ return 0;
2697
+ }
2698
+
2699
+ for (int8_t i = 0; i < n_type_ids; i++) {
2700
+ if (type_ids[i] != i) {
2701
+ return 0;
2702
+ }
2703
+ }
2704
+
2705
+ return 1;
2706
+ }
2707
+
2708
+ static inline int8_t _ArrowUnionTypeIdsWillEqualChildIndices(
2709
+ const char* type_id_str, int64_t n_children) {
2710
+ int8_t type_ids[128];
2711
+ int8_t n_type_ids = _ArrowParseUnionTypeIds(type_id_str, type_ids);
2712
+ return _ArrowParsedUnionTypeIdsWillEqualChildIndices(type_ids, n_type_ids,
2713
+ n_children);
2714
+ }
2715
+
2716
+ static inline ArrowErrorCode ArrowArrayStartAppending(
2717
+ struct ArrowArray* array) {
2718
+ struct ArrowArrayPrivateData* private_data =
2719
+ (struct ArrowArrayPrivateData*)array->private_data;
2720
+
2721
+ switch (private_data->storage_type) {
2722
+ case NANOARROW_TYPE_UNINITIALIZED:
2723
+ return EINVAL;
2724
+ case NANOARROW_TYPE_SPARSE_UNION:
2725
+ case NANOARROW_TYPE_DENSE_UNION:
2726
+ // Note that this value could be -1 if the type_ids string was invalid
2727
+ if (private_data->union_type_id_is_child_index != 1) {
2728
+ return EINVAL;
2729
+ } else {
2730
+ break;
2731
+ }
2732
+ default:
2733
+ break;
2734
+ }
2735
+ if (private_data->storage_type == NANOARROW_TYPE_UNINITIALIZED) {
2736
+ return EINVAL;
2737
+ }
2738
+
2739
+ // Initialize any data offset buffer with a single zero
2740
+ for (int i = 0; i < 3; i++) {
2741
+ if (private_data->layout.buffer_type[i] ==
2742
+ NANOARROW_BUFFER_TYPE_DATA_OFFSET &&
2743
+ private_data->layout.element_size_bits[i] == 64) {
2744
+ NANOARROW_RETURN_NOT_OK(
2745
+ ArrowBufferAppendInt64(ArrowArrayBuffer(array, i), 0));
2746
+ } else if (private_data->layout.buffer_type[i] ==
2747
+ NANOARROW_BUFFER_TYPE_DATA_OFFSET &&
2748
+ private_data->layout.element_size_bits[i] == 32) {
2749
+ NANOARROW_RETURN_NOT_OK(
2750
+ ArrowBufferAppendInt32(ArrowArrayBuffer(array, i), 0));
2751
+ }
2752
+ }
2753
+
2754
+ // Start building any child arrays or dictionaries
2755
+ for (int64_t i = 0; i < array->n_children; i++) {
2756
+ NANOARROW_RETURN_NOT_OK(ArrowArrayStartAppending(array->children[i]));
2757
+ }
2758
+
2759
+ if (array->dictionary != NULL) {
2760
+ NANOARROW_RETURN_NOT_OK(ArrowArrayStartAppending(array->dictionary));
2761
+ }
2762
+
2763
+ return NANOARROW_OK;
2764
+ }
2765
+
2766
+ static inline ArrowErrorCode ArrowArrayShrinkToFit(struct ArrowArray* array) {
2767
+ for (int64_t i = 0; i < 3; i++) {
2768
+ struct ArrowBuffer* buffer = ArrowArrayBuffer(array, i);
2769
+ NANOARROW_RETURN_NOT_OK(ArrowBufferResize(buffer, buffer->size_bytes, 1));
2770
+ }
2771
+
2772
+ for (int64_t i = 0; i < array->n_children; i++) {
2773
+ NANOARROW_RETURN_NOT_OK(ArrowArrayShrinkToFit(array->children[i]));
2774
+ }
2775
+
2776
+ if (array->dictionary != NULL) {
2777
+ NANOARROW_RETURN_NOT_OK(ArrowArrayShrinkToFit(array->dictionary));
2778
+ }
2779
+
2780
+ return NANOARROW_OK;
2781
+ }
2782
+
2783
+ static inline ArrowErrorCode _ArrowArrayAppendBits(struct ArrowArray* array,
2784
+ int64_t buffer_i,
2785
+ uint8_t value, int64_t n) {
2786
+ struct ArrowArrayPrivateData* private_data =
2787
+ (struct ArrowArrayPrivateData*)array->private_data;
2788
+ struct ArrowBuffer* buffer = ArrowArrayBuffer(array, buffer_i);
2789
+ int64_t bytes_required =
2790
+ _ArrowRoundUpToMultipleOf8(
2791
+ private_data->layout.element_size_bits[buffer_i] *
2792
+ (array->length + 1)) /
2793
+ 8;
2794
+ if (bytes_required > buffer->size_bytes) {
2795
+ NANOARROW_RETURN_NOT_OK(
2796
+ ArrowBufferAppendFill(buffer, 0, bytes_required - buffer->size_bytes));
2797
+ }
2798
+
2799
+ ArrowBitsSetTo(buffer->data, array->length, n, value);
2800
+ return NANOARROW_OK;
2801
+ }
2802
+
2803
+ static inline ArrowErrorCode _ArrowArrayAppendEmptyInternal(
2804
+ struct ArrowArray* array, int64_t n, uint8_t is_valid) {
2805
+ struct ArrowArrayPrivateData* private_data =
2806
+ (struct ArrowArrayPrivateData*)array->private_data;
2807
+
2808
+ if (n == 0) {
2809
+ return NANOARROW_OK;
2810
+ }
2811
+
2812
+ // Some type-specific handling
2813
+ switch (private_data->storage_type) {
2814
+ case NANOARROW_TYPE_NA:
2815
+ // (An empty value for a null array *is* a null)
2816
+ array->null_count += n;
2817
+ array->length += n;
2818
+ return NANOARROW_OK;
2819
+
2820
+ case NANOARROW_TYPE_DENSE_UNION: {
2821
+ // Add one null to the first child and append n references to that child
2822
+ int8_t type_id = _ArrowArrayUnionTypeId(array, 0);
2823
+ NANOARROW_RETURN_NOT_OK(
2824
+ _ArrowArrayAppendEmptyInternal(array->children[0], 1, is_valid));
2825
+ NANOARROW_RETURN_NOT_OK(
2826
+ ArrowBufferAppendFill(ArrowArrayBuffer(array, 0), type_id, n));
2827
+ for (int64_t i = 0; i < n; i++) {
2828
+ NANOARROW_RETURN_NOT_OK(
2829
+ ArrowBufferAppendInt32(ArrowArrayBuffer(array, 1),
2830
+ (int32_t)array->children[0]->length - 1));
2831
+ }
2832
+ // For the purposes of array->null_count, union elements are never
2833
+ // considered "null" even if some children contain nulls.
2834
+ array->length += n;
2835
+ return NANOARROW_OK;
2836
+ }
2837
+
2838
+ case NANOARROW_TYPE_SPARSE_UNION: {
2839
+ // Add n nulls to the first child and append n references to that child
2840
+ int8_t type_id = _ArrowArrayUnionTypeId(array, 0);
2841
+ NANOARROW_RETURN_NOT_OK(
2842
+ _ArrowArrayAppendEmptyInternal(array->children[0], n, is_valid));
2843
+ for (int64_t i = 1; i < array->n_children; i++) {
2844
+ NANOARROW_RETURN_NOT_OK(ArrowArrayAppendEmpty(array->children[i], n));
2845
+ }
2846
+
2847
+ NANOARROW_RETURN_NOT_OK(
2848
+ ArrowBufferAppendFill(ArrowArrayBuffer(array, 0), type_id, n));
2849
+ // For the purposes of array->null_count, union elements are never
2850
+ // considered "null" even if some children contain nulls.
2851
+ array->length += n;
2852
+ return NANOARROW_OK;
2853
+ }
2854
+
2855
+ case NANOARROW_TYPE_FIXED_SIZE_LIST:
2856
+ NANOARROW_RETURN_NOT_OK(ArrowArrayAppendEmpty(
2857
+ array->children[0], n * private_data->layout.child_size_elements));
2858
+ break;
2859
+ case NANOARROW_TYPE_STRUCT:
2860
+ for (int64_t i = 0; i < array->n_children; i++) {
2861
+ NANOARROW_RETURN_NOT_OK(ArrowArrayAppendEmpty(array->children[i], n));
2862
+ }
2863
+ break;
2864
+
2865
+ default:
2866
+ break;
2867
+ }
2868
+
2869
+ // Append n is_valid bits to the validity bitmap. If we haven't allocated a
2870
+ // bitmap yet and we need to append nulls, do it now.
2871
+ if (!is_valid && private_data->bitmap.buffer.data == NULL) {
2872
+ NANOARROW_RETURN_NOT_OK(
2873
+ ArrowBitmapReserve(&private_data->bitmap, array->length + n));
2874
+ ArrowBitmapAppendUnsafe(&private_data->bitmap, 1, array->length);
2875
+ ArrowBitmapAppendUnsafe(&private_data->bitmap, is_valid, n);
2876
+ } else if (private_data->bitmap.buffer.data != NULL) {
2877
+ NANOARROW_RETURN_NOT_OK(ArrowBitmapReserve(&private_data->bitmap, n));
2878
+ ArrowBitmapAppendUnsafe(&private_data->bitmap, is_valid, n);
2879
+ }
2880
+
2881
+ // Add appropriate buffer fill
2882
+ struct ArrowBuffer* buffer;
2883
+ int64_t size_bytes;
2884
+
2885
+ for (int i = 0; i < 3; i++) {
2886
+ buffer = ArrowArrayBuffer(array, i);
2887
+ size_bytes = private_data->layout.element_size_bits[i] / 8;
2888
+
2889
+ switch (private_data->layout.buffer_type[i]) {
2890
+ case NANOARROW_BUFFER_TYPE_NONE:
2891
+ case NANOARROW_BUFFER_TYPE_VALIDITY:
2892
+ continue;
2893
+ case NANOARROW_BUFFER_TYPE_DATA_OFFSET:
2894
+ // Append the current value at the end of the offset buffer for each
2895
+ // element
2896
+ NANOARROW_RETURN_NOT_OK(ArrowBufferReserve(buffer, size_bytes * n));
2897
+
2898
+ for (int64_t j = 0; j < n; j++) {
2899
+ ArrowBufferAppendUnsafe(
2900
+ buffer, buffer->data + size_bytes * (array->length + j),
2901
+ size_bytes);
2902
+ }
2903
+
2904
+ // Skip the data buffer
2905
+ i++;
2906
+ continue;
2907
+ case NANOARROW_BUFFER_TYPE_DATA:
2908
+ // Zero out the next bit of memory
2909
+ if (private_data->layout.element_size_bits[i] % 8 == 0) {
2910
+ NANOARROW_RETURN_NOT_OK(
2911
+ ArrowBufferAppendFill(buffer, 0, size_bytes * n));
2912
+ } else {
2913
+ NANOARROW_RETURN_NOT_OK(_ArrowArrayAppendBits(array, i, 0, n));
2914
+ }
2915
+ continue;
2916
+
2917
+ case NANOARROW_BUFFER_TYPE_TYPE_ID:
2918
+ case NANOARROW_BUFFER_TYPE_UNION_OFFSET:
2919
+ // These cases return above
2920
+ return EINVAL;
2921
+ }
2922
+ }
2923
+
2924
+ array->length += n;
2925
+ array->null_count += n * !is_valid;
2926
+ return NANOARROW_OK;
2927
+ }
2928
+
2929
+ static inline ArrowErrorCode ArrowArrayAppendNull(struct ArrowArray* array,
2930
+ int64_t n) {
2931
+ return _ArrowArrayAppendEmptyInternal(array, n, 0);
2932
+ }
2933
+
2934
+ static inline ArrowErrorCode ArrowArrayAppendEmpty(struct ArrowArray* array,
2935
+ int64_t n) {
2936
+ return _ArrowArrayAppendEmptyInternal(array, n, 1);
2937
+ }
2938
+
2939
+ static inline ArrowErrorCode ArrowArrayAppendInt(struct ArrowArray* array,
2940
+ int64_t value) {
2941
+ struct ArrowArrayPrivateData* private_data =
2942
+ (struct ArrowArrayPrivateData*)array->private_data;
2943
+
2944
+ struct ArrowBuffer* data_buffer = ArrowArrayBuffer(array, 1);
2945
+
2946
+ switch (private_data->storage_type) {
2947
+ case NANOARROW_TYPE_INT64:
2948
+ NANOARROW_RETURN_NOT_OK(
2949
+ ArrowBufferAppend(data_buffer, &value, sizeof(int64_t)));
2950
+ break;
2951
+ case NANOARROW_TYPE_INT32:
2952
+ _NANOARROW_CHECK_RANGE(value, INT32_MIN, INT32_MAX);
2953
+ NANOARROW_RETURN_NOT_OK(
2954
+ ArrowBufferAppendInt32(data_buffer, (int32_t)value));
2955
+ break;
2956
+ case NANOARROW_TYPE_INT16:
2957
+ _NANOARROW_CHECK_RANGE(value, INT16_MIN, INT16_MAX);
2958
+ NANOARROW_RETURN_NOT_OK(
2959
+ ArrowBufferAppendInt16(data_buffer, (int16_t)value));
2960
+ break;
2961
+ case NANOARROW_TYPE_INT8:
2962
+ _NANOARROW_CHECK_RANGE(value, INT8_MIN, INT8_MAX);
2963
+ NANOARROW_RETURN_NOT_OK(
2964
+ ArrowBufferAppendInt8(data_buffer, (int8_t)value));
2965
+ break;
2966
+ case NANOARROW_TYPE_UINT64:
2967
+ case NANOARROW_TYPE_UINT32:
2968
+ case NANOARROW_TYPE_UINT16:
2969
+ case NANOARROW_TYPE_UINT8:
2970
+ _NANOARROW_CHECK_RANGE(value, 0, INT64_MAX);
2971
+ return ArrowArrayAppendUInt(array, value);
2972
+ case NANOARROW_TYPE_DOUBLE:
2973
+ NANOARROW_RETURN_NOT_OK(
2974
+ ArrowBufferAppendDouble(data_buffer, (double)value));
2975
+ break;
2976
+ case NANOARROW_TYPE_FLOAT:
2977
+ NANOARROW_RETURN_NOT_OK(
2978
+ ArrowBufferAppendFloat(data_buffer, (float)value));
2979
+ break;
2980
+ case NANOARROW_TYPE_BOOL:
2981
+ NANOARROW_RETURN_NOT_OK(_ArrowArrayAppendBits(array, 1, value != 0, 1));
2982
+ break;
2983
+ default:
2984
+ return EINVAL;
2985
+ }
2986
+
2987
+ if (private_data->bitmap.buffer.data != NULL) {
2988
+ NANOARROW_RETURN_NOT_OK(
2989
+ ArrowBitmapAppend(ArrowArrayValidityBitmap(array), 1, 1));
2990
+ }
2991
+
2992
+ array->length++;
2993
+ return NANOARROW_OK;
2994
+ }
2995
+
2996
+ static inline ArrowErrorCode ArrowArrayAppendUInt(struct ArrowArray* array,
2997
+ uint64_t value) {
2998
+ struct ArrowArrayPrivateData* private_data =
2999
+ (struct ArrowArrayPrivateData*)array->private_data;
3000
+
3001
+ struct ArrowBuffer* data_buffer = ArrowArrayBuffer(array, 1);
3002
+
3003
+ switch (private_data->storage_type) {
3004
+ case NANOARROW_TYPE_UINT64:
3005
+ NANOARROW_RETURN_NOT_OK(
3006
+ ArrowBufferAppend(data_buffer, &value, sizeof(uint64_t)));
3007
+ break;
3008
+ case NANOARROW_TYPE_UINT32:
3009
+ _NANOARROW_CHECK_UPPER_LIMIT(value, UINT32_MAX);
3010
+ NANOARROW_RETURN_NOT_OK(
3011
+ ArrowBufferAppendUInt32(data_buffer, (uint32_t)value));
3012
+ break;
3013
+ case NANOARROW_TYPE_UINT16:
3014
+ _NANOARROW_CHECK_UPPER_LIMIT(value, UINT16_MAX);
3015
+ NANOARROW_RETURN_NOT_OK(
3016
+ ArrowBufferAppendUInt16(data_buffer, (uint16_t)value));
3017
+ break;
3018
+ case NANOARROW_TYPE_UINT8:
3019
+ _NANOARROW_CHECK_UPPER_LIMIT(value, UINT8_MAX);
3020
+ NANOARROW_RETURN_NOT_OK(
3021
+ ArrowBufferAppendUInt8(data_buffer, (uint8_t)value));
3022
+ break;
3023
+ case NANOARROW_TYPE_INT64:
3024
+ case NANOARROW_TYPE_INT32:
3025
+ case NANOARROW_TYPE_INT16:
3026
+ case NANOARROW_TYPE_INT8:
3027
+ _NANOARROW_CHECK_UPPER_LIMIT(value, INT64_MAX);
3028
+ return ArrowArrayAppendInt(array, value);
3029
+ case NANOARROW_TYPE_DOUBLE:
3030
+ NANOARROW_RETURN_NOT_OK(
3031
+ ArrowBufferAppendDouble(data_buffer, (double)value));
3032
+ break;
3033
+ case NANOARROW_TYPE_FLOAT:
3034
+ NANOARROW_RETURN_NOT_OK(
3035
+ ArrowBufferAppendFloat(data_buffer, (float)value));
3036
+ break;
3037
+ case NANOARROW_TYPE_BOOL:
3038
+ NANOARROW_RETURN_NOT_OK(_ArrowArrayAppendBits(array, 1, value != 0, 1));
3039
+ break;
3040
+ default:
3041
+ return EINVAL;
3042
+ }
3043
+
3044
+ if (private_data->bitmap.buffer.data != NULL) {
3045
+ NANOARROW_RETURN_NOT_OK(
3046
+ ArrowBitmapAppend(ArrowArrayValidityBitmap(array), 1, 1));
3047
+ }
3048
+
3049
+ array->length++;
3050
+ return NANOARROW_OK;
3051
+ }
3052
+
3053
+ static inline ArrowErrorCode ArrowArrayAppendDouble(struct ArrowArray* array,
3054
+ double value) {
3055
+ struct ArrowArrayPrivateData* private_data =
3056
+ (struct ArrowArrayPrivateData*)array->private_data;
3057
+
3058
+ struct ArrowBuffer* data_buffer = ArrowArrayBuffer(array, 1);
3059
+
3060
+ switch (private_data->storage_type) {
3061
+ case NANOARROW_TYPE_DOUBLE:
3062
+ NANOARROW_RETURN_NOT_OK(
3063
+ ArrowBufferAppend(data_buffer, &value, sizeof(double)));
3064
+ break;
3065
+ case NANOARROW_TYPE_FLOAT:
3066
+ NANOARROW_RETURN_NOT_OK(
3067
+ ArrowBufferAppendFloat(data_buffer, (float)value));
3068
+ break;
3069
+ default:
3070
+ return EINVAL;
3071
+ }
3072
+
3073
+ if (private_data->bitmap.buffer.data != NULL) {
3074
+ NANOARROW_RETURN_NOT_OK(
3075
+ ArrowBitmapAppend(ArrowArrayValidityBitmap(array), 1, 1));
3076
+ }
3077
+
3078
+ array->length++;
3079
+ return NANOARROW_OK;
3080
+ }
3081
+
3082
+ static inline ArrowErrorCode ArrowArrayAppendBytes(
3083
+ struct ArrowArray* array, struct ArrowBufferView value) {
3084
+ struct ArrowArrayPrivateData* private_data =
3085
+ (struct ArrowArrayPrivateData*)array->private_data;
3086
+
3087
+ struct ArrowBuffer* offset_buffer = ArrowArrayBuffer(array, 1);
3088
+ struct ArrowBuffer* data_buffer = ArrowArrayBuffer(
3089
+ array,
3090
+ 1 + (private_data->storage_type != NANOARROW_TYPE_FIXED_SIZE_BINARY));
3091
+ int32_t offset;
3092
+ int64_t large_offset;
3093
+ int64_t fixed_size_bytes = private_data->layout.element_size_bits[1] / 8;
3094
+
3095
+ switch (private_data->storage_type) {
3096
+ case NANOARROW_TYPE_STRING:
3097
+ case NANOARROW_TYPE_BINARY:
3098
+ offset = ((int32_t*)offset_buffer->data)[array->length];
3099
+ if ((((int64_t)offset) + value.size_bytes) > INT32_MAX) {
3100
+ return EOVERFLOW;
3101
+ }
3102
+
3103
+ offset += (int32_t)value.size_bytes;
3104
+ NANOARROW_RETURN_NOT_OK(
3105
+ ArrowBufferAppend(offset_buffer, &offset, sizeof(int32_t)));
3106
+ NANOARROW_RETURN_NOT_OK(
3107
+ ArrowBufferAppend(data_buffer, value.data.data, value.size_bytes));
3108
+ break;
3109
+
3110
+ case NANOARROW_TYPE_LARGE_STRING:
3111
+ case NANOARROW_TYPE_LARGE_BINARY:
3112
+ large_offset = ((int64_t*)offset_buffer->data)[array->length];
3113
+ large_offset += value.size_bytes;
3114
+ NANOARROW_RETURN_NOT_OK(
3115
+ ArrowBufferAppend(offset_buffer, &large_offset, sizeof(int64_t)));
3116
+ NANOARROW_RETURN_NOT_OK(
3117
+ ArrowBufferAppend(data_buffer, value.data.data, value.size_bytes));
3118
+ break;
3119
+
3120
+ case NANOARROW_TYPE_FIXED_SIZE_BINARY:
3121
+ if (value.size_bytes != fixed_size_bytes) {
3122
+ return EINVAL;
3123
+ }
3124
+
3125
+ NANOARROW_RETURN_NOT_OK(
3126
+ ArrowBufferAppend(data_buffer, value.data.data, value.size_bytes));
3127
+ break;
3128
+ default:
3129
+ return EINVAL;
3130
+ }
3131
+
3132
+ if (private_data->bitmap.buffer.data != NULL) {
3133
+ NANOARROW_RETURN_NOT_OK(
3134
+ ArrowBitmapAppend(ArrowArrayValidityBitmap(array), 1, 1));
3135
+ }
3136
+
3137
+ array->length++;
3138
+ return NANOARROW_OK;
3139
+ }
3140
+
3141
+ static inline ArrowErrorCode ArrowArrayAppendString(
3142
+ struct ArrowArray* array, struct ArrowStringView value) {
3143
+ struct ArrowArrayPrivateData* private_data =
3144
+ (struct ArrowArrayPrivateData*)array->private_data;
3145
+
3146
+ struct ArrowBufferView buffer_view;
3147
+ buffer_view.data.data = value.data;
3148
+ buffer_view.size_bytes = value.size_bytes;
3149
+
3150
+ switch (private_data->storage_type) {
3151
+ case NANOARROW_TYPE_STRING:
3152
+ case NANOARROW_TYPE_LARGE_STRING:
3153
+ case NANOARROW_TYPE_BINARY:
3154
+ case NANOARROW_TYPE_LARGE_BINARY:
3155
+ return ArrowArrayAppendBytes(array, buffer_view);
3156
+ default:
3157
+ return EINVAL;
3158
+ }
3159
+ }
3160
+
3161
+ static inline ArrowErrorCode ArrowArrayAppendInterval(
3162
+ struct ArrowArray* array, struct ArrowInterval* value) {
3163
+ struct ArrowArrayPrivateData* private_data =
3164
+ (struct ArrowArrayPrivateData*)array->private_data;
3165
+
3166
+ struct ArrowBuffer* data_buffer = ArrowArrayBuffer(array, 1);
3167
+
3168
+ switch (private_data->storage_type) {
3169
+ case NANOARROW_TYPE_INTERVAL_MONTHS: {
3170
+ if (value->type != NANOARROW_TYPE_INTERVAL_MONTHS) {
3171
+ return EINVAL;
3172
+ }
3173
+
3174
+ NANOARROW_RETURN_NOT_OK(
3175
+ ArrowBufferAppendInt32(data_buffer, value->months));
3176
+ break;
3177
+ }
3178
+ case NANOARROW_TYPE_INTERVAL_DAY_TIME: {
3179
+ if (value->type != NANOARROW_TYPE_INTERVAL_DAY_TIME) {
3180
+ return EINVAL;
3181
+ }
3182
+
3183
+ NANOARROW_RETURN_NOT_OK(ArrowBufferAppendInt32(data_buffer, value->days));
3184
+ NANOARROW_RETURN_NOT_OK(ArrowBufferAppendInt32(data_buffer, value->ms));
3185
+ break;
3186
+ }
3187
+ case NANOARROW_TYPE_INTERVAL_MONTH_DAY_NANO: {
3188
+ if (value->type != NANOARROW_TYPE_INTERVAL_MONTH_DAY_NANO) {
3189
+ return EINVAL;
3190
+ }
3191
+
3192
+ NANOARROW_RETURN_NOT_OK(
3193
+ ArrowBufferAppendInt32(data_buffer, value->months));
3194
+ NANOARROW_RETURN_NOT_OK(ArrowBufferAppendInt32(data_buffer, value->days));
3195
+ NANOARROW_RETURN_NOT_OK(ArrowBufferAppendInt64(data_buffer, value->ns));
3196
+ break;
3197
+ }
3198
+ default:
3199
+ return EINVAL;
3200
+ }
3201
+
3202
+ array->length++;
3203
+ return NANOARROW_OK;
3204
+ }
3205
+
3206
+ static inline ArrowErrorCode ArrowArrayAppendDecimal(
3207
+ struct ArrowArray* array, struct ArrowDecimal* value) {
3208
+ struct ArrowArrayPrivateData* private_data =
3209
+ (struct ArrowArrayPrivateData*)array->private_data;
3210
+ struct ArrowBuffer* data_buffer = ArrowArrayBuffer(array, 1);
3211
+
3212
+ switch (private_data->storage_type) {
3213
+ case NANOARROW_TYPE_DECIMAL128:
3214
+ if (value->n_words != 2) {
3215
+ return EINVAL;
3216
+ } else {
3217
+ NANOARROW_RETURN_NOT_OK(
3218
+ ArrowBufferAppend(data_buffer, value->words, 2 * sizeof(uint64_t)));
3219
+ break;
3220
+ }
3221
+ case NANOARROW_TYPE_DECIMAL256:
3222
+ if (value->n_words != 4) {
3223
+ return EINVAL;
3224
+ } else {
3225
+ NANOARROW_RETURN_NOT_OK(
3226
+ ArrowBufferAppend(data_buffer, value->words, 4 * sizeof(uint64_t)));
3227
+ break;
3228
+ }
3229
+ default:
3230
+ return EINVAL;
3231
+ }
3232
+
3233
+ if (private_data->bitmap.buffer.data != NULL) {
3234
+ NANOARROW_RETURN_NOT_OK(
3235
+ ArrowBitmapAppend(ArrowArrayValidityBitmap(array), 1, 1));
3236
+ }
3237
+
3238
+ array->length++;
3239
+ return NANOARROW_OK;
3240
+ }
3241
+
3242
+ static inline ArrowErrorCode ArrowArrayFinishElement(struct ArrowArray* array) {
3243
+ struct ArrowArrayPrivateData* private_data =
3244
+ (struct ArrowArrayPrivateData*)array->private_data;
3245
+
3246
+ int64_t child_length;
3247
+
3248
+ switch (private_data->storage_type) {
3249
+ case NANOARROW_TYPE_LIST:
3250
+ case NANOARROW_TYPE_MAP:
3251
+ child_length = array->children[0]->length;
3252
+ if (child_length > INT32_MAX) {
3253
+ return EOVERFLOW;
3254
+ }
3255
+ NANOARROW_RETURN_NOT_OK(ArrowBufferAppendInt32(ArrowArrayBuffer(array, 1),
3256
+ (int32_t)child_length));
3257
+ break;
3258
+ case NANOARROW_TYPE_LARGE_LIST:
3259
+ child_length = array->children[0]->length;
3260
+ NANOARROW_RETURN_NOT_OK(
3261
+ ArrowBufferAppendInt64(ArrowArrayBuffer(array, 1), child_length));
3262
+ break;
3263
+ case NANOARROW_TYPE_FIXED_SIZE_LIST:
3264
+ child_length = array->children[0]->length;
3265
+ if (child_length !=
3266
+ ((array->length + 1) * private_data->layout.child_size_elements)) {
3267
+ return EINVAL;
3268
+ }
3269
+ break;
3270
+ case NANOARROW_TYPE_STRUCT:
3271
+ for (int64_t i = 0; i < array->n_children; i++) {
3272
+ child_length = array->children[i]->length;
3273
+ if (child_length != (array->length + 1)) {
3274
+ return EINVAL;
3275
+ }
3276
+ }
3277
+ break;
3278
+ default:
3279
+ return EINVAL;
3280
+ }
3281
+
3282
+ if (private_data->bitmap.buffer.data != NULL) {
3283
+ NANOARROW_RETURN_NOT_OK(
3284
+ ArrowBitmapAppend(ArrowArrayValidityBitmap(array), 1, 1));
3285
+ }
3286
+
3287
+ array->length++;
3288
+ return NANOARROW_OK;
3289
+ }
3290
+
3291
+ static inline ArrowErrorCode ArrowArrayFinishUnionElement(
3292
+ struct ArrowArray* array, int8_t type_id) {
3293
+ struct ArrowArrayPrivateData* private_data =
3294
+ (struct ArrowArrayPrivateData*)array->private_data;
3295
+
3296
+ int64_t child_index = _ArrowArrayUnionChildIndex(array, type_id);
3297
+ if (child_index < 0 || child_index >= array->n_children) {
3298
+ return EINVAL;
3299
+ }
3300
+
3301
+ switch (private_data->storage_type) {
3302
+ case NANOARROW_TYPE_DENSE_UNION:
3303
+ // Append the target child length to the union offsets buffer
3304
+ _NANOARROW_CHECK_RANGE(array->children[child_index]->length, 0,
3305
+ INT32_MAX);
3306
+ NANOARROW_RETURN_NOT_OK(ArrowBufferAppendInt32(
3307
+ ArrowArrayBuffer(array, 1),
3308
+ (int32_t)array->children[child_index]->length - 1));
3309
+ break;
3310
+ case NANOARROW_TYPE_SPARSE_UNION:
3311
+ // Append one empty to any non-target column that isn't already the right
3312
+ // length or abort if appending a null will result in a column with
3313
+ // invalid length
3314
+ for (int64_t i = 0; i < array->n_children; i++) {
3315
+ if (i == child_index ||
3316
+ array->children[i]->length == (array->length + 1)) {
3317
+ continue;
3318
+ }
3319
+
3320
+ if (array->children[i]->length != array->length) {
3321
+ return EINVAL;
3322
+ }
3323
+
3324
+ NANOARROW_RETURN_NOT_OK(ArrowArrayAppendEmpty(array->children[i], 1));
3325
+ }
3326
+
3327
+ break;
3328
+ default:
3329
+ return EINVAL;
3330
+ }
3331
+
3332
+ // Write to the type_ids buffer
3333
+ NANOARROW_RETURN_NOT_OK(
3334
+ ArrowBufferAppendInt8(ArrowArrayBuffer(array, 0), (int8_t)type_id));
3335
+ array->length++;
3336
+ return NANOARROW_OK;
3337
+ }
3338
+
3339
+ static inline void ArrowArrayViewMove(struct ArrowArrayView* src,
3340
+ struct ArrowArrayView* dst) {
3341
+ memcpy(dst, src, sizeof(struct ArrowArrayView));
3342
+ ArrowArrayViewInitFromType(src, NANOARROW_TYPE_UNINITIALIZED);
3343
+ }
3344
+
3345
+ static inline int8_t ArrowArrayViewIsNull(struct ArrowArrayView* array_view,
3346
+ int64_t i) {
3347
+ const uint8_t* validity_buffer = array_view->buffer_views[0].data.as_uint8;
3348
+ i += array_view->offset;
3349
+ switch (array_view->storage_type) {
3350
+ case NANOARROW_TYPE_NA:
3351
+ return 0x01;
3352
+ case NANOARROW_TYPE_DENSE_UNION:
3353
+ case NANOARROW_TYPE_SPARSE_UNION:
3354
+ // Unions are "never null" in Arrow land
3355
+ return 0x00;
3356
+ default:
3357
+ return validity_buffer != NULL && !ArrowBitGet(validity_buffer, i);
3358
+ }
3359
+ }
3360
+
3361
+ static inline int8_t ArrowArrayViewUnionTypeId(
3362
+ struct ArrowArrayView* array_view, int64_t i) {
3363
+ switch (array_view->storage_type) {
3364
+ case NANOARROW_TYPE_DENSE_UNION:
3365
+ case NANOARROW_TYPE_SPARSE_UNION:
3366
+ return array_view->buffer_views[0].data.as_int8[i];
3367
+ default:
3368
+ return -1;
3369
+ }
3370
+ }
3371
+
3372
+ static inline int8_t ArrowArrayViewUnionChildIndex(
3373
+ struct ArrowArrayView* array_view, int64_t i) {
3374
+ int8_t type_id = ArrowArrayViewUnionTypeId(array_view, i);
3375
+ if (array_view->union_type_id_map == NULL) {
3376
+ return type_id;
3377
+ } else {
3378
+ return array_view->union_type_id_map[type_id];
3379
+ }
3380
+ }
3381
+
3382
+ static inline int64_t ArrowArrayViewUnionChildOffset(
3383
+ struct ArrowArrayView* array_view, int64_t i) {
3384
+ switch (array_view->storage_type) {
3385
+ case NANOARROW_TYPE_DENSE_UNION:
3386
+ return array_view->buffer_views[1].data.as_int32[i];
3387
+ case NANOARROW_TYPE_SPARSE_UNION:
3388
+ return i;
3389
+ default:
3390
+ return -1;
3391
+ }
3392
+ }
3393
+
3394
+ static inline int64_t ArrowArrayViewListChildOffset(
3395
+ struct ArrowArrayView* array_view, int64_t i) {
3396
+ switch (array_view->storage_type) {
3397
+ case NANOARROW_TYPE_LIST:
3398
+ return array_view->buffer_views[1].data.as_int32[i];
3399
+ case NANOARROW_TYPE_LARGE_LIST:
3400
+ return array_view->buffer_views[1].data.as_int64[i];
3401
+ default:
3402
+ return -1;
3403
+ }
3404
+ }
3405
+
3406
+ static inline int64_t ArrowArrayViewGetIntUnsafe(
3407
+ struct ArrowArrayView* array_view, int64_t i) {
3408
+ struct ArrowBufferView* data_view = &array_view->buffer_views[1];
3409
+ i += array_view->offset;
3410
+ switch (array_view->storage_type) {
3411
+ case NANOARROW_TYPE_INT64:
3412
+ return data_view->data.as_int64[i];
3413
+ case NANOARROW_TYPE_UINT64:
3414
+ return data_view->data.as_uint64[i];
3415
+ case NANOARROW_TYPE_INT32:
3416
+ return data_view->data.as_int32[i];
3417
+ case NANOARROW_TYPE_UINT32:
3418
+ return data_view->data.as_uint32[i];
3419
+ case NANOARROW_TYPE_INT16:
3420
+ return data_view->data.as_int16[i];
3421
+ case NANOARROW_TYPE_UINT16:
3422
+ return data_view->data.as_uint16[i];
3423
+ case NANOARROW_TYPE_INT8:
3424
+ return data_view->data.as_int8[i];
3425
+ case NANOARROW_TYPE_UINT8:
3426
+ return data_view->data.as_uint8[i];
3427
+ case NANOARROW_TYPE_DOUBLE:
3428
+ return (int64_t)data_view->data.as_double[i];
3429
+ case NANOARROW_TYPE_FLOAT:
3430
+ return (int64_t)data_view->data.as_float[i];
3431
+ case NANOARROW_TYPE_BOOL:
3432
+ return ArrowBitGet(data_view->data.as_uint8, i);
3433
+ default:
3434
+ return INT64_MAX;
3435
+ }
3436
+ }
3437
+
3438
+ static inline uint64_t ArrowArrayViewGetUIntUnsafe(
3439
+ struct ArrowArrayView* array_view, int64_t i) {
3440
+ i += array_view->offset;
3441
+ struct ArrowBufferView* data_view = &array_view->buffer_views[1];
3442
+ switch (array_view->storage_type) {
3443
+ case NANOARROW_TYPE_INT64:
3444
+ return data_view->data.as_int64[i];
3445
+ case NANOARROW_TYPE_UINT64:
3446
+ return data_view->data.as_uint64[i];
3447
+ case NANOARROW_TYPE_INT32:
3448
+ return data_view->data.as_int32[i];
3449
+ case NANOARROW_TYPE_UINT32:
3450
+ return data_view->data.as_uint32[i];
3451
+ case NANOARROW_TYPE_INT16:
3452
+ return data_view->data.as_int16[i];
3453
+ case NANOARROW_TYPE_UINT16:
3454
+ return data_view->data.as_uint16[i];
3455
+ case NANOARROW_TYPE_INT8:
3456
+ return data_view->data.as_int8[i];
3457
+ case NANOARROW_TYPE_UINT8:
3458
+ return data_view->data.as_uint8[i];
3459
+ case NANOARROW_TYPE_DOUBLE:
3460
+ return (uint64_t)data_view->data.as_double[i];
3461
+ case NANOARROW_TYPE_FLOAT:
3462
+ return (uint64_t)data_view->data.as_float[i];
3463
+ case NANOARROW_TYPE_BOOL:
3464
+ return ArrowBitGet(data_view->data.as_uint8, i);
3465
+ default:
3466
+ return UINT64_MAX;
3467
+ }
3468
+ }
3469
+
3470
+ static inline double ArrowArrayViewGetDoubleUnsafe(
3471
+ struct ArrowArrayView* array_view, int64_t i) {
3472
+ i += array_view->offset;
3473
+ struct ArrowBufferView* data_view = &array_view->buffer_views[1];
3474
+ switch (array_view->storage_type) {
3475
+ case NANOARROW_TYPE_INT64:
3476
+ return (double)data_view->data.as_int64[i];
3477
+ case NANOARROW_TYPE_UINT64:
3478
+ return (double)data_view->data.as_uint64[i];
3479
+ case NANOARROW_TYPE_INT32:
3480
+ return data_view->data.as_int32[i];
3481
+ case NANOARROW_TYPE_UINT32:
3482
+ return data_view->data.as_uint32[i];
3483
+ case NANOARROW_TYPE_INT16:
3484
+ return data_view->data.as_int16[i];
3485
+ case NANOARROW_TYPE_UINT16:
3486
+ return data_view->data.as_uint16[i];
3487
+ case NANOARROW_TYPE_INT8:
3488
+ return data_view->data.as_int8[i];
3489
+ case NANOARROW_TYPE_UINT8:
3490
+ return data_view->data.as_uint8[i];
3491
+ case NANOARROW_TYPE_DOUBLE:
3492
+ return data_view->data.as_double[i];
3493
+ case NANOARROW_TYPE_FLOAT:
3494
+ return data_view->data.as_float[i];
3495
+ case NANOARROW_TYPE_BOOL:
3496
+ return ArrowBitGet(data_view->data.as_uint8, i);
3497
+ default:
3498
+ return DBL_MAX;
3499
+ }
3500
+ }
3501
+
3502
+ static inline struct ArrowStringView ArrowArrayViewGetStringUnsafe(
3503
+ struct ArrowArrayView* array_view, int64_t i) {
3504
+ i += array_view->offset;
3505
+ struct ArrowBufferView* offsets_view = &array_view->buffer_views[1];
3506
+ const char* data_view = array_view->buffer_views[2].data.as_char;
3507
+
3508
+ struct ArrowStringView view;
3509
+ switch (array_view->storage_type) {
3510
+ case NANOARROW_TYPE_STRING:
3511
+ case NANOARROW_TYPE_BINARY:
3512
+ view.data = data_view + offsets_view->data.as_int32[i];
3513
+ view.size_bytes =
3514
+ offsets_view->data.as_int32[i + 1] - offsets_view->data.as_int32[i];
3515
+ break;
3516
+ case NANOARROW_TYPE_LARGE_STRING:
3517
+ case NANOARROW_TYPE_LARGE_BINARY:
3518
+ view.data = data_view + offsets_view->data.as_int64[i];
3519
+ view.size_bytes =
3520
+ offsets_view->data.as_int64[i + 1] - offsets_view->data.as_int64[i];
3521
+ break;
3522
+ case NANOARROW_TYPE_FIXED_SIZE_BINARY:
3523
+ view.size_bytes = array_view->layout.element_size_bits[1] / 8;
3524
+ view.data =
3525
+ array_view->buffer_views[1].data.as_char + (i * view.size_bytes);
3526
+ break;
3527
+ default:
3528
+ view.data = NULL;
3529
+ view.size_bytes = 0;
3530
+ break;
3531
+ }
3532
+
3533
+ return view;
3534
+ }
3535
+
3536
+ static inline struct ArrowBufferView ArrowArrayViewGetBytesUnsafe(
3537
+ struct ArrowArrayView* array_view, int64_t i) {
3538
+ i += array_view->offset;
3539
+ struct ArrowBufferView* offsets_view = &array_view->buffer_views[1];
3540
+ const uint8_t* data_view = array_view->buffer_views[2].data.as_uint8;
3541
+
3542
+ struct ArrowBufferView view;
3543
+ switch (array_view->storage_type) {
3544
+ case NANOARROW_TYPE_STRING:
3545
+ case NANOARROW_TYPE_BINARY:
3546
+ view.size_bytes =
3547
+ offsets_view->data.as_int32[i + 1] - offsets_view->data.as_int32[i];
3548
+ view.data.as_uint8 = data_view + offsets_view->data.as_int32[i];
3549
+ break;
3550
+ case NANOARROW_TYPE_LARGE_STRING:
3551
+ case NANOARROW_TYPE_LARGE_BINARY:
3552
+ view.size_bytes =
3553
+ offsets_view->data.as_int64[i + 1] - offsets_view->data.as_int64[i];
3554
+ view.data.as_uint8 = data_view + offsets_view->data.as_int64[i];
3555
+ break;
3556
+ case NANOARROW_TYPE_FIXED_SIZE_BINARY:
3557
+ view.size_bytes = array_view->layout.element_size_bits[1] / 8;
3558
+ view.data.as_uint8 =
3559
+ array_view->buffer_views[1].data.as_uint8 + (i * view.size_bytes);
3560
+ break;
3561
+ default:
3562
+ view.data.data = NULL;
3563
+ view.size_bytes = 0;
3564
+ break;
3565
+ }
3566
+
3567
+ return view;
3568
+ }
3569
+
3570
+ static inline void ArrowArrayViewGetIntervalUnsafe(
3571
+ struct ArrowArrayView* array_view, int64_t i, struct ArrowInterval* out) {
3572
+ const uint8_t* data_view = array_view->buffer_views[1].data.as_uint8;
3573
+ switch (array_view->storage_type) {
3574
+ case NANOARROW_TYPE_INTERVAL_MONTHS: {
3575
+ const size_t size = sizeof(int32_t);
3576
+ memcpy(&out->months, data_view + i * size, sizeof(int32_t));
3577
+ break;
3578
+ }
3579
+ case NANOARROW_TYPE_INTERVAL_DAY_TIME: {
3580
+ const size_t size = sizeof(int32_t) + sizeof(int32_t);
3581
+ memcpy(&out->days, data_view + i * size, sizeof(int32_t));
3582
+ memcpy(&out->ms, data_view + i * size + 4, sizeof(int32_t));
3583
+ break;
3584
+ }
3585
+ case NANOARROW_TYPE_INTERVAL_MONTH_DAY_NANO: {
3586
+ const size_t size = sizeof(int32_t) + sizeof(int32_t) + sizeof(int64_t);
3587
+ memcpy(&out->months, data_view + i * size, sizeof(int32_t));
3588
+ memcpy(&out->days, data_view + i * size + 4, sizeof(int32_t));
3589
+ memcpy(&out->ns, data_view + i * size + 8, sizeof(int64_t));
3590
+ break;
3591
+ }
3592
+ default:
3593
+ break;
3594
+ }
3595
+ }
3596
+
3597
+ static inline void ArrowArrayViewGetDecimalUnsafe(
3598
+ struct ArrowArrayView* array_view, int64_t i, struct ArrowDecimal* out) {
3599
+ i += array_view->offset;
3600
+ const uint8_t* data_view = array_view->buffer_views[1].data.as_uint8;
3601
+ switch (array_view->storage_type) {
3602
+ case NANOARROW_TYPE_DECIMAL128:
3603
+ ArrowDecimalSetBytes(out, data_view + (i * 16));
3604
+ break;
3605
+ case NANOARROW_TYPE_DECIMAL256:
3606
+ ArrowDecimalSetBytes(out, data_view + (i * 32));
3607
+ break;
3608
+ default:
3609
+ memset(out->words, 0, sizeof(out->words));
3610
+ break;
3611
+ }
3612
+ }
3613
+
3614
+ #ifdef __cplusplus
3615
+ }
3616
+ #endif
3617
+
3618
+ #endif