aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/providers/common/codec/arrow/yql_codec_buf_input_stream.cpp
blob: 0ee4c166099cb0b1dae30435c71eeec4edfd5d0c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "yql_codec_buf_input_stream.h"

#include <yql/essentials/minikql/arrow/arrow_util.h>
#include <yql/essentials/public/udf/arrow/defs.h>

#include <arrow/buffer.h>
#include <arrow/buffer.h>

namespace NYql {
namespace NCommon {

arrow::Result<int64_t> TInputBufArrowInputStream::Read(int64_t bytesToRead, void* outBuffer) {
    auto outBufferPtr = static_cast<char*>(outBuffer);

    YQL_ENSURE(bytesToRead > 0);
    if (!Buffer_.TryRead(*outBufferPtr)) {
        EOSReached_ = true;
        return 0;
    }

    Buffer_.ReadMany(outBufferPtr + 1, bytesToRead - 1);
    BytesRead_ += bytesToRead;
    return bytesToRead;
}

arrow::Result<std::shared_ptr<arrow::Buffer>> TInputBufArrowInputStream::Read(int64_t nbytes) {
    auto outBuffer = ARROW_RESULT(AllocateResizableBuffer(nbytes, Pool_));
    auto bytesRead = ARROW_RESULT(Read(nbytes, outBuffer->mutable_data()));
    if (bytesRead == 0) {
        return NKikimr::NMiniKQL::MakeEmptyBuffer();
    }

    YQL_ENSURE(bytesRead == nbytes);
    return outBuffer;
}

} // NCommon
} // NYql