|
| 1 | +// |
| 2 | +// Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved. |
| 3 | +// |
| 4 | + |
| 5 | +#include "FixedSizeListConverter.hpp" |
| 6 | + |
| 7 | +namespace sf |
| 8 | +{ |
| 9 | +Logger* FixedSizeListConverter::logger = |
| 10 | + new Logger("snowflake.connector.FixedSizeListConverter"); |
| 11 | + |
| 12 | +FixedSizeListConverter::FixedSizeListConverter(ArrowArrayView* array) |
| 13 | +: m_array(array) |
| 14 | +{ |
| 15 | +} |
| 16 | + |
| 17 | +void FixedSizeListConverter::generateError(const std::string& msg) const |
| 18 | +{ |
| 19 | + logger->error(__FILE__, __func__, __LINE__, msg.c_str()); |
| 20 | + PyErr_SetString(PyExc_Exception, msg.c_str()); |
| 21 | +} |
| 22 | + |
| 23 | +PyObject* FixedSizeListConverter::toPyObject(int64_t rowIndex) const |
| 24 | +{ |
| 25 | + if (ArrowArrayViewIsNull(m_array, rowIndex)) |
| 26 | + { |
| 27 | + Py_RETURN_NONE; |
| 28 | + } |
| 29 | + |
| 30 | + if (m_array->n_children != 1) |
| 31 | + { |
| 32 | + std::string errorInfo = Logger::formatString( |
| 33 | + "[Snowflake Exception] invalid arrow element schema for fixed size " |
| 34 | + "list: got (%d) " |
| 35 | + "children", |
| 36 | + m_array->n_children); |
| 37 | + this->generateError(errorInfo); |
| 38 | + return nullptr; |
| 39 | + } |
| 40 | + |
| 41 | + // m_array->length represents the number of fixed size lists in the array |
| 42 | + // m_array->children[0] has a buffer view that contains the actual data of |
| 43 | + // each list, back-to-back m_array->children[0]->length represents the sum of |
| 44 | + // the lengths of the fixed size lists in the array. |
| 45 | + |
| 46 | + ArrowArrayView* elements = m_array->children[0]; |
| 47 | + const auto fixedSizeArrayLength = elements->length / m_array->length; |
| 48 | + PyObject* list = PyList_New(fixedSizeArrayLength); |
| 49 | + |
| 50 | + const int64_t startIndexWithoutOffset = rowIndex * fixedSizeArrayLength; |
| 51 | + for (int64_t i = 0; i < fixedSizeArrayLength; ++i) |
| 52 | + { |
| 53 | + const auto bufferIndexWithoutOffset = startIndexWithoutOffset + i; |
| 54 | + // Currently, the backend only sends back INT32 and FLOAT32, but the |
| 55 | + // remaining types are enumerated for future use. |
| 56 | + switch (elements->storage_type) |
| 57 | + { |
| 58 | + case NANOARROW_TYPE_INT8: |
| 59 | + case NANOARROW_TYPE_INT16: |
| 60 | + case NANOARROW_TYPE_INT32: |
| 61 | + case NANOARROW_TYPE_INT64: |
| 62 | + { |
| 63 | + const auto value = |
| 64 | + ArrowArrayViewGetIntUnsafe(elements, bufferIndexWithoutOffset); |
| 65 | + PyList_SetItem(list, i, PyLong_FromLongLong(value)); |
| 66 | + } break; |
| 67 | + case NANOARROW_TYPE_HALF_FLOAT: |
| 68 | + case NANOARROW_TYPE_FLOAT: |
| 69 | + case NANOARROW_TYPE_DOUBLE: |
| 70 | + { |
| 71 | + const auto value = |
| 72 | + ArrowArrayViewGetDoubleUnsafe(elements, bufferIndexWithoutOffset); |
| 73 | + PyList_SetItem(list, i, PyFloat_FromDouble(value)); |
| 74 | + } break; |
| 75 | + default: |
| 76 | + std::string errorInfo = Logger::formatString( |
| 77 | + "[Snowflake Exception] invalid arrow element type for fixed size " |
| 78 | + "list: got (%s)", |
| 79 | + ArrowTypeString(elements->storage_type)); |
| 80 | + this->generateError(errorInfo); |
| 81 | + return nullptr; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return list; |
| 86 | +} |
| 87 | + |
| 88 | +} // namespace sf |
0 commit comments