From 58c4c8cca488ad21c8ad50938ef0952d4a839138 Mon Sep 17 00:00:00 2001 From: xuqichen Date: Thu, 15 Aug 2024 11:56:12 +0800 Subject: [PATCH] 0.1 --- hybridse/src/codegen/udf_ir_builder_test.cc | 10 +++++ hybridse/src/udf/default_defs/array_def.cc | 6 +-- hybridse/src/udf/udf.cc | 46 ++++++++++----------- hybridse/src/udf/udf.h | 40 ++++++++++++++++-- 4 files changed, 73 insertions(+), 29 deletions(-) diff --git a/hybridse/src/codegen/udf_ir_builder_test.cc b/hybridse/src/codegen/udf_ir_builder_test.cc index 91a56b83c49..14e527f78e5 100644 --- a/hybridse/src/codegen/udf_ir_builder_test.cc +++ b/hybridse/src/codegen/udf_ir_builder_test.cc @@ -1599,6 +1599,16 @@ TEST_F(UdfIRBuilderTest, GetJsonObject) { } } +// array_padding normal check +// TEST_F(UdfIRBuilderTest, ArrayPadding) { +// CheckUdf, Nullable, int32_t, Nullable>( +// "array_padding", "[1,2,3,4,5,6,7,8,9,1]", "[1,2,3,4,5,6,7,8,9]", 10, 1); +// CheckUdf, Nullable, int32_t, Nullable>( +// "array_padding", "[1,2,3,4,5,6,7,8,9,10]", "[1,2,3,4,5,6,7,8,9,10]", 2, 1); +// CheckUdf, Nullable, int32_t, Nullable>( +// "array_padding", "[]", "[1,2,3,4,5,6,7,8,9,10]", -1, 2); +// } + } // namespace codegen } // namespace hybridse diff --git a/hybridse/src/udf/default_defs/array_def.cc b/hybridse/src/udf/default_defs/array_def.cc index 984de4be939..b7f41e621a3 100644 --- a/hybridse/src/udf/default_defs/array_def.cc +++ b/hybridse/src/udf/default_defs/array_def.cc @@ -227,10 +227,10 @@ void DefaultUdfLibrary::InitArrayUdfs() { @since 0.9.2 )"); - RegisterExternalTemplate<>("array_padding") - .returns>() + RegisterExternal>("array_padding") .return_by_arg(true) - .args_in, bool, int16_t, int32_t, int64_t, float, double, Timestamp, Date, StringRef>(reinterpret_cast(array_padding)) + .returns>() + .args() .doc(R"( @brief Expand the array to the specified length by padding the specified value. diff --git a/hybridse/src/udf/udf.cc b/hybridse/src/udf/udf.cc index 5ada426ea1b..0199c436111 100644 --- a/hybridse/src/udf/udf.cc +++ b/hybridse/src/udf/udf.cc @@ -1504,29 +1504,29 @@ 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_; - } - } - } -} +// 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 diff --git a/hybridse/src/udf/udf.h b/hybridse/src/udf/udf.h index 6924e9b13af..02424e4dc82 100644 --- a/hybridse/src/udf/udf.h +++ b/hybridse/src/udf/udf.h @@ -525,9 +525,43 @@ 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); +template +struct ArrayPadding { + + using Args = std::tuple, int32_t, T>; + + bool operator()(ArrayRef *arr, int32_t target_size, T default_value, ArrayRef *out, bool *is_null) { + if (target_size < 0) { + out->size = 0; + out->raw = nullptr; + out->nullables = nullptr; + } + else if (arr->size >= target_size) { + // v1::AllocManagedArray(out, arr->size); + out->size = arr->size; + for (int i=0; isize; ++i) { + out->raw[i] = arr->raw[i]; + out->nullables[i] = arr->nullables[i]; + } + } + else { + // v1::AllocManagedArray(out, target_size); + out->size = target_size; + for (int i = 0; i < target_size; ++i) { + if (i < arr->size) { + out->nullables[i] = arr->nullables[i]; + out->raw[i] = arr->raw[i]; + } else { + out->nullables[i] = false; + out->raw[i] = default_value; + // out->raw[i]->data_ = default_value.data_; + // out->raw[i]->size_ = default_value.size_; + } + } + } +}; +}; + } // namespace v1 /// \brief register native udf related methods into given UdfLibrary `lib`