Skip to content

GH-46791: [C++] Add Status::OrElse, IntoStatus<T> and ToStatus #46792

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

Merged
merged 1 commit into from
Jun 30, 2025

Conversation

pitrou
Copy link
Member

@pitrou pitrou commented Jun 12, 2025

Rationale for this change

In #46711 (comment) it was mentioned that the macro RETURN_NOT_OK_ELSE is confusing and can easily be misunderstood. We would like a better way to conditionally chain error-handling code if a Status does not indicate success.

What changes are included in this PR?

  1. Add a type trait IntoStatus<T> that can be implemented to provide conversions from other error-like types
  2. Add a global ToStatus function that calls the aforementioned type trait
  3. Add a Status::OrElse method that calls a functor on error
  4. Remove the RETURN_NOT_OK_ELSE macro

Are these changes tested?

Yes.

Are there any user-facing changes?

No, the RETURN_NOT_OK_ELSE was not supposed to be called by third-party code as it's not prefixed with ARROW_.

Copy link

⚠️ GitHub issue #46791 has been automatically assigned in GitHub to PR creator.

@pitrou
Copy link
Member Author

pitrou commented Jun 12, 2025

@zanmato1984 @bkietz @paleolimbot What do you think?

@pitrou
Copy link
Member Author

pitrou commented Jun 12, 2025

Ah, clang is being picky.


/Users/runner/work/arrow/arrow/cpp/src/arrow/status.h:363:16: error: cannot pass object of non-trivial type 'arrow::Status' through variadic method; call will abort at runtime [-Wnon-pod-varargs]
      on_error(*this);
               ^
/Users/runner/work/arrow/arrow/cpp/src/parquet/arrow/writer.cc:392:31: note: in instantiation of function template specialization 'arrow::Status::OrElse<(lambda at /Users/runner/work/arrow/arrow/cpp/src/parquet/arrow/writer.cc:391:7)>' requested here
          WriteRowGroup(0, 0).OrElse([&](...) { PARQUET_IGNORE_NOT_OK(Close()); }));
                              ^

@pitrou pitrou force-pushed the status-or-else branch 3 times, most recently from 1c2266a to e2cd325 Compare June 12, 2025 10:43
@pitrou pitrou marked this pull request as ready for review June 12, 2025 11:29
@pitrou pitrou requested review from AlenkaF, raulcd, rok, wgtmac and kou as code owners June 12, 2025 11:29
Copy link
Member

@paleolimbot paleolimbot left a comment

Choose a reason for hiding this comment

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

These changes seems reasonable to me! The behaviour seems to line up exactly with Rust's .or_else() and I agree the macro was difficult to understand. The general maintenance around status handling is also great!

@github-actions github-actions bot added awaiting review Awaiting review awaiting merge Awaiting merge and removed awaiting review Awaiting review labels Jun 12, 2025
@pitrou
Copy link
Member Author

pitrou commented Jun 12, 2025

The behaviour seems to line up exactly with Rust's .or_else()

You can guess it's intended :)

Copy link
Contributor

@zanmato1984 zanmato1984 left a comment

Choose a reason for hiding this comment

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

This looks nice! A few nits.

@github-actions github-actions bot added awaiting review Awaiting review awaiting committer review Awaiting committer review and removed awaiting review Awaiting review awaiting merge Awaiting merge labels Jun 13, 2025
Copy link
Member

@bkietz bkietz left a comment

Choose a reason for hiding this comment

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

LGTM, just some nits and an idea for an alternate approach

@github-actions github-actions bot added awaiting changes Awaiting changes and removed awaiting committer review Awaiting committer review labels Jun 16, 2025
@AlenkaF AlenkaF removed their request for review June 24, 2025 07:59
@github-actions github-actions bot added awaiting change review Awaiting change review and removed awaiting changes Awaiting changes labels Jun 26, 2025
@pitrou pitrou changed the title GH-46791: [C++] Add Status::OrElse GH-46791: [C++] Add Status::OrElse, IntoStatus<T> and ToStatus Jun 26, 2025
@pitrou
Copy link
Member Author

pitrou commented Jun 26, 2025

@github-actions crossbow submit -g cpp

This comment was marked as outdated.

@pitrou
Copy link
Member Author

pitrou commented Jun 26, 2025

@github-actions crossbow submit -g cpp

Copy link

Revision: 65723c8

Submitted crossbow builds: ursacomputing/crossbow @ actions-dcfad22df7

Task Status
example-cpp-minimal-build-static GitHub Actions
example-cpp-minimal-build-static-system-dependency GitHub Actions
example-cpp-tutorial GitHub Actions
test-build-cpp-fuzz GitHub Actions
test-conda-cpp GitHub Actions
test-conda-cpp-valgrind GitHub Actions
test-cuda-cpp-ubuntu-22.04-cuda-11.7.1 GitHub Actions
test-debian-12-cpp-amd64 GitHub Actions
test-debian-12-cpp-i386 GitHub Actions
test-fedora-39-cpp GitHub Actions
test-ubuntu-22.04-cpp GitHub Actions
test-ubuntu-22.04-cpp-20 GitHub Actions
test-ubuntu-22.04-cpp-bundled GitHub Actions
test-ubuntu-22.04-cpp-emscripten GitHub Actions
test-ubuntu-22.04-cpp-no-threading GitHub Actions
test-ubuntu-24.04-cpp GitHub Actions
test-ubuntu-24.04-cpp-bundled-offline GitHub Actions
test-ubuntu-24.04-cpp-gcc-13-bundled GitHub Actions
test-ubuntu-24.04-cpp-gcc-14 GitHub Actions
test-ubuntu-24.04-cpp-minimal-with-formats GitHub Actions
test-ubuntu-24.04-cpp-thread-sanitizer GitHub Actions

@pitrou
Copy link
Member Author

pitrou commented Jun 26, 2025

@zanmato1984 @paleolimbot @bkietz I've tried to address all your comments and suggestions, can you take a look again?

Copy link
Member

@paleolimbot paleolimbot left a comment

Choose a reason for hiding this comment

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

This is a nice improvement on the ergonomics of working with the Status! I took a look through for anything out of place although some of the C++ metaprogramming is over my head here 🙂

@github-actions github-actions bot added awaiting merge Awaiting merge and removed awaiting change review Awaiting change review labels Jun 28, 2025
Copy link
Contributor

@zanmato1984 zanmato1984 left a comment

Choose a reason for hiding this comment

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

LGTM

@pitrou pitrou merged commit 0140089 into apache:main Jun 30, 2025
38 of 39 checks passed
@pitrou pitrou removed the awaiting merge Awaiting merge label Jun 30, 2025
@pitrou pitrou deleted the status-or-else branch June 30, 2025 07:14
Copy link

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

There were no benchmark performance regressions. 🎉

The full Conbench report has more details.

Sign up for free to 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.

4 participants