|
18 | 18 |
|
19 | 19 | from __future__ import annotations |
20 | 20 |
|
| 21 | +import builtins |
21 | 22 | from typing import TYPE_CHECKING, Any |
22 | 23 |
|
23 | 24 | import pyarrow as pa |
|
139 | 140 | "degrees", |
140 | 141 | "dense_rank", |
141 | 142 | "digest", |
| 143 | + "element_at", |
142 | 144 | "empty", |
143 | 145 | "encode", |
144 | 146 | "ends_with", |
|
202 | 204 | "make_array", |
203 | 205 | "make_date", |
204 | 206 | "make_list", |
| 207 | + "make_map", |
205 | 208 | "make_time", |
| 209 | + "map_entries", |
| 210 | + "map_extract", |
| 211 | + "map_keys", |
| 212 | + "map_values", |
206 | 213 | "max", |
207 | 214 | "md5", |
208 | 215 | "mean", |
@@ -3374,6 +3381,120 @@ def empty(array: Expr) -> Expr: |
3374 | 3381 | return array_empty(array) |
3375 | 3382 |
|
3376 | 3383 |
|
| 3384 | +# map functions |
| 3385 | + |
| 3386 | + |
| 3387 | +def make_map(*args: Expr) -> Expr: |
| 3388 | + """Returns a map created from key and value expressions. |
| 3389 | +
|
| 3390 | + Accepts an even number of arguments, alternating between keys and values. |
| 3391 | + For example, ``make_map(k1, v1, k2, v2)`` creates a map ``{k1: v1, k2: v2}``. |
| 3392 | +
|
| 3393 | + Examples: |
| 3394 | + >>> ctx = dfn.SessionContext() |
| 3395 | + >>> df = ctx.from_pydict({"a": [1]}) |
| 3396 | + >>> result = df.select( |
| 3397 | + ... dfn.functions.make_map( |
| 3398 | + ... dfn.lit("a"), dfn.lit(1), |
| 3399 | + ... dfn.lit("b"), dfn.lit(2), |
| 3400 | + ... ).alias("map")) |
| 3401 | + >>> result.collect_column("map")[0].as_py() |
| 3402 | + [('a', 1), ('b', 2)] |
| 3403 | + """ |
| 3404 | + if len(args) % 2 != 0: |
| 3405 | + msg = "make_map requires an even number of arguments" |
| 3406 | + raise ValueError(msg) |
| 3407 | + keys = [args[i].expr for i in builtins.range(0, len(args), 2)] |
| 3408 | + values = [args[i].expr for i in builtins.range(1, len(args), 2)] |
| 3409 | + return Expr(f.make_map(keys, values)) |
| 3410 | + |
| 3411 | + |
| 3412 | +def map_keys(map: Expr) -> Expr: |
| 3413 | + """Returns a list of all keys in the map. |
| 3414 | +
|
| 3415 | + Examples: |
| 3416 | + >>> ctx = dfn.SessionContext() |
| 3417 | + >>> df = ctx.from_pydict({"a": [1]}) |
| 3418 | + >>> result = df.select( |
| 3419 | + ... dfn.functions.map_keys( |
| 3420 | + ... dfn.functions.make_map( |
| 3421 | + ... dfn.lit("x"), dfn.lit(1), |
| 3422 | + ... dfn.lit("y"), dfn.lit(2), |
| 3423 | + ... ) |
| 3424 | + ... ).alias("keys")) |
| 3425 | + >>> result.collect_column("keys")[0].as_py() |
| 3426 | + ['x', 'y'] |
| 3427 | + """ |
| 3428 | + return Expr(f.map_keys(map.expr)) |
| 3429 | + |
| 3430 | + |
| 3431 | +def map_values(map: Expr) -> Expr: |
| 3432 | + """Returns a list of all values in the map. |
| 3433 | +
|
| 3434 | + Examples: |
| 3435 | + >>> ctx = dfn.SessionContext() |
| 3436 | + >>> df = ctx.from_pydict({"a": [1]}) |
| 3437 | + >>> result = df.select( |
| 3438 | + ... dfn.functions.map_values( |
| 3439 | + ... dfn.functions.make_map( |
| 3440 | + ... dfn.lit("x"), dfn.lit(1), |
| 3441 | + ... dfn.lit("y"), dfn.lit(2), |
| 3442 | + ... ) |
| 3443 | + ... ).alias("vals")) |
| 3444 | + >>> result.collect_column("vals")[0].as_py() |
| 3445 | + [1, 2] |
| 3446 | + """ |
| 3447 | + return Expr(f.map_values(map.expr)) |
| 3448 | + |
| 3449 | + |
| 3450 | +def map_extract(map: Expr, key: Expr) -> Expr: |
| 3451 | + """Returns the value for the given key in the map, or an empty list if absent. |
| 3452 | +
|
| 3453 | + Examples: |
| 3454 | + >>> ctx = dfn.SessionContext() |
| 3455 | + >>> df = ctx.from_pydict({"a": [1]}) |
| 3456 | + >>> result = df.select( |
| 3457 | + ... dfn.functions.map_extract( |
| 3458 | + ... dfn.functions.make_map( |
| 3459 | + ... dfn.lit("x"), dfn.lit(1), |
| 3460 | + ... dfn.lit("y"), dfn.lit(2), |
| 3461 | + ... ), |
| 3462 | + ... dfn.lit("x"), |
| 3463 | + ... ).alias("val")) |
| 3464 | + >>> result.collect_column("val")[0].as_py() |
| 3465 | + [1] |
| 3466 | + """ |
| 3467 | + return Expr(f.map_extract(map.expr, key.expr)) |
| 3468 | + |
| 3469 | + |
| 3470 | +def map_entries(map: Expr) -> Expr: |
| 3471 | + """Returns a list of all entries (key-value struct pairs) in the map. |
| 3472 | +
|
| 3473 | + Examples: |
| 3474 | + >>> ctx = dfn.SessionContext() |
| 3475 | + >>> df = ctx.from_pydict({"a": [1]}) |
| 3476 | + >>> result = df.select( |
| 3477 | + ... dfn.functions.map_entries( |
| 3478 | + ... dfn.functions.make_map( |
| 3479 | + ... dfn.lit("x"), dfn.lit(1), |
| 3480 | + ... dfn.lit("y"), dfn.lit(2), |
| 3481 | + ... ) |
| 3482 | + ... ).alias("entries")) |
| 3483 | + >>> result.collect_column("entries")[0].as_py() |
| 3484 | + [{'key': 'x', 'value': 1}, {'key': 'y', 'value': 2}] |
| 3485 | + """ |
| 3486 | + return Expr(f.map_entries(map.expr)) |
| 3487 | + |
| 3488 | + |
| 3489 | +def element_at(map: Expr, key: Expr) -> Expr: |
| 3490 | + """Returns the value for the given key in the map, or an empty list if absent. |
| 3491 | +
|
| 3492 | + See Also: |
| 3493 | + This is an alias for :py:func:`map_extract`. |
| 3494 | + """ |
| 3495 | + return map_extract(map, key) |
| 3496 | + |
| 3497 | + |
3377 | 3498 | # aggregate functions |
3378 | 3499 | def approx_distinct( |
3379 | 3500 | expression: Expr, |
|
0 commit comments