1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/common/vector_operations/vector_operations.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/common/types/data_chunk.hpp"
12#include "duckdb/common/types/vector.hpp"
13
14#include <functional>
15
16namespace duckdb {
17
18// VectorOperations contains a set of operations that operate on sets of
19// vectors. In general, the operators must all have the same type, otherwise an
20// exception is thrown. Note that the functions underneath use restrict
21// pointers, hence the data that the vectors point to (and hence the vector
22// themselves) should not be equal! For example, if you call the function Add(A,
23// B, A) then ASSERT_RESTRICT will be triggered. Instead call AddInPlace(A, B)
24// or Add(A, B, C)
25struct VectorOperations {
26 //===--------------------------------------------------------------------===//
27 // In-Place Operators
28 //===--------------------------------------------------------------------===//
29 //! A += B
30 static void AddInPlace(Vector &A, int64_t B, idx_t count);
31
32 //===--------------------------------------------------------------------===//
33 // NULL Operators
34 //===--------------------------------------------------------------------===//
35 //! result = IS NOT NULL(A)
36 static void IsNotNull(Vector &A, Vector &result, idx_t count);
37 //! result = IS NULL (A)
38 static void IsNull(Vector &A, Vector &result, idx_t count);
39 // Returns whether or not a vector has a NULL value
40 static bool HasNull(Vector &A, idx_t count);
41 static bool HasNotNull(Vector &A, idx_t count);
42
43 //===--------------------------------------------------------------------===//
44 // Boolean Operations
45 //===--------------------------------------------------------------------===//
46 // result = A && B
47 static void And(Vector &A, Vector &B, Vector &result, idx_t count);
48 // result = A || B
49 static void Or(Vector &A, Vector &B, Vector &result, idx_t count);
50 // result = NOT(A)
51 static void Not(Vector &A, Vector &result, idx_t count);
52
53 //===--------------------------------------------------------------------===//
54 // Comparison Operations
55 //===--------------------------------------------------------------------===//
56 // result = A == B
57 static void Equals(Vector &A, Vector &B, Vector &result, idx_t count);
58 // result = A != B
59 static void NotEquals(Vector &A, Vector &B, Vector &result, idx_t count);
60 // result = A > B
61 static void GreaterThan(Vector &A, Vector &B, Vector &result, idx_t count);
62 // result = A >= B
63 static void GreaterThanEquals(Vector &A, Vector &B, Vector &result, idx_t count);
64 // result = A < B
65 static void LessThan(Vector &A, Vector &B, Vector &result, idx_t count);
66 // result = A <= B
67 static void LessThanEquals(Vector &A, Vector &B, Vector &result, idx_t count);
68
69 //===--------------------------------------------------------------------===//
70 // Scatter methods
71 //===--------------------------------------------------------------------===//
72 // make sure dest.count is set for gather methods!
73 struct Gather {
74 //! dest.data[i] = ptr[i]. NullValue<T> is checked for and converted to the nullmask in dest. The source
75 //! addresses are incremented by the size of the type.
76 static void Set(Vector &source, Vector &dest, idx_t count);
77 };
78
79 //===--------------------------------------------------------------------===//
80 // Hash functions
81 //===--------------------------------------------------------------------===//
82 // result = HASH(A)
83 static void Hash(Vector &input, Vector &hashes, idx_t count);
84 static void Hash(Vector &input, Vector &hashes, const SelectionVector &rsel, idx_t count);
85 // A ^= HASH(B)
86 static void CombineHash(Vector &hashes, Vector &B, idx_t count);
87 static void CombineHash(Vector &hashes, Vector &B, const SelectionVector &rsel, idx_t count);
88
89 //===--------------------------------------------------------------------===//
90 // Generate functions
91 //===--------------------------------------------------------------------===//
92 static void GenerateSequence(Vector &result, idx_t count, int64_t start = 0, int64_t increment = 1);
93 static void GenerateSequence(Vector &result, idx_t count, const SelectionVector &sel, int64_t start = 0,
94 int64_t increment = 1);
95 //===--------------------------------------------------------------------===//
96 // Helpers
97 //===--------------------------------------------------------------------===//
98 // Cast the data from the source type to the target type
99 static void Cast(Vector &source, Vector &result, SQLType source_type, SQLType target_type, idx_t count,
100 bool strict = false);
101 // Cast the data from the source type to the target type
102 static void Cast(Vector &source, Vector &result, idx_t count, bool strict = false);
103
104 // Copy the data of <source> to the target vector
105 static void Copy(Vector &source, Vector &target, idx_t source_count, idx_t source_offset, idx_t target_offset);
106 static void Copy(Vector &source, Vector &target, const SelectionVector &sel, idx_t source_count,
107 idx_t source_offset, idx_t target_offset);
108
109 // Copy the data of <source> to the target location, setting null values to
110 // NullValue<T>. Used to store data without separate NULL mask.
111 static void WriteToStorage(Vector &source, idx_t count, data_ptr_t target);
112 // Reads the data of <source> to the target vector, setting the nullmask
113 // for any NullValue<T> of source. Used to go back from storage to a proper vector
114 static void ReadFromStorage(data_ptr_t source, idx_t count, Vector &result);
115};
116} // namespace duckdb
117