arrow icon indicating copy to clipboard operation
arrow copied to clipboard

GH-46791: [C++] Add `Status::OrElse`

Open pitrou opened this issue 6 months ago • 4 comments

Rationale for this change

In https://github.com/apache/arrow/pull/46711#discussion_r2139246309 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 global ToArrowStatus function to allow registering third-party conversions to Status
  2. Add a Status::OrElse method that calls a functor on error
  3. 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_.

  • GitHub Issue: #46791

pitrou avatar Jun 12 '25 09:06 pitrou

:warning: GitHub issue #46791 has been automatically assigned in GitHub to PR creator.

github-actions[bot] avatar Jun 12 '25 09:06 github-actions[bot]

@zanmato1984 @bkietz @paleolimbot What do you think?

pitrou avatar Jun 12 '25 09:06 pitrou

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 avatar Jun 12 '25 09:06 pitrou

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

You can guess it's intended :)

pitrou avatar Jun 12 '25 16:06 pitrou

@github-actions crossbow submit -g cpp

pitrou avatar Jun 26 '25 09:06 pitrou

Revision: 7a16cd01469da210e59984180231501683a76910

Submitted crossbow builds: ursacomputing/crossbow @ actions-500c512008

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

github-actions[bot] avatar Jun 26 '25 09:06 github-actions[bot]

@github-actions crossbow submit -g cpp

pitrou avatar Jun 26 '25 09:06 pitrou

Revision: 65723c88fbc84c38cbfbde95a029c97209234a90

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

github-actions[bot] avatar Jun 26 '25 09:06 github-actions[bot]

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

pitrou avatar Jun 26 '25 09:06 pitrou

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

There were no benchmark performance regressions. 🎉

The full Conbench report has more details.