Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit cddf8a0

Browse files
author
Sergey Vasilyev
committed
Convert source_table & schema to overridable properties for attrs compatibility
1 parent 4091e49 commit cddf8a0

File tree

1 file changed

+27
-34
lines changed

1 file changed

+27
-34
lines changed

data_diff/queries/ast_classes.py

+27-34
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,13 @@ def _drop_skips_dict(exprs_dict):
8383

8484

8585
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
8893

8994
def select(self, *exprs, distinct=SKIP, optimizer_hints=SKIP, **named_exprs) -> "ITable":
9095
"""Choose new columns, based on the old ones. (aka Projection)
@@ -389,11 +394,7 @@ def type(self):
389394
@dataclass
390395
class TablePath(ExprNode, ITable):
391396
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
397398

398399
# Statement shorthands
399400
def create(self, source_table: ITable = None, *, if_not_exists: bool = False, primary_keys: List[str] = None):
@@ -475,9 +476,17 @@ def time_travel(
475476

476477
@dataclass
477478
class TableAlias(ExprNode, ITable):
478-
source_table: ITable
479+
table: ITable
479480
name: str
480481

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+
481490

482491
@dataclass
483492
class Join(ExprNode, ITable, Root):
@@ -487,11 +496,7 @@ class Join(ExprNode, ITable, Root):
487496
columns: Sequence[Expr] = None
488497

489498
@property
490-
def source_table(self) -> Self:
491-
return self
492-
493-
@property
494-
def schema(self):
499+
def schema(self) -> Schema:
495500
assert self.columns # TODO Implement SELECT *
496501
s = self.source_tables[0].schema # TODO validate types match between both tables
497502
return type(s)({c.name: c.type for c in self.columns})
@@ -533,10 +538,6 @@ class GroupBy(ExprNode, ITable, Root):
533538
values: Sequence[Expr] = None
534539
having_exprs: Sequence[Expr] = None
535540

536-
@property
537-
def source_table(self):
538-
return self
539-
540541
def __post_init__(self):
541542
assert self.keys or self.values
542543

@@ -564,17 +565,13 @@ class TableOp(ExprNode, ITable, Root):
564565
table1: ITable
565566
table2: ITable
566567

567-
@property
568-
def source_table(self):
569-
return self
570-
571568
@property
572569
def type(self):
573570
# TODO ensure types of both tables are compatible
574571
return self.table1.type
575572

576573
@property
577-
def schema(self):
574+
def schema(self) -> Schema:
578575
s1 = self.table1.schema
579576
s2 = self.table2.schema
580577
assert len(s1) == len(s2)
@@ -594,16 +591,12 @@ class Select(ExprNode, ITable, Root):
594591
optimizer_hints: Sequence[Expr] = None
595592

596593
@property
597-
def schema(self):
594+
def schema(self) -> Schema:
598595
s = self.table.schema
599596
if s is None or self.columns is None:
600597
return s
601598
return type(s)({c.name: c.type for c in self.columns})
602599

603-
@property
604-
def source_table(self):
605-
return self
606-
607600
@classmethod
608601
def make(cls, table: ITable, distinct: bool = SKIP, optimizer_hints: str = SKIP, **kwargs):
609602
assert "table" not in kwargs
@@ -642,14 +635,18 @@ def make(cls, table: ITable, distinct: bool = SKIP, optimizer_hints: str = SKIP,
642635

643636
@dataclass
644637
class Cte(ExprNode, ITable):
645-
source_table: Expr
638+
table: Expr
646639
name: str = None
647640
params: Sequence[str] = None
648641

649642
@property
650-
def schema(self):
643+
def source_table(self) -> "ITable":
644+
return self.table
645+
646+
@property
647+
def schema(self) -> Schema:
651648
# TODO add cte to schema
652-
return self.source_table.schema
649+
return self.table.schema
653650

654651

655652
def _named_exprs_as_aliases(named_exprs):
@@ -820,7 +817,3 @@ class Param(ExprNode, ITable):
820817
"""A value placeholder, to be specified at compilation time using the `cv_params` context variable."""
821818

822819
name: str
823-
824-
@property
825-
def source_table(self):
826-
return self

0 commit comments

Comments
 (0)