Skip to content

GH-40431: [C++] Move key_hash/key_map/light_array related files to internal for prevent using by users - #40484

Merged
pitrou merged 4 commits into
apache:mainfrom
ZhangHuiGui:try-fix-40431
Apr 2, 2024
Merged

GH-40431: [C++] Move key_hash/key_map/light_array related files to internal for prevent using by users#40484
pitrou merged 4 commits into
apache:mainfrom
ZhangHuiGui:try-fix-40431

Conversation

@ZhangHuiGui

@ZhangHuiGuiZhangHuiGui commented Mar 12, 2024

Copy link
Copy Markdown
Contributor

Rationale for this change

These files expose implementation details and APIs that are not meant for third-party use. This PR explicitly marks them internal, which also avoids having them installed.

Are these changes tested?

By existing builds and tests.

Are there any user-facing changes?

No, except hiding some header files that were not supposed to be included externally.

@ZhangHuiGui

Copy link
Copy Markdown
ContributorAuthor

cc @kou , it's a temporary fix. And i haven't add ut for now. PTAL?

Comment threadcpp/src/arrow/compute/key_hash.cc Outdated
Comment on lines 480 to 481

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this for hash_temp_buf and null_hash_temp_buf?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Yes

Comment threadcpp/src/arrow/compute/key_hash.cc Outdated
Comment on lines 482 to 483

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this for null_indices_buf?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Yes

Comment threadcpp/src/arrow/compute/key_hash.cc Outdated
Comment on lines 477 to 485

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why do you want to do this in HashBatch() not HashMultiColumn() (that has a real allocation logic)?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Yes, the codes here are unreasonable. It's temporary codes and refactored.

Comment threadcpp/src/arrow/compute/key_hash.cc Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this safe?
I think that stack is invalid outside of this block.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Oh,,, i lost my mind!

Comment threadcpp/src/arrow/compute/key_hash.cc Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You want to pass nullptr for temp_stack for your use case, right?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Yes, i think if user want to use the HashBatch as an independent api and they may needn't care about the stack size.

Comment threadcpp/src/arrow/compute/key_hash.cc Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We have similar codes in Hashing64::HashBatch(). Can we avoid it?

Comment threadcpp/src/arrow/compute/util.h Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
constint64_tbuffer_size() const { returnbuffer_size_; }
int64_tbuffer_size() const { returnbuffer_size_; }

Comment threadcpp/src/arrow/compute/util.h Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we provide RequiredSize(), EstimateSize() or something instead of providing this?

@github-actionsgithub-actionsBot added awaiting changes Awaiting changes awaiting change review Awaiting change review and removed awaiting review Awaiting review awaiting changes Awaiting changes labels Mar 12, 2024
Comment threadcpp/src/arrow/compute/util.h Outdated

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

We have already added the kPadding in PaddedAllocationSize, it's unnecessary to add it again.

@ZhangHuiGui

Copy link
Copy Markdown
ContributorAuthor

@kou how about this refactor?

Comment threadcpp/src/arrow/compute/key_hash.cc Outdated
Comment on lines 386 to 387

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we use auto here?

Suggested change
constuint32_t alloc_batch_size = std::min(num_rows, max_batch_size);
constint64_t estimate_alloc_size = EstimateBatchStackSize<uint32_t>(alloc_batch_size);
constauto alloc_batch_size = std::min(num_rows, max_batch_size);
constauto estimate_alloc_size = EstimateBatchStackSize<uint32_t>(alloc_batch_size);

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

done

Comment threadcpp/src/arrow/compute/key_hash.cc Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could you set nullptr to ctx->stack before this function is exited?

Comment threadcpp/src/arrow/compute/key_hash.cc Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could you revert a needless change?

Comment threadcpp/src/arrow/compute/util.cc Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could you move this comment to CheckAllocSizeValid()?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

done

Comment threadcpp/src/arrow/compute/util.cc Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could you return arrow::Status instead of void here?

TempVectorStack::alloc() will not be able to use it for now but Hashing32::HashBatch() can use it.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Yes, reasonable. Refactored!

Comment threadcpp/src/arrow/compute/util.cc Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think that we should receive additional allocation size instead of total new allocation size here:

Suggested change
ARROW_DCHECK_LE(estimate_alloc_size, buffer_size_)
ARROW_DCHECK_LE(top_ + alloc_size, buffer_size_)

Comment threadcpp/src/arrow/compute/util.h Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we use EstimateAllocSize() here?

Comment threadcpp/src/arrow/compute/util.h Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we need this?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
constint32_t batch_size = static_cast<int32_t>(arr->length());
constauto batch_size = static_cast<int32_t>(arr->length());

Comment threadcpp/src/arrow/compute/key_hash.h Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we need to export this?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Actually, i want to unify the logic in Hashing32 and Hashing64. But seems unnecessary.

