From 6d951cdd36e446037a17d737d7a3be5805e0d745 Mon Sep 17 00:00:00 2001 From: xuqichen Date: Fri, 9 Aug 2024 14:50:32 +0800 Subject: [PATCH] init --- hybridse/src/udf/default_defs/array_def.cc | 14 +++++++++++++ hybridse/src/udf/udf.cc | 24 ++++++++++++++++++++++ hybridse/src/udf/udf.h | 4 ++++ 3 files changed, 42 insertions(+) diff --git a/hybridse/src/udf/default_defs/array_def.cc b/hybridse/src/udf/default_defs/array_def.cc index 5f1bebfaaf6..d0be421957d 100644 --- a/hybridse/src/udf/default_defs/array_def.cc +++ b/hybridse/src/udf/default_defs/array_def.cc @@ -226,6 +226,20 @@ void DefaultUdfLibrary::InitArrayUdfs() { @endcode @since 0.9.2 )"); + + RegisterUdfTemplate("array_padding") + .returns>() + .return_by_arg(true) + .args_in, bool, int16_t, int32_t, int64_t, float, double, Timestamp, Date, StringRef>(reinterpret_cast(array_padding)) + .doc(R"( + @brief Expand the array to the specified length by padding the specified value. + + @code{.sql} + select array_padding([1, 2], 4, 0); + -- output [1, 2, 0, 0] + @endcode + + @since 0.7.0)"); } } // namespace udf } // namespace hybridse diff --git a/hybridse/src/udf/udf.cc b/hybridse/src/udf/udf.cc index 2fe393cf12b..5ada426ea1b 100644 --- a/hybridse/src/udf/udf.cc +++ b/hybridse/src/udf/udf.cc @@ -1504,6 +1504,30 @@ void array_combine(codec::StringRef *del, int32_t cnt, ArrayRefsize = real_sz; } +void array_padding(ArrayRef *arr, int32_t target_size, T default_value, + ArrayRef *out, bool *is_null) { + if (arr->size >= target_size) { + // v1::AllocManagedArray(out, arr->size); + out->nullables = arr->nullables; + out->raw = arr->raw; + } + else { + v1::AllocManagedArray(out, target_size); + for (int i = 0; i < target_size; ++i) { + if (i < arr->size) { + // deep copy + out->nullables[i] = arr->nullables[i]; + out->raw[i]->data_ = arr->raw[i]->data_; + out->raw[i]->size_ = arr->raw[i]->size_; + } else { + out->nullables[i] = false; + out->raw[i]->data_ = default_value.data_; + out->raw[i]->size_ = default_value.size_; + } + } + } +} + } // namespace v1 bool RegisterMethod(UdfLibrary *lib, const std::string &fn_name, hybridse::node::TypeNode *ret, diff --git a/hybridse/src/udf/udf.h b/hybridse/src/udf/udf.h index 554db565b0f..6924e9b13af 100644 --- a/hybridse/src/udf/udf.h +++ b/hybridse/src/udf/udf.h @@ -524,6 +524,10 @@ void unhex(StringRef *str, StringRef *output, bool* is_null); void printLog(const char* fmt); void array_combine(codec::StringRef *del, int32_t cnt, ArrayRef **data, ArrayRef *out); + +template +void array_padding(ArrayRef *arr, int32_t target_size, T default_value, + ArrayRef *out); } // namespace v1 /// \brief register native udf related methods into given UdfLibrary `lib`