|
| 1 | +from pydough.qdag import PyDoughCollectionQDAG |
| 2 | +from pydough.qdag.abstract_pydough_qdag import PyDoughQDAG |
| 3 | +from pydough.qdag.errors import PyDoughQDAGException |
| 4 | +from pydough.qdag.expressions.collation_expression import CollationExpression |
| 5 | +from pydough.qdag.expressions.reference import Reference |
| 6 | + |
| 7 | +from .child_access import ChildAccess |
| 8 | +from .collection_tree_form import CollectionTreeForm |
| 9 | +from .user_collections import PyDoughUserGeneratedCollection |
| 10 | + |
| 11 | + |
| 12 | +# or should it be CollectionAccess? |
| 13 | +class PyDoughUserGeneratedCollectionQDag(ChildAccess): |
| 14 | + def __init__( |
| 15 | + self, |
| 16 | + ancestor: PyDoughCollectionQDAG, |
| 17 | + collection: PyDoughUserGeneratedCollection, |
| 18 | + ): |
| 19 | + assert ancestor is not None |
| 20 | + super().__init__(ancestor) |
| 21 | + self._collection = collection |
| 22 | + |
| 23 | + def clone_with_parent( |
| 24 | + self, new_ancestor: PyDoughCollectionQDAG |
| 25 | + ) -> "PyDoughUserGeneratedCollectionQDag": |
| 26 | + """ |
| 27 | + Copies `self` but with a new ancestor node that presumably has the |
| 28 | + original ancestor in its predecessor chain. |
| 29 | +
|
| 30 | + Args: |
| 31 | + `new_ancestor`: the node to use as the new parent of the clone. |
| 32 | +
|
| 33 | + Returns: |
| 34 | + The cloned version of `self`. |
| 35 | + """ |
| 36 | + return PyDoughUserGeneratedCollectionQDag(new_ancestor, self._collection) |
| 37 | + |
| 38 | + def to_tree_form(self, is_last: bool) -> CollectionTreeForm: |
| 39 | + raise NotImplementedError |
| 40 | + |
| 41 | + @property |
| 42 | + def collection(self) -> PyDoughUserGeneratedCollection: |
| 43 | + """ |
| 44 | + The metadata for the table that is being referenced by the collection |
| 45 | + node. |
| 46 | + """ |
| 47 | + return self._collection |
| 48 | + |
| 49 | + @property |
| 50 | + def name(self) -> str: |
| 51 | + return self.collection.name |
| 52 | + |
| 53 | + @property |
| 54 | + def preceding_context(self) -> PyDoughCollectionQDAG | None: |
| 55 | + return None |
| 56 | + |
| 57 | + @property |
| 58 | + def ordering(self) -> list[CollationExpression] | None: |
| 59 | + return None |
| 60 | + |
| 61 | + @property |
| 62 | + def calc_terms(self) -> set[str]: |
| 63 | + return set(self.collection.columns) |
| 64 | + |
| 65 | + @property |
| 66 | + def all_terms(self) -> set[str]: |
| 67 | + """ |
| 68 | + The set of expression/subcollection names accessible by the context. |
| 69 | + """ |
| 70 | + return self.calc_terms |
| 71 | + |
| 72 | + @property |
| 73 | + def ancestral_mapping(self) -> dict[str, int]: |
| 74 | + return self._ancestor.ancestral_mapping |
| 75 | + |
| 76 | + @property |
| 77 | + def inherited_downstreamed_terms(self) -> set[str]: |
| 78 | + if self._ancestor: |
| 79 | + return self._ancestor.inherited_downstreamed_terms |
| 80 | + else: |
| 81 | + return set() |
| 82 | + |
| 83 | + def is_singular(self, context: "PyDoughCollectionQDAG") -> bool: |
| 84 | + return False |
| 85 | + |
| 86 | + def get_term(self, term_name: str) -> PyDoughQDAG: |
| 87 | + if term_name not in self.collection.columns: |
| 88 | + raise PyDoughQDAGException(self.name_mismatch_error(term_name)) |
| 89 | + |
| 90 | + return Reference(self._ancestor, term_name) |
| 91 | + |
| 92 | + def get_expression_position(self, expr_name: str) -> int: |
| 93 | + raise PyDoughQDAGException(f"Cannot call get_expression_position on {self!r}") |
| 94 | + |
| 95 | + @property |
| 96 | + def unique_terms(self) -> list[str]: |
| 97 | + return self.collection.columns |
| 98 | + |
| 99 | + @property |
| 100 | + def standalone_string(self) -> str: |
| 101 | + """ |
| 102 | + Returns a string representation of the collection in a standalone form. |
| 103 | + This is used for debugging and logging purposes. |
| 104 | + """ |
| 105 | + return f"UserGeneratedCollection({self.name}, {', '.join(self.collection.columns)})" |
| 106 | + |
| 107 | + @property |
| 108 | + def key(self) -> str: |
| 109 | + return f"USER_GENERATED_COLLECTION-{self.name}" |
| 110 | + |
| 111 | + def to_string(self) -> str: |
| 112 | + # Stringify as "name(column_name) |
| 113 | + return f"{self.name}({', '.join(self.collection.columns)})" |
| 114 | + |
| 115 | + @property |
| 116 | + def tree_item_string(self) -> str: |
| 117 | + return f"UserGeneratedCollection({self.name})" |
0 commit comments