|
1 | 1 | use crate::db::DB; |
2 | | -use crate::{Value, jsonb_to_serde, serde_to_jsonb}; |
| 2 | +use crate::{SerdeWrapper, Value, make_static}; |
3 | 3 | use jsonb_schema::Number; |
4 | | -use jsonpath_rust::query::js_path_vals; |
5 | 4 | use std::cmp::Ordering; |
6 | 5 | use std::collections::BTreeMap; |
7 | 6 | use tracing::{Level, span}; |
@@ -256,20 +255,41 @@ impl<'a> Iterator for OffsetOperator<'a> { |
256 | 255 | fn evaluate_expression(expr: &Expression, doc: &Value) -> Value { |
257 | 256 | match expr { |
258 | 257 | Expression::FieldReference(path) => get_path(doc, path).unwrap_or(Value::Null), |
259 | | - Expression::JsonPath(path) => { |
260 | | - let serde_val = jsonb_to_serde(doc); |
261 | | - if let Ok(nodes) = js_path_vals(path, &serde_val) { |
262 | | - if nodes.is_empty() { |
263 | | - Value::Null |
264 | | - } else if nodes.len() == 1 { |
265 | | - serde_to_jsonb(nodes[0].clone()) |
| 258 | + Expression::JsonPath(path_str) => { |
| 259 | + let wrapper = SerdeWrapper(doc); |
| 260 | + if let Ok(blob) = jsonb_schema::to_owned_jsonb(&wrapper) { |
| 261 | + // Parse path |
| 262 | + if let Ok(json_path) = jsonb_schema::jsonpath::parse_json_path(path_str.as_bytes()) |
| 263 | + { |
| 264 | + // Execute select_by_path on RawJsonb |
| 265 | + if let Ok(results) = blob.as_raw().select_by_path(&json_path) { |
| 266 | + if results.is_empty() { |
| 267 | + Value::Null |
| 268 | + } else if results.len() == 1 { |
| 269 | + // Extract single value |
| 270 | + let owned = results.into_iter().next().unwrap(); |
| 271 | + let vec = owned.to_vec(); |
| 272 | + if let Ok(val) = jsonb_schema::from_slice(&vec) { |
| 273 | + make_static(&val) |
| 274 | + } else { |
| 275 | + Value::Null |
| 276 | + } |
| 277 | + } else { |
| 278 | + // Array of values |
| 279 | + let mut arr = Vec::new(); |
| 280 | + for owned in results { |
| 281 | + let vec = owned.to_vec(); |
| 282 | + if let Ok(val) = jsonb_schema::from_slice(&vec) { |
| 283 | + arr.push(make_static(&val)); |
| 284 | + } |
| 285 | + } |
| 286 | + Value::Array(arr) |
| 287 | + } |
| 288 | + } else { |
| 289 | + Value::Null |
| 290 | + } |
266 | 291 | } else { |
267 | | - Value::Array( |
268 | | - nodes |
269 | | - .into_iter() |
270 | | - .map(|n| serde_to_jsonb(n.clone())) |
271 | | - .collect(), |
272 | | - ) |
| 292 | + Value::Null |
273 | 293 | } |
274 | 294 | } else { |
275 | 295 | Value::Null |
@@ -517,6 +537,7 @@ pub fn execute_plan<'a>( |
517 | 537 | #[cfg(test)] |
518 | 538 | mod tests { |
519 | 539 | use super::*; |
| 540 | + use crate::serde_to_jsonb; |
520 | 541 | use jsonb_schema::Value as JsonbValue; |
521 | 542 | use serde_json::json; |
522 | 543 |
|
|
0 commit comments