forked from alibaba/AliSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute_case.cpp
More file actions
226 lines (208 loc) · 7.53 KB
/
execute_case.cpp
File metadata and controls
226 lines (208 loc) · 7.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "duckdb/common/uhugeint.hpp"
#include "duckdb/common/vector_operations/vector_operations.hpp"
#include "duckdb/execution/expression_executor.hpp"
#include "duckdb/planner/expression/bound_case_expression.hpp"
namespace duckdb {
struct CaseExpressionState : public ExpressionState {
CaseExpressionState(const Expression &expr, ExpressionExecutorState &root)
: ExpressionState(expr, root), true_sel(STANDARD_VECTOR_SIZE), false_sel(STANDARD_VECTOR_SIZE) {
}
SelectionVector true_sel;
SelectionVector false_sel;
};
unique_ptr<ExpressionState> ExpressionExecutor::InitializeState(const BoundCaseExpression &expr,
ExpressionExecutorState &root) {
auto result = make_uniq<CaseExpressionState>(expr, root);
for (auto &case_check : expr.case_checks) {
result->AddChild(*case_check.when_expr);
result->AddChild(*case_check.then_expr);
}
result->AddChild(*expr.else_expr);
result->Finalize();
return std::move(result);
}
void ExpressionExecutor::Execute(const BoundCaseExpression &expr, ExpressionState *state_p, const SelectionVector *sel,
idx_t count, Vector &result) {
auto &state = state_p->Cast<CaseExpressionState>();
state.intermediate_chunk.Reset();
// first execute the check expression
auto current_true_sel = &state.true_sel;
auto current_false_sel = &state.false_sel;
auto current_sel = sel;
idx_t current_count = count;
for (idx_t i = 0; i < expr.case_checks.size(); i++) {
auto &case_check = expr.case_checks[i];
auto &intermediate_result = state.intermediate_chunk.data[i * 2 + 1];
auto check_state = state.child_states[i * 2].get();
auto then_state = state.child_states[i * 2 + 1].get();
idx_t tcount =
Select(*case_check.when_expr, check_state, current_sel, current_count, current_true_sel, current_false_sel);
if (tcount == 0) {
// everything is false: do nothing
continue;
}
idx_t fcount = current_count - tcount;
if (fcount == 0 && current_count == count) {
// everything is true in the first CHECK statement
// we can skip the entire case and only execute the TRUE side
Execute(*case_check.then_expr, then_state, sel, count, result);
return;
} else {
// we need to execute and then fill in the desired tuples in the result
Execute(*case_check.then_expr, then_state, current_true_sel, tcount, intermediate_result);
FillSwitch(intermediate_result, result, *current_true_sel, NumericCast<sel_t>(tcount));
}
// continue with the false tuples
current_sel = current_false_sel;
current_count = fcount;
if (fcount == 0) {
// everything is true: we are done
break;
}
}
if (current_count > 0) {
auto else_state = state.child_states.back().get();
if (current_count == count) {
// everything was false, we can just evaluate the else expression directly
Execute(*expr.else_expr, else_state, sel, count, result);
return;
} else {
auto &intermediate_result = state.intermediate_chunk.data[expr.case_checks.size() * 2];
D_ASSERT(current_sel);
Execute(*expr.else_expr, else_state, current_sel, current_count, intermediate_result);
FillSwitch(intermediate_result, result, *current_sel, NumericCast<sel_t>(current_count));
}
}
if (sel) {
result.Slice(*sel, count);
}
}
template <class T>
void TemplatedFillLoop(Vector &vector, Vector &result, const SelectionVector &sel, sel_t count) {
result.SetVectorType(VectorType::FLAT_VECTOR);
auto res = FlatVector::GetData<T>(result);
auto &result_mask = FlatVector::Validity(result);
if (vector.GetVectorType() == VectorType::CONSTANT_VECTOR) {
auto data = ConstantVector::GetData<T>(vector);
if (ConstantVector::IsNull(vector)) {
for (idx_t i = 0; i < count; i++) {
result_mask.SetInvalid(sel.get_index(i));
}
} else {
for (idx_t i = 0; i < count; i++) {
res[sel.get_index(i)] = *data;
}
}
} else {
UnifiedVectorFormat vdata;
vector.ToUnifiedFormat(count, vdata);
auto data = UnifiedVectorFormat::GetData<T>(vdata);
for (idx_t i = 0; i < count; i++) {
auto source_idx = vdata.sel->get_index(i);
auto res_idx = sel.get_index(i);
res[res_idx] = data[source_idx];
result_mask.Set(res_idx, vdata.validity.RowIsValid(source_idx));
}
}
}
void ValidityFillLoop(Vector &vector, Vector &result, const SelectionVector &sel, sel_t count) {
result.SetVectorType(VectorType::FLAT_VECTOR);
auto &result_mask = FlatVector::Validity(result);
if (vector.GetVectorType() == VectorType::CONSTANT_VECTOR) {
if (ConstantVector::IsNull(vector)) {
for (idx_t i = 0; i < count; i++) {
result_mask.SetInvalid(sel.get_index(i));
}
}
} else {
UnifiedVectorFormat vdata;
vector.ToUnifiedFormat(count, vdata);
if (vdata.validity.AllValid()) {
return;
}
for (idx_t i = 0; i < count; i++) {
auto source_idx = vdata.sel->get_index(i);
if (!vdata.validity.RowIsValid(source_idx)) {
result_mask.SetInvalid(sel.get_index(i));
}
}
}
}
void ExpressionExecutor::FillSwitch(Vector &vector, Vector &result, const SelectionVector &sel, sel_t count) {
switch (result.GetType().InternalType()) {
case PhysicalType::BOOL:
case PhysicalType::INT8:
TemplatedFillLoop<int8_t>(vector, result, sel, count);
break;
case PhysicalType::INT16:
TemplatedFillLoop<int16_t>(vector, result, sel, count);
break;
case PhysicalType::INT32:
TemplatedFillLoop<int32_t>(vector, result, sel, count);
break;
case PhysicalType::INT64:
TemplatedFillLoop<int64_t>(vector, result, sel, count);
break;
case PhysicalType::UINT8:
TemplatedFillLoop<uint8_t>(vector, result, sel, count);
break;
case PhysicalType::UINT16:
TemplatedFillLoop<uint16_t>(vector, result, sel, count);
break;
case PhysicalType::UINT32:
TemplatedFillLoop<uint32_t>(vector, result, sel, count);
break;
case PhysicalType::UINT64:
TemplatedFillLoop<uint64_t>(vector, result, sel, count);
break;
case PhysicalType::INT128:
TemplatedFillLoop<hugeint_t>(vector, result, sel, count);
break;
case PhysicalType::UINT128:
TemplatedFillLoop<uhugeint_t>(vector, result, sel, count);
break;
case PhysicalType::FLOAT:
TemplatedFillLoop<float>(vector, result, sel, count);
break;
case PhysicalType::DOUBLE:
TemplatedFillLoop<double>(vector, result, sel, count);
break;
case PhysicalType::INTERVAL:
TemplatedFillLoop<interval_t>(vector, result, sel, count);
break;
case PhysicalType::VARCHAR:
TemplatedFillLoop<string_t>(vector, result, sel, count);
StringVector::AddHeapReference(result, vector);
break;
case PhysicalType::STRUCT: {
auto &vector_entries = StructVector::GetEntries(vector);
auto &result_entries = StructVector::GetEntries(result);
ValidityFillLoop(vector, result, sel, count);
D_ASSERT(vector_entries.size() == result_entries.size());
for (idx_t i = 0; i < vector_entries.size(); i++) {
FillSwitch(*vector_entries[i], *result_entries[i], sel, count);
}
break;
}
case PhysicalType::LIST: {
idx_t offset = ListVector::GetListSize(result);
auto &list_child = ListVector::GetEntry(vector);
ListVector::Append(result, list_child, ListVector::GetListSize(vector));
// all the false offsets need to be incremented by true_child.count
TemplatedFillLoop<list_entry_t>(vector, result, sel, count);
if (offset == 0) {
break;
}
auto result_data = FlatVector::GetData<list_entry_t>(result);
for (idx_t i = 0; i < count; i++) {
auto result_idx = sel.get_index(i);
result_data[result_idx].offset += offset;
}
Vector::Verify(result, sel, count);
break;
}
default:
throw NotImplementedException("Unimplemented type for case expression: %s", result.GetType().ToString());
}
}
} // namespace duckdb