Skip to content

Commit d1f3348

Browse files
committed
save
1 parent 4718e25 commit d1f3348

File tree

7 files changed

+89
-25
lines changed

7 files changed

+89
-25
lines changed

pgdog/src/backend/pool/connection/buffer.rs

+44-17
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{cmp::Ordering, collections::VecDeque};
44

55
use crate::{
66
frontend::router::parser::{Aggregate, OrderBy},
7-
net::messages::{DataRow, FromBytes, Message, Protocol, RowDescription, ToBytes},
7+
net::messages::{DataRow, FromBytes, Message, Protocol, RowDescription, ToBytes, Vector},
88
};
99

1010
use super::Aggregates;
@@ -34,33 +34,60 @@ impl Buffer {
3434

3535
/// Sort the buffer.
3636
pub(super) fn sort(&mut self, columns: &[OrderBy], rd: &RowDescription) {
37-
// Calculate column indecies once, since
38-
// fetching indecies by name is O(n).
37+
// Calculate column indicies once, since
38+
// fetching indicies by name is O(n).
3939
let mut cols = vec![];
4040
for column in columns {
41-
if let Some(index) = column.index() {
42-
cols.push(Some((index, column.asc())));
43-
} else if let Some(name) = column.name() {
44-
if let Some(index) = rd.field_index(name) {
45-
cols.push(Some((index, column.asc())));
46-
} else {
47-
cols.push(None);
41+
match column {
42+
OrderBy::Asc(_) => cols.push(column.clone()),
43+
OrderBy::AscColumn(name) => {
44+
if let Some(index) = rd.field_index(name) {
45+
cols.push(OrderBy::Asc(index + 1));
46+
}
47+
}
48+
OrderBy::Desc(_) => cols.push(column.clone()),
49+
OrderBy::DescColumn(name) => {
50+
if let Some(index) = rd.field_index(name) {
51+
cols.push(OrderBy::Desc(index + 1));
52+
}
53+
}
54+
OrderBy::AscVectorL2(_, _) => cols.push(column.clone()),
55+
OrderBy::AscVectorL2Column(name, vector) => {
56+
if let Some(index) = rd.field_index(name) {
57+
cols.push(OrderBy::AscVectorL2(index + 1, vector.clone()));
58+
}
4859
}
49-
} else {
50-
cols.push(None);
5160
};
5261
}
5362

5463
// Sort rows.
5564
let order_by = move |a: &DataRow, b: &DataRow| -> Ordering {
56-
for col in cols.iter().flatten() {
57-
let (index, asc) = col;
58-
let left = a.get_column(*index, rd);
59-
let right = b.get_column(*index, rd);
65+
for col in cols.iter() {
66+
let index = col.index();
67+
let asc = col.asc();
68+
let index = if let Some(index) = index {
69+
index
70+
} else {
71+
continue;
72+
};
73+
let left = a.get_column(index, rd);
74+
let right = b.get_column(index, rd);
6075

6176
let ordering = match (left, right) {
6277
(Ok(Some(left)), Ok(Some(right))) => {
63-
if *asc {
78+
if let OrderBy::AscVectorL2(_, vector) = col {
79+
let left: Option<Vector> = left.value.try_into().ok();
80+
let right: Option<Vector> = right.value.try_into().ok();
81+
82+
if let (Some(left), Some(right)) = (left, right) {
83+
let left = left.distance_l2(vector);
84+
let right = right.distance_l2(vector);
85+
86+
left.partial_cmp(&right)
87+
} else {
88+
Some(Ordering::Equal)
89+
}
90+
} else if asc {
6491
left.value.partial_cmp(&right.value)
6592
} else {
6693
right.value.partial_cmp(&left.value)

pgdog/src/frontend/router/parser/order_by.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,19 @@ pub enum OrderBy {
88
Desc(usize),
99
AscColumn(String),
1010
DescColumn(String),
11-
AscVectorL2(String, Vector),
11+
AscVectorL2Column(String, Vector),
12+
AscVectorL2(usize, Vector),
1213
}
1314

1415
impl OrderBy {
1516
/// ORDER BY x ASC
1617
pub fn asc(&self) -> bool {
1718
matches!(
1819
self,
19-
OrderBy::Asc(_) | OrderBy::AscColumn(_) | OrderBy::AscVectorL2(_, _)
20+
OrderBy::Asc(_)
21+
| OrderBy::AscColumn(_)
22+
| OrderBy::AscVectorL2Column(_, _)
23+
| OrderBy::AscVectorL2(_, _)
2024
)
2125
}
2226

@@ -25,6 +29,7 @@ impl OrderBy {
2529
match self {
2630
OrderBy::Asc(column) => Some(*column - 1),
2731
OrderBy::Desc(column) => Some(*column - 1),
32+
OrderBy::AscVectorL2(column, _) => Some(*column - 1),
2833
_ => None,
2934
}
3035
}
@@ -34,15 +39,15 @@ impl OrderBy {
3439
match self {
3540
OrderBy::AscColumn(ref name) => Some(name.as_str()),
3641
OrderBy::DescColumn(ref name) => Some(name.as_str()),
37-
OrderBy::AscVectorL2(ref name, _) => Some(name.as_str()),
42+
OrderBy::AscVectorL2Column(ref name, _) => Some(name.as_str()),
3843
_ => None,
3944
}
4045
}
4146

4247
/// ORDER BY clause contains a vector.
4348
pub fn vector(&self) -> Option<&Vector> {
4449
match self {
45-
OrderBy::AscVectorL2(_, vector) => Some(&vector),
50+
OrderBy::AscVectorL2Column(_, vector) => Some(&vector),
4651
_ => None,
4752
}
4853
}

pgdog/src/frontend/router/parser/query.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ impl QueryParser {
345345

346346
if let Some(vector) = vector {
347347
if let Some(column) = column {
348-
order_by.push(OrderBy::AscVectorL2(
348+
order_by.push(OrderBy::AscVectorL2Column(
349349
column, vector,
350350
));
351351
}

pgdog/src/net/messages/data_types/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ pub enum Datum {
4545
Uuid(Uuid),
4646
/// NUMERIC, REAL, DOUBLE PRECISION.
4747
Numeric(Numeric),
48+
/// Vector
49+
Vector(Vector),
4850
/// NULL.
4951
Null,
5052
}
@@ -63,6 +65,7 @@ impl ToDataRowColumn for Datum {
6365
TimestampTz(tz) => tz.to_data_row_column(),
6466
Uuid(uuid) => uuid.to_data_row_column(),
6567
Numeric(num) => num.to_data_row_column(),
68+
Vector(vector) => vector.to_data_row_column(),
6669
Null => Data::null(),
6770
}
6871
}
@@ -104,6 +107,7 @@ impl Datum {
104107
DataType::Uuid => Ok(Datum::Uuid(Uuid::decode(bytes, encoding)?)),
105108
DataType::Timestamp => Ok(Datum::Timestamp(Timestamp::decode(bytes, encoding)?)),
106109
DataType::TimestampTz => Ok(Datum::TimestampTz(TimestampTz::decode(bytes, encoding)?)),
110+
DataType::Vector => Ok(Datum::Vector(Vector::decode(bytes, encoding)?)),
107111
_ => Ok(Datum::Null),
108112
}
109113
}
@@ -130,4 +134,5 @@ pub enum DataType {
130134
Numeric,
131135
Other(i32),
132136
Uuid,
137+
Vector,
133138
}

pgdog/src/net/messages/data_types/numeric.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl Add for Numeric {
6565

6666
impl Ord for Numeric {
6767
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
68-
match self.partial_cmp(other) {
68+
match self.data.partial_cmp(&other.data) {
6969
Some(ordering) => ordering,
7070
None => {
7171
if self.data.is_nan() || other.data.is_nan() {

pgdog/src/net/messages/data_types/vector.rs

+28-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use crate::{
22
frontend::router::sharding::vector::Distance,
3-
net::{messages::Format, Error},
3+
net::{
4+
messages::{Format, ToDataRowColumn},
5+
Error,
6+
},
47
};
58
use bytes::Bytes;
69
use serde::{
@@ -10,7 +13,7 @@ use serde::{
1013
};
1114
use std::{ops::Deref, str::from_utf8};
1215

13-
use super::{FromDataType, Numeric};
16+
use super::{Datum, FromDataType, Numeric};
1417

1518
#[derive(Debug, Clone, PartialEq, PartialOrd, Ord, Eq, Hash)]
1619
#[repr(C)]
@@ -51,6 +54,12 @@ impl FromDataType for Vector {
5154
}
5255
}
5356

57+
impl ToDataRowColumn for Vector {
58+
fn to_data_row_column(&self) -> crate::net::messages::data_row::Data {
59+
Bytes::from(self.encode(Format::Text).unwrap()).into()
60+
}
61+
}
62+
5463
impl Vector {
5564
/// Length of the vector.
5665
pub fn len(&self) -> usize {
@@ -100,6 +109,23 @@ impl TryFrom<&str> for Vector {
100109
}
101110
}
102111

112+
impl Into<Datum> for Vector {
113+
fn into(self) -> Datum {
114+
Datum::Vector(self)
115+
}
116+
}
117+
118+
impl TryFrom<Datum> for Vector {
119+
type Error = Error;
120+
121+
fn try_from(value: Datum) -> Result<Self, Self::Error> {
122+
match value {
123+
Datum::Vector(vector) => Ok(vector),
124+
_ => Err(Error::UnexpectedPayload),
125+
}
126+
}
127+
}
128+
103129
struct VectorVisitor;
104130

105131
impl<'de> Visitor<'de> for VectorVisitor {

pgdog/src/net/messages/row_description.rs

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ impl Field {
9292
1184 => DataType::TimestampTz,
9393
1186 => DataType::Interval,
9494
2950 => DataType::Uuid,
95+
27231 => DataType::Vector, // TODO: this is actually variable.
9596
_ => DataType::Other(self.type_oid),
9697
}
9798
}

0 commit comments

Comments
 (0)