@@ -83,8 +83,13 @@ def _drop_skips_dict(exprs_dict):
83
83
84
84
85
85
class ITable :
86
- source_table : Any
87
- schema : Schema = None
86
+ @property
87
+ def source_table (self ) -> "ITable" : # not always Self, it can be a substitute
88
+ return self
89
+
90
+ @property
91
+ def schema (self ) -> Optional [Schema ]:
92
+ return None
88
93
89
94
def select (self , * exprs , distinct = SKIP , optimizer_hints = SKIP , ** named_exprs ) -> "ITable" :
90
95
"""Choose new columns, based on the old ones. (aka Projection)
@@ -389,11 +394,7 @@ def type(self):
389
394
@dataclass
390
395
class TablePath (ExprNode , ITable ):
391
396
path : DbPath
392
- schema : Optional [Schema ] = field (default = None , repr = False )
393
-
394
- @property
395
- def source_table (self ) -> Self :
396
- return self
397
+ schema : Optional [Schema ] = None # overrides the inherited property
397
398
398
399
# Statement shorthands
399
400
def create (self , source_table : ITable = None , * , if_not_exists : bool = False , primary_keys : List [str ] = None ):
@@ -475,9 +476,17 @@ def time_travel(
475
476
476
477
@dataclass
477
478
class TableAlias (ExprNode , ITable ):
478
- source_table : ITable
479
+ table : ITable
479
480
name : str
480
481
482
+ @property
483
+ def source_table (self ) -> ITable :
484
+ return self .table
485
+
486
+ @property
487
+ def schema (self ) -> Schema :
488
+ return self .table .schema
489
+
481
490
482
491
@dataclass
483
492
class Join (ExprNode , ITable , Root ):
@@ -487,11 +496,7 @@ class Join(ExprNode, ITable, Root):
487
496
columns : Sequence [Expr ] = None
488
497
489
498
@property
490
- def source_table (self ) -> Self :
491
- return self
492
-
493
- @property
494
- def schema (self ):
499
+ def schema (self ) -> Schema :
495
500
assert self .columns # TODO Implement SELECT *
496
501
s = self .source_tables [0 ].schema # TODO validate types match between both tables
497
502
return type (s )({c .name : c .type for c in self .columns })
@@ -533,10 +538,6 @@ class GroupBy(ExprNode, ITable, Root):
533
538
values : Sequence [Expr ] = None
534
539
having_exprs : Sequence [Expr ] = None
535
540
536
- @property
537
- def source_table (self ):
538
- return self
539
-
540
541
def __post_init__ (self ):
541
542
assert self .keys or self .values
542
543
@@ -564,17 +565,13 @@ class TableOp(ExprNode, ITable, Root):
564
565
table1 : ITable
565
566
table2 : ITable
566
567
567
- @property
568
- def source_table (self ):
569
- return self
570
-
571
568
@property
572
569
def type (self ):
573
570
# TODO ensure types of both tables are compatible
574
571
return self .table1 .type
575
572
576
573
@property
577
- def schema (self ):
574
+ def schema (self ) -> Schema :
578
575
s1 = self .table1 .schema
579
576
s2 = self .table2 .schema
580
577
assert len (s1 ) == len (s2 )
@@ -594,16 +591,12 @@ class Select(ExprNode, ITable, Root):
594
591
optimizer_hints : Sequence [Expr ] = None
595
592
596
593
@property
597
- def schema (self ):
594
+ def schema (self ) -> Schema :
598
595
s = self .table .schema
599
596
if s is None or self .columns is None :
600
597
return s
601
598
return type (s )({c .name : c .type for c in self .columns })
602
599
603
- @property
604
- def source_table (self ):
605
- return self
606
-
607
600
@classmethod
608
601
def make (cls , table : ITable , distinct : bool = SKIP , optimizer_hints : str = SKIP , ** kwargs ):
609
602
assert "table" not in kwargs
@@ -642,14 +635,18 @@ def make(cls, table: ITable, distinct: bool = SKIP, optimizer_hints: str = SKIP,
642
635
643
636
@dataclass
644
637
class Cte (ExprNode , ITable ):
645
- source_table : Expr
638
+ table : Expr
646
639
name : str = None
647
640
params : Sequence [str ] = None
648
641
649
642
@property
650
- def schema (self ):
643
+ def source_table (self ) -> "ITable" :
644
+ return self .table
645
+
646
+ @property
647
+ def schema (self ) -> Schema :
651
648
# TODO add cte to schema
652
- return self .source_table .schema
649
+ return self .table .schema
653
650
654
651
655
652
def _named_exprs_as_aliases (named_exprs ):
@@ -820,7 +817,3 @@ class Param(ExprNode, ITable):
820
817
"""A value placeholder, to be specified at compilation time using the `cv_params` context variable."""
821
818
822
819
name : str
823
-
824
- @property
825
- def source_table (self ):
826
- return self
0 commit comments