1#include "duckdb/common/exception.hpp"
2#include "duckdb/common/types/null_value.hpp"
3#include "duckdb/common/vector_operations/vector_operations.hpp"
4
5namespace duckdb {
6
7template <class T> static void CopyToStorageLoop(VectorData &vdata, idx_t count, data_ptr_t target) {
8 auto ldata = (T *)vdata.data;
9 auto result_data = (T *)target;
10 for (idx_t i = 0; i < count; i++) {
11 auto idx = vdata.sel->get_index(i);
12 if ((*vdata.nullmask)[idx]) {
13 result_data[i] = NullValue<T>();
14 } else {
15 result_data[i] = ldata[idx];
16 }
17 }
18}
19
20void VectorOperations::WriteToStorage(Vector &source, idx_t count, data_ptr_t target) {
21 if (count == 0) {
22 return;
23 }
24 VectorData vdata;
25 source.Orrify(count, vdata);
26
27 switch (source.type) {
28 case TypeId::BOOL:
29 case TypeId::INT8:
30 CopyToStorageLoop<int8_t>(vdata, count, target);
31 break;
32 case TypeId::INT16:
33 CopyToStorageLoop<int16_t>(vdata, count, target);
34 break;
35 case TypeId::INT32:
36 CopyToStorageLoop<int32_t>(vdata, count, target);
37 break;
38 case TypeId::INT64:
39 CopyToStorageLoop<int64_t>(vdata, count, target);
40 break;
41 case TypeId::HASH:
42 CopyToStorageLoop<hash_t>(vdata, count, target);
43 break;
44 case TypeId::POINTER:
45 CopyToStorageLoop<uintptr_t>(vdata, count, target);
46 break;
47 case TypeId::FLOAT:
48 CopyToStorageLoop<float>(vdata, count, target);
49 break;
50 case TypeId::DOUBLE:
51 CopyToStorageLoop<double>(vdata, count, target);
52 break;
53 default:
54 throw NotImplementedException("Unimplemented type for CopyToStorage");
55 }
56}
57
58template <class T> static void ReadFromStorageLoop(data_ptr_t source, idx_t count, Vector &result) {
59 auto ldata = (T *)source;
60 auto result_data = FlatVector::GetData<T>(result);
61 auto &nullmask = FlatVector::Nullmask(result);
62 for (idx_t i = 0; i < count; i++) {
63 if (IsNullValue<T>(ldata[i])) {
64 nullmask[i] = true;
65 } else {
66 result_data[i] = ldata[i];
67 }
68 }
69}
70
71void VectorOperations::ReadFromStorage(data_ptr_t source, idx_t count, Vector &result) {
72 result.vector_type = VectorType::FLAT_VECTOR;
73 switch (result.type) {
74 case TypeId::BOOL:
75 case TypeId::INT8:
76 ReadFromStorageLoop<int8_t>(source, count, result);
77 break;
78 case TypeId::INT16:
79 ReadFromStorageLoop<int16_t>(source, count, result);
80 break;
81 case TypeId::INT32:
82 ReadFromStorageLoop<int32_t>(source, count, result);
83 break;
84 case TypeId::INT64:
85 ReadFromStorageLoop<int64_t>(source, count, result);
86 break;
87 case TypeId::HASH:
88 ReadFromStorageLoop<hash_t>(source, count, result);
89 break;
90 case TypeId::POINTER:
91 ReadFromStorageLoop<uintptr_t>(source, count, result);
92 break;
93 case TypeId::FLOAT:
94 ReadFromStorageLoop<float>(source, count, result);
95 break;
96 case TypeId::DOUBLE:
97 ReadFromStorageLoop<double>(source, count, result);
98 break;
99 default:
100 throw NotImplementedException("Unimplemented type for CopyToStorage");
101 }
102}
103
104} // namespace duckdb
105