Skip to content

Commit

Permalink
feat(udf): isin
Browse files Browse the repository at this point in the history
  • Loading branch information
aceforeverd committed May 27, 2024
1 parent 59d79f6 commit 66ff6a3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
19 changes: 19 additions & 0 deletions cases/query/udf_query.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,25 @@ cases:
data: |
true, true, false, false, true, false, true, false, true, false, true
- id: isin
mode: request-unsupport
inputs:
- name: t1
columns: ["col1:int32", "std_ts:timestamp", "col2:string"]
indexs: ["index1:col1:std_ts"]
rows:
- [1, 1590115420001, "ABCabcabc"]
sql: |
select
isin(2, [2,2]) as c0,
isin(cast(3 as int64), ARRAY<INT64>[NULL, 1, 2]) as c1
expect:
columns:
- c0 bool
- c1 bool
data: |
true, false
- id: array_split
mode: request-unsupport
inputs:
Expand Down
41 changes: 39 additions & 2 deletions hybridse/src/udf/default_defs/array_def.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,30 @@ struct ArrayContains {
// - bool/intxx/float/double -> bool/intxx/float/double
// - Timestamp/Date/StringRef -> Timestamp*/Date*/StringRef*
bool operator()(ArrayRef<T>* arr, ParamType v, bool is_null) {
// NOTE: array_contains([null], null) returns null
// this might not expected
for (uint64_t i = 0; i < arr->size; ++i) {
if constexpr (std::is_pointer_v<ParamType>) {
// null or same value returns true
if ((is_null && arr->nullables[i]) || (!arr->nullables[i] && *arr->raw[i] == *v)) {
return true;
}
} else {
if ((is_null && arr->nullables[i]) || (!arr->nullables[i] && arr->raw[i] == v)) {
return true;
}
}
}
return false;
}
};

template <typename T>
struct IsIn {
// udf registry types
using Args = std::tuple<Nullable<T>, ArrayRef<T>>;

using ParamType = typename DataTypeTrait<T>::CCallArgType;

bool operator()(ParamType v, bool is_null, ArrayRef<T>* arr) {
for (uint64_t i = 0; i < arr->size; ++i) {
if constexpr (std::is_pointer_v<ParamType>) {
// null or same value returns true
Expand Down Expand Up @@ -98,6 +120,21 @@ void DefaultUdfLibrary::InitArrayUdfs() {
@since 0.7.0
)");

RegisterExternalTemplate<IsIn>("isin")
.args_in<bool, int16_t, int32_t, int64_t, float, double, Timestamp, Date, StringRef>()
.doc(R"(
@brief isin(value, array) - Returns true if the array contains the value.
Example:
@code{.sql}
select isin(2, [2,2]) as c0;
-- output true
@endcode
@since 0.9.1
)");

RegisterExternal("split_array")
.returns<ArrayRef<StringRef>>()
.return_by_arg(true)
Expand Down

0 comments on commit 66ff6a3

Please sign in to comment.