|
16 | 16 |
|
17 | 17 | import dataclasses |
18 | 18 | import functools |
19 | | -from typing import Mapping, Optional, Sequence, Tuple |
| 19 | +from typing import Callable, Mapping, Optional, Sequence, Tuple |
20 | 20 |
|
21 | 21 | from bigframes.core import bq_data, identifiers, nodes |
22 | 22 | import bigframes.core.expression as ex |
23 | 23 | from bigframes.core.ordering import OrderingExpression |
24 | 24 | import bigframes.dtypes |
25 | 25 |
|
| 26 | +# SQL Nodes are generally terminal, so don't support rich transformation methods |
| 27 | +# like remap_vars, remap_refs, etc. |
| 28 | +# Still, fields should be defined on them, as typing info is still used for |
| 29 | +# dispatching some operators in the emitter, and for validation. |
| 30 | + |
26 | 31 |
|
27 | 32 | # TODO: Join node, union node |
28 | 33 | @dataclasses.dataclass(frozen=True) |
@@ -84,6 +89,127 @@ def remap_refs( |
84 | 89 | raise NotImplementedError() # type: ignore |
85 | 90 |
|
86 | 91 |
|
| 92 | +@dataclasses.dataclass(frozen=True) |
| 93 | +class SqlWithCtesNode(nodes.BigFrameNode): |
| 94 | + # def, name pairs |
| 95 | + child: nodes.BigFrameNode |
| 96 | + cte_names: tuple[str, ...] |
| 97 | + cte_defs: tuple[nodes.BigFrameNode, ...] |
| 98 | + |
| 99 | + @property |
| 100 | + def child_nodes(self) -> Sequence[nodes.BigFrameNode]: |
| 101 | + return (self.child, *self.cte_defs) |
| 102 | + |
| 103 | + @property |
| 104 | + def fields(self) -> Sequence[nodes.Field]: |
| 105 | + return self.child.fields |
| 106 | + |
| 107 | + @property |
| 108 | + def variables_introduced(self) -> int: |
| 109 | + # This operation only renames variables, doesn't actually create new ones |
| 110 | + return 0 |
| 111 | + |
| 112 | + @property |
| 113 | + def defines_namespace(self) -> bool: |
| 114 | + return True |
| 115 | + |
| 116 | + @property |
| 117 | + def explicitly_ordered(self) -> bool: |
| 118 | + return False |
| 119 | + |
| 120 | + @property |
| 121 | + def order_ambiguous(self) -> bool: |
| 122 | + return True |
| 123 | + |
| 124 | + @property |
| 125 | + def row_count(self) -> Optional[int]: |
| 126 | + return self.child.row_count |
| 127 | + |
| 128 | + @property |
| 129 | + def node_defined_ids(self) -> Tuple[identifiers.ColumnId, ...]: |
| 130 | + return tuple(self.ids) |
| 131 | + |
| 132 | + @property |
| 133 | + def consumed_ids(self): |
| 134 | + return () |
| 135 | + |
| 136 | + @property |
| 137 | + def _node_expressions(self): |
| 138 | + return () |
| 139 | + |
| 140 | + def remap_vars( |
| 141 | + self, mappings: Mapping[identifiers.ColumnId, identifiers.ColumnId] |
| 142 | + ) -> SqlWithCtesNode: |
| 143 | + raise NotImplementedError() |
| 144 | + |
| 145 | + def remap_refs( |
| 146 | + self, mappings: Mapping[identifiers.ColumnId, identifiers.ColumnId] |
| 147 | + ) -> SqlWithCtesNode: |
| 148 | + raise NotImplementedError() # type: ignore |
| 149 | + |
| 150 | + def transform_children( |
| 151 | + self, transform: Callable[[nodes.BigFrameNode], nodes.BigFrameNode] |
| 152 | + ) -> SqlWithCtesNode: |
| 153 | + return SqlWithCtesNode( |
| 154 | + transform(self.child), |
| 155 | + self.cte_names, |
| 156 | + tuple(transform(cte) for cte in self.cte_defs), |
| 157 | + ) |
| 158 | + |
| 159 | + |
| 160 | +@dataclasses.dataclass(frozen=True) |
| 161 | +class SqlCteRefNode(nodes.LeafNode): |
| 162 | + cte_name: str |
| 163 | + cte_schema: tuple[nodes.Field, ...] |
| 164 | + |
| 165 | + @property |
| 166 | + def fields(self) -> Sequence[nodes.Field]: |
| 167 | + return self.cte_schema |
| 168 | + |
| 169 | + @property |
| 170 | + def variables_introduced(self) -> int: |
| 171 | + # This operation only renames variables, doesn't actually create new ones |
| 172 | + return 0 |
| 173 | + |
| 174 | + @property |
| 175 | + def defines_namespace(self) -> bool: |
| 176 | + return True |
| 177 | + |
| 178 | + @property |
| 179 | + def explicitly_ordered(self) -> bool: |
| 180 | + return False |
| 181 | + |
| 182 | + @property |
| 183 | + def order_ambiguous(self) -> bool: |
| 184 | + return True |
| 185 | + |
| 186 | + @property |
| 187 | + def row_count(self) -> Optional[int]: |
| 188 | + raise NotImplementedError() |
| 189 | + |
| 190 | + @property |
| 191 | + def node_defined_ids(self) -> Tuple[identifiers.ColumnId, ...]: |
| 192 | + return tuple(self.ids) |
| 193 | + |
| 194 | + @property |
| 195 | + def consumed_ids(self): |
| 196 | + return () |
| 197 | + |
| 198 | + @property |
| 199 | + def _node_expressions(self): |
| 200 | + return () |
| 201 | + |
| 202 | + def remap_vars( |
| 203 | + self, mappings: Mapping[identifiers.ColumnId, identifiers.ColumnId] |
| 204 | + ) -> SqlCteRefNode: |
| 205 | + raise NotImplementedError() |
| 206 | + |
| 207 | + def remap_refs( |
| 208 | + self, mappings: Mapping[identifiers.ColumnId, identifiers.ColumnId] |
| 209 | + ) -> SqlCteRefNode: |
| 210 | + raise NotImplementedError() # type: ignore |
| 211 | + |
| 212 | + |
87 | 213 | @dataclasses.dataclass(frozen=True) |
88 | 214 | class SqlSelectNode(nodes.UnaryNode): |
89 | 215 | selections: tuple[nodes.ColumnDef, ...] = () |
|
0 commit comments