linkrs_query/executor/expression/evaluation_context/
row_context.rs1use linkrs_core::Value;
7use std::collections::HashMap;
8
9#[derive(Debug, Clone)]
16pub struct RowExpressionContext {
17 row: Vec<Value>,
19 col_name_index: HashMap<String, usize>,
21 variables: HashMap<String, Value>,
23}
24
25impl RowExpressionContext {
26 pub fn new(row: Vec<Value>, col_names: Vec<String>) -> Self {
28 let col_name_index: HashMap<String, usize> = col_names
29 .into_iter()
30 .enumerate()
31 .map(|(i, name)| (name, i))
32 .collect();
33
34 Self {
35 row,
36 col_name_index,
37 variables: HashMap::new(),
38 }
39 }
40
41 pub fn from_dataset(row: &[Value], col_names: &[String]) -> Self {
43 Self::new(row.to_vec(), col_names.to_vec())
44 }
45
46 pub fn get_value_by_name(&self, name: &str) -> Option<&Value> {
48 let result = self
49 .col_name_index
50 .get(name)
51 .and_then(|&idx| self.row.get(idx));
52 result
53 }
54}
55
56impl crate::executor::expression::evaluator::traits::ExpressionContext for RowExpressionContext {
57 fn get_variable(&self, name: &str) -> Option<Value> {
58 if let Some(value) = self.variables.get(name) {
60 return Some(value.clone());
61 }
62
63 if let Some(value) = self.get_value_by_name(name) {
65 return Some(value.clone());
66 }
67
68 None
69 }
70
71 fn set_variable(&mut self, name: String, value: Value) {
72 self.variables.insert(name, value);
73 }
74
75 fn get_function(
76 &self,
77 name: &str,
78 ) -> Option<crate::executor::expression::functions::OwnedFunctionRef> {
79 let _ = name;
80 None
81 }
82}