@@ -163,12 +163,32 @@ impl Metadata {
163163 table_index
164164 }
165165
166- pub fn find_smallest_column_within ( & self , indices : & [ usize ] ) -> usize {
167- let entries = indices
166+ /// find_smallest_column in given indices.
167+ pub fn find_smallest_column ( & self , indices : & [ usize ] ) -> usize {
168+ let mut smallest_index = indices. iter ( ) . min ( ) . expect ( "indices must be valid" ) ;
169+ let mut smallest_size = usize:: MAX ;
170+ for idx in indices. iter ( ) {
171+ let entry = self . column ( * idx) ;
172+ if let Ok ( bytes) = entry. data_type . data_type_id ( ) . numeric_byte_size ( ) {
173+ if smallest_size > bytes {
174+ smallest_size = bytes;
175+ smallest_index = & entry. column_index ;
176+ }
177+ }
178+ }
179+ * smallest_index
180+ }
181+
182+ /// find_smallest_column_by_table_index by given table_index
183+ pub fn find_smallest_column_by_table_index ( & self , table_index : IndexType ) -> usize {
184+ let indices: Vec < usize > = self
185+ . columns
168186 . iter ( )
169- . map ( |i| self . column ( * i) . clone ( ) )
170- . collect :: < Vec < _ > > ( ) ;
171- find_smallest_column ( entries. as_slice ( ) )
187+ . filter ( |v| v. table_index == Some ( table_index) )
188+ . map ( |v| v. column_index )
189+ . collect ( ) ;
190+
191+ self . find_smallest_column ( & indices)
172192 }
173193}
174194
@@ -295,24 +315,3 @@ impl ColumnEntry {
295315 self . path_indices . is_some ( )
296316 }
297317}
298-
299- /// TODO(xuanwo): migrate this as a function of metadata.
300- pub fn find_smallest_column ( entries : & [ ColumnEntry ] ) -> usize {
301- debug_assert ! ( !entries. is_empty( ) ) ;
302- let mut column_indexes = entries
303- . iter ( )
304- . map ( |entry| entry. column_index )
305- . collect :: < Vec < IndexType > > ( ) ;
306- column_indexes. sort ( ) ;
307- let mut smallest_index = column_indexes[ 0 ] ;
308- let mut smallest_size = usize:: MAX ;
309- for ( idx, column_entry) in entries. iter ( ) . enumerate ( ) {
310- if let Ok ( bytes) = column_entry. data_type . data_type_id ( ) . numeric_byte_size ( ) {
311- if smallest_size > bytes {
312- smallest_size = bytes;
313- smallest_index = entries[ idx] . column_index ;
314- }
315- }
316- }
317- smallest_index
318- }
0 commit comments