@@ -4,7 +4,7 @@ use std::{cmp::Ordering, collections::VecDeque};
4
4
5
5
use crate :: {
6
6
frontend:: router:: parser:: { Aggregate , OrderBy } ,
7
- net:: messages:: { DataRow , FromBytes , Message , Protocol , RowDescription , ToBytes } ,
7
+ net:: messages:: { DataRow , FromBytes , Message , Protocol , RowDescription , ToBytes , Vector } ,
8
8
} ;
9
9
10
10
use super :: Aggregates ;
@@ -34,33 +34,60 @@ impl Buffer {
34
34
35
35
/// Sort the buffer.
36
36
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).
39
39
let mut cols = vec ! [ ] ;
40
40
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
+ }
48
59
}
49
- } else {
50
- cols. push ( None ) ;
51
60
} ;
52
61
}
53
62
54
63
// Sort rows.
55
64
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) ;
60
75
61
76
let ordering = match ( left, right) {
62
77
( 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 {
64
91
left. value . partial_cmp ( & right. value )
65
92
} else {
66
93
right. value . partial_cmp ( & left. value )
0 commit comments