|
16 | 16 |
|
17 | 17 | import sqlglot.expressions as sge |
18 | 18 |
|
| 19 | +from bigframes import dtypes |
19 | 20 | from bigframes import operations as ops |
| 21 | +from bigframes.core.compile.constants import UNIT_TO_US_CONVERSION_FACTORS |
20 | 22 | from bigframes.core.compile.sqlglot.expressions.typed_expr import TypedExpr |
21 | 23 | import bigframes.core.compile.sqlglot.scalar_compiler as scalar_compiler |
22 | 24 |
|
@@ -81,22 +83,73 @@ def _(expr: TypedExpr) -> sge.Expression: |
81 | 83 |
|
82 | 84 | @register_unary_op(ops.StrftimeOp, pass_op=True) |
83 | 85 | def _(expr: TypedExpr, op: ops.StrftimeOp) -> sge.Expression: |
84 | | - return sge.func("FORMAT_TIMESTAMP", sge.convert(op.date_format), expr.expr) |
| 86 | + func_name = "" |
| 87 | + if expr.dtype == dtypes.DATE_DTYPE: |
| 88 | + func_name = "FORMAT_DATE" |
| 89 | + elif expr.dtype == dtypes.DATETIME_DTYPE: |
| 90 | + func_name = "FORMAT_DATETIME" |
| 91 | + elif expr.dtype == dtypes.TIME_DTYPE: |
| 92 | + func_name = "FORMAT_TIME" |
| 93 | + elif expr.dtype == dtypes.TIMESTAMP_DTYPE: |
| 94 | + func_name = "FORMAT_TIMESTAMP" |
| 95 | + |
| 96 | + return sge.func(func_name, sge.convert(op.date_format), expr.expr) |
85 | 97 |
|
86 | 98 |
|
87 | 99 | @register_unary_op(ops.time_op) |
88 | 100 | def _(expr: TypedExpr) -> sge.Expression: |
89 | 101 | return sge.func("TIME", expr.expr) |
90 | 102 |
|
91 | 103 |
|
92 | | -@register_unary_op(ops.ToDatetimeOp) |
93 | | -def _(expr: TypedExpr) -> sge.Expression: |
94 | | - return sge.Cast(this=sge.func("TIMESTAMP_SECONDS", expr.expr), to="DATETIME") |
95 | | - |
| 104 | +@register_unary_op(ops.ToDatetimeOp, pass_op=True) |
| 105 | +def _(expr: TypedExpr, op: ops.ToDatetimeOp) -> sge.Expression: |
| 106 | + if op.format: |
| 107 | + result = expr.expr |
| 108 | + if expr.dtype != dtypes.STRING_DTYPE: |
| 109 | + result = sge.Cast(this=result, to="STRING") |
| 110 | + result = sge.func( |
| 111 | + "PARSE_TIMESTAMP", sge.convert(op.format), result, sge.convert("UTC") |
| 112 | + ) |
| 113 | + return sge.Cast(this=result, to="DATETIME") |
| 114 | + |
| 115 | + if expr.dtype == dtypes.STRING_DTYPE: |
| 116 | + return sge.TryCast(this=expr.expr, to="DATETIME") |
| 117 | + |
| 118 | + value = expr.expr |
| 119 | + unit = op.unit or "ns" |
| 120 | + factor = UNIT_TO_US_CONVERSION_FACTORS[unit] |
| 121 | + if factor != 1: |
| 122 | + value = sge.Mul(this=value, expression=sge.convert(factor)) |
| 123 | + value = sge.func("TRUNC", value) |
| 124 | + return sge.Cast( |
| 125 | + this=sge.func("TIMESTAMP_MICROS", sge.Cast(this=value, to="INT64")), |
| 126 | + to="DATETIME", |
| 127 | + ) |
| 128 | + |
| 129 | + |
| 130 | +@register_unary_op(ops.ToTimestampOp, pass_op=True) |
| 131 | +def _(expr: TypedExpr, op: ops.ToTimestampOp) -> sge.Expression: |
| 132 | + if op.format: |
| 133 | + result = expr.expr |
| 134 | + if expr.dtype != dtypes.STRING_DTYPE: |
| 135 | + result = sge.Cast(this=result, to="STRING") |
| 136 | + return sge.func( |
| 137 | + "PARSE_TIMESTAMP", sge.convert(op.format), expr.expr, sge.convert("UTC") |
| 138 | + ) |
96 | 139 |
|
97 | | -@register_unary_op(ops.ToTimestampOp) |
98 | | -def _(expr: TypedExpr) -> sge.Expression: |
99 | | - return sge.func("TIMESTAMP_SECONDS", expr.expr) |
| 140 | + if expr.dtype == dtypes.STRING_DTYPE: |
| 141 | + return sge.func("TIMESTAMP", expr.expr) |
| 142 | + |
| 143 | + value = expr.expr |
| 144 | + unit = op.unit or "ns" |
| 145 | + factor = UNIT_TO_US_CONVERSION_FACTORS[unit] |
| 146 | + if factor != 1: |
| 147 | + value = sge.Mul(this=value, expression=sge.convert(factor)) |
| 148 | + value = sge.func("TRUNC", value) |
| 149 | + return sge.Cast( |
| 150 | + this=sge.func("TIMESTAMP_MICROS", sge.Cast(this=value, to="INT64")), |
| 151 | + to="TIMESTAMP", |
| 152 | + ) |
100 | 153 |
|
101 | 154 |
|
102 | 155 | @register_unary_op(ops.UnixMicros) |
|
0 commit comments