|
| 1 | +#include "numpy/numpy_scan.h" |
| 2 | + |
| 3 | +#include "common/types/timestamp_t.h" |
| 4 | +#include "pandas/pandas_bind.h" |
| 5 | + |
| 6 | +namespace kuzu { |
| 7 | + |
| 8 | +using namespace kuzu::common; |
| 9 | + |
| 10 | +template<class T> |
| 11 | +void ScanNumpyColumn( |
| 12 | + py::array& npArray, uint64_t offset, ValueVector* outputVector, uint64_t count) { |
| 13 | + auto srcData = (T*)npArray.data(); |
| 14 | + memcpy(outputVector->getData(), srcData + offset, count * sizeof(T)); |
| 15 | +} |
| 16 | + |
| 17 | +template<class T> |
| 18 | +void scanNumpyMasked( |
| 19 | + PandasColumnBindData* bindData, uint64_t count, uint64_t offset, ValueVector* outputVector) { |
| 20 | + KU_ASSERT(bindData->pandasCol->getBackEnd() == PandasColumnBackend::NUMPY); |
| 21 | + auto& npColumn = reinterpret_cast<PandasNumpyColumn&>(*bindData->pandasCol); |
| 22 | + ScanNumpyColumn<T>(npColumn.array, offset, outputVector, count); |
| 23 | + if (bindData->mask != nullptr) { |
| 24 | + KU_UNREACHABLE; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +template<typename T> |
| 29 | +void setNullIfNan(T value, uint64_t pos, ValueVector* outputVector) { |
| 30 | + if (std::isnan(value)) { |
| 31 | + outputVector->setNull(pos, true /* isNull */); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +template<class T> |
| 36 | +void ScanNumpyFpColumn( |
| 37 | + const T* srcData, uint64_t count, uint64_t offset, ValueVector* outputVector) { |
| 38 | + memcpy(outputVector->getData(), srcData + offset, count * sizeof(T)); |
| 39 | + for (auto i = 0u; i < count; i++) { |
| 40 | + setNullIfNan(outputVector->getValue<T>(i), i, outputVector); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +void NumpyScan::scan(PandasColumnBindData* bindData, uint64_t count, uint64_t offset, |
| 45 | + common::ValueVector* outputVector) { |
| 46 | + KU_ASSERT(bindData->pandasCol->getBackEnd() == PandasColumnBackend::NUMPY); |
| 47 | + auto& npCol = reinterpret_cast<PandasNumpyColumn&>(*bindData->pandasCol); |
| 48 | + auto& array = npCol.array; |
| 49 | + |
| 50 | + switch (bindData->npType.type) { |
| 51 | + case NumpyNullableType::BOOL: |
| 52 | + scanNumpyMasked<bool>(bindData, count, offset, outputVector); |
| 53 | + break; |
| 54 | + case NumpyNullableType::UINT_8: |
| 55 | + scanNumpyMasked<uint8_t>(bindData, count, offset, outputVector); |
| 56 | + break; |
| 57 | + case NumpyNullableType::UINT_16: |
| 58 | + scanNumpyMasked<uint16_t>(bindData, count, offset, outputVector); |
| 59 | + break; |
| 60 | + case NumpyNullableType::UINT_32: |
| 61 | + scanNumpyMasked<uint32_t>(bindData, count, offset, outputVector); |
| 62 | + break; |
| 63 | + case NumpyNullableType::UINT_64: |
| 64 | + scanNumpyMasked<uint64_t>(bindData, count, offset, outputVector); |
| 65 | + break; |
| 66 | + case NumpyNullableType::INT_8: |
| 67 | + scanNumpyMasked<int8_t>(bindData, count, offset, outputVector); |
| 68 | + break; |
| 69 | + case NumpyNullableType::INT_16: |
| 70 | + scanNumpyMasked<int16_t>(bindData, count, offset, outputVector); |
| 71 | + break; |
| 72 | + case NumpyNullableType::INT_32: |
| 73 | + scanNumpyMasked<int32_t>(bindData, count, offset, outputVector); |
| 74 | + break; |
| 75 | + case NumpyNullableType::INT_64: |
| 76 | + scanNumpyMasked<int64_t>(bindData, count, offset, outputVector); |
| 77 | + break; |
| 78 | + case NumpyNullableType::FLOAT_32: |
| 79 | + ScanNumpyFpColumn<float>( |
| 80 | + reinterpret_cast<const float*>(array.data()), count, offset, outputVector); |
| 81 | + break; |
| 82 | + case NumpyNullableType::FLOAT_64: |
| 83 | + ScanNumpyFpColumn<double>( |
| 84 | + reinterpret_cast<const double*>(array.data()), count, offset, outputVector); |
| 85 | + break; |
| 86 | + case NumpyNullableType::DATETIME_NS: |
| 87 | + case NumpyNullableType::DATETIME_US: { |
| 88 | + auto sourceData = reinterpret_cast<const int64_t*>(array.data()); |
| 89 | + auto dstData = reinterpret_cast<timestamp_t*>(outputVector->getData()); |
| 90 | + auto timestampCastFunc = bindData->npType.type == NumpyNullableType::DATETIME_NS ? |
| 91 | + Timestamp::fromEpochNanoSeconds : |
| 92 | + Timestamp::fromEpochMicroSeconds; |
| 93 | + for (auto i = 0u; i < count; i++) { |
| 94 | + auto pos = offset + i; |
| 95 | + dstData[i] = timestampCastFunc(sourceData[pos]); |
| 96 | + outputVector->setNull(i, false /* isNull */); |
| 97 | + } |
| 98 | + break; |
| 99 | + } |
| 100 | + case NumpyNullableType::TIMEDELTA: { |
| 101 | + auto sourceData = reinterpret_cast<const int64_t*>(array.data()); |
| 102 | + auto dstData = reinterpret_cast<interval_t*>(outputVector->getData()); |
| 103 | + for (auto i = 0u; i < count; i++) { |
| 104 | + auto pos = offset + i; |
| 105 | + auto micro = sourceData[pos] / 1000; |
| 106 | + auto days = micro / Interval::MICROS_PER_DAY; |
| 107 | + micro = micro % Interval::MICROS_PER_DAY; |
| 108 | + auto months = days / Interval::DAYS_PER_MONTH; |
| 109 | + days = days % Interval::DAYS_PER_MONTH; |
| 110 | + interval_t interval; |
| 111 | + interval.months = months; |
| 112 | + interval.days = days; |
| 113 | + interval.micros = micro; |
| 114 | + dstData[i] = interval; |
| 115 | + outputVector->setNull(i, false /* isNull */); |
| 116 | + } |
| 117 | + break; |
| 118 | + } |
| 119 | + default: |
| 120 | + KU_UNREACHABLE; |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +} // namespace kuzu |
0 commit comments