@@ -7,9 +7,8 @@ use itertools::Itertools;
77
88use super :: table:: InMemoryTableInnerRef ;
99use super :: { InMemoryRowHandler , InMemoryTable , InMemoryTxnIterator } ;
10- use crate :: array:: { ArrayBuilderImpl , ArrayImplBuilderPickExt , ArrayImplSortExt , DataChunk } ;
11- use crate :: catalog:: { find_sort_key_id, ColumnCatalog } ;
12- use crate :: storage:: { ScanOptions , StorageColumnRef , StorageResult , Transaction } ;
10+ use crate :: array:: { ArrayBuilderImpl , ArrayImplBuilderPickExt , DataChunk } ;
11+ use crate :: storage:: { ScanOptions , StorageColumnRef , StorageResult , Table , Transaction } ;
1312
1413/// A transaction running on `InMemoryStorage`.
1514pub struct InMemoryTransaction {
@@ -34,31 +33,42 @@ pub struct InMemoryTransaction {
3433 /// Snapshot of all deleted rows
3534 deleted_rows : Arc < HashSet < usize > > ,
3635
37- /// All information about columns
38- column_infos : Arc < [ ColumnCatalog ] > ,
36+ /// Ordered primary key indexes in `column_infos`
37+ ordered_pk_idx : Vec < usize > ,
3938}
4039
4140impl InMemoryTransaction {
4241 pub ( super ) fn start ( table : & InMemoryTable ) -> StorageResult < Self > {
4342 let inner = table. inner . read ( ) . unwrap ( ) ;
43+ let ordered_pk_idx = table
44+ . ordered_pk_ids ( )
45+ . iter ( )
46+ . map ( |id| {
47+ table
48+ . columns
49+ . iter ( )
50+ . position ( |c| c. id ( ) == * id)
51+ . expect ( "Malformed table object" )
52+ } )
53+ . collect_vec ( ) ;
4454 Ok ( Self {
4555 finished : false ,
4656 buffer : vec ! [ ] ,
4757 delete_buffer : vec ! [ ] ,
4858 table : table. inner . clone ( ) ,
4959 snapshot : Arc :: new ( inner. get_all_chunks ( ) ) ,
5060 deleted_rows : Arc :: new ( inner. get_all_deleted_rows ( ) ) ,
51- column_infos : table . columns . clone ( ) ,
61+ ordered_pk_idx ,
5262 } )
5363 }
5464}
5565
5666/// If primary key is found in [`ColumnCatalog`], sort all in-memory data using that key.
5767fn sort_datachunk_by_pk (
5868 chunks : & Arc < Vec < DataChunk > > ,
59- column_infos : & [ ColumnCatalog ] ,
69+ ordered_pk_idx : & [ usize ] ,
6070) -> Arc < Vec < DataChunk > > {
61- if let Some ( sort_key_id ) = find_sort_key_id ( column_infos ) {
71+ if !ordered_pk_idx . is_empty ( ) {
6272 if chunks. is_empty ( ) {
6373 return chunks. clone ( ) ;
6474 }
@@ -78,7 +88,15 @@ fn sort_datachunk_by_pk(
7888 . into_iter ( )
7989 . map ( |builder| builder. finish ( ) )
8090 . collect_vec ( ) ;
81- let sorted_index = arrays[ sort_key_id] . get_sorted_indices ( ) ;
91+
92+ let pk_arrays = Vec :: from ( ordered_pk_idx)
93+ . iter ( )
94+ . map ( |idx| & arrays[ * idx] )
95+ . collect_vec ( ) ;
96+ let pk_array = itertools:: izip!( pk_arrays) . collect_vec ( ) ;
97+ let sorted_index = ( 0 ..pk_array. len ( ) )
98+ . sorted_by_key ( |idx| pk_array[ * idx] )
99+ . collect_vec ( ) ;
82100
83101 let chunk = arrays
84102 . into_iter ( )
@@ -109,7 +127,7 @@ impl Transaction for InMemoryTransaction {
109127 assert ! ( !opts. reversed, "reverse iterator is not supported for now" ) ;
110128
111129 let snapshot = if opts. is_sorted {
112- sort_datachunk_by_pk ( & self . snapshot , & self . column_infos )
130+ sort_datachunk_by_pk ( & self . snapshot , & self . ordered_pk_idx )
113131 } else {
114132 self . snapshot . clone ( )
115133 } ;
0 commit comments