-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3a3255a
commit f4d5031
Showing
9 changed files
with
396 additions
and
0 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
pydough/pydough_ast/collections/back_reference_collection.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
""" | ||
TODO: add file-level docstring | ||
""" | ||
|
||
__all__ = ["BackReferenceCollection"] | ||
|
||
|
||
from pydough.pydough_ast.abstract_pydough_ast import PyDoughAST | ||
from .collection_ast import PyDoughCollectionAST | ||
from .table_collection import TableCollection | ||
from pydough.pydough_ast.errors import PyDoughASTException | ||
|
||
|
||
class BackReferenceCollection(TableCollection): | ||
""" | ||
The AST node implementation class representing a subcollection of an | ||
ancestor collection. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
parent: PyDoughCollectionAST, | ||
term_name: str, | ||
back_levels: int, | ||
): | ||
self._parent: PyDoughAST = parent | ||
self._back_levels: int = back_levels | ||
self._ancestor: PyDoughCollectionAST = parent | ||
for _ in range(back_levels): | ||
self._ancestor = self._ancestor.ancestor_context | ||
if self._ancestor is None: | ||
raise PyDoughASTException( | ||
f"Cannot reference back {back_levels} levels above {parent!r}" | ||
) | ||
super.__init__(self._ancestor.get_term(term_name)) | ||
|
||
@property | ||
def parent(self) -> PyDoughCollectionAST: | ||
""" | ||
The parent node that the collection node is a subcollection of. | ||
""" | ||
return self._parent | ||
|
||
@property | ||
def back_levels(self) -> int: | ||
""" | ||
The number of levels upward that the backreference refers to. | ||
""" | ||
return self._back_levels | ||
|
||
@property | ||
def ancestor(self) -> PyDoughCollectionAST: | ||
""" | ||
The specific ancestor collection that the ancestor refers to. | ||
""" | ||
return self._ancestor | ||
|
||
@property | ||
def ancestor_context(self) -> PyDoughCollectionAST | None: | ||
return self.parent | ||
|
||
def to_string(self) -> str: | ||
return f"BackReference[{self.back_levels}.{self.collection.to_string()}" | ||
|
||
def to_tree_string(self) -> str: | ||
raise NotImplementedError | ||
|
||
def equals(self, other: "BackReferenceCollection") -> bool: | ||
return super().equals(other) and self.ancestor == other.ancestor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
pydough/pydough_ast/collections/hidden_back_reference_collection.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
""" | ||
TODO: add file-level docstring | ||
""" | ||
|
||
__all__ = ["HiddenBackReferenceCollection"] | ||
|
||
|
||
from .collection_ast import PyDoughCollectionAST | ||
from .back_reference_collection import BackReferenceCollection | ||
|
||
|
||
class HiddenBackReferenceCollection(BackReferenceCollection): | ||
""" | ||
The AST node implementation class representing a subcollection of an | ||
ancestor collection. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
collection: PyDoughCollectionAST, | ||
ancestor: PyDoughCollectionAST, | ||
term_name: str, | ||
back_levels: int, | ||
): | ||
self._collection: PyDoughCollectionAST = collection | ||
self._term_name: str = term_name | ||
self._back_levels: int = back_levels | ||
self._ancestor: PyDoughCollectionAST = ancestor | ||
super(BackReferenceCollection, self).__init__( | ||
self._ancestor.get_term(term_name) | ||
) | ||
|
||
def to_string(self) -> str: | ||
return f"HiddenBackReferenceCollection[{self.back_levels}.{self.collection.to_string()}" | ||
|
||
def to_tree_string(self) -> str: | ||
raise NotImplementedError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
pydough/pydough_ast/expressions/back_reference_expression.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
""" | ||
TODO: add file-level docstring | ||
""" | ||
|
||
__all__ = ["BackReferenceExpression"] | ||
|
||
from . import PyDoughExpressionAST | ||
from pydough.types import PyDoughType | ||
from pydough.pydough_ast.collections import PyDoughCollectionAST | ||
from pydough.pydough_ast.errors import PyDoughASTException | ||
from .reference import Reference | ||
|
||
|
||
class BackReferenceExpression(Reference): | ||
""" | ||
The AST node implementation class representing a reference to a term in | ||
the ancestor context. | ||
""" | ||
|
||
def __init__( | ||
self, collection: PyDoughCollectionAST, term_name: str, back_levels: int | ||
): | ||
self._collection: PyDoughCollectionAST = collection | ||
self._term_name: str = term_name | ||
self._back_levels: int = back_levels | ||
self._ancestor: PyDoughCollectionAST = collection | ||
for _ in range(back_levels): | ||
self._ancestor = self._ancestor.ancestor_context | ||
if self._ancestor is None: | ||
raise PyDoughASTException( | ||
f"Cannot reference back {back_levels} levels above {collection!r}" | ||
) | ||
self._expression: PyDoughExpressionAST = self._ancestor.get_term(term_name) | ||
|
||
@property | ||
def back_levels(self) -> int: | ||
""" | ||
The number of levels upward that the backreference refers to. | ||
""" | ||
return self._back_levels | ||
|
||
@property | ||
def ancestor(self) -> PyDoughCollectionAST: | ||
""" | ||
The specific ancestor collection that the ancestor refers to. | ||
""" | ||
return self._ancestor | ||
|
||
@property | ||
def expression(self) -> PyDoughExpressionAST: | ||
""" | ||
The original expression that the reference refers to. | ||
""" | ||
return self._expression | ||
|
||
@property | ||
def pydough_type(self) -> PyDoughType: | ||
return self.expression.pydough_type | ||
|
||
@property | ||
def is_aggregation(self) -> bool: | ||
return self.expression.is_aggregation | ||
|
||
def requires_enclosing_parens(self, parent: PyDoughExpressionAST) -> bool: | ||
return False | ||
|
||
def to_string(self) -> str: | ||
return f"BackReferenceExpression[{self.back_levels}:{self.term_name}]" | ||
|
||
def equals(self, other: "BackReferenceExpression") -> bool: | ||
return super().equals(other) and self.ancestor.equals(other.ancestor) |
33 changes: 33 additions & 0 deletions
33
pydough/pydough_ast/expressions/hidden_back_reference_expression.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
""" | ||
TODO: add file-level docstring | ||
""" | ||
|
||
__all__ = ["HiddenBackReferenceExpression"] | ||
|
||
from . import PyDoughExpressionAST | ||
from pydough.pydough_ast.collections import PyDoughCollectionAST | ||
from .back_reference_expression import BackReferenceExpression | ||
|
||
|
||
class HiddenBackReferenceExpression(BackReferenceExpression): | ||
""" | ||
The AST node implementation class representing a reference to a term in | ||
the ancestor context through the lens of a compound relationship's | ||
inherited properties. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
collection: PyDoughCollectionAST, | ||
ancestor: PyDoughCollectionAST, | ||
term_name: str, | ||
back_levels: int, | ||
): | ||
self._collection: PyDoughCollectionAST = collection | ||
self._term_name: str = term_name | ||
self._back_levels: int = back_levels | ||
self._ancestor: PyDoughCollectionAST = ancestor | ||
self._expression: PyDoughExpressionAST = self._ancestor.get_term(term_name) | ||
|
||
def to_string(self) -> str: | ||
return f"HiddenBackReferenceExpression[{self.back_levels}:{self.term_name}]" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.