| 1 | //===--------------------------------------------------------------------===// |
| 2 | // gather.cpp |
| 3 | // Description: This file contains the implementation of the gather operators |
| 4 | //===--------------------------------------------------------------------===// |
| 5 | |
| 6 | #include "duckdb/common/exception.hpp" |
| 7 | #include "duckdb/common/operator/constant_operators.hpp" |
| 8 | #include "duckdb/common/types/null_value.hpp" |
| 9 | #include "duckdb/common/vector_operations/vector_operations.hpp" |
| 10 | |
| 11 | using namespace duckdb; |
| 12 | using namespace std; |
| 13 | |
| 14 | template <class T> static void templated_gather_loop(Vector &source, Vector &dest, idx_t count) { |
| 15 | auto addresses = FlatVector::GetData<uintptr_t>(source); |
| 16 | auto data = FlatVector::GetData<T>(dest); |
| 17 | auto &nullmask = FlatVector::Nullmask(dest); |
| 18 | |
| 19 | for (idx_t i = 0; i < count; i++) { |
| 20 | auto dataptr = (T *)addresses[i]; |
| 21 | if (IsNullValue<T>(*dataptr)) { |
| 22 | nullmask[i] = true; |
| 23 | } else { |
| 24 | data[i] = *dataptr; |
| 25 | } |
| 26 | addresses[i] += sizeof(T); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | void VectorOperations::Gather::Set(Vector &source, Vector &dest, idx_t count) { |
| 31 | assert(source.vector_type == VectorType::FLAT_VECTOR); |
| 32 | assert(source.type == TypeId::POINTER); // "Cannot gather from non-pointer type!" |
| 33 | |
| 34 | dest.vector_type = VectorType::FLAT_VECTOR; |
| 35 | switch (dest.type) { |
| 36 | case TypeId::BOOL: |
| 37 | case TypeId::INT8: |
| 38 | templated_gather_loop<int8_t>(source, dest, count); |
| 39 | break; |
| 40 | case TypeId::INT16: |
| 41 | templated_gather_loop<int16_t>(source, dest, count); |
| 42 | break; |
| 43 | case TypeId::INT32: |
| 44 | templated_gather_loop<int32_t>(source, dest, count); |
| 45 | break; |
| 46 | case TypeId::INT64: |
| 47 | templated_gather_loop<int64_t>(source, dest, count); |
| 48 | break; |
| 49 | case TypeId::FLOAT: |
| 50 | templated_gather_loop<float>(source, dest, count); |
| 51 | break; |
| 52 | case TypeId::DOUBLE: |
| 53 | templated_gather_loop<double>(source, dest, count); |
| 54 | break; |
| 55 | case TypeId::POINTER: |
| 56 | templated_gather_loop<uintptr_t>(source, dest, count); |
| 57 | break; |
| 58 | case TypeId::VARCHAR: |
| 59 | templated_gather_loop<string_t>(source, dest, count); |
| 60 | break; |
| 61 | default: |
| 62 | throw NotImplementedException("Unimplemented type for gather" ); |
| 63 | } |
| 64 | } |
| 65 | |