From a55d65caf849e5b29ff20ccd62e4eebd4cfdb0fb Mon Sep 17 00:00:00 2001 From: aceforeverd Date: Wed, 19 Jun 2024 06:24:17 +0000 Subject: [PATCH] fix(array_combine): behavior tweaks - use empty string if delimiter is null - restrict to array_combine(string, array ...) --- cases/query/udf_query.yaml | 17 +++++++++++++++-- hybridse/src/udf/default_defs/array_def.cc | 8 ++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/cases/query/udf_query.yaml b/cases/query/udf_query.yaml index 564d4f7594c..528ee7942a5 100644 --- a/cases/query/udf_query.yaml +++ b/cases/query/udf_query.yaml @@ -591,13 +591,15 @@ cases: array_join(array_combine("-", [1, 2], [3, 4]), ",") c0, array_join(array_combine("-", [1, 2], array[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 @@ -605,12 +607,14 @@ cases: select array_join(array_combine("-", [1, NULL], [3, 4]), ",") c0, array_join(array_combine("-", ARRAY[NULL], ["9", "8"]), ",") c1, + array_join(array_combine(string(NULL), ARRAY[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 @@ -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 diff --git a/hybridse/src/udf/default_defs/array_def.cc b/hybridse/src/udf/default_defs/array_def.cc index 5c4a6dc7226..96cc12fc909 100644 --- a/hybridse/src/udf/default_defs/array_def.cc +++ b/hybridse/src/udf/default_defs/array_def.cc @@ -145,7 +145,7 @@ void DefaultUdfLibrary::InitArrayUdfs() { RegisterExternal("array_join") .args, Nullable>(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: @@ -156,10 +156,10 @@ void DefaultUdfLibrary::InitArrayUdfs() { @since 0.9.2)"); RegisterCodeGenUdf("array_combine") - .variadic_args( + .variadic_args>( [](UdfResolveContext* ctx, const ExprAttrNode& delimit, const std::vector& 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"); } @@ -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: