reifydb_sub_flow/operator/scan/
table.rs1use std::sync::Arc;
5
6use reifydb_core::{
7 encoded::shape::RowShape,
8 interface::{
9 catalog::{flow::FlowNodeId, shape::ShapeId, table::Table},
10 change::{Change, Diff},
11 },
12 key::row::RowKey,
13 value::column::{ColumnWithName, buffer::ColumnBuffer, columns::Columns},
14};
15use reifydb_type::{
16 Result,
17 fragment::Fragment,
18 value::{datetime::DateTime, row_number::RowNumber},
19};
20
21use crate::{Operator, operator::sink::decode_dictionary_columns, transaction::FlowTransaction};
22
23pub struct PrimitiveTableOperator {
24 node: FlowNodeId,
25 table: Table,
26}
27
28impl PrimitiveTableOperator {
29 pub fn new(node: FlowNodeId, table: Table) -> Self {
30 Self {
31 node,
32 table,
33 }
34 }
35}
36
37impl Operator for PrimitiveTableOperator {
38 fn id(&self) -> FlowNodeId {
39 self.node
40 }
41
42 fn apply(&self, txn: &mut FlowTransaction, change: Change) -> Result<Change> {
43 let mut decoded_diffs = Vec::with_capacity(change.diffs.len());
44 for diff in change.diffs {
45 decoded_diffs.push(match diff {
46 Diff::Insert {
47 post,
48 } => {
49 let mut decoded = post;
50 decode_dictionary_columns(Arc::make_mut(&mut decoded), txn)?;
51 Diff::insert_arc(decoded)
52 }
53 Diff::Update {
54 pre,
55 post,
56 } => {
57 let mut decoded_pre = pre;
58 let mut decoded_post = post;
59 decode_dictionary_columns(Arc::make_mut(&mut decoded_pre), txn)?;
60 decode_dictionary_columns(Arc::make_mut(&mut decoded_post), txn)?;
61 Diff::update_arc(decoded_pre, decoded_post)
62 }
63 Diff::Remove {
64 pre,
65 } => {
66 let mut decoded = pre;
67 decode_dictionary_columns(Arc::make_mut(&mut decoded), txn)?;
68 Diff::remove_arc(decoded)
69 }
70 });
71 }
72 Ok(Change::from_flow(self.node, change.version, decoded_diffs, change.changed_at))
73 }
74
75 fn pull(&self, txn: &mut FlowTransaction, rows: &[RowNumber]) -> Result<Columns> {
76 if rows.is_empty() {
77 return Ok(Columns::from_catalog_columns(&self.table.columns));
78 }
79
80 let shape: RowShape = (&self.table.columns).into();
81 let fields = shape.fields();
82
83 let mut columns_vec: Vec<ColumnWithName> = Vec::with_capacity(fields.len());
85 for field in fields.iter() {
86 columns_vec.push(ColumnWithName {
87 name: Fragment::internal(&field.name),
88 data: ColumnBuffer::with_capacity(field.constraint.get_type(), rows.len()),
89 });
90 }
91 let mut row_numbers = Vec::with_capacity(rows.len());
92 let mut created_at = Vec::with_capacity(rows.len());
93 let mut updated_at = Vec::with_capacity(rows.len());
94
95 for row_num in rows {
96 let key = RowKey::encoded(ShapeId::table(self.table.id), *row_num);
97 if let Some(encoded) = txn.get(&key)? {
98 row_numbers.push(*row_num);
99 created_at.push(DateTime::from_nanos(encoded.created_at_nanos()));
100 updated_at.push(DateTime::from_nanos(encoded.updated_at_nanos()));
101 for (i, _field) in fields.iter().enumerate() {
103 let value = shape.get_value(&encoded, i);
104 columns_vec[i].data.push_value(value);
105 }
106 }
107 }
108
109 if row_numbers.is_empty() {
110 Ok(Columns::from_catalog_columns(&self.table.columns))
111 } else {
112 Ok(Columns::with_system_columns(columns_vec, row_numbers, created_at, updated_at))
113 }
114 }
115}