1010#include " BinaryConverter.hpp"
1111#include " BooleanConverter.hpp"
1212#include " DateConverter.hpp"
13+ #include " TimeStampConverter.hpp"
1314#include " TimeConverter.hpp"
14- #include < iostream>
1515#include " logging.hpp"
1616
1717namespace sf
@@ -37,8 +37,7 @@ void CArrowChunkIterator::reset()
3737 m_currentBatchIndex = -1 ;
3838 m_rowIndexInBatch = -1 ;
3939 m_rowCountInBatch = 0 ;
40- Py_XDECREF (m_latestReturnedRow);
41- m_latestReturnedRow = nullptr ;
40+ m_latestReturnedRow.reset ();
4241
4342 logger.info (" Arrow chunk info: batchCount %d, columnCount %d" , m_batchCount,
4443 m_columnCount);
@@ -47,12 +46,11 @@ void CArrowChunkIterator::reset()
4746PyObject* CArrowChunkIterator::nextRow ()
4847{
4948 m_rowIndexInBatch++;
50- Py_XDECREF (m_latestReturnedRow);
51- m_latestReturnedRow = nullptr ;
5249
5350 if (m_rowIndexInBatch < m_rowCountInBatch)
5451 {
55- return this ->currentRowAsTuple ();
52+ this ->currentRowAsTuple ();
53+ return m_latestReturnedRow.get ();
5654 }
5755 else
5856 {
@@ -66,7 +64,8 @@ PyObject* CArrowChunkIterator::nextRow()
6664 logger.info (" Current batch index: %d, rows in current batch: %d" ,
6765 m_currentBatchIndex, m_rowCountInBatch);
6866
69- return this ->currentRowAsTuple ();
67+ this ->currentRowAsTuple ();
68+ return m_latestReturnedRow.get ();
7069 }
7170 }
7271
@@ -75,15 +74,16 @@ PyObject* CArrowChunkIterator::nextRow()
7574 return Py_None;
7675}
7776
78- PyObject* CArrowChunkIterator::currentRowAsTuple ()
77+ void CArrowChunkIterator::currentRowAsTuple ()
7978{
80- PyObject* tuple = PyTuple_New (m_columnCount);
79+ m_latestReturnedRow. reset ( PyTuple_New (m_columnCount) );
8180 for (int i = 0 ; i < m_columnCount; i++)
8281 {
8382 PyTuple_SET_ITEM (
84- tuple, i, m_currentBatchConverters[i]->toPyObject (m_rowIndexInBatch));
83+ m_latestReturnedRow.get (), i,
84+ m_currentBatchConverters[i]->toPyObject (m_rowIndexInBatch));
8585 }
86- return m_latestReturnedRow = tuple ;
86+ return ;
8787}
8888
8989void CArrowChunkIterator::initColumnConverters ()
@@ -189,10 +189,9 @@ void CArrowChunkIterator::initColumnConverters()
189189
190190 default :
191191 {
192- /* * cout is playing a placeholder here and will be replaced by
193- * exception soon */
194- std::cout << " unknown arrow internal data type (" << dt->id ()
195- << " ) for FIXED data" << std::endl;
192+ /* * TODO: how to throw an exception will be decided later */
193+ logger.error (" unknown arrow internal data type(%d) for FIXED data" ,
194+ dt->id ());
196195 break ;
197196 }
198197 }
@@ -259,23 +258,130 @@ void CArrowChunkIterator::initColumnConverters()
259258
260259 default :
261260 {
262- /* * cout is playing a placeholder here and will be replaced by
263- * exception soon */
264- std::cout << " unknown arrow internal data type (" << dt->id ()
265- << " ) for TIME data" << std::endl;
261+ /* * TODO: how to throw an exception will be decided later */
262+ logger.error (" unknown arrow internal data type(%d) for TIME data" ,
263+ dt->id ());
266264 break ;
267265 }
268266 }
269267 break ;
270268 }
271269
270+ case SnowflakeType::Type::TIMESTAMP_NTZ:
271+ {
272+ int scale = metaData
273+ ? std::stoi (metaData->value (metaData->FindKey (" scale" )))
274+ : 9 ;
275+ switch (dt->id ())
276+ {
277+ case arrow::Type::type::INT64:
278+ {
279+ m_currentBatchConverters.push_back (
280+ std::make_shared<sf::OneFieldTimeStampNTZConverter>(
281+ columnArray, scale, m_context));
282+ break ;
283+ }
284+
285+ case arrow::Type::type::STRUCT:
286+ {
287+ m_currentBatchConverters.push_back (
288+ std::make_shared<sf::TwoFieldTimeStampNTZConverter>(
289+ columnArray, scale, m_context));
290+ break ;
291+ }
292+
293+ default :
294+ {
295+ /* * TODO: how to throw an exception will be decided later */
296+ logger.error (
297+ " unknown arrow internal data type(%d) for TIMESTAMP_NTZ data" ,
298+ dt->id ());
299+ break ;
300+ }
301+ }
302+ break ;
303+ }
304+
305+ case SnowflakeType::Type::TIMESTAMP_LTZ:
306+ {
307+ int scale = metaData
308+ ? std::stoi (metaData->value (metaData->FindKey (" scale" )))
309+ : 9 ;
310+ switch (dt->id ())
311+ {
312+ case arrow::Type::type::INT64:
313+ {
314+ m_currentBatchConverters.push_back (
315+ std::make_shared<sf::OneFieldTimeStampLTZConverter>(
316+ columnArray, scale, m_context));
317+ break ;
318+ }
319+
320+ case arrow::Type::type::STRUCT:
321+ {
322+ m_currentBatchConverters.push_back (
323+ std::make_shared<sf::TwoFieldTimeStampLTZConverter>(
324+ columnArray, scale, m_context));
325+ break ;
326+ }
327+
328+ default :
329+ {
330+ /* * TODO: how to throw an exception will be decided later */
331+ logger.error (
332+ " unknown arrow internal data type(%d) for TIMESTAMP_LTZ data" ,
333+ dt->id ());
334+ break ;
335+ }
336+ }
337+ break ;
338+ }
339+
340+ case SnowflakeType::Type::TIMESTAMP_TZ:
341+ {
342+ int scale = metaData
343+ ? std::stoi (metaData->value (metaData->FindKey (" scale" )))
344+ : 9 ;
345+ int byteLength =
346+ metaData
347+ ? std::stoi (metaData->value (metaData->FindKey (" byteLength" )))
348+ : 16 ;
349+ switch (byteLength)
350+ {
351+ case 8 :
352+ {
353+ m_currentBatchConverters.push_back (
354+ std::make_shared<sf::TwoFieldTimeStampTZConverter>(
355+ columnArray, scale, m_context));
356+ break ;
357+ }
358+
359+ case 16 :
360+ {
361+ m_currentBatchConverters.push_back (
362+ std::make_shared<sf::ThreeFieldTimeStampTZConverter>(
363+ columnArray, scale, m_context));
364+ break ;
365+ }
366+
367+ default :
368+ {
369+ /* * TODO: how to throw an exception will be decided later */
370+ logger.error (
371+ " unknown arrow internal data type(%d) for TIMESTAMP_TZ data" ,
372+ dt->id ());
373+ break ;
374+ }
375+ }
376+
377+ break ;
378+ }
379+
272380 default :
273381 {
274- /* * cout is playing a placeholder here and will be replaced by exception
275- * soon */
276- std::cout << " [ERROR] unknown snowflake data type : "
277- << metaData->value (metaData->FindKey (" logicalType" ))
278- << std::endl;
382+ /* * TODO: how to throw an exception will be decided later */
383+ logger.error (" unknown snowflake data type : %d" ,
384+ metaData->value (metaData->FindKey (" logicalType" )));
279385 break ;
280386 }
281387 }
0 commit comments