Skip to content

Commit 9732ae1

Browse files
authored
[ENH]: implement Display for Where (#5755)
1 parent 9785503 commit 9732ae1

File tree

2 files changed

+123
-2
lines changed

2 files changed

+123
-2
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/types/src/metadata.rs

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,18 @@ pub enum MetadataValue {
410410
SparseVector(SparseVector),
411411
}
412412

413+
impl std::fmt::Display for MetadataValue {
414+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
415+
match self {
416+
MetadataValue::Bool(v) => write!(f, "{}", v),
417+
MetadataValue::Int(v) => write!(f, "{}", v),
418+
MetadataValue::Float(v) => write!(f, "{}", v),
419+
MetadataValue::Str(v) => write!(f, "\"{}\"", v),
420+
MetadataValue::SparseVector(v) => write!(f, "SparseVector(len={})", v.values.len()),
421+
}
422+
}
423+
}
424+
413425
impl Eq for MetadataValue {}
414426

415427
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
@@ -924,6 +936,27 @@ pub enum Where {
924936
Metadata(MetadataExpression),
925937
}
926938

939+
impl std::fmt::Display for Where {
940+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
941+
match self {
942+
Where::Composite(composite) => {
943+
let fragment = composite
944+
.children
945+
.iter()
946+
.map(|child| format!("{}", child))
947+
.collect::<Vec<_>>()
948+
.join(match composite.operator {
949+
BooleanOperator::And => " & ",
950+
BooleanOperator::Or => " | ",
951+
});
952+
write!(f, "({})", fragment)
953+
}
954+
Where::Metadata(expr) => write!(f, "{}", expr),
955+
Where::Document(expr) => write!(f, "{}", expr),
956+
}
957+
}
958+
}
959+
927960
impl serde::Serialize for Where {
928961
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
929962
where
@@ -1230,6 +1263,18 @@ pub struct DocumentExpression {
12301263
pub pattern: String,
12311264
}
12321265

1266+
impl std::fmt::Display for DocumentExpression {
1267+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1268+
let op_str = match self.operator {
1269+
DocumentOperator::Contains => "CONTAINS",
1270+
DocumentOperator::NotContains => "NOT CONTAINS",
1271+
DocumentOperator::Regex => "REGEX",
1272+
DocumentOperator::NotRegex => "NOT REGEX",
1273+
};
1274+
write!(f, "#document {} \"{}\"", op_str, self.pattern)
1275+
}
1276+
}
1277+
12331278
impl From<chroma_proto::DirectWhereDocument> for DocumentExpression {
12341279
fn from(value: chroma_proto::DirectWhereDocument) -> Self {
12351280
Self {
@@ -1285,6 +1330,19 @@ pub struct MetadataExpression {
12851330
pub comparison: MetadataComparison,
12861331
}
12871332

1333+
impl std::fmt::Display for MetadataExpression {
1334+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1335+
match &self.comparison {
1336+
MetadataComparison::Primitive(op, value) => {
1337+
write!(f, "{} {} {}", self.key, op, value)
1338+
}
1339+
MetadataComparison::Set(op, set_value) => {
1340+
write!(f, "{} {} {}", self.key, op, set_value)
1341+
}
1342+
}
1343+
}
1344+
}
1345+
12881346
impl TryFrom<chroma_proto::DirectComparison> for MetadataExpression {
12891347
type Error = WhereConversionError;
12901348

@@ -1428,6 +1486,20 @@ pub enum PrimitiveOperator {
14281486
LessThanOrEqual,
14291487
}
14301488

1489+
impl std::fmt::Display for PrimitiveOperator {
1490+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1491+
let op_str = match self {
1492+
PrimitiveOperator::Equal => "=",
1493+
PrimitiveOperator::NotEqual => "≠",
1494+
PrimitiveOperator::GreaterThan => ">",
1495+
PrimitiveOperator::GreaterThanOrEqual => "≥",
1496+
PrimitiveOperator::LessThan => "<",
1497+
PrimitiveOperator::LessThanOrEqual => "≤",
1498+
};
1499+
write!(f, "{}", op_str)
1500+
}
1501+
}
1502+
14311503
impl From<chroma_proto::GenericComparator> for PrimitiveOperator {
14321504
fn from(value: chroma_proto::GenericComparator) -> Self {
14331505
match value {
@@ -1476,13 +1548,23 @@ impl TryFrom<PrimitiveOperator> for chroma_proto::NumberComparator {
14761548
}
14771549
}
14781550

1479-
#[derive(Clone, Debug, PartialEq)]
1551+
#[derive(Clone, Debug, PartialEq, Eq)]
14801552
#[cfg_attr(feature = "testing", derive(proptest_derive::Arbitrary))]
14811553
pub enum SetOperator {
14821554
In,
14831555
NotIn,
14841556
}
14851557

1558+
impl std::fmt::Display for SetOperator {
1559+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1560+
let op_str = match self {
1561+
SetOperator::In => "∈",
1562+
SetOperator::NotIn => "∉",
1563+
};
1564+
write!(f, "{}", op_str)
1565+
}
1566+
}
1567+
14861568
impl From<chroma_proto::ListOperator> for SetOperator {
14871569
fn from(value: chroma_proto::ListOperator) -> Self {
14881570
match value {
@@ -1510,6 +1592,45 @@ pub enum MetadataSetValue {
15101592
Str(Vec<String>),
15111593
}
15121594

1595+
impl std::fmt::Display for MetadataSetValue {
1596+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1597+
match self {
1598+
MetadataSetValue::Bool(values) => {
1599+
let values_str = values
1600+
.iter()
1601+
.map(|v| format!("\"{}\"", v))
1602+
.collect::<Vec<_>>()
1603+
.join(", ");
1604+
write!(f, "[{}]", values_str)
1605+
}
1606+
MetadataSetValue::Int(values) => {
1607+
let values_str = values
1608+
.iter()
1609+
.map(|v| v.to_string())
1610+
.collect::<Vec<_>>()
1611+
.join(", ");
1612+
write!(f, "[{}]", values_str)
1613+
}
1614+
MetadataSetValue::Float(values) => {
1615+
let values_str = values
1616+
.iter()
1617+
.map(|v| v.to_string())
1618+
.collect::<Vec<_>>()
1619+
.join(", ");
1620+
write!(f, "[{}]", values_str)
1621+
}
1622+
MetadataSetValue::Str(values) => {
1623+
let values_str = values
1624+
.iter()
1625+
.map(|v| format!("\"{}\"", v))
1626+
.collect::<Vec<_>>()
1627+
.join(", ");
1628+
write!(f, "[{}]", values_str)
1629+
}
1630+
}
1631+
}
1632+
}
1633+
15131634
impl MetadataSetValue {
15141635
pub fn value_type(&self) -> MetadataValueType {
15151636
match self {

0 commit comments

Comments
 (0)