-
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?
Conversation
|
573867e
to
649e3b0
Compare
649e3b0
to
68bb513
Compare
explicit ArrayIterator(const ArraySpan& arr) : arr(arr), position(0) {} | ||
|
||
T operator()() { | ||
const auto array_ptr = arr.ToArray(); |
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 like value_slice
on the ArraySpan
directly, although I'm not sure if the ArraySpan
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:
- Get offset, length
- Subslice the value array
- Build
ListScalar
/LargeListScalar
from the child array?
Or materialize the child array, and using the sub array. arr.ToArray()
every call is too expansive?
@pitrou @jorisvandenbossche would either of you be able to take a look here? |
@@ -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 comment
The 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 RangeDataEqualsImpl
in arrow/compare.cc
.
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.
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 RegisterScalarComparison
. With what you are suggesting, I'm guessing I should be creating a new registry function along with that like RegisterRangeComparison
right?
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.
Also I'm guessing the RangeDataEqualsImpl
is supposed to work when comparing two arrays, but not when comparing an array with a scalar
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 comment
The reason will be displayed to describe this comment to others. Learn more.
With what you are suggesting, I'm guessing I should be creating a new registry function along with that like
RegisterRangeComparison
right?
I think we can avoid that by directly calling into RangeDataEqualsImpl
.
Also I'm guessing the RangeDataEqualsImpl is supposed to work when comparing two arrays, but not when comparing an array with a scalar
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 comment
The reason will be displayed to describe this comment to others. Learn more.
OK took a closer look at this. So AFAICT the RangeDataEqualsImpl
returns a scalar bool value, rather than an array of booleans like we would need in the result here. That class is also private to the compare.cc
module and doesn't expose any suitable entrypoint in compare.h
that I think would work here.
Are you thinking we should refactor the RangeDataEqualsImpl
to support vector functions and move it to make it accessible to the compute module, or do you think we should just create a dedicated class drawing some inspiration from it in scalar_compute.cc
?
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
So AFAICT the
RangeDataEqualsImpl
returns a scalar bool value, rather than an array of booleans like we would need in the result here.
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 GetScalar
anyway?).
That class is also private to the
compare.cc
module and doesn't expose any suitable entrypoint incompare.h
that I think would work here.
Well, we could add a suitable entrypoint in compare_internal.h
if that's useful.
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 :)
68bb513
to
3b055a8
Compare
I currently have no time to review this in depth, but API-wise one remark: right now (for primitive arrays), nulls propagate in an operation like |
The current (rather slow) implementation just does an elementwise compare, dispatching to the logical list scalar type. Therefore, since: >>> l1 = pa.scalar([], type=pa.list_(pa.int32()))
>>> l2 = pa.scalar([], type=pa.list_(pa.int32()))
>>> l1 == l2
True Wrapping that in an array does not change the behavior: >>> arr1 = pa.array([l1])
>>> arr2 = pa.array([l2])
>>> pc.equal(arr1, arr2)
<pyarrow.lib.BooleanArray object at 0x71e8e9232560>
[
true
] |
3b055a8
to
ba3b114
Compare
@@ -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 comment
The 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)
explicit ArrayIterator(const ArraySpan& arr) : arr(arr), position(0) {} | ||
|
||
T operator()() { | ||
const auto array_ptr = arr.ToArray(); |
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:
- Get offset, length
- Subslice the value array
- Build
ListScalar
/LargeListScalar
from the child array?
Or materialize the child array, and using the sub array. arr.ToArray()
every call is too expansive?
Rationale for this change
While equality exists for ListScalars, it is not available through the compute module. This makes that now possible.
What changes are included in this PR?
I have added equals and not_equals support to the compute module for list types
Are these changes tested?
Yes - see added changes
Are there any user-facing changes?
Yes - the new feature to allow list comparison through the compute module