akar_processor/physical/
misc.rs1use crate::physical::types::{OperatorResult, PhysicalOperatorExec};
4use akar_common::vector::{DataChunk, ValueVector};
5
6pub struct PhysicalEmptyResult;
8
9impl PhysicalOperatorExec for PhysicalEmptyResult {
10 fn operator_type(&self) -> &str {
11 "empty_result"
12 }
13
14 fn execute(&self, _input: Vec<DataChunk>) -> OperatorResult {
15 Ok(vec![])
16 }
17}
18
19pub struct PhysicalMultiplicityReducer {
21 pub key_columns: Vec<usize>,
22}
23
24impl PhysicalOperatorExec for PhysicalMultiplicityReducer {
25 fn operator_type(&self) -> &str {
26 "multiplicity_reducer"
27 }
28
29 fn execute(&self, input: Vec<DataChunk>) -> OperatorResult {
30 if input.is_empty() {
31 return Ok(input);
32 }
33
34 let mut result = Vec::new();
35 let mut seen = std::collections::HashSet::new();
36
37 for chunk in input {
38 let mut filter_mask = vec![false; chunk.size];
39 for i in 0..chunk.size {
40 let mut row_keys = Vec::new();
41 for &col_idx in &self.key_columns {
42 let val = chunk.get_value(col_idx, i).unwrap_or(akar_common::types::Value::Null);
43 row_keys.push(val);
44 }
45
46 if seen.insert(format!("{:?}", row_keys)) {
48 filter_mask[i] = true;
49 }
50 }
51
52 let filtered_size = filter_mask.iter().filter(|&&b| b).count();
53 if filtered_size > 0 {
54 let mut new_fields = Vec::new();
55 let mut new_field_types = Vec::new();
56 for (col_idx, _field) in chunk.fields.iter().enumerate() {
57 let phys_type = chunk.field_types[col_idx];
58 let mut new_field = ValueVector::new(phys_type, filtered_size);
59 let mut current = 0;
60 for (i, &keep) in filter_mask.iter().enumerate() {
61 if keep {
62 if let Some(val) = chunk.get_value(col_idx, i) {
63 let _ = new_field.set_value(current, &val);
64 }
65 current += 1;
66 }
67 }
68 new_fields.push(akar_common::arrow_vector::ArrowVector::from_legacy(&new_field).array);
69 new_field_types.push(phys_type);
70 }
71 result.push(DataChunk {
72 fields: new_fields,
73 field_types: new_field_types,
74 size: filtered_size,
75 field_names: chunk.field_names.clone(),
76 sel_vector: None,
77 });
78 }
79 }
80 Ok(result)
81 }
82}
83
84pub struct PhysicalSkip {
86 pub skip_count: usize,
87}
88
89impl PhysicalOperatorExec for PhysicalSkip {
90 fn operator_type(&self) -> &str {
91 "skip"
92 }
93
94 fn execute(&self, input: Vec<DataChunk>) -> OperatorResult {
95 let mut remaining_skip = self.skip_count;
96 let mut output = Vec::new();
97
98 for chunk in input {
99 if remaining_skip == 0 {
100 output.push(chunk);
101 continue;
102 }
103
104 if chunk.size <= remaining_skip {
105 remaining_skip -= chunk.size;
106 continue;
107 }
108
109 let keep_size = chunk.size - remaining_skip;
111 let mut sliced_fields = Vec::new();
112 let mut sliced_types = Vec::new();
113
114 for (col_idx, _field) in chunk.fields.iter().enumerate() {
115 let phys_type = chunk.field_types[col_idx];
116 let mut new_field = ValueVector::new(phys_type, keep_size);
117 for i in 0..keep_size {
118 new_field
119 .set_value(
120 i,
121 &chunk
122 .get_value(col_idx, remaining_skip + i)
123 .unwrap_or(akar_common::types::Value::Null),
124 )
125 .unwrap();
126 }
127 sliced_fields.push(akar_common::arrow_vector::ArrowVector::from_legacy(&new_field).array);
128 sliced_types.push(phys_type);
129 }
130
131 output.push(DataChunk {
132 fields: sliced_fields,
133 field_types: sliced_types,
134 size: keep_size,
135 field_names: chunk.field_names.clone(),
136 sel_vector: None,
137 });
138 remaining_skip = 0;
139 }
140
141 Ok(output)
142 }
143}
144
145pub struct PhysicalUnionAllScan;
147
148impl PhysicalOperatorExec for PhysicalUnionAllScan {
149 fn operator_type(&self) -> &str {
150 "union_all_scan"
151 }
152
153 fn execute(&self, input: Vec<DataChunk>) -> OperatorResult {
154 Ok(input)
155 }
156}
157
158pub struct PhysicalInsert {
160 pub table_name: String,
161 pub table_id: u64,
162 pub columns: Vec<String>,
163 pub values: Vec<Vec<akar_common::types::Value>>,
164 pub table_catalog: std::sync::Arc<akar_storage::table::TableCatalog>,
165}
166
167impl PhysicalOperatorExec for PhysicalInsert {
168 fn operator_type(&self) -> &str {
169 "insert"
170 }
171
172 fn execute(&self, _input: Vec<DataChunk>) -> OperatorResult {
173 let mut inserted = 0;
174
175 if let Some(mut rel_tbl) = self.table_catalog.get_rel_table_by_name_mut(&self.table_name) {
177 let mut rels_to_insert = Vec::new();
179 for row_values in &self.values {
180 if row_values.len() >= 2 {
181 let src = if let akar_common::types::Value::Int64(v) = row_values[0] {
183 v as u64
184 } else {
185 0
186 };
187 let dst = if let akar_common::types::Value::Int64(v) = row_values[1] {
188 v as u64
189 } else {
190 0
191 };
192 let props = if row_values.len() > 2 {
193 row_values[2..].to_vec()
194 } else {
195 vec![]
196 };
197 rels_to_insert.push((src, dst, props));
198 }
199 }
200 if !rels_to_insert.is_empty() {
201 if let Ok(count) = rel_tbl.insert_rels_batch(&rels_to_insert) {
202 inserted += count;
203 }
204 }
205 } else if let Some(mut node_tbl) = self.table_catalog.get_node_table_by_name_mut(&self.table_name) {
206 for row_values in &self.values {
208 if node_tbl.insert_row(row_values.clone()).is_ok() {
209 inserted += 1;
210 }
211 }
212 } else {
213 return Err(format!("Table '{}' not found for INSERT", self.table_name).into());
214 }
215
216 let mut v = ValueVector::new(akar_common::types::PhysicalTypeID::Int64, 1);
217 v.resize(1);
218 v.set_i64(0, inserted as i64);
219 let arr = akar_common::arrow_vector::ArrowVector::from_legacy(&v).array;
220 Ok(vec![DataChunk::new(
221 vec![arr],
222 vec![akar_common::types::PhysicalTypeID::Int64],
223 )])
224 }
225}
226
227pub struct PhysicalExtensionClause {
229 pub action: akar_parser::ast::ExtensionAction,
230 pub extension_name: String,
231}
232
233impl PhysicalOperatorExec for PhysicalExtensionClause {
234 fn operator_type(&self) -> &str {
235 "extension_clause"
236 }
237
238 fn execute(&self, _input: Vec<DataChunk>) -> OperatorResult {
239 let msg = match self.action {
240 akar_parser::ast::ExtensionAction::Install => {
241 format!("Extension '{}' installed successfully.", self.extension_name)
242 }
243 akar_parser::ast::ExtensionAction::Load => {
244 if self.extension_name.to_lowercase() == "httpfs" {
246 format!(
247 "Extension '{}' loaded (HTTP/S3 virtual file system).",
248 self.extension_name
249 )
250 } else if self.extension_name.to_lowercase() == "fts" {
251 format!("Extension '{}' loaded (Full Text Search).", self.extension_name)
252 } else {
253 format!("Extension '{}' loaded.", self.extension_name)
254 }
255 }
256 akar_parser::ast::ExtensionAction::Uninstall => {
257 format!("Extension '{}' uninstalled.", self.extension_name)
258 }
259 };
260
261 tracing::info!("{}", msg);
262
263 let mut field = ValueVector::new(akar_common::types::PhysicalTypeID::String, 1);
264 let _ = field.set_value(0, &akar_common::types::Value::String(msg));
265 let arr = akar_common::arrow_vector::ArrowVector::from_legacy(&field).array;
266
267 Ok(vec![DataChunk {
268 fields: vec![arr],
269 field_types: vec![akar_common::types::PhysicalTypeID::String],
270 size: 1,
271 field_names: vec!["message".into()],
272 sel_vector: None,
273 }])
274 }
275}