|
| 1 | +# Copyright 2025 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 | +from __future__ import annotations |
| 15 | + |
| 16 | +import functools |
| 17 | +import typing |
| 18 | + |
| 19 | +import sqlglot.expressions as sge |
| 20 | + |
| 21 | +from bigframes.core import expression, window_spec |
| 22 | +from bigframes.core.compile.sqlglot.expressions import typed_expr |
| 23 | +import bigframes.core.compile.sqlglot.scalar_compiler as scalar_compiler |
| 24 | +import bigframes.core.compile.sqlglot.sqlglot_ir as ir |
| 25 | +import bigframes.operations as ops |
| 26 | + |
| 27 | + |
| 28 | +def compile_aggregate( |
| 29 | + aggregate: expression.Aggregation, |
| 30 | + order_by: tuple[sge.Expression, ...], |
| 31 | +) -> sge.Expression: |
| 32 | + """Compiles BigFrames aggregation expression into SQLGlot expression.""" |
| 33 | + if isinstance(aggregate, expression.NullaryAggregation): |
| 34 | + return compile_nullary_agg(aggregate.op) |
| 35 | + if isinstance(aggregate, expression.UnaryAggregation): |
| 36 | + column = typed_expr.TypedExpr( |
| 37 | + scalar_compiler.compile_scalar_expression(aggregate.arg), |
| 38 | + aggregate.arg.output_type, |
| 39 | + ) |
| 40 | + if not aggregate.op.order_independent: |
| 41 | + return compile_ordered_unary_agg(aggregate.op, column, order_by=order_by) |
| 42 | + else: |
| 43 | + return compile_unary_agg(aggregate.op, column) |
| 44 | + elif isinstance(aggregate, expression.BinaryAggregation): |
| 45 | + left = typed_expr.TypedExpr( |
| 46 | + scalar_compiler.compile_scalar_expression(aggregate.left), |
| 47 | + aggregate.left.output_type, |
| 48 | + ) |
| 49 | + right = typed_expr.TypedExpr( |
| 50 | + scalar_compiler.compile_scalar_expression(aggregate.right), |
| 51 | + aggregate.right.output_type, |
| 52 | + ) |
| 53 | + return compile_binary_agg(aggregate.op, left, right) |
| 54 | + else: |
| 55 | + raise ValueError(f"Unexpected aggregation: {aggregate}") |
| 56 | + |
| 57 | + |
| 58 | +@functools.singledispatch |
| 59 | +def compile_nullary_agg( |
| 60 | + op: ops.aggregations.WindowOp, |
| 61 | + window: typing.Optional[window_spec.WindowSpec] = None, |
| 62 | +) -> sge.Expression: |
| 63 | + raise ValueError(f"Can't compile unrecognized operation: {op}") |
| 64 | + |
| 65 | + |
| 66 | +@functools.singledispatch |
| 67 | +def compile_binary_agg( |
| 68 | + op: ops.aggregations.WindowOp, |
| 69 | + left: typed_expr.TypedExpr, |
| 70 | + right: typed_expr.TypedExpr, |
| 71 | + window: typing.Optional[window_spec.WindowSpec] = None, |
| 72 | +) -> sge.Expression: |
| 73 | + raise ValueError(f"Can't compile unrecognized operation: {op}") |
| 74 | + |
| 75 | + |
| 76 | +@functools.singledispatch |
| 77 | +def compile_unary_agg( |
| 78 | + op: ops.aggregations.WindowOp, |
| 79 | + column: typed_expr.TypedExpr, |
| 80 | + window: typing.Optional[window_spec.WindowSpec] = None, |
| 81 | +) -> sge.Expression: |
| 82 | + raise ValueError(f"Can't compile unrecognized operation: {op}") |
| 83 | + |
| 84 | + |
| 85 | +@functools.singledispatch |
| 86 | +def compile_ordered_unary_agg( |
| 87 | + op: ops.aggregations.WindowOp, |
| 88 | + column: typed_expr.TypedExpr, |
| 89 | + window: typing.Optional[window_spec.WindowSpec] = None, |
| 90 | + order_by: typing.Sequence[sge.Expression] = [], |
| 91 | +) -> sge.Expression: |
| 92 | + raise ValueError(f"Can't compile unrecognized operation: {op}") |
| 93 | + |
| 94 | + |
| 95 | +@compile_unary_agg.register |
| 96 | +def _( |
| 97 | + op: ops.aggregations.SumOp, |
| 98 | + column: typed_expr.TypedExpr, |
| 99 | + window: typing.Optional[window_spec.WindowSpec] = None, |
| 100 | +) -> sge.Expression: |
| 101 | + # Will be null if all inputs are null. Pandas defaults to zero sum though. |
| 102 | + expr = _apply_window_if_present(sge.func("SUM", column.expr), window) |
| 103 | + return sge.func("IFNULL", expr, ir._literal(0, column.dtype)) |
| 104 | + |
| 105 | + |
| 106 | +def _apply_window_if_present( |
| 107 | + value: sge.Expression, |
| 108 | + window: typing.Optional[window_spec.WindowSpec] = None, |
| 109 | +) -> sge.Expression: |
| 110 | + if window is not None: |
| 111 | + raise NotImplementedError("Can't apply window to the expression.") |
| 112 | + return value |
0 commit comments