39 #include "ogr_recordbatch.h"
41 class CPL_DLL OGRArrowArrayHelper
43 OGRArrowArrayHelper(
const OGRArrowArrayHelper &) =
delete;
44 OGRArrowArrayHelper &operator=(
const OGRArrowArrayHelper &) =
delete;
47 bool m_bIncludeFID =
false;
48 int m_nMaxBatchSize = 0;
50 const int m_nFieldCount = 0;
51 const int m_nGeomFieldCount = 0;
52 std::vector<int> m_mapOGRFieldToArrowField{};
53 std::vector<int> m_mapOGRGeomFieldToArrowField{};
54 std::vector<bool> m_abNullableFields{};
55 std::vector<uint32_t> m_anArrowFieldMaxAlloc{};
56 std::vector<int> m_anTZFlags{};
57 int64_t *m_panFIDValues =
nullptr;
58 struct ArrowArray *m_out_array =
nullptr;
60 static uint32_t GetMemLimit();
63 GetMaxFeaturesInBatch(
const CPLStringList &aosArrowArrayStreamOptions);
67 struct ArrowArray *out_array);
69 bool SetNull(
int iArrowField,
int iFeat)
71 auto psArray = m_out_array->children[iArrowField];
72 ++psArray->null_count;
74 static_cast<uint8_t *
>(
const_cast<void *
>(psArray->buffers[0]));
75 if (psArray->buffers[0] ==
nullptr)
77 pabyNull =
static_cast<uint8_t *
>(
79 if (pabyNull ==
nullptr)
83 memset(pabyNull, 0xFF, (m_nMaxBatchSize + 7) / 8);
84 psArray->buffers[0] = pabyNull;
86 pabyNull[iFeat / 8] &=
static_cast<uint8_t
>(~(1 << (iFeat % 8)));
88 if (psArray->n_buffers == 3)
91 static_cast<int32_t *
>(
const_cast<void *
>(psArray->buffers[1]));
92 panOffsets[iFeat + 1] = panOffsets[iFeat];
97 inline static void SetBoolOn(
struct ArrowArray *psArray,
int iFeat)
99 static_cast<uint8_t *
>(
100 const_cast<void *
>(psArray->buffers[1]))[iFeat / 8] |=
101 static_cast<uint8_t
>(1 << (iFeat % 8));
104 inline static void SetInt8(
struct ArrowArray *psArray,
int iFeat,
107 static_cast<int8_t *
>(
const_cast<void *
>(psArray->buffers[1]))[iFeat] =
111 inline static void SetUInt8(
struct ArrowArray *psArray,
int iFeat,
114 static_cast<uint8_t *
>(
const_cast<void *
>(psArray->buffers[1]))[iFeat] =
118 inline static void SetInt16(
struct ArrowArray *psArray,
int iFeat,
121 static_cast<int16_t *
>(
const_cast<void *
>(psArray->buffers[1]))[iFeat] =
125 inline static void SetUInt16(
struct ArrowArray *psArray,
int iFeat,
128 static_cast<uint16_t *
>(
129 const_cast<void *
>(psArray->buffers[1]))[iFeat] = nVal;
132 inline static void SetInt32(
struct ArrowArray *psArray,
int iFeat,
135 static_cast<int32_t *
>(
const_cast<void *
>(psArray->buffers[1]))[iFeat] =
139 inline static void SetUInt32(
struct ArrowArray *psArray,
int iFeat,
142 static_cast<uint32_t *
>(
143 const_cast<void *
>(psArray->buffers[1]))[iFeat] = nVal;
146 inline static void SetInt64(
struct ArrowArray *psArray,
int iFeat,
149 static_cast<int64_t *
>(
const_cast<void *
>(psArray->buffers[1]))[iFeat] =
153 inline static void SetUInt64(
struct ArrowArray *psArray,
int iFeat,
156 static_cast<uint64_t *
>(
157 const_cast<void *
>(psArray->buffers[1]))[iFeat] = nVal;
160 inline static void SetFloat(
struct ArrowArray *psArray,
int iFeat,
163 static_cast<float *
>(
const_cast<void *
>(psArray->buffers[1]))[iFeat] =
167 inline static void SetDouble(
struct ArrowArray *psArray,
int iFeat,
170 static_cast<double *
>(
const_cast<void *
>(psArray->buffers[1]))[iFeat] =
174 static void SetDate(
struct ArrowArray *psArray,
int iFeat,
175 struct tm &brokenDown,
const OGRField &ogrField)
177 brokenDown.tm_year = ogrField.Date.Year - 1900;
178 brokenDown.tm_mon = ogrField.Date.Month - 1;
179 brokenDown.tm_mday = ogrField.Date.Day;
180 brokenDown.tm_hour = 0;
181 brokenDown.tm_min = 0;
182 brokenDown.tm_sec = 0;
183 static_cast<int32_t *
>(
const_cast<void *
>(psArray->buffers[1]))[iFeat] =
184 static_cast<int>(CPLYMDHMSToUnixTime(&brokenDown) / 86400);
187 static void SetDateTime(
struct ArrowArray *psArray,
int iFeat,
188 struct tm &brokenDown,
int nFieldTZFlag,
191 brokenDown.tm_year = ogrField.Date.Year - 1900;
192 brokenDown.tm_mon = ogrField.Date.Month - 1;
193 brokenDown.tm_mday = ogrField.Date.Day;
194 brokenDown.tm_hour = ogrField.Date.Hour;
195 brokenDown.tm_min = ogrField.Date.Minute;
196 brokenDown.tm_sec =
static_cast<int>(ogrField.Date.Second);
198 CPLYMDHMSToUnixTime(&brokenDown) * 1000 +
199 (
static_cast<int>(ogrField.Date.Second * 1000 + 0.5) % 1000);
204 const int TZOffset = (ogrField.Date.TZFlag -
OGR_TZFLAG_UTC) * 15;
205 const int TZOffsetMS = TZOffset * 60 * 1000;
208 static_cast<int64_t *
>(
const_cast<void *
>(psArray->buffers[1]))[iFeat] =
212 GByte *GetPtrForStringOrBinary(
int iArrowField,
int iFeat,
size_t nLen)
214 auto psArray = m_out_array->children[iArrowField];
216 static_cast<int32_t *
>(
const_cast<void *
>(psArray->buffers[1]));
217 const uint32_t nCurLength =
static_cast<uint32_t
>(panOffsets[iFeat]);
218 if (nLen > m_anArrowFieldMaxAlloc[iArrowField] - nCurLength)
221 static_cast<uint32_t
>(std::numeric_limits<int32_t>::max()) -
225 "Too large string or binary content");
228 uint32_t nNewSize = nCurLength +
static_cast<uint32_t
>(nLen);
229 if ((m_anArrowFieldMaxAlloc[iArrowField] >> 31) == 0)
231 const uint32_t nDoubleSize =
232 2U * m_anArrowFieldMaxAlloc[iArrowField];
233 if (nNewSize < nDoubleSize)
234 nNewSize = nDoubleSize;
237 if (newBuffer ==
nullptr)
239 m_anArrowFieldMaxAlloc[iArrowField] = nNewSize;
240 memcpy(newBuffer, psArray->buffers[2], nCurLength);
242 psArray->buffers[2] = newBuffer;
245 static_cast<GByte *
>(
const_cast<void *
>(psArray->buffers[2])) +
247 panOffsets[iFeat + 1] = panOffsets[iFeat] +
static_cast<int32_t
>(nLen);
251 static void SetEmptyStringOrBinary(
struct ArrowArray *psArray,
int iFeat)
254 static_cast<int32_t *
>(
const_cast<void *
>(psArray->buffers[1]));
255 panOffsets[iFeat + 1] = panOffsets[iFeat];
258 void Shrink(
int nFeatures)
260 if (nFeatures < m_nMaxBatchSize)
262 m_out_array->length = nFeatures;
263 for (
int i = 0; i < m_nChildren; i++)
265 m_out_array->children[i]->length = nFeatures;
272 if (m_out_array->release)
273 m_out_array->release(m_out_array);
274 memset(m_out_array, 0,
sizeof(*m_out_array));
277 static bool FillDict(
struct ArrowArray *psChild,
String list class designed around our use of C "char**" string lists.
Definition: cpl_string.h:449
A set of associated raster bands, usually from one file.
Definition: gdal_priv.h:503
Definition of a coded / enumerated field domain.
Definition: ogr_feature.h:1758
Definition of a feature class or feature layer.
Definition: ogr_feature.h:517
#define CPLE_AppDefined
Application defined error.
Definition: cpl_error.h:100
void CPLError(CPLErr eErrClass, CPLErrorNum err_no, const char *fmt,...)
Report an error.
Definition: cpl_error.cpp:330
unsigned char GByte
Unsigned byte type.
Definition: cpl_port.h:185
#define VSI_MALLOC_ALIGNED_AUTO_VERBOSE(size)
VSIMallocAlignedAutoVerbose() with FILE and LINE reporting.
Definition: cpl_vsi.h:321
void VSIFreeAligned(void *ptr)
Free a buffer allocated with VSIMallocAligned().
Definition: cpl_vsisimple.cpp:995
#define OGR_TZFLAG_UTC
Time zone flag indicating UTC.
Definition: ogr_core.h:899
#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:891
Classes related to registration of format support, and opening datasets.
OGRFeature field attribute value union.
Definition: ogr_core.h:910