|
| 1 | +# Copyright 2023 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +import abc |
| 18 | +import dataclasses |
| 19 | +import functools |
| 20 | +import itertools |
| 21 | +import typing |
| 22 | +from typing import Callable, Mapping, TypeVar |
| 23 | + |
| 24 | +from bigframes import dtypes |
| 25 | +from bigframes.core import expression |
| 26 | +import bigframes.core.identifiers as ids |
| 27 | +import bigframes.operations.aggregations as agg_ops |
| 28 | + |
| 29 | +TExpression = TypeVar("TExpression", bound="Aggregation") |
| 30 | + |
| 31 | + |
| 32 | +@dataclasses.dataclass(frozen=True) |
| 33 | +class Aggregation(expression.Expression): |
| 34 | + """Represents windowing or aggregation over a column.""" |
| 35 | + |
| 36 | + op: agg_ops.WindowOp = dataclasses.field() |
| 37 | + |
| 38 | + @property |
| 39 | + def column_references(self) -> typing.Tuple[ids.ColumnId, ...]: |
| 40 | + return tuple( |
| 41 | + itertools.chain.from_iterable( |
| 42 | + map(lambda x: x.column_references, self.inputs) |
| 43 | + ) |
| 44 | + ) |
| 45 | + |
| 46 | + @functools.cached_property |
| 47 | + def is_resolved(self) -> bool: |
| 48 | + return all(input.is_resolved for input in self.inputs) |
| 49 | + |
| 50 | + @functools.cached_property |
| 51 | + def output_type(self) -> dtypes.ExpressionType: |
| 52 | + if not self.is_resolved: |
| 53 | + raise ValueError(f"Type of expression {self.op} has not been fixed.") |
| 54 | + |
| 55 | + input_types = [input.output_type for input in self.inputs] |
| 56 | + |
| 57 | + return self.op.output_type(*input_types) |
| 58 | + |
| 59 | + @property |
| 60 | + @abc.abstractmethod |
| 61 | + def inputs( |
| 62 | + self, |
| 63 | + ) -> typing.Tuple[expression.Expression, ...]: |
| 64 | + ... |
| 65 | + |
| 66 | + @property |
| 67 | + def free_variables(self) -> typing.Tuple[str, ...]: |
| 68 | + return tuple( |
| 69 | + itertools.chain.from_iterable(map(lambda x: x.free_variables, self.inputs)) |
| 70 | + ) |
| 71 | + |
| 72 | + @property |
| 73 | + def is_const(self) -> bool: |
| 74 | + return all(child.is_const for child in self.inputs) |
| 75 | + |
| 76 | + @abc.abstractmethod |
| 77 | + def replace_args(self: TExpression, *arg) -> TExpression: |
| 78 | + ... |
| 79 | + |
| 80 | + def transform_children( |
| 81 | + self: TExpression, t: Callable[[expression.Expression], expression.Expression] |
| 82 | + ) -> TExpression: |
| 83 | + return self.replace_args(*(t(arg) for arg in self.inputs)) |
| 84 | + |
| 85 | + def bind_variables( |
| 86 | + self: TExpression, |
| 87 | + bindings: Mapping[str, expression.Expression], |
| 88 | + allow_partial_bindings: bool = False, |
| 89 | + ) -> TExpression: |
| 90 | + return self.transform_children( |
| 91 | + lambda x: x.bind_variables(bindings, allow_partial_bindings) |
| 92 | + ) |
| 93 | + |
| 94 | + def bind_refs( |
| 95 | + self: TExpression, |
| 96 | + bindings: Mapping[ids.ColumnId, expression.Expression], |
| 97 | + allow_partial_bindings: bool = False, |
| 98 | + ) -> TExpression: |
| 99 | + return self.transform_children( |
| 100 | + lambda x: x.bind_refs(bindings, allow_partial_bindings) |
| 101 | + ) |
| 102 | + |
| 103 | + |
| 104 | +@dataclasses.dataclass(frozen=True) |
| 105 | +class NullaryAggregation(Aggregation): |
| 106 | + op: agg_ops.NullaryWindowOp = dataclasses.field() |
| 107 | + |
| 108 | + @property |
| 109 | + def inputs( |
| 110 | + self, |
| 111 | + ) -> typing.Tuple[expression.Expression, ...]: |
| 112 | + return () |
| 113 | + |
| 114 | + def replace_args(self, *arg) -> NullaryAggregation: |
| 115 | + return self |
| 116 | + |
| 117 | + |
| 118 | +@dataclasses.dataclass(frozen=True) |
| 119 | +class UnaryAggregation(Aggregation): |
| 120 | + op: agg_ops.UnaryWindowOp |
| 121 | + arg: expression.Expression |
| 122 | + |
| 123 | + @property |
| 124 | + def inputs( |
| 125 | + self, |
| 126 | + ) -> typing.Tuple[expression.Expression, ...]: |
| 127 | + return (self.arg,) |
| 128 | + |
| 129 | + def replace_args(self, arg: expression.Expression) -> UnaryAggregation: |
| 130 | + return UnaryAggregation( |
| 131 | + self.op, |
| 132 | + arg, |
| 133 | + ) |
| 134 | + |
| 135 | + |
| 136 | +@dataclasses.dataclass(frozen=True) |
| 137 | +class BinaryAggregation(Aggregation): |
| 138 | + op: agg_ops.BinaryAggregateOp = dataclasses.field() |
| 139 | + left: expression.Expression = dataclasses.field() |
| 140 | + right: expression.Expression = dataclasses.field() |
| 141 | + |
| 142 | + @property |
| 143 | + def inputs( |
| 144 | + self, |
| 145 | + ) -> typing.Tuple[expression.Expression, ...]: |
| 146 | + return (self.left, self.right) |
| 147 | + |
| 148 | + def replace_args( |
| 149 | + self, larg: expression.Expression, rarg: expression.Expression |
| 150 | + ) -> BinaryAggregation: |
| 151 | + return BinaryAggregation(self.op, larg, rarg) |
0 commit comments