Skip to content

Commit 2c3155a

Browse files
committed
use interface distinction between datetime strings and payload keys
1 parent e49b498 commit 2c3155a

9 files changed

Lines changed: 81 additions & 53 deletions

File tree

docs/grpc/docs.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2715,7 +2715,8 @@ The JSON representation for `Value` is a JSON value.
27152715
| variable | [string](#string) | | Payload key or reference to score. |
27162716
| condition | [Condition](#qdrant-Condition) | | Payload condition. If true, becomes 1.0; otherwise 0.0 |
27172717
| geo_distance | [GeoDistance](#qdrant-GeoDistance) | | Geographic distance in meters |
2718-
| datetime | [string](#string) | | Date-time constant or payload key |
2718+
| datetime | [string](#string) | | Date-time constant |
2719+
| datetime_key | [string](#string) | | Payload key with date-time values |
27192720
| mult | [MultExpression](#qdrant-MultExpression) | | Multiply |
27202721
| sum | [SumExpression](#qdrant-SumExpression) | | Sum |
27212722
| div | [DivExpression](#qdrant-DivExpression) | | Divide |

docs/redoc/master/openapi.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14476,6 +14476,9 @@
1447614476
{
1447714477
"$ref": "#/components/schemas/DatetimeExpression"
1447814478
},
14479+
{
14480+
"$ref": "#/components/schemas/DatetimeKeyExpression"
14481+
},
1447914482
{
1448014483
"$ref": "#/components/schemas/MultExpression"
1448114484
},
@@ -14555,6 +14558,17 @@
1455514558
}
1455614559
}
1455714560
},
14561+
"DatetimeKeyExpression": {
14562+
"type": "object",
14563+
"required": [
14564+
"datetime_key"
14565+
],
14566+
"properties": {
14567+
"datetime_key": {
14568+
"type": "string"
14569+
}
14570+
}
14571+
},
1455814572
"MultExpression": {
1455914573
"type": "object",
1456014574
"required": [

lib/api/src/grpc/conversions.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2809,10 +2809,14 @@ fn unparse_expression(
28092809
origin: Some(GeoPoint::from(origin)),
28102810
to: key.to_string(),
28112811
}),
2812-
ParsedExpression::Datetime(dt_expr) => Variant::Datetime(match dt_expr {
2813-
DatetimeExpression::Constant(date_time_wrapper) => date_time_wrapper.to_string(),
2814-
DatetimeExpression::PayloadVariable(json_path) => json_path.to_string(),
2815-
}),
2812+
ParsedExpression::Datetime(dt_expr) => match dt_expr {
2813+
DatetimeExpression::Constant(date_time_wrapper) => {
2814+
Variant::Datetime(date_time_wrapper.to_string())
2815+
}
2816+
DatetimeExpression::PayloadVariable(json_path) => {
2817+
Variant::DatetimeKey(json_path.to_string())
2818+
}
2819+
},
28162820
ParsedExpression::Mult(exprs) => Variant::Mult(MultExpression {
28172821
mult: exprs
28182822
.into_iter()

lib/api/src/grpc/proto/points.proto

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -583,20 +583,21 @@ message Expression {
583583
string variable = 2; // Payload key or reference to score.
584584
Condition condition = 3; // Payload condition. If true, becomes 1.0; otherwise 0.0
585585
GeoDistance geo_distance = 4; // Geographic distance in meters
586-
string datetime = 5; // Date-time constant or payload key
587-
MultExpression mult = 6; // Multiply
588-
SumExpression sum = 7; // Sum
589-
DivExpression div = 8; // Divide
590-
Expression neg = 9; // Negate
591-
Expression abs = 10; // Absolute value
592-
Expression sqrt = 11; // Square root
593-
PowExpression pow = 12; // Power
594-
Expression exp = 13; // Exponential
595-
Expression log10 = 14; // Logarithm
596-
Expression ln = 15; // Natural logarithm
597-
DecayParamsExpression exp_decay = 16; // Exponential decay
598-
DecayParamsExpression gauss_decay = 17; // Gaussian decay
599-
DecayParamsExpression lin_decay = 18; // Linear decay
586+
string datetime = 5; // Date-time constant
587+
string datetime_key = 6; // Payload key with date-time values
588+
MultExpression mult = 7; // Multiply
589+
SumExpression sum = 8; // Sum
590+
DivExpression div = 9; // Divide
591+
Expression neg = 10; // Negate
592+
Expression abs = 11; // Absolute value
593+
Expression sqrt = 12; // Square root
594+
PowExpression pow = 13; // Power
595+
Expression exp = 14; // Exponential
596+
Expression log10 = 15; // Logarithm
597+
Expression ln = 16; // Natural logarithm
598+
DecayParamsExpression exp_decay = 17; // Exponential decay
599+
DecayParamsExpression gauss_decay = 18; // Gaussian decay
600+
DecayParamsExpression lin_decay = 19; // Linear decay
600601
}
601602
}
602603

lib/api/src/grpc/qdrant.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5182,7 +5182,7 @@ pub struct Formula {
51825182
pub struct Expression {
51835183
#[prost(
51845184
oneof = "expression::Variant",
5185-
tags = "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18"
5185+
tags = "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19"
51865186
)]
51875187
pub variant: ::core::option::Option<expression::Variant>,
51885188
}
@@ -5203,47 +5203,50 @@ pub mod expression {
52035203
/// Geographic distance in meters
52045204
#[prost(message, tag = "4")]
52055205
GeoDistance(super::GeoDistance),
5206-
/// Date-time constant or payload key
5206+
/// Date-time constant
52075207
#[prost(string, tag = "5")]
52085208
Datetime(::prost::alloc::string::String),
5209+
/// Payload key with date-time values
5210+
#[prost(string, tag = "6")]
5211+
DatetimeKey(::prost::alloc::string::String),
52095212
/// Multiply
5210-
#[prost(message, tag = "6")]
5213+
#[prost(message, tag = "7")]
52115214
Mult(super::MultExpression),
52125215
/// Sum
5213-
#[prost(message, tag = "7")]
5216+
#[prost(message, tag = "8")]
52145217
Sum(super::SumExpression),
52155218
/// Divide
5216-
#[prost(message, tag = "8")]
5219+
#[prost(message, tag = "9")]
52175220
Div(::prost::alloc::boxed::Box<super::DivExpression>),
52185221
/// Negate
5219-
#[prost(message, tag = "9")]
5222+
#[prost(message, tag = "10")]
52205223
Neg(::prost::alloc::boxed::Box<super::Expression>),
52215224
/// Absolute value
5222-
#[prost(message, tag = "10")]
5225+
#[prost(message, tag = "11")]
52235226
Abs(::prost::alloc::boxed::Box<super::Expression>),
52245227
/// Square root
5225-
#[prost(message, tag = "11")]
5228+
#[prost(message, tag = "12")]
52265229
Sqrt(::prost::alloc::boxed::Box<super::Expression>),
52275230
/// Power
5228-
#[prost(message, tag = "12")]
5231+
#[prost(message, tag = "13")]
52295232
Pow(::prost::alloc::boxed::Box<super::PowExpression>),
52305233
/// Exponential
5231-
#[prost(message, tag = "13")]
5234+
#[prost(message, tag = "14")]
52325235
Exp(::prost::alloc::boxed::Box<super::Expression>),
52335236
/// Logarithm
5234-
#[prost(message, tag = "14")]
5237+
#[prost(message, tag = "15")]
52355238
Log10(::prost::alloc::boxed::Box<super::Expression>),
52365239
/// Natural logarithm
5237-
#[prost(message, tag = "15")]
5240+
#[prost(message, tag = "16")]
52385241
Ln(::prost::alloc::boxed::Box<super::Expression>),
52395242
/// Exponential decay
5240-
#[prost(message, tag = "16")]
5243+
#[prost(message, tag = "17")]
52415244
ExpDecay(::prost::alloc::boxed::Box<super::DecayParamsExpression>),
52425245
/// Gaussian decay
5243-
#[prost(message, tag = "17")]
5246+
#[prost(message, tag = "18")]
52445247
GaussDecay(::prost::alloc::boxed::Box<super::DecayParamsExpression>),
52455248
/// Linear decay
5246-
#[prost(message, tag = "18")]
5249+
#[prost(message, tag = "19")]
52475250
LinDecay(::prost::alloc::boxed::Box<super::DecayParamsExpression>),
52485251
}
52495252
}

lib/api/src/rest/schema.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,7 @@ pub enum Expression {
642642
Condition(Box<Condition>),
643643
GeoDistance(GeoDistance),
644644
Datetime(DatetimeExpression),
645+
DatetimeKey(DatetimeKeyExpression),
645646
Mult(MultExpression),
646647
Sum(SumExpression),
647648
Neg(NegExpression),
@@ -675,6 +676,11 @@ pub struct DatetimeExpression {
675676
pub datetime: String,
676677
}
677678

679+
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
680+
pub struct DatetimeKeyExpression {
681+
pub datetime_key: String,
682+
}
683+
678684
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
679685
pub struct MultExpression {
680686
pub mult: Vec<Expression>,

lib/collection/src/operations/universal_query/formula.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use segment::index::query_optimization::rescore_formula::parsed_formula::{
88
DatetimeExpression, DecayKind, ParsedExpression, ParsedFormula, PreciseScore, VariableId,
99
};
1010
use segment::json_path::JsonPath;
11-
use segment::types::{Condition, GeoPoint};
11+
use segment::types::{Condition, GeoPoint, PayloadKeyType};
1212
use serde_json::Value;
1313

1414
use crate::operations::types::{CollectionError, CollectionResult};
@@ -29,6 +29,7 @@ pub enum ExpressionInternal {
2929
to: JsonPath,
3030
},
3131
Datetime(String),
32+
DatetimeKey(String),
3233
Mult(Vec<ExpressionInternal>),
3334
Sum(Vec<ExpressionInternal>),
3435
Neg(Box<ExpressionInternal>),
@@ -80,11 +81,18 @@ impl ExpressionInternal {
8081
ParsedExpression::new_geo_distance(origin, to)
8182
}
8283
ExpressionInternal::Datetime(dt_str) => {
83-
let dt_expr = dt_str.parse()?;
84-
if let DatetimeExpression::PayloadVariable(json_path) = &dt_expr {
85-
payload_vars.insert(json_path.clone());
86-
}
87-
ParsedExpression::Datetime(dt_expr)
84+
ParsedExpression::Datetime(DatetimeExpression::Constant(
85+
dt_str
86+
.parse()
87+
.map_err(|err: chrono::ParseError| err.to_string())?,
88+
))
89+
}
90+
ExpressionInternal::DatetimeKey(dt_key) => {
91+
let json_path: PayloadKeyType = dt_key
92+
.parse()
93+
.map_err(|_| format!("Invalid datetime payload variable: {dt_key}"))?;
94+
payload_vars.insert(json_path.clone());
95+
ParsedExpression::Datetime(DatetimeExpression::PayloadVariable(json_path))
8896
}
8997
ExpressionInternal::Mult(internal_expressions) => ParsedExpression::Mult(
9098
internal_expressions
@@ -209,6 +217,9 @@ impl From<rest::Expression> for ExpressionInternal {
209217
rest::Expression::Datetime(rest::DatetimeExpression { datetime }) => {
210218
ExpressionInternal::Datetime(datetime)
211219
}
220+
rest::Expression::DatetimeKey(rest::DatetimeKeyExpression { datetime_key }) => {
221+
ExpressionInternal::DatetimeKey(datetime_key)
222+
}
212223
rest::Expression::Mult(rest::MultExpression { mult: exprs }) => {
213224
ExpressionInternal::Mult(exprs.into_iter().map(ExpressionInternal::from).collect())
214225
}

lib/collection/src/operations/universal_query/shard_query.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ impl TryFrom<grpc::Expression> for ExpressionInternal {
398398
ExpressionInternal::GeoDistance { origin, to }
399399
}
400400
Variant::Datetime(dt_str) => ExpressionInternal::Datetime(dt_str),
401+
Variant::DatetimeKey(dt_key) => ExpressionInternal::DatetimeKey(dt_key),
401402
Variant::Mult(grpc::MultExpression { mult }) => {
402403
let mult = mult
403404
.into_iter()

lib/segment/src/index/query_optimization/rescore_formula/parsed_formula.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,19 +106,6 @@ pub enum DatetimeExpression {
106106
PayloadVariable(JsonPath),
107107
}
108108

109-
impl FromStr for DatetimeExpression {
110-
type Err = String;
111-
112-
fn from_str(s: &str) -> Result<Self, Self::Err> {
113-
// try as date
114-
DateTimePayloadType::from_str(s)
115-
.map(DatetimeExpression::Constant)
116-
// else as payload key
117-
.or_else(|_| JsonPath::from_str(s).map(DatetimeExpression::PayloadVariable))
118-
.map_err(|_| format!("Invalid date time expression: {s}"))
119-
}
120-
}
121-
122109
impl ParsedExpression {
123110
pub fn new_div(
124111
left: ParsedExpression,

0 commit comments

Comments
 (0)