Skip to content

Commit

Permalink
Fix out of memory errors causing logic_error when using ManagedRandom…
Browse files Browse the repository at this point in the history
…AccessFile (#471)
  • Loading branch information
adamreeve authored Jul 2, 2024
1 parent f1f2eda commit 62ae832
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 25 deletions.
6 changes: 5 additions & 1 deletion cpp/Enums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ namespace
static_assert(::arrow::TimeUnit::type::MILLI == 1);
static_assert(::arrow::TimeUnit::type::MICRO == 2);
static_assert(::arrow::TimeUnit::type::NANO == 3);
}

// Arrow StatusCode values that should be kept up to date with ArrowStatusCode.cs
static_assert((int) ::arrow::StatusCode::OutOfMemory == 1);
static_assert((int) ::arrow::StatusCode::IOError == 5);
static_assert((int) ::arrow::StatusCode::UnknownError == 9);
}
}
45 changes: 26 additions & 19 deletions cpp/ExceptionInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <exception>
#include <string>

#include <parquet/exception.h>

struct ExceptionInfo final
{
ExceptionInfo(const char* type, const char* message);
Expand All @@ -14,23 +16,28 @@ struct ExceptionInfo final
};

#define SINGLE_ARG(...) __VA_ARGS__
#define TRYCATCH(expression) \
try \
{ \
expression \
return nullptr; \
} \
catch (const std::bad_alloc& exception) \
{ \
return new ExceptionInfo("OutOfMemoryException", exception.what()); \
} \
catch (const std::exception& exception) \
{ \
return new ExceptionInfo(exception); \
} \
catch (...) \
{ \
return new ExceptionInfo("unkown", "uncaught exception"); \
} \

#define TRYCATCH(expression) \
try \
{ \
expression \
return nullptr; \
} \
catch (const std::bad_alloc& exception) \
{ \
return new ExceptionInfo("OutOfMemoryException", exception.what()); \
} \
catch (const parquet::ParquetStatusException& exception) \
{ \
return exception.status().IsOutOfMemory() \
? new ExceptionInfo("OutOfMemoryException", exception.what()) \
: new ExceptionInfo(exception); \
} \
catch (const std::exception& exception) \
{ \
return new ExceptionInfo(exception); \
} \
catch (...) \
{ \
return new ExceptionInfo("unknown", "uncaught exception"); \
} \

2 changes: 1 addition & 1 deletion cpp/ManagedRandomAccessFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class ManagedRandomAccessFile final : public arrow::io::RandomAccessFile
return Result<T>(result);
}

return Result<T>(Status(statusCode, exception));
return Result<T>(Status(statusCode, exception == nullptr ? "" : exception));
}

static Status GetStatus(const StatusCode statusCode, const char* const exception)
Expand Down
12 changes: 12 additions & 0 deletions csharp/ArrowStatusCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace ParquetSharp
{
/// <summary>
/// Subset of Arrow StatusCode enums used from ParquetSharp.
/// </summary>
internal enum ArrowStatusCode
{
OutOfMemory = 1,
IOError = 5,
UnknownError = 9,
}
}
6 changes: 3 additions & 3 deletions csharp/IO/ManagedRandomAccessFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,16 @@ private byte HandleException(Exception error, out string? exception)
if (error is OutOfMemoryException)
{
exception = _exceptionMessage = null;
return 1;
return (byte) ArrowStatusCode.OutOfMemory;
}
if (error is IOException)
{
exception = _exceptionMessage = error.ToString();
return 5;
return (byte) ArrowStatusCode.IOError;
}

exception = _exceptionMessage = error.ToString();
return 9;
return (byte) ArrowStatusCode.UnknownError;
}

[DllImport(ParquetDll.Name)]
Expand Down
2 changes: 1 addition & 1 deletion csharp/ParquetSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<NoWarn>1591;</NoWarn>
<VersionPrefix>16.1.0-beta1</VersionPrefix>
<VersionPrefix>16.1.0-beta2</VersionPrefix>
<Company>G-Research</Company>
<Authors>G-Research</Authors>
<Product>ParquetSharp</Product>
Expand Down

0 comments on commit 62ae832

Please sign in to comment.