|
| 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 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +import sqlglot.expressions as sge |
| 18 | + |
| 19 | +from bigframes import operations as ops |
| 20 | +from bigframes.core.compile.sqlglot.expressions.typed_expr import TypedExpr |
| 21 | +import bigframes.core.compile.sqlglot.scalar_compiler as scalar_compiler |
| 22 | + |
| 23 | +register_unary_op = scalar_compiler.scalar_op_compiler.register_unary_op |
| 24 | + |
| 25 | + |
| 26 | +@register_unary_op(ops.FloorDtOp, pass_op=True) |
| 27 | +def _(expr: TypedExpr, op: ops.FloorDtOp) -> sge.Expression: |
| 28 | + # TODO: Remove this method when it is covered by ops.FloorOp |
| 29 | + return sge.TimestampTrunc(this=expr.expr, unit=sge.Identifier(this=op.freq)) |
| 30 | + |
| 31 | + |
| 32 | +@register_unary_op(ops.hour_op) |
| 33 | +def _(expr: TypedExpr) -> sge.Expression: |
| 34 | + return sge.Extract(this=sge.Identifier(this="HOUR"), expression=expr.expr) |
| 35 | + |
| 36 | + |
| 37 | +@register_unary_op(ops.minute_op) |
| 38 | +def _(expr: TypedExpr) -> sge.Expression: |
| 39 | + return sge.Extract(this=sge.Identifier(this="MINUTE"), expression=expr.expr) |
| 40 | + |
| 41 | + |
| 42 | +@register_unary_op(ops.month_op) |
| 43 | +def _(expr: TypedExpr) -> sge.Expression: |
| 44 | + return sge.Extract(this=sge.Identifier(this="MONTH"), expression=expr.expr) |
| 45 | + |
| 46 | + |
| 47 | +@register_unary_op(ops.normalize_op) |
| 48 | +def _(expr: TypedExpr) -> sge.Expression: |
| 49 | + return sge.TimestampTrunc(this=expr.expr, unit=sge.Identifier(this="DAY")) |
| 50 | + |
| 51 | + |
| 52 | +@register_unary_op(ops.quarter_op) |
| 53 | +def _(expr: TypedExpr) -> sge.Expression: |
| 54 | + return sge.Extract(this=sge.Identifier(this="QUARTER"), expression=expr.expr) |
| 55 | + |
| 56 | + |
| 57 | +@register_unary_op(ops.second_op) |
| 58 | +def _(expr: TypedExpr) -> sge.Expression: |
| 59 | + return sge.Extract(this=sge.Identifier(this="SECOND"), expression=expr.expr) |
| 60 | + |
| 61 | + |
| 62 | +@register_unary_op(ops.StrftimeOp, pass_op=True) |
| 63 | +def _(expr: TypedExpr, op: ops.StrftimeOp) -> sge.Expression: |
| 64 | + return sge.func("FORMAT_TIMESTAMP", sge.convert(op.date_format), expr.expr) |
| 65 | + |
| 66 | + |
| 67 | +@register_unary_op(ops.time_op) |
| 68 | +def _(expr: TypedExpr) -> sge.Expression: |
| 69 | + return sge.func("TIME", expr.expr) |
| 70 | + |
| 71 | + |
| 72 | +@register_unary_op(ops.ToDatetimeOp) |
| 73 | +def _(expr: TypedExpr) -> sge.Expression: |
| 74 | + return sge.Cast(this=sge.func("TIMESTAMP_SECONDS", expr.expr), to="DATETIME") |
| 75 | + |
| 76 | + |
| 77 | +@register_unary_op(ops.ToTimestampOp) |
| 78 | +def _(expr: TypedExpr) -> sge.Expression: |
| 79 | + return sge.func("TIMESTAMP_SECONDS", expr.expr) |
| 80 | + |
| 81 | + |
| 82 | +@register_unary_op(ops.UnixMicros) |
| 83 | +def _(expr: TypedExpr) -> sge.Expression: |
| 84 | + return sge.func("UNIX_MICROS", expr.expr) |
| 85 | + |
| 86 | + |
| 87 | +@register_unary_op(ops.UnixMillis) |
| 88 | +def _(expr: TypedExpr) -> sge.Expression: |
| 89 | + return sge.func("UNIX_MILLIS", expr.expr) |
| 90 | + |
| 91 | + |
| 92 | +@register_unary_op(ops.UnixSeconds, pass_op=True) |
| 93 | +def _(expr: TypedExpr, op: ops.UnixSeconds) -> sge.Expression: |
| 94 | + return sge.func("UNIX_SECONDS", expr.expr) |
| 95 | + |
| 96 | + |
| 97 | +@register_unary_op(ops.year_op) |
| 98 | +def _(expr: TypedExpr) -> sge.Expression: |
| 99 | + return sge.Extract(this=sge.Identifier(this="YEAR"), expression=expr.expr) |
0 commit comments