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