-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
GH-45167: [C++] Implement Compute Equals for List Types #45272
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -141,6 +141,13 @@ struct GetViewType<Type, enable_if_t<is_base_binary_type<Type>::value || | |
static T LogicalValue(PhysicalType value) { return value; } | ||
}; | ||
|
||
template <typename Type> | ||
struct GetViewType<Type, enable_if_list_type<Type>> { | ||
using T = typename TypeTraits<Type>::ScalarType; | ||
|
||
static T LogicalValue(T value) { return value; } | ||
}; | ||
|
||
template <> | ||
struct GetViewType<Decimal32Type> { | ||
using T = Decimal32; | ||
|
@@ -322,6 +329,26 @@ struct ArrayIterator<Type, enable_if_base_binary<Type>> { | |
} | ||
}; | ||
|
||
template <typename Type> | ||
struct ArrayIterator<Type, enable_if_list_type<Type>> { | ||
using T = typename TypeTraits<Type>::ScalarType; | ||
using ArrayT = typename TypeTraits<Type>::ArrayType; | ||
using offset_type = typename Type::offset_type; | ||
|
||
const ArraySpan& arr; | ||
int64_t position; | ||
|
||
explicit ArrayIterator(const ArraySpan& arr) : arr(arr), position(0) {} | ||
|
||
T operator()() { | ||
const auto array_ptr = arr.ToArray(); | ||
const auto array = checked_cast<const ArrayT*>(array_ptr.get()); | ||
|
||
T result{array->value_slice(position++)}; | ||
return result; | ||
} | ||
}; | ||
|
||
template <> | ||
struct ArrayIterator<FixedSizeBinaryType> { | ||
const ArraySpan& arr; | ||
|
@@ -390,6 +417,12 @@ struct UnboxScalar<Type, enable_if_has_string_view<Type>> { | |
} | ||
}; | ||
|
||
template <typename Type> | ||
struct UnboxScalar<Type, enable_if_list_type<Type>> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So fixed_size_list is also declared as scalar, but not being registered in compute? ( It's ok to me, just to make sure this) |
||
using T = typename TypeTraits<Type>::ScalarType; | ||
static const T& Unbox(const Scalar& val) { return checked_cast<const T&>(val); } | ||
}; | ||
|
||
template <> | ||
struct UnboxScalar<Decimal32Type> { | ||
using T = Decimal32; | ||
|
@@ -1383,6 +1416,22 @@ ArrayKernelExec GenerateDecimal(detail::GetTypeId get_id) { | |
} | ||
} | ||
|
||
// Generate a kernel given a templated functor for list types | ||
// | ||
// See "Numeric" above for description of the generator functor | ||
template <template <typename...> class Generator, typename Type0, typename... Args> | ||
ArrayKernelExec GenerateList(detail::GetTypeId get_id) { | ||
switch (get_id.id) { | ||
case Type::LIST: | ||
return Generator<Type0, ListType, Args...>::Exec; | ||
case Type::LARGE_LIST: | ||
return Generator<Type0, LargeListType, Args...>::Exec; | ||
default: | ||
DCHECK(false); | ||
return nullptr; | ||
} | ||
} | ||
|
||
// END of kernel generator-dispatchers | ||
// ---------------------------------------------------------------------- | ||
// BEGIN of DispatchBest helpers | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -445,6 +445,14 @@ std::shared_ptr<ScalarFunction> MakeCompareFunction(std::string name, FunctionDo | |
DCHECK_OK(func->AddKernel({ty, ty}, boolean(), std::move(exec))); | ||
} | ||
|
||
if constexpr (std::is_same_v<Op, Equal> || std::is_same_v<Op, NotEqual>) { | ||
for (const auto id : {Type::LIST, Type::LARGE_LIST}) { | ||
auto exec = GenerateList<applicator::ScalarBinaryEqualTypes, BooleanType, Op>(id); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another approach with perhaps a better performance potential would be to leverage the existing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the heads up - I will give that a look. So I see all of the functions right now in the compare module are registered via There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also I'm guessing the FWIW though I did benchmark the current implementation and it was definitely slow. Seemed about 1000x slower than an equivalent comparison using primitive types There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think we can avoid that by directly calling into
A list scalar's value is actually an array, so that should not necessarily be a problem. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK took a closer look at this. So AFAICT the Are you thinking we should refactor the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks again for the guidance and patience here! Trying to wrap my head around the structure of the compute modules There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That's right, so it would need to be called once for each list element (which is admittedly non optimal, but probably better than using
Well, we could add a suitable entrypoint in Another possible approach would be to leverage the comparison kernel for the child type, but that would probably be even more involved. So that's up to how much work you want to put into this :) |
||
DCHECK_OK( | ||
func->AddKernel({InputType(id), InputType(id)}, boolean(), std::move(exec))); | ||
} | ||
} | ||
|
||
return func; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The alternative to calling
ToArray
with the cast would be to implement something likevalue_slice
on theArraySpan
directly, although I'm not sure if theArraySpan
is supposed to return anything but pointers to primitives (as is currently implemeted)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is going to be slow, so we probably want to avoid this IMHO.
You may want to run a crude benchmark from Python to check this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about:
ListScalar
/LargeListScalar
from the child array?Or materialize the child array, and using the sub array.
arr.ToArray()
every call is too expansive?