Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add concat_ws Spark function #8854

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions velox/docs/functions/spark/string.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ Unless specified otherwise, all functions return NULL if at least one of the arg
If ``n < 0``, the result is an empty string.
If ``n >= 256``, the result is equivalent to chr(``n % 256``).

.. spark:function:: concat_ws(separator, [string]/[array<string>], ...) -> varchar

Returns the concatenation for ``string`` and all elements in ``array<string>``, separated
by ``separator``. The type of ``separator`` is VARCHAR . It can take variable number of
remaining arguments, where ``string`` and ``array<string>`` can be used in combination. NULL
element is skipped in the concatenation. If ``separator`` is NULL, returns NULL, regardless
of the following inputs. For non-NULL ``separator``, if no remaining input or all remaining
inputs are NULL, returns an empty string. ::

SELECT concat_ws('~', 'a', 'b', 'c'); -- 'a~b~c'
Copy link
Collaborator

@rui-mo rui-mo Apr 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does concat_ws behave when element is empty string or array, like concat_ws('~', 'a', 'b', ''), or concat_ws('~', [], ['d'])?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just confirmed. Empty string & its neighbors are also separated in Spark. Just fixed this and updated doc.

SELECT concat_ws('~', ['a', 'b', 'c'], ['d']); -- 'a~b~c~d'
SELECT concat_ws('~', 'a', ['b', 'c']); -- 'a~b~c'
SELECT concat_ws('~', '', [''], ['a', '']); -- '~~a~'
SELECT concat_ws(NULL, 'a'); -- NULL
SELECT concat_ws('~'); -- ''
SELECT concat_ws('~', NULL, [NULL], 'a', 'b'); -- 'a~b'
SELECT concat_ws('~', NULL, NULL); -- ''
SELECT concat_ws('~', [NULL]); -- ''

.. spark:function:: contains(left, right) -> boolean

Returns true if 'right' is found in 'left'. Otherwise, returns false. ::
Expand Down
7 changes: 6 additions & 1 deletion velox/expression/fuzzer/SparkExpressionFuzzerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ int main(int argc, char** argv) {
"chr",
"replace",
"might_contain",
"unix_timestamp"};
"unix_timestamp",
// Skip concat_ws due to the below issue:
// We use "any" type in its signature to allow mixed
// using of VARCHAR & ARRAY<VARCHAR>. But the fuzzer
// couldn't generate correct expressions for it.
"concat_ws"};

// Required by spark_partition_id function.
std::unordered_map<std::string, std::string> queryConfigs = {
Expand Down
1 change: 1 addition & 0 deletions velox/functions/sparksql/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ add_library(
ArraySort.cpp
Bitwise.cpp
Comparisons.cpp
ConcatWs.cpp
DecimalArithmetic.cpp
DecimalCompare.cpp
Hash.cpp
Expand Down
Loading
Loading