Skip to content

Commit

Permalink
0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
QiChenX committed Aug 15, 2024
1 parent 0800da3 commit 58c4c8c
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 29 deletions.
10 changes: 10 additions & 0 deletions hybridse/src/codegen/udf_ir_builder_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1599,6 +1599,16 @@ TEST_F(UdfIRBuilderTest, GetJsonObject) {
}
}

// array_padding normal check
// TEST_F(UdfIRBuilderTest, ArrayPadding) {
// CheckUdf<Nullable<StringRef>, Nullable<StringRef>, int32_t, Nullable<StringRef>>(
// "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<StringRef>, Nullable<StringRef>, int32_t, Nullable<StringRef>>(
// "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<StringRef>, Nullable<StringRef>, int32_t, Nullable<StringRef>>(
// "array_padding", "[]", "[1,2,3,4,5,6,7,8,9,10]", -1, 2);
// }

} // namespace codegen
} // namespace hybridse

Expand Down
6 changes: 3 additions & 3 deletions hybridse/src/udf/default_defs/array_def.cc
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,10 @@ void DefaultUdfLibrary::InitArrayUdfs() {
@since 0.9.2
)");

RegisterExternalTemplate<>("array_padding")
.returns<ArrayRef<T>>()
RegisterExternal<v1::ArrayPadding<int32_t>>("array_padding")
.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))
.returns<ArrayRef<int32_t>>()
.args<int32_t>()
.doc(R"(
@brief Expand the array to the specified length by padding the specified value.
Expand Down
46 changes: 23 additions & 23 deletions hybridse/src/udf/udf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1504,29 +1504,29 @@ 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_;
}
}
}
}
// 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

Expand Down
40 changes: 37 additions & 3 deletions hybridse/src/udf/udf.h
Original file line number Diff line number Diff line change
Expand Up @@ -525,9 +525,43 @@ 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);
template <typename T>
struct ArrayPadding {

using Args = std::tuple<ArrayRef<T>, int32_t, T>;

bool operator()(ArrayRef<T> *arr, int32_t target_size, T default_value, ArrayRef<T> *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; i<arr->size; ++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`
Expand Down

0 comments on commit 58c4c8c

Please sign in to comment.