@@ -27,7 +27,7 @@ impl<S: LogSink> LensesSink<S> {
27
27
}
28
28
29
29
/// Adds a [`Lens`] to this sink.
30
- pub fn with_lens ( mut self , lens : LensN ) -> Self {
30
+ pub fn with_lens ( mut self , lens : Lens ) -> Self {
31
31
self . registry . lenses . push ( lens) ;
32
32
self
33
33
}
@@ -79,11 +79,11 @@ impl<S: LogSink> LogSink for LensesSink<S> {
79
79
80
80
#[ derive( Default ) ]
81
81
struct LensRegistry {
82
- lenses : Vec < LensN > ,
82
+ lenses : Vec < Lens > ,
83
83
}
84
84
85
85
impl LensRegistry {
86
- fn relevant ( & self , chunk : & Chunk ) -> impl Iterator < Item = & LensN > {
86
+ fn relevant ( & self , chunk : & Chunk ) -> impl Iterator < Item = & Lens > {
87
87
self . lenses
88
88
. iter ( )
89
89
. filter ( |lens| lens. input . entity_path_filter . matches ( chunk. entity_path ( ) ) )
@@ -205,29 +205,40 @@ pub enum Error {
205
205
///
206
206
/// Individual operations are wrapped to hide their implementation details.
207
207
pub enum Op {
208
- Cast ( op :: Cast ) ,
208
+ /// Extracts a specific field from a `StructArray`.
209
209
AccessField ( op:: AccessField ) ,
210
+
211
+ /// Efficiently casts a component to a new `DataType`.
212
+ Cast ( op:: Cast ) ,
213
+
214
+ /// A user-defined arbitrary function to convert a component column.
210
215
Func ( Box < dyn Fn ( ListArray ) -> Result < ListArray , Error > + Sync + Send > ) ,
211
216
}
212
217
213
218
impl Op {
219
+ /// Extracts a specific field from a `StructArray`.
214
220
pub fn access_field ( field_name : impl Into < String > ) -> Self {
215
221
Self :: AccessField ( op:: AccessField {
216
222
field_name : field_name. into ( ) ,
217
223
} )
218
224
}
219
225
226
+ /// Efficiently casts a component to a new `DataType`.
220
227
pub fn cast ( data_type : DataType ) -> Self {
221
228
Self :: Cast ( op:: Cast {
222
229
to_inner_type : data_type,
223
230
} )
224
231
}
225
232
226
- // TODO: this should be improved
233
+ /// Ignores any input and returns a constant `ListArray`.
234
+ ///
235
+ /// Mostl commonly used with [`LensBuilder::static_output_column`].
236
+ /// When used in non-static columns this function will _not_ guarantee the correct amount of rows.
227
237
pub fn constant ( value : ListArray ) -> Self {
228
238
Self :: func ( move |_| Ok ( value. clone ( ) ) )
229
239
}
230
240
241
+ /// A user-defined arbitrary function to convert a component column.
231
242
pub fn func < F > ( func : F ) -> Self
232
243
where
233
244
F : Fn ( ListArray ) -> Result < ListArray , Error > + Send + Sync + ' static ,
@@ -259,14 +270,15 @@ struct OutputColumn {
259
270
}
260
271
261
272
/// Provides convenient function to create a [`Lens`].
262
- pub struct LensBuilder ( LensN ) ;
273
+ pub struct LensBuilder ( Lens ) ;
263
274
264
275
impl LensBuilder {
276
+ /// The column on which this [`Lens`] will operate on.
265
277
pub fn input_column (
266
278
entity_path_filter : EntityPathFilter ,
267
279
component : impl Into < ComponentIdentifier > ,
268
280
) -> Self {
269
- Self ( LensN {
281
+ Self ( Lens {
270
282
input : InputColumn {
271
283
entity_path_filter : entity_path_filter. resolve_without_substitutions ( ) ,
272
284
component : component. into ( ) ,
@@ -275,6 +287,7 @@ impl LensBuilder {
275
287
} )
276
288
}
277
289
290
+ /// Can be used to define one or more output columns that are derived from the [`Self::input_column`].
278
291
pub fn output_column (
279
292
mut self ,
280
293
entity_path : impl Into < EntityPath > ,
@@ -291,6 +304,7 @@ impl LensBuilder {
291
304
self
292
305
}
293
306
307
+ /// Can be used to define one or more output columns that are derived from the [`Self::input_column`].
294
308
pub fn static_output_column (
295
309
mut self ,
296
310
entity_path : impl Into < EntityPath > ,
@@ -307,7 +321,8 @@ impl LensBuilder {
307
321
self
308
322
}
309
323
310
- pub fn build ( self ) -> LensN {
324
+ // Finalizes this builder and returns the corresponding lens.
325
+ pub fn build ( self ) -> Lens {
311
326
self . 0
312
327
}
313
328
}
@@ -320,15 +335,15 @@ impl LensBuilder {
320
335
///
321
336
/// # Assumptions
322
337
///
323
- /// Works on component column within a chunk. Because what goes into a chunk
324
- /// is non-deterministic, and dependent on the batcher no assumptions should be
325
- /// made for values contained in each row of a column .
326
- pub struct LensN {
338
+ /// Works on component columns within a chunk. Because what goes into a chunk
339
+ /// is non-deterministic, and dependent on the batcher, no assumptions should be
340
+ /// made for values across rows .
341
+ pub struct Lens {
327
342
input : InputColumn ,
328
343
outputs : Vec < OutputColumn > ,
329
344
}
330
345
331
- impl LensN {
346
+ impl Lens {
332
347
/// Returns a new [`LensBuilder`] with the given input column.
333
348
pub fn input_column (
334
349
entity_path_filter : EntityPathFilter ,
@@ -338,15 +353,15 @@ impl LensN {
338
353
}
339
354
}
340
355
341
- impl LensN {
356
+ impl Lens {
342
357
/// Applies this lens and crates one or more chunks.
343
358
fn apply ( & self , chunk : & Chunk ) -> Vec < Chunk > {
344
359
let found = chunk
345
360
. components ( )
346
361
. iter ( )
347
362
. find ( |( descr, _array) | descr. component == self . input . component ) ;
348
363
349
- // TODO: This means we drop chunks that belong to the same entity but don't have the component.
364
+ // This means we drop chunks that belong to the same entity but don't have the component.
350
365
let Some ( ( _component_descr, list_array) ) = found else {
351
366
return Default :: default ( ) ;
352
367
} ;
@@ -566,7 +581,7 @@ mod test {
566
581
println ! ( "{original_chunk}" ) ;
567
582
568
583
let destructure =
569
- LensN :: input_column ( EntityPathFilter :: parse_forgiving ( "nullability" ) , "structs" )
584
+ Lens :: input_column ( EntityPathFilter :: parse_forgiving ( "nullability" ) , "structs" )
570
585
. output_column (
571
586
"nullability/a" ,
572
587
Scalars :: descriptor_scalars ( ) ,
@@ -591,7 +606,7 @@ mod test {
591
606
println ! ( "{original_chunk}" ) ;
592
607
593
608
let destructure =
594
- LensN :: input_column ( EntityPathFilter :: parse_forgiving ( "nullability" ) , "structs" )
609
+ Lens :: input_column ( EntityPathFilter :: parse_forgiving ( "nullability" ) , "structs" )
595
610
. output_column (
596
611
"nullability/b" ,
597
612
Scalars :: descriptor_scalars ( ) ,
@@ -633,19 +648,18 @@ mod test {
633
648
Ok ( builder. finish ( ) )
634
649
} ;
635
650
636
- let count =
637
- LensN :: input_column ( EntityPathFilter :: parse_forgiving ( "nullability" ) , "strings" )
638
- . output_column (
639
- "nullability/b_count" ,
640
- ComponentDescriptor :: partial ( "counts" ) ,
641
- [ Op :: func ( count_fn) ] ,
642
- )
643
- . output_column (
644
- "nullability/b_count" ,
645
- ComponentDescriptor :: partial ( "original" ) ,
646
- [ ] , // no operations
647
- )
648
- . build ( ) ;
651
+ let count = Lens :: input_column ( EntityPathFilter :: parse_forgiving ( "nullability" ) , "strings" )
652
+ . output_column (
653
+ "nullability/b_count" ,
654
+ ComponentDescriptor :: partial ( "counts" ) ,
655
+ [ Op :: func ( count_fn) ] ,
656
+ )
657
+ . output_column (
658
+ "nullability/b_count" ,
659
+ ComponentDescriptor :: partial ( "original" ) ,
660
+ [ ] , // no operations
661
+ )
662
+ . build ( ) ;
649
663
650
664
let pipeline = LensRegistry {
651
665
lenses : vec ! [ count] ,
@@ -675,7 +689,7 @@ mod test {
675
689
metadata_builder_b. append ( true ) ;
676
690
677
691
let static_lens =
678
- LensN :: input_column ( EntityPathFilter :: parse_forgiving ( "nullability" ) , "strings" )
692
+ Lens :: input_column ( EntityPathFilter :: parse_forgiving ( "nullability" ) , "strings" )
679
693
. static_output_column (
680
694
"nullability/static" ,
681
695
ComponentDescriptor :: partial ( "static_metadata_a" ) ,
0 commit comments