-
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-33592: [C++] support casting nullable fields to non-nullable if there are no null values #43782
base: main
Are you sure you want to change the base?
GH-33592: [C++] support casting nullable fields to non-nullable if there are no null values #43782
Conversation
Thanks for opening a pull request! If this is not a minor PR. Could you open an issue for this pull request on GitHub? https://github.com/apache/arrow/issues/new/choose Opening GitHub issues ahead of time contributes to the Openness of the Apache Arrow project. Then could you also rename the pull request title in the following format?
or
In the case of PARQUET issues on JIRA the title also supports:
See also: |
|
52100dd
to
49d5a99
Compare
This PR looks pretty good to me, what additional help would you like getting it merged? |
Thanks for the help! I'm a random contributor, so I'm not sure about the exact workflow, but I think both getting the CI run approved and having a C++ owner approve it are needed. Any more detailed thoughts on if the tests are adequate, if there's anywhere else in the code base that you think needs to change, my handling of the go implementation by just deleting the shortcircuit, or any other more detailed thoughts? Basically anything that would reduce the mental load on the code owner I think would increase the odds they approve it :) |
You can mark as ready for review? Or this is still wip? |
Oops, didn't mean for this to still be marked WIP. Looks like a few failures that need to get fixed, but still a high-level review of the general approach would still be appreciated in the meantime. |
You probably need the same corresponding check on the Go side to verify that it's only allowed if there are no nulls. Also, the Go implementation has been moved to the apache/arrow-go repository. So please file the PR there for the Go side. Sorry for the confusion, we just haven't removed the Go code from here yet. |
Thanks @zeroshade , will do. Do we need both PRs to land at about the same time, or can I do that independently? I will undo my changes to the go code here, leaving it untouched. |
They can land independently, no issues there. Thanks! |
49d5a99
to
eb9e7b6
Compare
Just pushed a new version:
We will see if this passes CI now... |
eb9e7b6
to
e6d54d3
Compare
Pushed a new version, hopefully that fixed the broken tests:
|
@zeroshade I think this is ready to review/merge, the failing CI runs look like flakes when trying to setup the environment? |
Can we move this to apache/arrow-go? |
Ah, the Go part was removed from this PR. |
e6d54d3
to
106e627
Compare
Those CI failures look unrelated? Not sure, CI history looks pretty flaky to me.
|
workaround for: ttps://github.com/apache/arrow/pull/43782
f2e4079
to
fdb99d1
Compare
…o null values Fixes apache#33592
fdb99d1
to
1b2c940
Compare
The current CI failures look to be caused by #44985 (or similar), not by this PR. I rebased on main, everything looks pretty much the same as before. @zeroshade can you give this a review when you get the chance, or let me know how I can make it easier for you to do so? @rustyconover if you want to take a look again, maybe that will help. Thank you both! |
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.
A small question about null count checking.
if (field_index == kFillNullSentinel) { | ||
ARROW_ASSIGN_OR_RAISE(auto nulls, | ||
MakeArrayOfNull(target_type->GetSharedPtr(), batch.length)); | ||
out_array->child_data.push_back(nulls->data()); | ||
} else { | ||
const auto& values = (in_array.child_data[field_index].ToArrayData()->Slice( | ||
in_array.offset, in_array.length)); | ||
if (!target_field->nullable() && values->null_count > 0) { |
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.
Is it possible if values->null_count
is kUnknownNullCount
? In which case, we should probably use child array's GetNullCount()
instead?
And if we are not sure about that, can we have a dedicated test for it?
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.
I think values->null_count
can likely to be kUnknownNullCount
. values->null_count > 0
means must has null, however, when values->null_count = kUnknownNullCount`, it would be wrongly casted to new field?
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.
I have never heard of kUnknownNullCount. I'm not sure what we should do here, I think I need to know a lot more background. Can someone who has experience with null counts help?
Some questions this brings up:
- This sounds like we sometimes don't know the null count, I assume because it is expensive to compute. Would this be a good reason to actually force computation to happen (behavior A), or should we be conservative, and if we find kUnkownNullCount, then we assume that there could be nulls and don't let the cast happen and therefore error (behavior B). Behavior A seems much more correct to me.
- I don't think this function should have the responsibility of looking at the child arrays null counts. Should we add some higher level API to arrays like nullCountOrChildNullCount() that actually does this messy work??
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.
I have never heard of kUnknownNullCount. I'm not sure what we should do here, I think I need to know a lot more background. Can someone who has experience with null counts help?
Sorry I'm not sure if there is a well-documented rule of dealing with kUnknownNullCount
, maybe @pitrou does? But in practice, I (and many other developers I suppose) just keep in mind that the null count can be unknown and is lazily computed. One can enforce such computation once the null count is absolutely needed (there are various APIs helping on this).
- This sounds like we sometimes don't know the null count, I assume because it is expensive to compute. Would this be a good reason to actually force computation to happen (behavior A), or should we be conservative, and if we find kUnkownNullCount, then we assume that there could be nulls and don't let the cast happen and therefore error (behavior B). Behavior A seems much more correct to me.
Yeah your assumption is right. And I would go A (enforcing the null count computation) because IMO this is essential for what this PR is trying to do. Plus, null count is unknown by default, so B will make this change almost useless.
- I don't think this function should have the responsibility of looking at the child arrays null counts. Should we add some higher level API to arrays like nullCountOrChildNullCount() that actually does this messy work??
IMO this is where you must make contact with child arrays, given that you are in the context of dealing with a Struct
array - which is essentially a container of child arrays. And the original code already does so. Defining an API like nullCountOrChildNullCount()
could be semantically hard/useless because: 1) the validity (null or not) of the whole struct and 2) the validity of each individual field (and you have to specify which field), mean very different things. So the null count of a particular field is a property of this particular field only and should be computed/checked individually.
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.
You should just call GetNullCount
so that the null count is computed if not already known. Computation is reasonably cheap.
if (field_index == kFillNullSentinel) { | ||
ARROW_ASSIGN_OR_RAISE(auto nulls, | ||
MakeArrayOfNull(target_type->GetSharedPtr(), batch.length)); | ||
out_array->child_data.push_back(nulls->data()); | ||
} else { | ||
const auto& values = (in_array.child_data[field_index].ToArrayData()->Slice( | ||
in_array.offset, in_array.length)); | ||
if (!target_field->nullable() && values->null_count > 0) { | ||
return Status::TypeError("field '", target_field->name(), |
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.
Besides, should we discard the buffer[0]
if new data doesn't contains any nulls?
if (field_index == kFillNullSentinel) { | ||
ARROW_ASSIGN_OR_RAISE(auto nulls, | ||
MakeArrayOfNull(target_type->GetSharedPtr(), batch.length)); | ||
out_array->child_data.push_back(nulls->data()); | ||
} else { | ||
const auto& values = (in_array.child_data[field_index].ToArrayData()->Slice( | ||
in_array.offset, in_array.length)); | ||
if (!target_field->nullable() && values->null_count > 0) { | ||
return Status::TypeError("field '", target_field->name(), |
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.
Since this is data-dependent, this should be Status::Invalid
.
Notes for myself/fixer:
cmake .. --preset ninja-debug-basic
, thencmake --build .
, thenctest -j16 --output-on-failure
to run all tests, orctest -R 'arrow-compute-scalar-cast-test'
to run the specific one