-
Notifications
You must be signed in to change notification settings - Fork 2k
[ENH] Serialize Where clause in Rust #5665
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,5 @@ | ||||||||||||||||||||||
| use chroma_error::{ChromaError, ErrorCodes}; | ||||||||||||||||||||||
| use serde::{Deserialize, Deserializer, Serialize, Serializer}; | ||||||||||||||||||||||
| use serde::{ser::SerializeMap, Deserialize, Deserializer, Serialize, Serializer}; | ||||||||||||||||||||||
| use serde_json::{Number, Value}; | ||||||||||||||||||||||
| use sprs::CsVec; | ||||||||||||||||||||||
| use std::{ | ||||||||||||||||||||||
|
|
@@ -873,11 +873,74 @@ pub enum Where { | |||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| impl serde::Serialize for Where { | ||||||||||||||||||||||
| fn serialize<S>(&self, _serializer: S) -> Result<S::Ok, S::Error> | ||||||||||||||||||||||
| fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||||||||||||||||||||||
| where | ||||||||||||||||||||||
| S: Serializer, | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| todo!() | ||||||||||||||||||||||
| match self { | ||||||||||||||||||||||
| Where::Composite(composite) => { | ||||||||||||||||||||||
| let mut map = serializer.serialize_map(Some(1))?; | ||||||||||||||||||||||
| let op_key = match composite.operator { | ||||||||||||||||||||||
| BooleanOperator::And => "$and", | ||||||||||||||||||||||
| BooleanOperator::Or => "$or", | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
| map.serialize_entry(op_key, &composite.children)?; | ||||||||||||||||||||||
| map.end() | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| Where::Document(doc) => { | ||||||||||||||||||||||
| let mut outer_map = serializer.serialize_map(Some(1))?; | ||||||||||||||||||||||
| let mut inner_map = serde_json::Map::new(); | ||||||||||||||||||||||
| let op_key = match doc.operator { | ||||||||||||||||||||||
| DocumentOperator::Contains => "$contains", | ||||||||||||||||||||||
| DocumentOperator::NotContains => "$not_contains", | ||||||||||||||||||||||
| DocumentOperator::Regex => "$regex", | ||||||||||||||||||||||
| DocumentOperator::NotRegex => "$not_regex", | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
| inner_map.insert( | ||||||||||||||||||||||
| op_key.to_string(), | ||||||||||||||||||||||
| serde_json::Value::String(doc.pattern.clone()), | ||||||||||||||||||||||
| ); | ||||||||||||||||||||||
| outer_map.serialize_entry("#document", &inner_map)?; | ||||||||||||||||||||||
| outer_map.end() | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| Where::Metadata(meta) => { | ||||||||||||||||||||||
| let mut outer_map = serializer.serialize_map(Some(1))?; | ||||||||||||||||||||||
| let mut inner_map = serde_json::Map::new(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| match &meta.comparison { | ||||||||||||||||||||||
| MetadataComparison::Primitive(op, value) => { | ||||||||||||||||||||||
| let op_key = match op { | ||||||||||||||||||||||
| PrimitiveOperator::Equal => "$eq", | ||||||||||||||||||||||
| PrimitiveOperator::NotEqual => "$ne", | ||||||||||||||||||||||
| PrimitiveOperator::GreaterThan => "$gt", | ||||||||||||||||||||||
| PrimitiveOperator::GreaterThanOrEqual => "$gte", | ||||||||||||||||||||||
| PrimitiveOperator::LessThan => "$lt", | ||||||||||||||||||||||
| PrimitiveOperator::LessThanOrEqual => "$lte", | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
| let value_json = | ||||||||||||||||||||||
| serde_json::to_value(value).map_err(serde::ser::Error::custom)?; | ||||||||||||||||||||||
| inner_map.insert(op_key.to_string(), value_json); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| MetadataComparison::Set(op, set_value) => { | ||||||||||||||||||||||
| let op_key = match op { | ||||||||||||||||||||||
| SetOperator::In => "$in", | ||||||||||||||||||||||
| SetOperator::NotIn => "$nin", | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
| let values_json = match set_value { | ||||||||||||||||||||||
| MetadataSetValue::Bool(v) => serde_json::to_value(v), | ||||||||||||||||||||||
| MetadataSetValue::Int(v) => serde_json::to_value(v), | ||||||||||||||||||||||
| MetadataSetValue::Float(v) => serde_json::to_value(v), | ||||||||||||||||||||||
| MetadataSetValue::Str(v) => serde_json::to_value(v), | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| .map_err(serde::ser::Error::custom)?; | ||||||||||||||||||||||
| inner_map.insert(op_key.to_string(), values_json); | ||||||||||||||||||||||
|
Comment on lines
+929
to
+936
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [BestPractice] This First, modify the #[derive(Clone, Debug, PartialEq, Serialize)]
#[cfg_attr(feature = "testing", derive(proptest_derive::Arbitrary))]
#[serde(untagged)]
pub enum MetadataSetValue {
Bool(Vec<bool>),
Int(Vec<i64>),
Float(Vec<f64>),
Str(Vec<String>),
}Then you can simplify this block:
Suggested change
⚡ Committable suggestion Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Context for Agents
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could work, but this is fine for now |
||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| outer_map.serialize_entry(&meta.key, &inner_map)?; | ||||||||||||||||||||||
| outer_map.end() | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.