@github-actionsgithub-actionsBot added awaiting changes Awaiting changes and removed awaiting change review Awaiting change review labels Mar 25, 2024
@github-actionsgithub-actionsBot added awaiting change review Awaiting change review and removed awaiting changes Awaiting changes labels Mar 26, 2024
@ZhangHuiGui

Copy link
Copy Markdown
ContributorAuthor

@kou Thank you for your review!
@westonpace PTAL? This is a refactoring job, in order to allow users in need to better use the HashBatch related API.

@mapleFU

Copy link
Copy Markdown
Member

cc @zanmato1984 if you're interested in this

@zanmato1984zanmato1984 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The idea of generalizing the hashing APIs is nice. Some suggestions.

Comment threadcpp/src/arrow/compute/util.cc Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We probably should use ARROW_CHECK_OK here?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Yes, you're right. ARROW_DCHECK_OK seems not work in NDEBUG mode.

Comment threadcpp/src/arrow/compute/util.h Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe we can align all the function names to either XxxAllocSize or XxxAllocationSize?

Comment threadcpp/src/arrow/compute/key_hash.cc Outdated
Comment on lines 385 to 386

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe it can be more clear if we combine these two lines to const uint32_t max_batch_size = std::min(num_rows, util::MiniBatch::kMiniBatchLength);.

Comment threadcpp/src/arrow/compute/key_hash.cc Outdated
Comment on lines 390 to 394

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The scope of these three variables doesn't have to be this function, right? We can put them into the if statement below?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Actually the alloc_size is used both in if and else statement, it's not suitable to move these three variables into if statement.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You are right, thanks.

Comment threadcpp/src/arrow/compute/key_hash.cc Outdated

@zanmato1984zanmato1984Mar 26, 2024

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
std::shared_ptr<util::TempVectorStack> temp_stack(nullptr);
auto stack = ctx->stack;
std::unique_ptr<util::TempVectorStack> temp_stack(nullptr);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Point is you don't really have to set the temp_stack pointer into the ctx. Just a regular temp variable will do. So you don't have to clear ctx->stack at the end.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Yes, totally agree!

Comment threadcpp/src/arrow/compute/key_hash.cc Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
RETURN_NOT_OK(ctx->stack->CheckAllocOverflow(alloc_size));
RETURN_NOT_OK(stack->CheckAllocOverflow(alloc_size));

Comment threadcpp/src/arrow/compute/key_hash.cc Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
auto hash_temp_buf = util::TempVectorHolder<uint32_t>(ctx->stack, alloc_batch_size);
auto hash_temp_buf = util::TempVectorHolder<uint32_t>(stack, alloc_batch_size);

Comment threadcpp/src/arrow/compute/key_hash.cc Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
auto null_indices_buf = util::TempVectorHolder<uint16_t>(ctx->stack, alloc_batch_size);
auto null_indices_buf = util::TempVectorHolder<uint16_t>(stack, alloc_batch_size);

Comment threadcpp/src/arrow/compute/key_hash.cc Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
util::TempVectorHolder<uint32_t>(ctx->stack, alloc_batch_size);
util::TempVectorHolder<uint32_t>(stack, alloc_batch_size);

Comment threadcpp/src/arrow/compute/key_hash.cc Outdated
Comment on lines 483 to 485

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (temp_stack) {
ctx->stack = nullptr;
}

Comment threadcpp/src/arrow/compute/key_hash.cc Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
temp_stack = std::make_shared<util::TempVectorStack>();
temp_stack = std::make_unique<util::TempVectorStack>();

@ZhangHuiGui

Copy link
Copy Markdown
ContributorAuthor

@zanmato1984 Thank you very much for your suggestion, the code looks clearer!

Comment threadcpp/src/arrow/compute/key_hash.cc Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would suggest keeping the name max_batch_size. It carries the meaning of how many rows to process in each iteration. In addition, this name is used everywhere in hash join related code so keeping it may complies with existing code base more. Last, it doesn't seem to be in the same category of the following three alloc family variables - we can think any alloc variable is solely to make sure the stack is large enough.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe we can use ASSERT_RAISES_WITH_MESSAGE to check the detailed error message.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Yes, i've considered this. But the message has some detail numbers which related with internal alloc size. This is not convenient for future maintenance (for example, if some variables that require stack allocation are removed in HashMultiColumn, this test will need to be modified).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yeah, you are right. Thanks.

@zanmato1984zanmato1984 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Some minor suggestions.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
//alloc stack by HashBatch internal
//HashBatch using internally allocated buffer.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
//alloc stack overflow in HashBatch
//HashBatch using pre-allocated buffer of insufficient size raises stack overflow.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
//alloc stack normally in HashBatch
//HashBatch using big enough pre-allocated buffer.

@zanmato1984zanmato1984 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

My one last suggestion :)

Comment threadcpp/src/arrow/compute/util.cc Outdated
Comment on lines 64 to 65

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
return Status::Invalid("TempVectorStack alloc overflow. (Actual ", buffer_size_,
"Bytes, expect ", alloc_size, "Bytes)");
return Status::Invalid("TempVectorStack allocation overflow: capacity ", buffer_size_, ", current size ", top, ", attempt allocating ", alloc_size);

