Skip to content

Commit c192a6e

Browse files
committed
add collection back
1 parent 499c000 commit c192a6e

12 files changed

+48
-47
lines changed

pydough/conversion/hybrid_translator.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,26 +1118,22 @@ def make_hybrid_expr(
11181118
return HybridBackRefExpr(expr_name, expr.back_levels, expr.pydough_type)
11191119
case Reference():
11201120
if hybrid.ancestral_mapping.get(expr.term_name, 0) > 0:
1121-
# HA: FIXME: raise error for now.
1122-
raise NotImplementedError(
1123-
"Unable to resolve collection context for Reference. "
1121+
collection = expr.collection
1122+
while (
1123+
isinstance(collection, PartitionChild)
1124+
and expr.term_name in collection.child_access.ancestral_mapping
1125+
):
1126+
collection = collection.child_access
1127+
return self.make_hybrid_expr(
1128+
hybrid,
1129+
BackReferenceExpression(
1130+
collection,
1131+
expr.term_name,
1132+
hybrid.ancestral_mapping[expr.term_name],
1133+
),
1134+
child_ref_mapping,
1135+
inside_agg,
11241136
)
1125-
# collection = expr.collection
1126-
# while (
1127-
# isinstance(collection, PartitionChild)
1128-
# and expr.term_name in collection.child_access.ancestral_mapping
1129-
# ):
1130-
# collection = collection.child_access
1131-
# return self.make_hybrid_expr(
1132-
# hybrid,
1133-
# BackReferenceExpression(
1134-
# collection,
1135-
# expr.term_name,
1136-
# hybrid.ancestral_mapping[expr.term_name],
1137-
# ),
1138-
# child_ref_mapping,
1139-
# inside_agg,
1140-
# )
11411137
expr_name = hybrid.pipeline[-1].renamings.get(
11421138
expr.term_name, expr.term_name
11431139
)

pydough/conversion/relational_converter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,12 +1320,12 @@ def preprocess_root(
13201320
if output_cols is None:
13211321
for name in node.calc_terms:
13221322
name_typ: PyDoughType = node.get_expr(name).pydough_type
1323-
final_terms.append((name, Reference(name, name_typ)))
1323+
final_terms.append((name, Reference(node, name, name_typ)))
13241324
final_terms.sort(key=lambda term: node.get_expression_position(term[0]))
13251325
else:
13261326
for _, column in output_cols:
13271327
column_typ: PyDoughType = node.get_expr(column).pydough_type
1328-
final_terms.append((column, Reference(column, column_typ)))
1328+
final_terms.append((column, Reference(node, column, column_typ)))
13291329
children: list[PyDoughCollectionQDAG] = []
13301330
final_calc: Calculate = Calculate(node, children).with_terms(final_terms)
13311331
return final_calc

pydough/qdag/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ table_collection = builder.build_child_access("Nations", global_context_node)
6767
# Equivalent PyDough code: `TPCH.Nations.name`
6868
ref_name = "name"
6969
pydough_type = table_collection.get_expr(ref_name).pydough_type
70-
reference_node = builder.build_reference(ref_name, pydough_type)
70+
reference_node = builder.build_reference(table_collection, ref_name, pydough_type)
7171

7272
# Build an expression function call node
7373
# Equivalent PyDough code: `LOWER(TPCH.Nations.name)`
@@ -104,7 +104,7 @@ nations_sub_collection = builder.build_child_access("nations", regions_collectio
104104

105105
ref_name = "key"
106106
pydough_type = nations_sub_collection.get_expr(ref_name).pydough_type
107-
key_ref = builder.build_reference(ref_name, pydough_type)
107+
key_ref = builder.build_reference(nations_sub_collection, ref_name, pydough_type)
108108
literal_4 = builder.build_literal(4, NumericType())
109109
condition = builder.build_expression_function_call("EQU", [key_ref, literal_4])
110110
# Build WHERE node with condition
@@ -115,7 +115,7 @@ singular_node = builder.build_singular(where_node)
115115
# Build reference node for name
116116
ref_name = "name"
117117
pydough_type = singular_node.get_expr(ref_name).pydough_type
118-
reference_node = builder.build_reference(ref_name, pydough_type)
118+
reference_node = builder.build_reference(singular_node, ref_name, pydough_type)
119119
# Build CALCULATE node with calculated term
120120
calculate_node = builder.build_calc(regions_collection, [nations_sub_collection])
121121
calculate_node = calculate_node.with_terms([("n_4_nation", reference_node)])
@@ -139,7 +139,7 @@ top_k_node = top_k_node.with_collation([collation_expression])
139139
part_collection = builder.build_child_access("Parts", global_context_node)
140140
ref_name = "part_type"
141141
pydough_type = part_collection.get_expr(ref_name).pydough_type
142-
partition_key = builder.build_reference(ref_name, pydough_type)
142+
partition_key = builder.build_reference(part_collection, ref_name, pydough_type)
143143
partition_by_node = builder.build_partition(part_collection, child_collection, "p")
144144
partition_by_node = partition_by_node.with_keys([partition_key])
145145

pydough/qdag/collections/augmenting_child_operator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def get_term(self, term_name: str) -> PyDoughQDAG:
8585
term = term.clone_with_parent(self)
8686
elif isinstance(term, PyDoughExpressionQDAG):
8787
typ = self.preceding_context.get_expr(term_name).pydough_type
88-
term = Reference(term_name, typ)
88+
term = Reference(self.preceding_context, term_name, typ)
8989
return term
9090

9191
@cache

pydough/qdag/collections/collection_access.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ def get_term(self, term_name: str) -> PyDoughQDAG:
129129
else:
130130
assert context.ancestor_context is not None
131131
context = context.ancestor_context
132-
return Reference(term_name, context.get_expr(term_name).pydough_type)
132+
return Reference(
133+
context, term_name, context.get_expr(term_name).pydough_type
134+
)
133135

134136
if term_name not in self.all_terms:
135137
raise PyDoughQDAGException(self.name_mismatch_error(term_name))

pydough/qdag/collections/partition_child.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ def get_term(self, term_name: str):
102102
else:
103103
assert context.ancestor_context is not None
104104
context = context.ancestor_context
105-
return Reference(term_name, context.get_expr(term_name).pydough_type)
105+
return Reference(
106+
context, term_name, context.get_expr(term_name).pydough_type
107+
)
106108

107109
elif term_name not in self.all_terms:
108110
raise PyDoughQDAGException(self.name_mismatch_error(term_name))

pydough/qdag/expressions/back_reference_expression.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,6 @@ def __init__(
4040
self._expression = self._ancestor.get_expr(term_name)
4141
self._term_type = self._expression.pydough_type
4242

43-
@property
44-
def collection(self) -> PyDoughCollectionQDAG:
45-
"""
46-
The collection that the Reference term comes from.
47-
"""
48-
return self._collection
49-
5043
@property
5144
def expression(self) -> PyDoughExpressionQDAG:
5245
"""

pydough/qdag/expressions/child_reference_expression.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,6 @@ def __init__(
3535
f"Cannot reference plural expression {self.expression} from {self.collection}"
3636
)
3737

38-
@property
39-
def collection(self) -> PyDoughCollectionQDAG:
40-
"""
41-
The collection that the Reference term comes from.
42-
"""
43-
return self._collection
44-
4538
@property
4639
def expression(self) -> PyDoughExpressionQDAG:
4740
"""

pydough/qdag/expressions/reference.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88

99
from pydough.qdag.abstract_pydough_qdag import PyDoughQDAG
10+
from pydough.qdag.collections.collection_qdag import PyDoughCollectionQDAG
1011
from pydough.types import PyDoughType
1112

1213
from .expression_qdag import PyDoughExpressionQDAG
@@ -18,10 +19,20 @@ class Reference(PyDoughExpressionQDAG):
1819
a preceding collection.
1920
"""
2021

21-
def __init__(self, term_name: str, term_type: PyDoughType):
22+
def __init__(
23+
self, collection: PyDoughCollectionQDAG, term_name: str, term_type: PyDoughType
24+
):
25+
self._collection: PyDoughCollectionQDAG = collection
2226
self._term_name: str = term_name
2327
self._term_type: PyDoughType = term_type
2428

29+
@property
30+
def collection(self) -> PyDoughCollectionQDAG:
31+
"""
32+
The collection that the Reference term comes from.
33+
"""
34+
return self._collection
35+
2536
@property
2637
def term_name(self) -> str:
2738
"""
@@ -51,5 +62,6 @@ def equals(self, other: object) -> bool:
5162
return (
5263
isinstance(other, Reference)
5364
and self.term_name == other.term_name
65+
and self.collection.equals(other.collection)
5466
and self.pydough_type == other.pydough_type
5567
)

pydough/qdag/node_builder.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,14 @@ def build_window_call(
155155
window_operator, qualified_args, collation_args, levels, kwargs
156156
)
157157

158-
def build_reference(self, name: str, typ: PyDoughType) -> Reference:
158+
def build_reference(
159+
self, collection: PyDoughCollectionQDAG, name: str, typ: PyDoughType
160+
) -> Reference:
159161
"""
160162
Creates a new reference to an expression in the collection.
161163
162164
Args:
165+
`collection`: the collection that the reference comes from.
163166
`name`: the name of the expression being referenced.
164167
`typ`: the PyDough type of the expression being referenced.
165168
@@ -170,7 +173,7 @@ def build_reference(self, name: str, typ: PyDoughType) -> Reference:
170173
`PyDoughQDAGException`: if `name` does not refer to an expression in
171174
the collection.
172175
"""
173-
return Reference(name, typ)
176+
return Reference(collection, name, typ)
174177

175178
def build_child_reference_expression(
176179
self,

0 commit comments

Comments
 (0)