This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathsql_nodes.py
More file actions
161 lines (129 loc) · 4.8 KB
/
sql_nodes.py
File metadata and controls
161 lines (129 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import dataclasses
import functools
from typing import Mapping, Optional, Sequence, Tuple
from bigframes.core import bq_data, identifiers, nodes
import bigframes.core.expression as ex
from bigframes.core.ordering import OrderingExpression
import bigframes.dtypes
# TODO: Join node, union node
@dataclasses.dataclass(frozen=True)
class SqlDataSource(nodes.LeafNode):
source: bq_data.BigqueryDataSource
@functools.cached_property
def fields(self) -> Sequence[nodes.Field]:
return tuple(
nodes.Field(
identifiers.ColumnId(source_id),
self.source.schema.get_type(source_id),
self.source.table.schema_by_id[source_id].is_nullable,
)
for source_id in self.source.schema.names
)
@property
def variables_introduced(self) -> int:
# This operation only renames variables, doesn't actually create new ones
return 0
@property
def defines_namespace(self) -> bool:
return True
@property
def explicitly_ordered(self) -> bool:
return False
@property
def order_ambiguous(self) -> bool:
return True
@property
def row_count(self) -> Optional[int]:
return self.source.n_rows
@property
def node_defined_ids(self) -> Tuple[identifiers.ColumnId, ...]:
return tuple(self.ids)
@property
def consumed_ids(self):
return ()
@property
def _node_expressions(self):
return ()
def remap_vars(
self, mappings: Mapping[identifiers.ColumnId, identifiers.ColumnId]
) -> SqlSelectNode:
raise NotImplementedError()
def remap_refs(
self, mappings: Mapping[identifiers.ColumnId, identifiers.ColumnId]
) -> SqlSelectNode:
raise NotImplementedError() # type: ignore
@dataclasses.dataclass(frozen=True)
class SqlSelectNode(nodes.UnaryNode):
selections: tuple[nodes.ColumnDef, ...] = ()
predicates: tuple[ex.Expression, ...] = ()
sorting: tuple[OrderingExpression, ...] = ()
limit: Optional[int] = None
@functools.cached_property
def fields(self) -> Sequence[nodes.Field]:
fields = []
for cdef in self.selections:
bound_expr = ex.bind_schema_fields(cdef.expression, self.child.field_by_id)
field = nodes.Field(
cdef.id,
bigframes.dtypes.dtype_for_etype(bound_expr.output_type),
nullable=bound_expr.nullable,
)
# Special case until we get better nullability inference in expression objects themselves
if bound_expr.is_identity and not any(
self.child.field_by_id[id].nullable
for id in cdef.expression.column_references
):
field = field.with_nonnull()
fields.append(field)
return tuple(fields)
@property
def variables_introduced(self) -> int:
# This operation only renames variables, doesn't actually create new ones
return 0
@property
def defines_namespace(self) -> bool:
return True
@property
def row_count(self) -> Optional[int]:
if self.child.row_count is not None:
if self.limit is not None:
return min([self.limit, self.child.row_count])
return self.child.row_count
return None
@property
def node_defined_ids(self) -> Tuple[identifiers.ColumnId, ...]:
return tuple(cdef.id for cdef in self.selections)
@property
def consumed_ids(self):
raise NotImplementedError()
@property
def _node_expressions(self):
raise NotImplementedError()
@property
def is_star_selection(self) -> bool:
return tuple(self.ids) == tuple(self.child.ids)
@functools.cache
def get_id_mapping(self) -> dict[identifiers.ColumnId, ex.Expression]:
return {cdef.id: cdef.expression for cdef in self.selections}
def remap_vars(
self, mappings: Mapping[identifiers.ColumnId, identifiers.ColumnId]
) -> SqlSelectNode:
raise NotImplementedError()
def remap_refs(
self, mappings: Mapping[identifiers.ColumnId, identifiers.ColumnId]
) -> SqlSelectNode:
raise NotImplementedError() # type: ignore