Skip to content

Commit

Permalink
feat: add array_append support all engines (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
eakmanrq authored Jun 29, 2024
1 parent b8dea22 commit eeffd41
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 3 deletions.
13 changes: 13 additions & 0 deletions sqlframe/base/function_alternatives.py
Original file line number Diff line number Diff line change
Expand Up @@ -1529,6 +1529,19 @@ def to_unix_timestamp_include_default_format(
return to_unix_timestamp(timestamp, format)


def array_append_list_append(col: ColumnOrName, value: ColumnOrLiteral) -> Column:
lit = get_func_from_session("lit")
value = value if isinstance(value, Column) else lit(value)
return Column.invoke_anonymous_function(col, "LIST_APPEND", value)


def array_append_using_array_cat(col: ColumnOrName, value: ColumnOrLiteral) -> Column:
lit = get_func_from_session("lit")
array = get_func_from_session("array")
value = value if isinstance(value, Column) else lit(value)
return Column.invoke_anonymous_function(col, "ARRAY_CONCAT", array(value))


def day_with_try_to_timestamp(col: ColumnOrName) -> Column:
from sqlframe.base.functions import day

Expand Down
2 changes: 1 addition & 1 deletion sqlframe/base/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1300,7 +1300,7 @@ def array_agg(col: ColumnOrName) -> Column:
return Column.invoke_expression_over_column(col, expression.ArrayAgg)


@meta(unsupported_engines="*")
@meta()
def array_append(col: ColumnOrName, value: ColumnOrLiteral) -> Column:
value = value if isinstance(value, Column) else lit(value)
return Column.invoke_anonymous_function(col, "ARRAY_APPEND", value)
Expand Down
1 change: 1 addition & 0 deletions sqlframe/bigquery/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
position_as_strpos as position,
try_to_timestamp_safe as try_to_timestamp,
_is_string_using_typeof_string as _is_string,
array_append_using_array_cat as array_append,
)


Expand Down
1 change: 1 addition & 0 deletions sqlframe/duckdb/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@
day_with_try_to_timestamp as day,
try_to_timestamp_strptime as try_to_timestamp,
_is_string_using_typeof_varchar as _is_string,
array_append_list_append as array_append,
)
5 changes: 3 additions & 2 deletions tests/integration/engines/test_int_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2067,9 +2067,10 @@ def test_array_agg(get_session_and_func):
]


def test_array_append(get_session_and_func):
def test_array_append(get_session_and_func, get_func):
session, array_append = get_session_and_func("array_append")
df = session.createDataFrame([Row(c1=["b", "a", "c"], c2="c")])
lit = get_func("lit", session)
df = session.range(1).select(lit(["b", "a", "c"]).alias("c1"), lit("c").alias("c2"))
assert df.select(array_append(df.c1, df.c2)).collect() == [
Row(value=["b", "a", "c", "c"]),
]
Expand Down

0 comments on commit eeffd41

Please sign in to comment.