23#include "ogr_recordbatch.h"
25class CPL_DLL OGRArrowArrayHelper
27 OGRArrowArrayHelper(
const OGRArrowArrayHelper &) =
delete;
28 OGRArrowArrayHelper &operator=(
const OGRArrowArrayHelper &) =
delete;
31 bool m_bIncludeFID =
false;
32 int m_nMaxBatchSize = 0;
34 const int m_nFieldCount = 0;
35 const int m_nGeomFieldCount = 0;
36 std::vector<int> m_mapOGRFieldToArrowField{};
37 std::vector<int> m_mapOGRGeomFieldToArrowField{};
38 std::vector<bool> m_abNullableFields{};
39 std::vector<uint32_t> m_anArrowFieldMaxAlloc{};
40 std::vector<int> m_anTZFlags{};
41 int64_t *m_panFIDValues =
nullptr;
42 struct ArrowArray *m_out_array =
nullptr;
44 static uint32_t GetMemLimit();
47 GetMaxFeaturesInBatch(
const CPLStringList &aosArrowArrayStreamOptions);
51 struct ArrowArray *out_array);
54 OGRArrowArrayHelper(
struct ArrowArray *out_array,
int nMaxBatchSize);
56 static bool SetNull(
struct ArrowArray *psArray,
int iFeat,
57 int nMaxBatchSize,
bool bAlignedMalloc)
59 ++psArray->null_count;
61 static_cast<uint8_t *
>(
const_cast<void *
>(psArray->buffers[0]));
62 if (psArray->buffers[0] ==
nullptr)
64 pabyNull =
static_cast<uint8_t *
>(
68 if (pabyNull ==
nullptr)
72 memset(pabyNull, 0xFF, (nMaxBatchSize + 7) / 8);
73 psArray->buffers[0] = pabyNull;
75 pabyNull[iFeat / 8] &=
static_cast<uint8_t
>(~(1 << (iFeat % 8)));
77 if (psArray->n_buffers == 3)
80 static_cast<int32_t *
>(
const_cast<void *
>(psArray->buffers[1]));
81 panOffsets[iFeat + 1] = panOffsets[iFeat];
86 bool SetNull(
int iArrowField,
int iFeat)
88 return SetNull(m_out_array->children[iArrowField], iFeat,
89 m_nMaxBatchSize,
true);
92 inline static void SetBoolOn(
struct ArrowArray *psArray,
int iFeat)
94 static_cast<uint8_t *
>(
95 const_cast<void *
>(psArray->buffers[1]))[iFeat / 8] |=
96 static_cast<uint8_t
>(1 << (iFeat % 8));
99 inline static void SetInt8(
struct ArrowArray *psArray,
int iFeat,
102 static_cast<int8_t *
>(
const_cast<void *
>(psArray->buffers[1]))[iFeat] =
106 inline static void SetUInt8(
struct ArrowArray *psArray,
int iFeat,
109 static_cast<uint8_t *
>(
const_cast<void *
>(psArray->buffers[1]))[iFeat] =
113 inline static void SetInt16(
struct ArrowArray *psArray,
int iFeat,
116 static_cast<int16_t *
>(
const_cast<void *
>(psArray->buffers[1]))[iFeat] =
120 inline static void SetUInt16(
struct ArrowArray *psArray,
int iFeat,
123 static_cast<uint16_t *
>(
124 const_cast<void *
>(psArray->buffers[1]))[iFeat] = nVal;
127 inline static void SetInt32(
struct ArrowArray *psArray,
int iFeat,
130 static_cast<int32_t *
>(
const_cast<void *
>(psArray->buffers[1]))[iFeat] =
134 inline static void SetUInt32(
struct ArrowArray *psArray,
int iFeat,
137 static_cast<uint32_t *
>(
138 const_cast<void *
>(psArray->buffers[1]))[iFeat] = nVal;
141 inline static void SetInt64(
struct ArrowArray *psArray,
int iFeat,
144 static_cast<int64_t *
>(
const_cast<void *
>(psArray->buffers[1]))[iFeat] =
148 inline static void SetUInt64(
struct ArrowArray *psArray,
int iFeat,
151 static_cast<uint64_t *
>(
152 const_cast<void *
>(psArray->buffers[1]))[iFeat] = nVal;
155 inline static void SetFloat(
struct ArrowArray *psArray,
int iFeat,
158 static_cast<float *
>(
const_cast<void *
>(psArray->buffers[1]))[iFeat] =
162 inline static void SetDouble(
struct ArrowArray *psArray,
int iFeat,
165 static_cast<double *
>(
const_cast<void *
>(psArray->buffers[1]))[iFeat] =
169 static void SetDate(
struct ArrowArray *psArray,
int iFeat,
170 struct tm &brokenDown,
const OGRField &ogrField)
172 brokenDown.tm_year = ogrField.Date.Year - 1900;
173 brokenDown.tm_mon = ogrField.Date.Month - 1;
174 brokenDown.tm_mday = ogrField.Date.Day;
175 brokenDown.tm_hour = 0;
176 brokenDown.tm_min = 0;
177 brokenDown.tm_sec = 0;
178 static_cast<int32_t *
>(
const_cast<void *
>(psArray->buffers[1]))[iFeat] =
179 static_cast<int>(CPLYMDHMSToUnixTime(&brokenDown) / 86400);
182 static void SetDateTime(
struct ArrowArray *psArray,
int iFeat,
183 struct tm &brokenDown,
int nFieldTZFlag,
186 brokenDown.tm_year = ogrField.Date.Year - 1900;
187 brokenDown.tm_mon = ogrField.Date.Month - 1;
188 brokenDown.tm_mday = ogrField.Date.Day;
189 brokenDown.tm_hour = ogrField.Date.Hour;
190 brokenDown.tm_min = ogrField.Date.Minute;
191 brokenDown.tm_sec =
static_cast<int>(ogrField.Date.Second);
193 CPLYMDHMSToUnixTime(&brokenDown) * 1000 +
194 (
static_cast<int>(ogrField.Date.Second * 1000 + 0.5f) % 1000);
199 const int TZOffset = (ogrField.Date.TZFlag -
OGR_TZFLAG_UTC) * 15;
200 const int TZOffsetMS = TZOffset * 60 * 1000;
203 if (psArray->n_children == 2)
205 static_cast<int64_t *
>(
const_cast<void *
>(
206 psArray->children[0]->buffers[1]))[iFeat] = nVal;
207 const int nOffsetMinutes =
211 static_cast<int16_t *
>(
212 const_cast<void *
>(psArray->children[1]->buffers[1]))[iFeat] =
213 static_cast<int16_t
>(nOffsetMinutes);
217 static_cast<int64_t *
>(
218 const_cast<void *
>(psArray->buffers[1]))[iFeat] = nVal;
222 static GByte *GetPtrForStringOrBinary(
struct ArrowArray *psArray,
int iFeat,
223 size_t nLen, uint32_t &nMaxAlloc,
227 static_cast<int32_t *
>(
const_cast<void *
>(psArray->buffers[1]));
228 const uint32_t nCurLength =
static_cast<uint32_t
>(panOffsets[iFeat]);
230 if (nLen > nMaxAlloc - nCurLength)
232 constexpr uint32_t INT32_MAX_AS_UINT32 =
233 static_cast<uint32_t
>(std::numeric_limits<int32_t>::max());
234 if (!(nCurLength <= INT32_MAX_AS_UINT32 &&
235 nLen <= INT32_MAX_AS_UINT32 - nCurLength))
238 "Too large string or binary content");
241 uint32_t nNewSize = nCurLength +
static_cast<uint32_t
>(nLen);
242 if (nMaxAlloc <= INT32_MAX_AS_UINT32)
244 const uint32_t nDoubleSize = 2U * nMaxAlloc;
245 if (nNewSize < nDoubleSize)
246 nNewSize = nDoubleSize;
252 if (newBuffer ==
nullptr)
254 nMaxAlloc = nNewSize;
255 memcpy(newBuffer, psArray->buffers[2], nCurLength);
262 const_cast<void *
>(psArray->buffers[2]), nNewSize);
263 if (newBuffer ==
nullptr)
265 nMaxAlloc = nNewSize;
267 psArray->buffers[2] = newBuffer;
271 static_cast<GByte *
>(
const_cast<void *
>(psArray->buffers[2])) +
273 panOffsets[iFeat + 1] = panOffsets[iFeat] +
static_cast<int32_t
>(nLen);
277 GByte *GetPtrForStringOrBinary(
int iArrowField,
int iFeat,
size_t nLen,
278 bool bAlignedMalloc =
true)
280 auto psArray = m_out_array->children[iArrowField];
281 return GetPtrForStringOrBinary(psArray, iFeat, nLen,
282 m_anArrowFieldMaxAlloc[iArrowField],
286 static void SetEmptyStringOrBinary(
struct ArrowArray *psArray,
int iFeat)
289 static_cast<int32_t *
>(
const_cast<void *
>(psArray->buffers[1]));
290 panOffsets[iFeat + 1] = panOffsets[iFeat];
293 void Shrink(
int nFeatures)
295 if (nFeatures < m_nMaxBatchSize)
297 m_out_array->length = nFeatures;
298 for (
int i = 0; i < m_nChildren; i++)
300 m_out_array->children[i]->length = nFeatures;
307 if (m_out_array->release)
308 m_out_array->release(m_out_array);
309 memset(m_out_array, 0,
sizeof(*m_out_array));
312 static bool FillDict(
struct ArrowArray *psChild,
String list class designed around our use of C "char**" string lists.
Definition cpl_string.h:476
A set of associated raster bands, usually from one file.
Definition gdal_dataset.h:77
Definition of a coded / enumerated field domain.
Definition ogr_feature.h:1911
Definition of a feature class or feature layer.
Definition ogr_feature.h:521
@ CE_Failure
Error that prevents the current operation to succeed.
Definition cpl_error.h:60
#define CPLE_AppDefined
Application defined error.
Definition cpl_error.h:108
unsigned char GByte
Unsigned byte type.
Definition cpl_port.h:165
#define VSI_REALLOC_VERBOSE(pOldPtr, nNewSize)
VSI_REALLOC_VERBOSE.
Definition cpl_vsi.h:405
#define VSI_MALLOC_ALIGNED_AUTO_VERBOSE(size)
VSIMallocAlignedAutoVerbose() with FILE and LINE reporting.
Definition cpl_vsi.h:348
#define VSI_MALLOC_VERBOSE(size)
VSI_MALLOC_VERBOSE.
Definition cpl_vsi.h:375
void VSIFreeAligned(void *ptr)
Free a buffer allocated with VSIMallocAligned().
Definition cpl_vsisimple.cpp:981
#define OGR_TZFLAG_UTC
Time zone flag indicating UTC.
Definition ogr_core.h:893
#define OGR_TZFLAG_MIXED_TZ
Time zone flag only returned by OGRFieldDefn::GetTZFlag() to indicate that all values in the field ha...
Definition ogr_core.h:885
Classes related to registration of format support, and opening datasets.
OGRFeature field attribute value union.
Definition ogr_core.h:904