Skip to content

Commit a684943

Browse files
committed
use interface distinction between datetime strings and payload keys
1 parent 8b707d6 commit a684943

9 files changed

Lines changed: 80 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
@@ -14470,6 +14470,9 @@
1447014470
{
1447114471
"$ref": "#/components/schemas/DatetimeExpression"
1447214472
},
14473+
{
14474+
"$ref": "#/components/schemas/DatetimeKeyExpression"
14475+
},
1447314476
{
1447414477
"$ref": "#/components/schemas/MultExpression"
1447514478
},
@@ -14549,6 +14552,17 @@
1454914552
}
1455014553
}
1455114554
},
14555+
"DatetimeKeyExpression": {
14556+
"type": "object",
14557+
"required": [
14558+
"datetime_key"
14559+
],
14560+
"properties": {
14561+
"datetime_key": {
14562+
"type": "string"
14563+
}
14564+
}
14565+
},
1455214566
"MultExpression": {
1455314567
"type": "object",
1455414568
"required": [

lib/api/src/grpc/conversions.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2806,10 +2806,14 @@ fn unparse_expression(
28062806
origin: Some(GeoPoint::from(origin)),
28072807
to: key.to_string(),
28082808
}),
2809-
ParsedExpression::Datetime(dt_expr) => Variant::Datetime(match dt_expr {
2810-
DatetimeExpression::Constant(date_time_wrapper) => date_time_wrapper.to_string(),
2811-
DatetimeExpression::PayloadVariable(json_path) => json_path.to_string(),
2812-
}),
2809+
ParsedExpression::Datetime(dt_expr) => match dt_expr {
2810+
DatetimeExpression::Constant(date_time_wrapper) => {
2811+
Variant::Datetime(date_time_wrapper.to_string())
2812+
}
2813+
DatetimeExpression::PayloadVariable(json_path) => {
2814+
Variant::DatetimeKey(json_path.to_string())
2815+
}
2816+
},
28132817
ParsedExpression::Mult(exprs) => Variant::Mult(MultExpression {
28142818
mult: exprs
28152819
.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: 17 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,49 @@ 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+
#[prost(string, tag = "6")]
5210+
DatetimeKey(::prost::alloc::string::String),
52095211
/// Multiply
5210-
#[prost(message, tag = "6")]
5212+
#[prost(message, tag = "7")]
52115213
Mult(super::MultExpression),
52125214
/// Sum
5213-
#[prost(message, tag = "7")]
5215+
#[prost(message, tag = "8")]
52145216
Sum(super::SumExpression),
52155217
/// Divide
5216-
#[prost(message, tag = "8")]
5218+
#[prost(message, tag = "9")]
52175219
Div(::prost::alloc::boxed::Box<super::DivExpression>),
52185220
/// Negate
5219-
#[prost(message, tag = "9")]
5221+
#[prost(message, tag = "10")]
52205222
Neg(::prost::alloc::boxed::Box<super::Expression>),
52215223
/// Absolute value
5222-
#[prost(message, tag = "10")]
5224+
#[prost(message, tag = "11")]
52235225
Abs(::prost::alloc::boxed::Box<super::Expression>),
52245226
/// Square root
5225-
#[prost(message, tag = "11")]
5227+
#[prost(message, tag = "12")]
52265228
Sqrt(::prost::alloc::boxed::Box<super::Expression>),
52275229
/// Power
5228-
#[prost(message, tag = "12")]
5230+
#[prost(message, tag = "13")]
52295231
Pow(::prost::alloc::boxed::Box<super::PowExpression>),
52305232
/// Exponential
5231-
#[prost(message, tag = "13")]
5233+
#[prost(message, tag = "14")]
52325234
Exp(::prost::alloc::boxed::Box<super::Expression>),
52335235
/// Logarithm
5234-
#[prost(message, tag = "14")]
5236+
#[prost(message, tag = "15")]
52355237
Log10(::prost::alloc::boxed::Box<super::Expression>),
52365238
/// Natural logarithm
5237-
#[prost(message, tag = "15")]
5239+
#[prost(message, tag = "16")]
52385240
Ln(::prost::alloc::boxed::Box<super::Expression>),
52395241
/// Exponential decay
5240-
#[prost(message, tag = "16")]
5242+
#[prost(message, tag = "17")]
52415243
ExpDecay(::prost::alloc::boxed::Box<super::DecayParamsExpression>),
52425244
/// Gaussian decay
5243-
#[prost(message, tag = "17")]
5245+
#[prost(message, tag = "18")]
52445246
GaussDecay(::prost::alloc::boxed::Box<super::DecayParamsExpression>),
52455247
/// Linear decay
5246-
#[prost(message, tag = "18")]
5248+
#[prost(message, tag = "19")]
52475249
LinDecay(::prost::alloc::boxed::Box<super::DecayParamsExpression>),
52485250
}
52495251
}

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)