Skip to content

Commit

Permalink
fix(array_combine): behavior tweaks
Browse files Browse the repository at this point in the history
- use empty string if delimiter is null
- restrict to array_combine(string, array<T> ...)
  • Loading branch information
aceforeverd committed Jun 19, 2024
1 parent 29250ca commit a55d65c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
17 changes: 15 additions & 2 deletions cases/query/udf_query.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -591,26 +591,30 @@ cases:
array_join(array_combine("-", [1, 2], [3, 4]), ",") c0,
array_join(array_combine("-", [1, 2], array<int64>[3], ["5", "6"]), ",") c1,
array_join(array_combine("|", ["1"], [timestamp(1717171200000), timestamp("2024-06-02 12:00:00")]), ",") c2,
array_join(array_combine("|", ["1"]), ",") c3,
expect:
columns:
- c0 string
- c1 string
- c2 string
- c3 string
rows:
- ["1-3,1-4,2-3,2-4", "1-3-5,1-3-6,2-3-5,2-3-6", "1|2024-06-01 00:00:00,1|2024-06-02 12:00:00"]
- ["1-3,1-4,2-3,2-4", "1-3-5,1-3-6,2-3-5,2-3-6", "1|2024-06-01 00:00:00,1|2024-06-02 12:00:00", "1"]
- id: array_combine_3
desc: null values skipped
mode: request-unsupport
sql: |
select
array_join(array_combine("-", [1, NULL], [3, 4]), ",") c0,
array_join(array_combine("-", ARRAY<INT>[NULL], ["9", "8"]), ",") c1,
array_join(array_combine(string(NULL), ARRAY<INT>[1], ["9", "8"]), ",") c2,
expect:
columns:
- c0 string
- c1 string
- c2 string
rows:
- ["1-3,1-4", ""]
- ["1-3,1-4", "", "19,18"]
- id: array_combine_4
desc: construct array from table
mode: request-unsupport
Expand All @@ -633,6 +637,15 @@ cases:
rows:
- [1, "1-foo,1-c2,10-foo,10-c2"]
- [2, "2-bar,2-c2,10-bar,10-c2"]
- id: array_combine_err1
mode: request-unsupport
sql: |
select
array_join(array_combine("-"), ",") c0,
expect:
success: false
msg: |
Fail to resolve expression: array_join(array_combine(-), ,)
# ================================================================
# Map data type
Expand Down
8 changes: 4 additions & 4 deletions hybridse/src/udf/default_defs/array_def.cc
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ void DefaultUdfLibrary::InitArrayUdfs() {
RegisterExternal("array_join")
.args<ArrayRef<StringRef>, Nullable<StringRef>>(array_join)
.doc(R"(
@brief array_join(array, delimiter) - Concatenates the elements of the given array using the delimiter and an optional string to replace nulls. Any null value is filtered.
@brief array_join(array, delimiter) - Concatenates the elements of the given array using the delimiter. Any null value is filtered.
Example:
Expand All @@ -156,10 +156,10 @@ void DefaultUdfLibrary::InitArrayUdfs() {
@since 0.9.2)");

RegisterCodeGenUdf("array_combine")
.variadic_args<AnyArg>(
.variadic_args<Nullable<StringRef>>(
[](UdfResolveContext* ctx, const ExprAttrNode& delimit, const std::vector<ExprAttrNode>& arg_attrs,
ExprAttrNode* out) -> base::Status {
CHECK_TRUE(delimit.type()->IsString(), common::kCodegenError, "delimiter must be string");
CHECK_TRUE(!arg_attrs.empty(), common::kCodegenError, "at least one array required by array_combine");
for (auto & val : arg_attrs) {
CHECK_TRUE(val.type()->IsArray(), common::kCodegenError, "argument to array_combine must be array");
}
Expand All @@ -179,7 +179,7 @@ void DefaultUdfLibrary::InitArrayUdfs() {
@brief array_combine(delimiter, array1, array2, ...)
return array of strings for input array1, array2, ... doing cartesian product. Each product is joined with
{delimiter} as a string
{delimiter} as a string. Empty string used if {delimiter} is null.
Example:
Expand Down

0 comments on commit a55d65c

Please sign in to comment.