Skip to content

Commit

Permalink
Add bytes option to array_join.
Browse files Browse the repository at this point in the history
The `array_join` function can now also be used to join bytes into a
single byte-string.
  • Loading branch information
vpetrovykh committed Feb 23, 2024
1 parent e4484de commit bae4bff
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
14 changes: 12 additions & 2 deletions docs/stdlib/array.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Arrays
- Finds the index of an element in the array.

* - :eql:func:`array_join`
- Renders an array to a string.
- Renders an array to a string or byte-string.

* - :eql:func:`array_fill`
- :eql:func-desc:`array_fill`
Expand Down Expand Up @@ -313,10 +313,12 @@ Reference


.. eql:function:: std::array_join(array: array<str>, delimiter: str) -> str
std::array_join(array: array<bytes>, \
delimiter: bytes) -> bytes
:index: join array_to_string implode

Renders an array to a string.
Renders an array to a string or byte-string.

Join a string array into a single string using a specified *delimiter*:

Expand All @@ -325,6 +327,14 @@ Reference
db> select array_join(['one', 'two', 'three'], ', ');
{'one, two, three'}
Similarly, an array of :eql:type:`bytes` can be joined as a single value
using a specified *delimiter*:

.. code-block:: edgeql-repl
db> select array_join([b'\x01', b'\x02', b'\x03'], b'\xff');
{b'\x01\xff\x02\xff\x03'}
----------

Expand Down
2 changes: 1 addition & 1 deletion edb/buildmeta.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@


# Increment this whenever the database layout or stdlib changes.
EDGEDB_CATALOG_VERSION = 2024_02_20_00_00
EDGEDB_CATALOG_VERSION = 2024_02_23_00_00
EDGEDB_MAJOR_VERSION = 5


Expand Down
14 changes: 14 additions & 0 deletions edb/lib/std/30-arrayfuncs.edgeql
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ std::array_join(array: array<std::str>, delimiter: std::str) -> std::str
};


CREATE FUNCTION
std::array_join(array: array<std::bytes>, delimiter: std::bytes) -> std::bytes
{
CREATE ANNOTATION std::description := 'Render an array to a byte-string.';
SET volatility := 'Immutable';
USING SQL $$
SELECT
COALESCE (string_agg(el, "delimiter"), '\x')
FROM
(SELECT unnest("array") AS el) AS t
$$;
};


## Array operators


Expand Down
37 changes: 37 additions & 0 deletions tests/test_edgeql_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3289,6 +3289,43 @@ async def test_edgeql_functions_array_join_01(self):
[''],
)

async def test_edgeql_functions_array_join_02(self):
await self.assert_query_result(
r'''SELECT array_join(['one', 'two', 'three'], {', ', '@!'});''',
{'one, two, three', 'one@!two@!three'},
)

async def test_edgeql_functions_array_join_03(self):
await self.assert_query_result(
r'''SELECT array_join([b'one', b'two', b'three'], b', ');''',
[base64.b64encode(b'one, two, three').decode()],
[b'one, two, three'],
)

await self.assert_query_result(
r'''SELECT array_join([b'one', b'two', b'three'], b'');''',
[base64.b64encode(b'onetwothree').decode()],
[b'onetwothree'],
)

await self.assert_query_result(
r'''SELECT array_join(<array<bytes>>[], b', ');''',
[base64.b64encode(b'').decode()],
[b''],
)

async def test_edgeql_functions_array_join_04(self):
await self.assert_query_result(
r'''
SELECT array_join([b'one', b'two', b'three'], {b', ', b'@!'});
''',
{
base64.b64encode(b'one, two, three').decode(),
base64.b64encode(b'one@!two@!three').decode(),
},
{b'one, two, three', b'one@!two@!three'},
)

async def test_edgeql_functions_str_split_01(self):
await self.assert_query_result(
r'''SELECT str_split('one, two, three', ', ');''',
Expand Down

0 comments on commit bae4bff

Please sign in to comment.