|
| 1 | +/* |
| 2 | + * Copyright (c) 2013-2019 Snowflake Computing |
| 3 | + */ |
| 4 | +#ifndef PC_TIMECONVERTER_HPP |
| 5 | +#define PC_TIMECONVERTER_HPP |
| 6 | + |
| 7 | +#include "IColumnConverter.hpp" |
| 8 | +#include "Python/Common.hpp" |
| 9 | +#include "Python/Helpers.hpp" |
| 10 | +#include "Util/time.hpp" |
| 11 | + |
| 12 | +namespace sf |
| 13 | +{ |
| 14 | + |
| 15 | +template <typename T> |
| 16 | +class TimeConverter : public IColumnConverter |
| 17 | +{ |
| 18 | +public: |
| 19 | + TimeConverter(std::shared_ptr<arrow::Array> array, int32_t scale) |
| 20 | + : m_array(std::dynamic_pointer_cast<T>(array)), m_scale(scale) |
| 21 | + { |
| 22 | + } |
| 23 | + |
| 24 | + PyObject* toPyObject(int64_t rowIndex) override; |
| 25 | + |
| 26 | +private: |
| 27 | + /** can be arrow::Int32Array and arrow::Int64Array */ |
| 28 | + std::shared_ptr<T> m_array; |
| 29 | + |
| 30 | + int32_t m_scale; |
| 31 | + |
| 32 | + static py::UniqueRef& m_pyDatetimeTime(); |
| 33 | +}; |
| 34 | + |
| 35 | +template <typename T> |
| 36 | +PyObject* TimeConverter<T>::toPyObject(int64_t rowIndex) |
| 37 | +{ |
| 38 | + if (m_array->IsValid(rowIndex)) |
| 39 | + { |
| 40 | + int64_t seconds = m_array->Value(rowIndex); |
| 41 | + using namespace internal; |
| 42 | + py::PyUniqueLock lock; |
| 43 | + return PyObject_CallFunction(m_pyDatetimeTime().get(), "iiii", |
| 44 | + getHourFromSeconds(seconds, m_scale), |
| 45 | + getMinuteFromSeconds(seconds, m_scale), |
| 46 | + getSecondFromSeconds(seconds, m_scale), |
| 47 | + getMicrosecondFromSeconds(seconds, m_scale)); |
| 48 | + } |
| 49 | + else |
| 50 | + { |
| 51 | + Py_RETURN_NONE; |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +template <typename T> |
| 56 | +py::UniqueRef& TimeConverter<T>::m_pyDatetimeTime() |
| 57 | +{ |
| 58 | + static py::UniqueRef pyDatetimeTime; |
| 59 | + if (pyDatetimeTime.empty()) |
| 60 | + { |
| 61 | + py::PyUniqueLock lock; |
| 62 | + py::UniqueRef pyDatetimeModule; |
| 63 | + py::importPythonModule("datetime", pyDatetimeModule); |
| 64 | + /** TODO : to check status here */ |
| 65 | + |
| 66 | + py::importFromModule(pyDatetimeModule, "time", pyDatetimeTime); |
| 67 | + } |
| 68 | + return pyDatetimeTime; |
| 69 | +} |
| 70 | + |
| 71 | +} // namespace sf |
| 72 | + |
| 73 | +#endif // PC_TIMECONVERTER_HPP |
0 commit comments