MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / eval

Function eval

nodedb/src/control/server/pgwire/pg_catalog/vquery/expr.rs:61–156  ·  view source on GitHub ↗

Evaluate an expression in the context of a single row.

(expr: &Expr, row: &[VValue], table: &VTable)

Source from the content-addressed store, hash-verified

59
60/// Evaluate an expression in the context of a single row.
61pub fn eval(expr: &Expr, row: &[VValue], table: &VTable) -> Result<VValue, EvalError> {
62 match expr {
63 Expr::Literal(v) => Ok(v.clone()),
64 Expr::Star => Ok(VValue::Null),
65 Expr::Column(name) => {
66 let idx = table
67 .column_index(name)
68 .ok_or_else(|| EvalError::UnknownColumn(name.clone()))?;
69 Ok(row[idx].clone())
70 }
71 Expr::UnaryNot(e) => {
72 let v = eval(e, row, table)?;
73 match v {
74 VValue::Null => Ok(VValue::Null),
75 VValue::Bool(b) => Ok(VValue::Bool(!b)),
76 _ => Err(EvalError::TypeMismatch("NOT requires boolean".into())),
77 }
78 }
79 Expr::UnaryNeg(e) => {
80 let v = eval(e, row, table)?;
81 match v {
82 VValue::Null => Ok(VValue::Null),
83 VValue::Int4(i) => Ok(VValue::Int4(-i)),
84 VValue::Int8(i) => Ok(VValue::Int8(-i)),
85 _ => Err(EvalError::TypeMismatch("unary - on non-integer".into())),
86 }
87 }
88 Expr::IsNull(e, negated) => {
89 let v = eval(e, row, table)?;
90 let is_null = v.is_null();
91 Ok(VValue::Bool(if *negated { !is_null } else { is_null }))
92 }
93 Expr::BinaryOp(l, op, r) => {
94 let lv = eval(l, row, table)?;
95 let rv = eval(r, row, table)?;
96 apply_binary(op, &lv, &rv)
97 }
98 Expr::InList(e, items, negated) => {
99 let v = eval(e, row, table)?;
100 if v.is_null() {
101 return Ok(VValue::Null);
102 }
103 let mut found = false;
104 let mut any_null = false;
105 for item in items {
106 let iv = eval(item, row, table)?;
107 if iv.is_null() {
108 any_null = true;
109 continue;
110 }
111 if let Some(std::cmp::Ordering::Equal) = v.sql_cmp(&iv) {
112 found = true;
113 break;
114 }
115 }
116 let result = if found {
117 true
118 } else if any_null {

Callers 4

executeFunction · 0.70
project_rowwiseFunction · 0.70
compute_aggregateFunction · 0.70
sort_rowsFunction · 0.70

Calls 7

apply_binaryFunction · 0.85
like_matchFunction · 0.85
sql_cmpMethod · 0.80
as_textMethod · 0.80
cloneMethod · 0.45
column_indexMethod · 0.45
is_nullMethod · 0.45

Tested by

no test coverage detected