@@ -414,47 +414,44 @@ impl<T> RelIter<T> {
414414 }
415415}
416416
417+ /// A borrowed version of [RelValue].
417418#[ derive( Debug , Clone , Copy ) ]
418419pub struct RelValueRef < ' a > {
419- pub head : & ' a Header ,
420420 pub data : & ' a ProductValue ,
421421}
422422
423423impl < ' a > RelValueRef < ' a > {
424- pub fn new ( head : & ' a Header , data : & ' a ProductValue ) -> Self {
425- Self { head , data }
424+ pub fn new ( data : & ' a ProductValue ) -> Self {
425+ Self { data }
426426 }
427427
428- pub fn get ( & self , col : & ' a FieldExpr ) -> & ' a AlgebraicValue {
428+ pub fn get ( & self , col : & ' a FieldExpr , header : & ' a Header ) -> & ' a AlgebraicValue {
429429 match col {
430430 FieldExpr :: Name ( col) => {
431- if let Some ( pos) = self . head . column_pos ( col) {
431+ if let Some ( pos) = header . column_pos ( col) {
432432 if let Some ( v) = self . data . elements . get ( pos) {
433433 v
434434 } else {
435435 unreachable ! ( "Field `{col}` at pos {pos} not found on row: {:?}" , self . data. elements)
436436 }
437437 } else {
438- unreachable ! (
439- "Field `{col}` not found on `{}`. Fields:{}" ,
440- self . head. table_name, self . head
441- )
438+ unreachable ! ( "Field `{col}` not found on `{}`. Fields:{}" , header. table_name, header)
442439 }
443440 }
444441 FieldExpr :: Value ( x) => x,
445442 }
446443 }
447444
448- pub fn project ( & self , cols : & [ FieldExpr ] ) -> Result < ProductValue , RelationError > {
445+ pub fn project ( & self , cols : & [ FieldExpr ] , header : & ' a Header ) -> Result < ProductValue , RelationError > {
449446 let mut elements = Vec :: with_capacity ( cols. len ( ) ) ;
450447
451448 for col in cols {
452449 match col {
453450 FieldExpr :: Name ( col) => {
454- if let Some ( pos) = self . head . column_pos ( col) {
451+ if let Some ( pos) = header . column_pos ( col) {
455452 elements. push ( self . data . elements [ pos] . clone ( ) ) ;
456453 } else {
457- return Err ( RelationError :: FieldNotFound ( self . head . clone ( ) , col. clone ( ) ) ) ;
454+ return Err ( RelationError :: FieldNotFound ( header . clone ( ) , col. clone ( ) ) ) ;
458455 }
459456 }
460457 FieldExpr :: Value ( col) => {
@@ -467,41 +464,29 @@ impl<'a> RelValueRef<'a> {
467464 }
468465}
469466
470- impl Relation for RelValueRef < ' _ > {
471- fn head ( & self ) -> Header {
472- self . head . clone ( )
473- }
474-
475- fn row_count ( & self ) -> RowCount {
476- RowCount :: exact ( 1 )
477- }
478- }
479-
480- /// The row/tuple generated by a [Relation] operator
467+ /// RelValue represents a materialized row during query execution.
468+ /// In particular it is the type generated/consumed by a [Relation] operator.
469+ /// This is in contrast to a `DataRef` which represents a row belonging to a table.
470+ /// The difference being that a RelValue's [DataKey] is optional since relational
471+ /// operators can modify their input rows.
481472#[ derive( Debug , Clone , Eq ) ]
482473pub struct RelValue {
483474 pub id : Option < DataKey > ,
484- pub head : Header ,
485475 pub data : ProductValue ,
486476}
487477
488478impl RelValue {
489- pub fn new ( head : & Header , data : & ProductValue , id : Option < DataKey > ) -> Self {
490- Self {
491- id,
492- head : head. clone ( ) ,
493- data : data. clone ( ) ,
494- }
479+ pub fn new ( data : ProductValue , id : Option < DataKey > ) -> Self {
480+ Self { id, data }
495481 }
496482
497483 pub fn as_val_ref ( & self ) -> RelValueRef {
498- RelValueRef :: new ( & self . head , & self . data )
484+ RelValueRef :: new ( & self . data )
499485 }
500486
501- pub fn extend ( self , head : & Header , with : RelValue ) -> RelValue {
487+ pub fn extend ( self , with : RelValue ) -> RelValue {
502488 let mut x = self ;
503489 x. id = None ;
504- x. head = head. clone ( ) ;
505490 x. data . elements . extend ( with. data . elements ) ;
506491 x
507492 }
@@ -558,14 +543,14 @@ impl MemTable {
558543
559544 pub fn from_value ( of : AlgebraicValue ) -> Self {
560545 let head = Header :: for_mem_table ( of. type_of ( ) . into ( ) ) ;
561- let row = RelValue :: new ( & head , & of. into ( ) , None ) ;
546+ let row = RelValue :: new ( of. into ( ) , None ) ;
562547 Self :: new ( & head, StAccess :: Public , & [ row] )
563548 }
564549
565550 pub fn from_iter ( head : & Header , data : impl Iterator < Item = ProductValue > ) -> Self {
566551 Self {
567552 head : head. clone ( ) ,
568- data : data. map ( |row| RelValue :: new ( head , & row, None ) ) . collect ( ) ,
553+ data : data. map ( |row| RelValue :: new ( row, None ) ) . collect ( ) ,
569554 table_access : StAccess :: Public ,
570555 }
571556 }
0 commit comments