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 68
Expand file tree
/
Copy pathordering.py
More file actions
394 lines (331 loc) · 13.1 KB
/
ordering.py
File metadata and controls
394 lines (331 loc) · 13.1 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# Copyright 2023 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
from dataclasses import dataclass, field
from enum import Enum
import typing
from typing import Callable, Mapping, Optional, Sequence, Set, Union
import bigframes.core.expression as expression
import bigframes.core.identifiers as ids
class OrderingDirection(Enum):
ASC = 1
DESC = 2
def reverse(self):
if self == OrderingDirection.ASC:
return OrderingDirection.DESC
else:
return OrderingDirection.ASC
@property
def is_ascending(self) -> bool:
return self == OrderingDirection.ASC
@dataclass(frozen=True)
class OrderingExpression:
"""References a column and how to order with respect to values in that column."""
scalar_expression: expression.Expression
direction: OrderingDirection = OrderingDirection.ASC
na_last: bool = True
@property
def referenced_columns(self) -> Set[ids.ColumnId]:
return set(self.scalar_expression.column_references)
@property
def deterministic(self) -> bool:
return self.scalar_expression.deterministic
def remap_column_refs(
self,
mapping: Mapping[ids.ColumnId, ids.ColumnId],
allow_partial_bindings: bool = False,
) -> OrderingExpression:
return self.bind_refs(
{old_id: expression.DerefOp(new_id) for old_id, new_id in mapping.items()},
allow_partial_bindings=allow_partial_bindings,
)
def bind_refs(
self,
mapping: Mapping[ids.ColumnId, expression.Expression],
allow_partial_bindings: bool = False,
) -> OrderingExpression:
return OrderingExpression(
self.scalar_expression.bind_refs(
mapping, allow_partial_bindings=allow_partial_bindings
),
self.direction,
self.na_last,
)
def with_reverse(self) -> OrderingExpression:
return OrderingExpression(
self.scalar_expression, self.direction.reverse(), not self.na_last
)
def transform_exprs(
self, t: Callable[[expression.Expression], expression.Expression]
) -> OrderingExpression:
return OrderingExpression(
t(self.scalar_expression),
self.direction,
self.na_last,
)
# Encoding classes specify additional properties for some ordering representations
@dataclass(frozen=True)
class IntegerEncoding:
"""Integer encoded order ids are guaranteed non-negative."""
is_encoded: bool = False
is_sequential: bool = False
@dataclass(frozen=True)
class RowOrdering:
"""Immutable object that holds information about the ordering of rows in a ArrayValue object. May not be unambiguous."""
ordering_value_columns: typing.Tuple[OrderingExpression, ...] = ()
integer_encoding: IntegerEncoding = IntegerEncoding(False)
@property
def all_ordering_columns(self) -> Sequence[OrderingExpression]:
return list(self.ordering_value_columns)
@property
def referenced_columns(self) -> Set[ids.ColumnId]:
return set(
col
for part in self.ordering_value_columns
for col in part.referenced_columns
)
@property
def is_sequential(self) -> bool:
return self.integer_encoding.is_encoded and self.integer_encoding.is_sequential
@property
def is_total_ordering(self) -> bool:
return False
@property
def total_order_col(self) -> Optional[OrderingExpression]:
"""Returns column id of columns that defines total ordering, if such as column exists"""
return None
def with_reverse(self) -> RowOrdering:
"""Reverses the ordering."""
return RowOrdering(
tuple([col.with_reverse() for col in self.ordering_value_columns]),
)
def remap_column_refs(
self,
mapping: typing.Mapping[ids.ColumnId, ids.ColumnId],
allow_partial_bindings: bool = False,
) -> RowOrdering:
new_value_columns = [
col.remap_column_refs(
mapping, allow_partial_bindings=allow_partial_bindings
)
for col in self.all_ordering_columns
]
return RowOrdering(
tuple(new_value_columns),
)
def with_non_sequential(self):
"""Create a copy that is marked as non-sequential.
This is useful when filtering, but not sorting, an expression.
"""
if self.integer_encoding.is_sequential:
return RowOrdering(
self.ordering_value_columns,
integer_encoding=IntegerEncoding(
self.integer_encoding.is_encoded, is_sequential=False
),
)
return self
def with_ordering_columns(
self,
ordering_value_columns: Sequence[OrderingExpression] = (),
) -> RowOrdering:
"""Creates a new ordering that reorders by the given columns.
Args:
ordering_value_columns:
In decreasing precedence order, the values used to sort the ordering
Returns:
Modified ExpressionOrdering
"""
# Truncate to remove any unneded col references after all total order cols included
new_ordering = self._truncate_ordering(
(*ordering_value_columns, *self.ordering_value_columns)
)
return RowOrdering(
new_ordering,
)
def join(
self,
other: RowOrdering,
) -> RowOrdering:
joined_refs = [*self.all_ordering_columns, *other.all_ordering_columns]
return RowOrdering(tuple(joined_refs))
def _truncate_ordering(
self, order_refs: tuple[OrderingExpression, ...]
) -> tuple[OrderingExpression, ...]:
# Truncate once we refer to a full key in bijective operations
columns_seen: Set[ids.ColumnId] = set()
truncated_refs = []
for order_part in order_refs:
expr = order_part.scalar_expression
if not set(expr.column_references).issubset(columns_seen):
if expr.is_bijective:
columns_seen.update(expr.column_references)
truncated_refs.append(order_part)
return tuple(truncated_refs)
@dataclass(frozen=True)
class TotalOrdering(RowOrdering):
"""Immutable object that holds information about the ordering of rows in a ArrayValue object. Guaranteed to be unambiguous."""
def __post_init__(self):
assert set(ref.id for ref in self.total_ordering_columns).issubset(
self.referenced_columns
)
# A table has a total ordering defined by the identities of a set of 1 or more columns.
# These columns must always be part of the ordering, in order to guarantee that the ordering is total.
# Therefore, any modifications(or drops) done to these columns must result in hidden copies being made.
total_ordering_columns: frozenset[expression.DerefOp] = field(
default_factory=frozenset
)
@classmethod
def from_offset_col(cls, col: Union[ids.ColumnId, str]) -> TotalOrdering:
col_id = ids.ColumnId(col) if isinstance(col, str) else col
return TotalOrdering(
(ascending_over(col),),
integer_encoding=IntegerEncoding(True, is_sequential=True),
total_ordering_columns=frozenset({expression.DerefOp(col_id)}),
)
@classmethod
def from_primary_key(cls, primary_key: Sequence[ids.ColumnId]) -> TotalOrdering:
return TotalOrdering(
tuple(ascending_over(col) for col in primary_key),
total_ordering_columns=frozenset(
{expression.DerefOp(col) for col in primary_key}
),
)
@property
def is_total_ordering(self) -> bool:
return True
def with_non_sequential(self):
"""Create a copy that is marked as non-sequential.
This is useful when filtering, but not sorting, an expression.
"""
if self.integer_encoding.is_sequential:
return TotalOrdering(
self.ordering_value_columns,
integer_encoding=IntegerEncoding(
self.integer_encoding.is_encoded, is_sequential=False
),
total_ordering_columns=self.total_ordering_columns,
)
return self
def with_ordering_columns(
self,
ordering_value_columns: Sequence[OrderingExpression] = (),
) -> TotalOrdering:
"""Creates a new ordering that reorders by the given columns.
Args:
ordering_value_columns:
In decreasing precedence order, the values used to sort the ordering
Returns:
Modified ExpressionOrdering
"""
# Truncate to remove any unneded col references after all total order cols included
new_ordering = self._truncate_ordering(
(*ordering_value_columns, *self.ordering_value_columns)
)
return TotalOrdering(
new_ordering,
total_ordering_columns=self.total_ordering_columns,
)
def _truncate_ordering(
self, order_refs: tuple[OrderingExpression, ...]
) -> tuple[OrderingExpression, ...]:
# Truncate once we refer to a full key in bijective operations
must_see = set(ref.id for ref in self.total_ordering_columns)
columns_seen: Set[ids.ColumnId] = set()
truncated_refs = []
for order_part in order_refs:
expr = order_part.scalar_expression
if not set(expr.column_references).issubset(columns_seen):
if expr.is_bijective:
columns_seen.update(expr.column_references)
truncated_refs.append(order_part)
if columns_seen.issuperset(must_see):
return tuple(truncated_refs)
if len(must_see) == 0:
return ()
raise ValueError("Ordering did not contain all total_order_cols")
def with_reverse(self):
"""Reverses the ordering."""
return TotalOrdering(
tuple([col.with_reverse() for col in self.ordering_value_columns]),
total_ordering_columns=self.total_ordering_columns,
)
def remap_column_refs(
self,
mapping: typing.Mapping[ids.ColumnId, ids.ColumnId],
allow_partial_bindings: bool = False,
):
new_value_columns = [
col.remap_column_refs(
mapping, allow_partial_bindings=allow_partial_bindings
)
for col in self.all_ordering_columns
]
new_total_order = frozenset(
expression.DerefOp(mapping.get(col_id.id, col_id.id))
for col_id in self.total_ordering_columns
)
return TotalOrdering(
tuple(new_value_columns),
integer_encoding=self.integer_encoding,
total_ordering_columns=new_total_order,
)
@typing.overload
def join(
self,
other: TotalOrdering,
) -> TotalOrdering:
...
@typing.overload
def join(
self,
other: RowOrdering,
) -> RowOrdering:
...
def join(
self,
other: RowOrdering,
) -> RowOrdering:
joined_refs = [*self.all_ordering_columns, *other.all_ordering_columns]
if isinstance(other, TotalOrdering):
left_total_order_cols = frozenset(self.total_ordering_columns)
right_total_order_cols = frozenset(other.total_ordering_columns)
return TotalOrdering(
ordering_value_columns=tuple(joined_refs),
total_ordering_columns=left_total_order_cols | right_total_order_cols,
)
else:
return RowOrdering(tuple(joined_refs))
@property
def total_order_col(self) -> Optional[OrderingExpression]:
"""Returns column id of columns that defines total ordering, if such as column exists"""
if len(self.ordering_value_columns) != 1:
return None
order_ref = self.ordering_value_columns[0]
if order_ref.direction != OrderingDirection.ASC:
return None
return order_ref
# Convenience functions
def ascending_over(
id: Union[ids.ColumnId, str], nulls_last: bool = True
) -> OrderingExpression:
col_id = ids.ColumnId(id) if isinstance(id, str) else id
return OrderingExpression(expression.DerefOp(col_id), na_last=nulls_last)
def descending_over(
id: Union[ids.ColumnId, str], nulls_last: bool = True
) -> OrderingExpression:
col_id = ids.ColumnId(id) if isinstance(id, str) else id
return OrderingExpression(
expression.DerefOp(col_id), direction=OrderingDirection.DESC, na_last=nulls_last
)