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 pathagg_expressions.py
More file actions
151 lines (120 loc) · 4.34 KB
/
agg_expressions.py
File metadata and controls
151 lines (120 loc) · 4.34 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
# 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
import abc
import dataclasses
import functools
import itertools
import typing
from typing import Callable, Mapping, TypeVar
from bigframes import dtypes
from bigframes.core import expression
import bigframes.core.identifiers as ids
import bigframes.operations.aggregations as agg_ops
TExpression = TypeVar("TExpression", bound="Aggregation")
@dataclasses.dataclass(frozen=True)
class Aggregation(expression.Expression):
"""Represents windowing or aggregation over a column."""
op: agg_ops.WindowOp = dataclasses.field()
@property
def column_references(self) -> typing.Tuple[ids.ColumnId, ...]:
return tuple(
itertools.chain.from_iterable(
map(lambda x: x.column_references, self.inputs)
)
)
@functools.cached_property
def is_resolved(self) -> bool:
return all(input.is_resolved for input in self.inputs)
@functools.cached_property
def output_type(self) -> dtypes.ExpressionType:
if not self.is_resolved:
raise ValueError(f"Type of expression {self.op} has not been fixed.")
input_types = [input.output_type for input in self.inputs]
return self.op.output_type(*input_types)
@property
@abc.abstractmethod
def inputs(
self,
) -> typing.Tuple[expression.Expression, ...]:
...
@property
def free_variables(self) -> typing.Tuple[str, ...]:
return tuple(
itertools.chain.from_iterable(map(lambda x: x.free_variables, self.inputs))
)
@property
def is_const(self) -> bool:
return all(child.is_const for child in self.inputs)
@abc.abstractmethod
def replace_args(self: TExpression, *arg) -> TExpression:
...
def transform_children(
self: TExpression, t: Callable[[expression.Expression], expression.Expression]
) -> TExpression:
return self.replace_args(*(t(arg) for arg in self.inputs))
def bind_variables(
self: TExpression,
bindings: Mapping[str, expression.Expression],
allow_partial_bindings: bool = False,
) -> TExpression:
return self.transform_children(
lambda x: x.bind_variables(bindings, allow_partial_bindings)
)
def bind_refs(
self: TExpression,
bindings: Mapping[ids.ColumnId, expression.Expression],
allow_partial_bindings: bool = False,
) -> TExpression:
return self.transform_children(
lambda x: x.bind_refs(bindings, allow_partial_bindings)
)
@dataclasses.dataclass(frozen=True)
class NullaryAggregation(Aggregation):
op: agg_ops.NullaryWindowOp = dataclasses.field()
@property
def inputs(
self,
) -> typing.Tuple[expression.Expression, ...]:
return ()
def replace_args(self, *arg) -> NullaryAggregation:
return self
@dataclasses.dataclass(frozen=True)
class UnaryAggregation(Aggregation):
op: agg_ops.UnaryWindowOp
arg: expression.Expression
@property
def inputs(
self,
) -> typing.Tuple[expression.Expression, ...]:
return (self.arg,)
def replace_args(self, arg: expression.Expression) -> UnaryAggregation:
return UnaryAggregation(
self.op,
arg,
)
@dataclasses.dataclass(frozen=True)
class BinaryAggregation(Aggregation):
op: agg_ops.BinaryAggregateOp = dataclasses.field()
left: expression.Expression = dataclasses.field()
right: expression.Expression = dataclasses.field()
@property
def inputs(
self,
) -> typing.Tuple[expression.Expression, ...]:
return (self.left, self.right)
def replace_args(
self, larg: expression.Expression, rarg: expression.Expression
) -> BinaryAggregation:
return BinaryAggregation(self.op, larg, rarg)