|
18 | 18 |
|
19 | 19 | import sqlglot.expressions as sge |
20 | 20 |
|
21 | | -from bigframes import dtypes |
22 | 21 | from bigframes import operations as ops |
23 | 22 | from bigframes.core.compile.sqlglot.expressions.typed_expr import TypedExpr |
24 | 23 | import bigframes.core.compile.sqlglot.scalar_compiler as scalar_compiler |
@@ -196,9 +195,6 @@ def _(expr: TypedExpr) -> sge.Expression: |
196 | 195 |
|
197 | 196 | @register_unary_op(ops.len_op) |
198 | 197 | def _(expr: TypedExpr) -> sge.Expression: |
199 | | - if dtypes.is_array_like(expr.dtype): |
200 | | - return sge.func("ARRAY_LENGTH", expr.expr) |
201 | | - |
202 | 198 | return sge.Length(this=expr.expr) |
203 | 199 |
|
204 | 200 |
|
@@ -243,7 +239,7 @@ def to_startswith(pat: str) -> sge.Expression: |
243 | 239 |
|
244 | 240 | @register_unary_op(ops.StrStripOp, pass_op=True) |
245 | 241 | def _(expr: TypedExpr, op: ops.StrStripOp) -> sge.Expression: |
246 | | - return sge.Trim(this=expr.expr, expression=sge.convert(op.to_strip)) |
| 242 | + return sge.Trim(this=sge.convert(op.to_strip), expression=expr.expr) |
247 | 243 |
|
248 | 244 |
|
249 | 245 | @register_unary_op(ops.StringSplitOp, pass_op=True) |
@@ -288,29 +284,27 @@ def _(left: TypedExpr, right: TypedExpr) -> sge.Expression: |
288 | 284 |
|
289 | 285 | @register_unary_op(ops.ZfillOp, pass_op=True) |
290 | 286 | def _(expr: TypedExpr, op: ops.ZfillOp) -> sge.Expression: |
291 | | - length_expr = sge.Greatest( |
292 | | - expressions=[sge.Length(this=expr.expr), sge.convert(op.width)] |
293 | | - ) |
294 | 287 | return sge.Case( |
295 | 288 | ifs=[ |
296 | 289 | sge.If( |
297 | | - this=sge.func( |
298 | | - "STARTS_WITH", |
299 | | - expr.expr, |
300 | | - sge.convert("-"), |
| 290 | + this=sge.EQ( |
| 291 | + this=sge.Substring( |
| 292 | + this=expr.expr, start=sge.convert(1), length=sge.convert(1) |
| 293 | + ), |
| 294 | + expression=sge.convert("-"), |
301 | 295 | ), |
302 | 296 | true=sge.Concat( |
303 | 297 | expressions=[ |
304 | 298 | sge.convert("-"), |
305 | 299 | sge.func( |
306 | 300 | "LPAD", |
307 | | - sge.Substring(this=expr.expr, start=sge.convert(2)), |
308 | | - length_expr - 1, |
| 301 | + sge.Substring(this=expr.expr, start=sge.convert(1)), |
| 302 | + sge.convert(op.width - 1), |
309 | 303 | sge.convert("0"), |
310 | 304 | ), |
311 | 305 | ] |
312 | 306 | ), |
313 | 307 | ) |
314 | 308 | ], |
315 | | - default=sge.func("LPAD", expr.expr, length_expr, sge.convert("0")), |
| 309 | + default=sge.func("LPAD", expr.expr, sge.convert(op.width), sge.convert("0")), |
316 | 310 | ) |
0 commit comments