Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/array padding #3977

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
)");

RegisterExternal<v1::ArrayPadding<int32_t>>("array_padding")
.return_by_arg(true)
.returns<ArrayRef<int32_t>>()
.args<int32_t>()
.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
38 changes: 38 additions & 0 deletions hybridse/src/udf/udf.h
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,44 @@ 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>
struct ArrayPadding {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[cpplint] reported by reviewdog 🐶
Redundant blank line at the start of a code block should be deleted. [whitespace/blank_line] [2]

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[cpplint] reported by reviewdog 🐶
An else should appear on the same line as the preceding } [whitespace/newline] [4]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[cpplint] reported by reviewdog 🐶
If an else has a brace on one side, it should have it on both [readability/braces] [5]

// v1::AllocManagedArray(out, arr->size);
out->size = arr->size;
for (int i=0; i<arr->size; ++i) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[cpplint] reported by reviewdog 🐶
Missing spaces around < [whitespace/operators] [3]

out->raw[i] = arr->raw[i];
out->nullables[i] = arr->nullables[i];
}
}
else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[cpplint] reported by reviewdog 🐶
An else should appear on the same line as the preceding } [whitespace/newline] [4]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[cpplint] reported by reviewdog 🐶
If an else has a brace on one side, it should have it on both [readability/braces] [5]

// 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_;
}
}
}
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[cpplint] reported by reviewdog 🐶
You don't need a ; after a } [readability/braces] [4]

};

} // namespace v1

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