@@ -11,6 +11,7 @@ use std::{slice, vec};
11
11
use ulid:: Generator ;
12
12
13
13
pub type TableName = Arc < String > ;
14
+ pub type PrimaryKeyIndices = Arc < Vec < usize > > ;
14
15
15
16
#[ derive( Debug , Clone , PartialEq ) ]
16
17
pub struct TableCatalog {
@@ -22,6 +23,7 @@ pub struct TableCatalog {
22
23
23
24
schema_ref : SchemaRef ,
24
25
primary_keys : Vec < ( usize , ColumnRef ) > ,
26
+ primary_key_indices : PrimaryKeyIndices ,
25
27
primary_key_type : Option < LogicalType > ,
26
28
}
27
29
@@ -32,6 +34,10 @@ pub struct TableMeta {
32
34
}
33
35
34
36
impl TableCatalog {
37
+ pub ( crate ) fn name ( & self ) -> & TableName {
38
+ & self . name
39
+ }
40
+
35
41
pub ( crate ) fn get_unique_index ( & self , col_id : & ColumnId ) -> Option < & IndexMetaRef > {
36
42
self . indexes
37
43
. iter ( )
@@ -79,6 +85,10 @@ impl TableCatalog {
79
85
& self . primary_keys
80
86
}
81
87
88
+ pub ( crate ) fn primary_keys_indices ( & self ) -> & PrimaryKeyIndices {
89
+ & self . primary_key_indices
90
+ }
91
+
82
92
pub ( crate ) fn types ( & self ) -> Vec < LogicalType > {
83
93
self . columns ( )
84
94
. map ( |column| column. datatype ( ) . clone ( ) )
@@ -186,6 +196,7 @@ impl TableCatalog {
186
196
indexes : vec ! [ ] ,
187
197
schema_ref : Arc :: new ( vec ! [ ] ) ,
188
198
primary_keys : vec ! [ ] ,
199
+ primary_key_indices : Default :: default ( ) ,
189
200
primary_key_type : None ,
190
201
} ;
191
202
let mut generator = Generator :: new ( ) ;
@@ -194,7 +205,11 @@ impl TableCatalog {
194
205
. add_column ( col_catalog, & mut generator)
195
206
. unwrap ( ) ;
196
207
}
197
- table_catalog. primary_keys = Self :: build_primary_keys ( & table_catalog. schema_ref ) ;
208
+ let ( primary_keys, primary_key_indices) =
209
+ Self :: build_primary_keys ( & table_catalog. schema_ref ) ;
210
+
211
+ table_catalog. primary_keys = primary_keys;
212
+ table_catalog. primary_key_indices = primary_key_indices;
198
213
199
214
Ok ( table_catalog)
200
215
}
@@ -216,7 +231,7 @@ impl TableCatalog {
216
231
columns. insert ( column_id, i) ;
217
232
}
218
233
let schema_ref = Arc :: new ( column_refs. clone ( ) ) ;
219
- let primary_keys = Self :: build_primary_keys ( & schema_ref) ;
234
+ let ( primary_keys, primary_key_indices ) = Self :: build_primary_keys ( & schema_ref) ;
220
235
221
236
Ok ( TableCatalog {
222
237
name,
@@ -225,12 +240,18 @@ impl TableCatalog {
225
240
indexes,
226
241
schema_ref,
227
242
primary_keys,
243
+ primary_key_indices,
228
244
primary_key_type : None ,
229
245
} )
230
246
}
231
247
232
- fn build_primary_keys ( schema_ref : & Arc < Vec < ColumnRef > > ) -> Vec < ( usize , ColumnRef ) > {
233
- schema_ref
248
+ fn build_primary_keys (
249
+ schema_ref : & Arc < Vec < ColumnRef > > ,
250
+ ) -> ( Vec < ( usize , ColumnRef ) > , PrimaryKeyIndices ) {
251
+ let mut primary_keys = Vec :: new ( ) ;
252
+ let mut primary_key_indices = Vec :: new ( ) ;
253
+
254
+ for ( _, ( i, column) ) in schema_ref
234
255
. iter ( )
235
256
. enumerate ( )
236
257
. filter_map ( |( i, column) | {
@@ -240,8 +261,12 @@ impl TableCatalog {
240
261
. map ( |p_i| ( p_i, ( i, column. clone ( ) ) ) )
241
262
} )
242
263
. sorted_by_key ( |( p_i, _) | * p_i)
243
- . map ( |( _, entry) | entry)
244
- . collect_vec ( )
264
+ {
265
+ primary_key_indices. push ( i) ;
266
+ primary_keys. push ( ( i, column) ) ;
267
+ }
268
+
269
+ ( primary_keys, Arc :: new ( primary_key_indices) )
245
270
}
246
271
}
247
272
0 commit comments