1 | //===--------------------------------------------------------------------===// |
2 | // generators.cpp |
3 | // Description: This file contains the implementation of different generators |
4 | //===--------------------------------------------------------------------===// |
5 | |
6 | #include "duckdb/common/exception.hpp" |
7 | #include "duckdb/common/vector_operations/vector_operations.hpp" |
8 | |
9 | using namespace duckdb; |
10 | using namespace std; |
11 | |
12 | template <class T> void templated_generate_sequence(Vector &result, idx_t count, int64_t start, int64_t increment) { |
13 | assert(TypeIsNumeric(result.type)); |
14 | if (start > numeric_limits<T>::max() || increment > numeric_limits<T>::max()) { |
15 | throw Exception("Sequence start or increment out of type range" ); |
16 | } |
17 | result.vector_type = VectorType::FLAT_VECTOR; |
18 | auto result_data = FlatVector::GetData<T>(result); |
19 | auto value = (T)start; |
20 | for (idx_t i = 0; i < count; i++) { |
21 | result_data[i] = value; |
22 | value += increment; |
23 | } |
24 | } |
25 | |
26 | void VectorOperations::GenerateSequence(Vector &result, idx_t count, int64_t start, int64_t increment) { |
27 | if (!TypeIsNumeric(result.type)) { |
28 | throw InvalidTypeException(result.type, "Can only generate sequences for numeric values!" ); |
29 | } |
30 | switch (result.type) { |
31 | case TypeId::INT8: |
32 | templated_generate_sequence<int8_t>(result, count, start, increment); |
33 | break; |
34 | case TypeId::INT16: |
35 | templated_generate_sequence<int16_t>(result, count, start, increment); |
36 | break; |
37 | case TypeId::INT32: |
38 | templated_generate_sequence<int32_t>(result, count, start, increment); |
39 | break; |
40 | case TypeId::INT64: |
41 | templated_generate_sequence<int64_t>(result, count, start, increment); |
42 | break; |
43 | case TypeId::FLOAT: |
44 | templated_generate_sequence<float>(result, count, start, increment); |
45 | break; |
46 | case TypeId::DOUBLE: |
47 | templated_generate_sequence<double>(result, count, start, increment); |
48 | break; |
49 | default: |
50 | throw NotImplementedException("Unimplemented type for generate sequence" ); |
51 | } |
52 | } |
53 | |
54 | template <class T> |
55 | void templated_generate_sequence(Vector &result, idx_t count, const SelectionVector &sel, int64_t start, |
56 | int64_t increment) { |
57 | assert(TypeIsNumeric(result.type)); |
58 | if (start > numeric_limits<T>::max() || increment > numeric_limits<T>::max()) { |
59 | throw Exception("Sequence start or increment out of type range" ); |
60 | } |
61 | result.vector_type = VectorType::FLAT_VECTOR; |
62 | auto result_data = FlatVector::GetData<T>(result); |
63 | auto value = (T)start; |
64 | for (idx_t i = 0; i < count; i++) { |
65 | auto idx = sel.get_index(i); |
66 | result_data[idx] = value + increment * idx; |
67 | } |
68 | } |
69 | |
70 | void VectorOperations::GenerateSequence(Vector &result, idx_t count, const SelectionVector &sel, int64_t start, |
71 | int64_t increment) { |
72 | if (!TypeIsNumeric(result.type)) { |
73 | throw InvalidTypeException(result.type, "Can only generate sequences for numeric values!" ); |
74 | } |
75 | switch (result.type) { |
76 | case TypeId::INT8: |
77 | templated_generate_sequence<int8_t>(result, count, sel, start, increment); |
78 | break; |
79 | case TypeId::INT16: |
80 | templated_generate_sequence<int16_t>(result, count, sel, start, increment); |
81 | break; |
82 | case TypeId::INT32: |
83 | templated_generate_sequence<int32_t>(result, count, sel, start, increment); |
84 | break; |
85 | case TypeId::INT64: |
86 | templated_generate_sequence<int64_t>(result, count, sel, start, increment); |
87 | break; |
88 | case TypeId::FLOAT: |
89 | templated_generate_sequence<float>(result, count, sel, start, increment); |
90 | break; |
91 | case TypeId::DOUBLE: |
92 | templated_generate_sequence<double>(result, count, sel, start, increment); |
93 | break; |
94 | default: |
95 | throw NotImplementedException("Unimplemented type for generate sequence" ); |
96 | } |
97 | } |
98 | |