Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
QiChenX committed Aug 9, 2024
1 parent 4fc33b9 commit 6d951cd
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
14 changes: 14 additions & 0 deletions hybridse/src/udf/default_defs/array_def.cc
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,20 @@ void DefaultUdfLibrary::InitArrayUdfs() {
@endcode
@since 0.9.2
)");

RegisterUdfTemplate("array_padding")
.returns<ArrayRef<T>>()
.return_by_arg(true)
.args_in<ArrayRef<T>, bool, int16_t, int32_t, int64_t, float, double, Timestamp, Date, StringRef>(reinterpret_cast<void*>(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
24 changes: 24 additions & 0 deletions hybridse/src/udf/udf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1504,6 +1504,30 @@ void array_combine(codec::StringRef *del, int32_t cnt, ArrayRef<codec::StringRef
out->size = real_sz;
}

void array_padding(ArrayRef<T> *arr, int32_t target_size, T default_value,
ArrayRef<T> *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,
Expand Down
4 changes: 4 additions & 0 deletions hybridse/src/udf/udf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<codec::StringRef> **data,
ArrayRef<codec::StringRef> *out);

template<typename T>
void array_padding(ArrayRef<T> *arr, int32_t target_size, T default_value,
ArrayRef<T> *out);
} // namespace v1

/// \brief register native udf related methods into given UdfLibrary `lib`
Expand Down

0 comments on commit 6d951cd

Please sign in to comment.