Files
ResInsight/ApplicationLibCode/FileInterface/RifOsduWellLogReader.cpp
T
Magne Sjaastad ca5465a660 Handles API changes for Arrow Parquet file reading
Adapts the code to handle different API versions of the Arrow library for reading Parquet files.
It uses preprocessor directives to select the correct API based on the `ARROW_VERSION_MAJOR`.
This ensures compatibility with both older and newer versions of the Arrow library.
2026-01-31 17:25:26 +01:00

88 lines
3.0 KiB
C++

/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2024 Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RifOsduWellLogReader.h"
#include "RifByteArrayArrowRandomAccessFile.h"
#include "cafAssert.h"
#include <iostream>
#include <vector>
#include "RifArrowTools.h"
#undef signals
#include <arrow/array/array_primitive.h>
#include <arrow/csv/api.h>
#include <arrow/io/api.h>
#include <arrow/scalar.h>
#include <parquet/arrow/reader.h>
#define signals Q_SIGNALS
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::pair<cvf::ref<RigOsduWellLogData>, QString> RifOsduWellLogReader::readWellLogData( const QByteArray& contents )
{
arrow::MemoryPool* pool = arrow::default_memory_pool();
std::shared_ptr<arrow::io::RandomAccessFile> input = std::make_shared<RifByteArrayArrowRandomAccessFile>( contents );
// Open Parquet file reader
#if ARROW_VERSION_MAJOR >= 20
// New API: OpenFile returns arrow::Result
auto result = parquet::arrow::OpenFile( input, pool );
if ( !result.ok() )
{
return { nullptr, "Unable to read parquet data." };
}
std::unique_ptr<parquet::arrow::FileReader> arrow_reader = std::move( result ).ValueOrDie();
#else
// Old API: OpenFile takes output parameter
std::unique_ptr<parquet::arrow::FileReader> arrow_reader;
if ( !parquet::arrow::OpenFile( input, pool, &arrow_reader ).ok() )
{
return { nullptr, "Unable to read parquet data." };
}
#endif
// Read entire file as a single Arrow table
std::shared_ptr<arrow::Table> table;
if ( !arrow_reader->ReadTable( &table ).ok() )
{
return { nullptr, "Unable to read parquet table." };
}
auto logData = cvf::make_ref<RigOsduWellLogData>();
for ( std::string columnName : table->ColumnNames() )
{
std::shared_ptr<arrow::ChunkedArray> column = table->GetColumnByName( columnName );
if ( column->type()->id() == arrow::Type::DOUBLE )
{
std::vector<double> columnVector = RifArrowTools::chunkedArrayToVector<arrow::DoubleArray, double>( column );
logData->setValues( QString::fromStdString( columnName ), columnVector );
}
}
logData->finalizeData();
return { logData, "" };
}