@zanmato1984zanmato1984 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

My one last suggestion :)

@ZhangHuiGui

Copy link
Copy Markdown
ContributorAuthor

My one last suggestion :)

Thanks!

@github-actionsgithub-actionsBot added the awaiting committer review Awaiting committer review label Mar 28, 2024
Comment threadcpp/src/arrow/compute/key_hash.cc Outdated
Comment on lines 400 to 405

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If there is a possibility that ctx->stack is nullptr, then it's better to declare a TempVectorStack * parameter explicitly so the caller can allocate a stack with the right memory pool instead of this function internally relying on the global default_memory_pool(). Most calls would be passing ctx, ctx->stack except for the ones that for some reason don't have a stack in the context.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Thanks, you're right. HashBatch is used internally the way you said!

@github-actionsgithub-actionsBot added awaiting changes Awaiting changes and removed awaiting committer review Awaiting committer review labels Mar 28, 2024
@github-actionsgithub-actionsBot added awaiting change review Awaiting change review and removed awaiting changes Awaiting changes labels Mar 30, 2024
@ZhangHuiGuiZhangHuiGui changed the title GH-40431: [C++] Try to check/alloc the TempVectorStack size as HashBatch neededGH-40431: [C++] Move key_hash/key_map/light_array related files to internal for prevent using by usersMar 30, 2024
@ZhangHuiGui

Copy link
Copy Markdown
ContributorAuthor

@pitrou PTAL!
The new commit include two things:

  1. Move these codes(key_hash/key_map/light_array) to internal! Besides, seems it's unnecessary to use internal namespace for them. The purpose we want is just prevent user's calling/
  2. Simple refactor to simplify some codes in TempVectorStack.

@westonpacewestonpace left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I agree these files were meant to be internal. Thanks for cleaning this up :)

@github-actionsgithub-actionsBot added awaiting merge Awaiting merge and removed awaiting change review Awaiting change review labels Apr 2, 2024
@pitrou

Copy link
Copy Markdown
Member

@github-actions crossbow submit -g cpp

Comment threadcpp/src/arrow/acero/schema_util.h Outdated
@github-actions

This comment was marked as outdated.

@pitrou

Copy link
Copy Markdown
Member

@github-actions crossbow submit -g cpp

@github-actions

This comment was marked as outdated.

@pitrou

Copy link
Copy Markdown
Member

@github-actions crossbow submit -g cpp

@github-actions

Copy link
Copy Markdown

Revision: 0722f88

Submitted crossbow builds: ursacomputing/crossbow @ actions-5141a1fc14

TaskStatus
test-alpine-linux-cppGitHub Actions
test-build-cpp-fuzzGitHub Actions
test-conda-cppGitHub Actions
test-conda-cpp-valgrindAzure
test-cuda-cppGitHub Actions
test-debian-12-cpp-amd64GitHub Actions
test-debian-12-cpp-i386GitHub Actions
test-fedora-39-cppGitHub Actions
test-ubuntu-20.04-cppGitHub Actions
test-ubuntu-20.04-cpp-bundledGitHub Actions
test-ubuntu-20.04-cpp-minimal-with-formatsGitHub Actions
test-ubuntu-20.04-cpp-thread-sanitizerGitHub Actions
test-ubuntu-22.04-cppGitHub Actions
test-ubuntu-22.04-cpp-20GitHub Actions
test-ubuntu-22.04-cpp-no-threadingGitHub Actions
test-ubuntu-24.04-cppGitHub Actions
test-ubuntu-24.04-cpp-gcc-14GitHub Actions

@pitrou

Copy link
Copy Markdown
Member

CI failures are unrelated, I'll merge.

@conbench-apache-arrow

Copy link
Copy Markdown

After merging your PR, Conbench analyzed the 6 benchmarking runs that have been run so far on merge-commit 8163d02.

There were no benchmark performance regressions. 🎉

The full Conbench report has more details. It also includes information about 14 possible false positives for unstable benchmarks that are known to sometimes produce them.

vibhatha pushed a commit to vibhatha/arrow that referenced this pull request May 25, 2024
… to internal for prevent using by users (apache#40484)
### Rationale for this change
These files expose implementation details and APIs that are not meant for third-party use. This PR explicitly marks them internal, which also avoids having them installed.
### Are these changes tested?
By existing builds and tests.
### Are there any user-facing changes?
No, except hiding some header files that were not supposed to be included externally.
* GitHub Issue: apache#40431
Lead-authored-by: ZhangHuiGui <hugo.zhang@openpie.com>
Co-authored-by: Antoine Pitrou <antoine@python.org>
Signed-off-by: Antoine Pitrou <antoine@python.org>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

@ZhangHuiGui@mapleFU@pitrou@kou@felipecrv@zanmato1984@westonpace