github-actions[bot] commented on code in PR #26709: URL: https://github.com/apache/doris/pull/26709#discussion_r1391915522
########## be/src/vec/exec/format/arrow/arrow_batch_reader.cpp: ########## @@ -0,0 +1,138 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "arrow_batch_reader.h" + +#include "arrow/array.h" +#include "arrow/io/buffered.h" +#include "arrow/io/stdio.h" +#include "arrow/ipc/options.h" +#include "arrow/ipc/reader.h" +#include "arrow/record_batch.h" +#include "arrow/result.h" +#include "common/logging.h" +#include "io/fs/stream_load_pipe.h" +#include "olap/wal_manager.h" +#include "runtime/runtime_state.h" + +#define DEFAULT_READ_BUF_CAP (4 * 1024 * 1024) Review Comment: warning: macro 'DEFAULT_READ_BUF_CAP' defines an integral constant; prefer an enum instead [modernize-macro-to-enum] ```cpp #define DEFAULT_READ_BUF_CAP (4 * 1024 * 1024) ^ ``` ########## be/src/vec/exec/format/arrow/arrow_batch_reader.cpp: ########## @@ -0,0 +1,138 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "arrow_batch_reader.h" + +#include "arrow/array.h" +#include "arrow/io/buffered.h" +#include "arrow/io/stdio.h" +#include "arrow/ipc/options.h" +#include "arrow/ipc/reader.h" +#include "arrow/record_batch.h" +#include "arrow/result.h" +#include "common/logging.h" +#include "io/fs/stream_load_pipe.h" +#include "olap/wal_manager.h" +#include "runtime/runtime_state.h" + +#define DEFAULT_READ_BUF_CAP (4 * 1024 * 1024) +#define BATCH_SIZE_LENGTH (4) + Review Comment: warning: replace macro with enum [modernize-macro-to-enum] ```suggestion enum { DEFAULT_READ_BUF_CAP = (4 * 1024 * 1024), BATCH_SIZE_LENGTH = (4)}; ``` ########## be/src/vec/exec/format/arrow/arrow_batch_reader.cpp: ########## @@ -0,0 +1,138 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "arrow_batch_reader.h" + +#include "arrow/array.h" +#include "arrow/io/buffered.h" +#include "arrow/io/stdio.h" +#include "arrow/ipc/options.h" +#include "arrow/ipc/reader.h" +#include "arrow/record_batch.h" +#include "arrow/result.h" +#include "common/logging.h" +#include "io/fs/stream_load_pipe.h" +#include "olap/wal_manager.h" +#include "runtime/runtime_state.h" + +#define DEFAULT_READ_BUF_CAP (4 * 1024 * 1024) +#define BATCH_SIZE_LENGTH (4) + +namespace doris::vectorized { + +ArrowBatchReader::ArrowBatchReader(io::FileReaderSPtr file_reader) + : _file_reader(file_reader), + _read_buf(new uint8_t[DEFAULT_READ_BUF_CAP]), + _read_buf_cap(DEFAULT_READ_BUF_CAP), + _read_buf_pos(0), + _read_buf_len(0), + _batch_size(-1) {} + +ArrowBatchReader::~ArrowBatchReader() = default; + +Status ArrowBatchReader::get_one_batch(uint8_t** data, int* length) { + _init(); + RETURN_IF_ERROR(_get_batch_size()); + RETURN_IF_ERROR(_get_batch_value()); + + *data = &_read_buf[_read_buf_pos]; + if (_batch_size <= 0) { + *length = 0; + } else { + *length = _read_buf_len; + } + return Status::OK(); +} + +void ArrowBatchReader::_init() { + _read_buf_pos = _read_buf_pos + _read_buf_len; + _batch_size = -1; + _read_buf_len = 0; +} + +Status ArrowBatchReader::_get_batch_size() { + if (_batch_size >= 0) { + return Status::OK(); + } + + if (_get_valid_cap() < BATCH_SIZE_LENGTH) { + memmove(_read_buf, _read_buf + _read_buf_pos, _read_buf_len); + } + + while (_read_buf_len < BATCH_SIZE_LENGTH) { + Slice file_slice(_read_buf + _read_buf_pos + _read_buf_len, _get_valid_cap()); + size_t read_length = 0; + RETURN_IF_ERROR(_file_reader->read_at(0, file_slice, &read_length, NULL)); + if (read_length == 0) { + return Status::OK(); + } + _read_buf_len += read_length; + } + + _batch_size = _convert_to_length(_read_buf); + + if (_batch_size < 0) { + return Status::InternalError("Invalid batch size"); + } + + _read_buf_pos += BATCH_SIZE_LENGTH; + _read_buf_len -= BATCH_SIZE_LENGTH; + + if (_batch_size > _read_buf_cap) { + while (_read_buf_cap < _batch_size) { + _read_buf_cap *= 2; + } + + uint8_t* new_read_buf = new uint8_t[_read_buf_cap]; + memmove(new_read_buf, _read_buf + _read_buf_pos, _read_buf_len); + delete[] _read_buf; + _read_buf = new_read_buf; + } + return Status::OK(); +} + +int ArrowBatchReader::_get_valid_cap() { + return _read_buf_cap - _read_buf_pos - _read_buf_len; +} + +uint32_t ArrowBatchReader::_convert_to_length(uint8_t* data) { Review Comment: warning: method '_convert_to_length' can be made static [readability-convert-member-functions-to-static] be/src/vec/exec/format/arrow/arrow_batch_reader.h:65: ```diff - uint32_t _convert_to_length(uint8_t* data); + static uint32_t _convert_to_length(uint8_t* data); ``` ########## be/src/vec/exec/format/arrow/arrow_batch_reader.cpp: ########## @@ -0,0 +1,138 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "arrow_batch_reader.h" + +#include "arrow/array.h" +#include "arrow/io/buffered.h" +#include "arrow/io/stdio.h" +#include "arrow/ipc/options.h" +#include "arrow/ipc/reader.h" +#include "arrow/record_batch.h" +#include "arrow/result.h" +#include "common/logging.h" +#include "io/fs/stream_load_pipe.h" +#include "olap/wal_manager.h" +#include "runtime/runtime_state.h" + +#define DEFAULT_READ_BUF_CAP (4 * 1024 * 1024) +#define BATCH_SIZE_LENGTH (4) + +namespace doris::vectorized { + +ArrowBatchReader::ArrowBatchReader(io::FileReaderSPtr file_reader) + : _file_reader(file_reader), + _read_buf(new uint8_t[DEFAULT_READ_BUF_CAP]), + _read_buf_cap(DEFAULT_READ_BUF_CAP), + _read_buf_pos(0), + _read_buf_len(0), + _batch_size(-1) {} + +ArrowBatchReader::~ArrowBatchReader() = default; + +Status ArrowBatchReader::get_one_batch(uint8_t** data, int* length) { + _init(); + RETURN_IF_ERROR(_get_batch_size()); + RETURN_IF_ERROR(_get_batch_value()); + + *data = &_read_buf[_read_buf_pos]; + if (_batch_size <= 0) { + *length = 0; + } else { + *length = _read_buf_len; + } + return Status::OK(); +} + +void ArrowBatchReader::_init() { + _read_buf_pos = _read_buf_pos + _read_buf_len; + _batch_size = -1; + _read_buf_len = 0; +} + +Status ArrowBatchReader::_get_batch_size() { + if (_batch_size >= 0) { + return Status::OK(); + } + + if (_get_valid_cap() < BATCH_SIZE_LENGTH) { + memmove(_read_buf, _read_buf + _read_buf_pos, _read_buf_len); + } + + while (_read_buf_len < BATCH_SIZE_LENGTH) { + Slice file_slice(_read_buf + _read_buf_pos + _read_buf_len, _get_valid_cap()); + size_t read_length = 0; + RETURN_IF_ERROR(_file_reader->read_at(0, file_slice, &read_length, NULL)); + if (read_length == 0) { + return Status::OK(); + } + _read_buf_len += read_length; + } + + _batch_size = _convert_to_length(_read_buf); + + if (_batch_size < 0) { + return Status::InternalError("Invalid batch size"); + } + + _read_buf_pos += BATCH_SIZE_LENGTH; + _read_buf_len -= BATCH_SIZE_LENGTH; + + if (_batch_size > _read_buf_cap) { + while (_read_buf_cap < _batch_size) { + _read_buf_cap *= 2; + } + + uint8_t* new_read_buf = new uint8_t[_read_buf_cap]; + memmove(new_read_buf, _read_buf + _read_buf_pos, _read_buf_len); + delete[] _read_buf; + _read_buf = new_read_buf; + } + return Status::OK(); +} + +int ArrowBatchReader::_get_valid_cap() { Review Comment: warning: method '_get_valid_cap' can be made const [readability-make-member-function-const] be/src/vec/exec/format/arrow/arrow_batch_reader.h:67: ```diff - int _get_valid_cap(); + int _get_valid_cap() const; ``` ```suggestion int ArrowBatchReader::_get_valid_cap() const { ``` ########## be/src/vec/exec/format/arrow/arrow_batch_reader.cpp: ########## @@ -0,0 +1,138 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "arrow_batch_reader.h" + +#include "arrow/array.h" +#include "arrow/io/buffered.h" +#include "arrow/io/stdio.h" +#include "arrow/ipc/options.h" +#include "arrow/ipc/reader.h" +#include "arrow/record_batch.h" +#include "arrow/result.h" +#include "common/logging.h" +#include "io/fs/stream_load_pipe.h" +#include "olap/wal_manager.h" +#include "runtime/runtime_state.h" + +#define DEFAULT_READ_BUF_CAP (4 * 1024 * 1024) +#define BATCH_SIZE_LENGTH (4) Review Comment: warning: macro 'BATCH_SIZE_LENGTH' defines an integral constant; prefer an enum instead [modernize-macro-to-enum] ```cpp #define BATCH_SIZE_LENGTH (4) ^ ``` ########## be/src/vec/exec/format/arrow/arrow_batch_reader.cpp: ########## @@ -0,0 +1,138 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "arrow_batch_reader.h" + +#include "arrow/array.h" +#include "arrow/io/buffered.h" +#include "arrow/io/stdio.h" +#include "arrow/ipc/options.h" +#include "arrow/ipc/reader.h" +#include "arrow/record_batch.h" +#include "arrow/result.h" +#include "common/logging.h" +#include "io/fs/stream_load_pipe.h" +#include "olap/wal_manager.h" +#include "runtime/runtime_state.h" + +#define DEFAULT_READ_BUF_CAP (4 * 1024 * 1024) +#define BATCH_SIZE_LENGTH (4) + +namespace doris::vectorized { + +ArrowBatchReader::ArrowBatchReader(io::FileReaderSPtr file_reader) + : _file_reader(file_reader), + _read_buf(new uint8_t[DEFAULT_READ_BUF_CAP]), + _read_buf_cap(DEFAULT_READ_BUF_CAP), + _read_buf_pos(0), + _read_buf_len(0), + _batch_size(-1) {} + +ArrowBatchReader::~ArrowBatchReader() = default; + +Status ArrowBatchReader::get_one_batch(uint8_t** data, int* length) { + _init(); + RETURN_IF_ERROR(_get_batch_size()); + RETURN_IF_ERROR(_get_batch_value()); + + *data = &_read_buf[_read_buf_pos]; + if (_batch_size <= 0) { + *length = 0; + } else { + *length = _read_buf_len; + } + return Status::OK(); +} + +void ArrowBatchReader::_init() { + _read_buf_pos = _read_buf_pos + _read_buf_len; + _batch_size = -1; + _read_buf_len = 0; +} + +Status ArrowBatchReader::_get_batch_size() { + if (_batch_size >= 0) { + return Status::OK(); + } + + if (_get_valid_cap() < BATCH_SIZE_LENGTH) { + memmove(_read_buf, _read_buf + _read_buf_pos, _read_buf_len); + } + + while (_read_buf_len < BATCH_SIZE_LENGTH) { + Slice file_slice(_read_buf + _read_buf_pos + _read_buf_len, _get_valid_cap()); + size_t read_length = 0; + RETURN_IF_ERROR(_file_reader->read_at(0, file_slice, &read_length, NULL)); + if (read_length == 0) { + return Status::OK(); + } + _read_buf_len += read_length; + } + + _batch_size = _convert_to_length(_read_buf); + + if (_batch_size < 0) { + return Status::InternalError("Invalid batch size"); + } + + _read_buf_pos += BATCH_SIZE_LENGTH; + _read_buf_len -= BATCH_SIZE_LENGTH; + + if (_batch_size > _read_buf_cap) { + while (_read_buf_cap < _batch_size) { + _read_buf_cap *= 2; + } + + uint8_t* new_read_buf = new uint8_t[_read_buf_cap]; + memmove(new_read_buf, _read_buf + _read_buf_pos, _read_buf_len); + delete[] _read_buf; + _read_buf = new_read_buf; + } + return Status::OK(); +} + +int ArrowBatchReader::_get_valid_cap() { + return _read_buf_cap - _read_buf_pos - _read_buf_len; +} + +uint32_t ArrowBatchReader::_convert_to_length(uint8_t* data) { + uint32_t len = (((uint32_t)data[0]) << 24) & 0xff000000; + len |= (((uint32_t)data[1]) << 16) & 0xff0000; Review Comment: warning: 16 is a magic number; consider replacing it with a named constant [readability-magic-numbers] ```cpp len |= (((uint32_t)data[1]) << 16) & 0xff0000; ^ ``` ########## be/src/vec/exec/format/arrow/arrow_batch_reader.cpp: ########## @@ -0,0 +1,138 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "arrow_batch_reader.h" + +#include "arrow/array.h" +#include "arrow/io/buffered.h" +#include "arrow/io/stdio.h" +#include "arrow/ipc/options.h" +#include "arrow/ipc/reader.h" +#include "arrow/record_batch.h" +#include "arrow/result.h" +#include "common/logging.h" +#include "io/fs/stream_load_pipe.h" +#include "olap/wal_manager.h" +#include "runtime/runtime_state.h" + +#define DEFAULT_READ_BUF_CAP (4 * 1024 * 1024) +#define BATCH_SIZE_LENGTH (4) + +namespace doris::vectorized { + +ArrowBatchReader::ArrowBatchReader(io::FileReaderSPtr file_reader) + : _file_reader(file_reader), + _read_buf(new uint8_t[DEFAULT_READ_BUF_CAP]), + _read_buf_cap(DEFAULT_READ_BUF_CAP), + _read_buf_pos(0), + _read_buf_len(0), + _batch_size(-1) {} + +ArrowBatchReader::~ArrowBatchReader() = default; + +Status ArrowBatchReader::get_one_batch(uint8_t** data, int* length) { + _init(); + RETURN_IF_ERROR(_get_batch_size()); + RETURN_IF_ERROR(_get_batch_value()); + + *data = &_read_buf[_read_buf_pos]; + if (_batch_size <= 0) { + *length = 0; + } else { + *length = _read_buf_len; + } + return Status::OK(); +} + +void ArrowBatchReader::_init() { + _read_buf_pos = _read_buf_pos + _read_buf_len; + _batch_size = -1; + _read_buf_len = 0; +} + +Status ArrowBatchReader::_get_batch_size() { + if (_batch_size >= 0) { + return Status::OK(); + } + + if (_get_valid_cap() < BATCH_SIZE_LENGTH) { + memmove(_read_buf, _read_buf + _read_buf_pos, _read_buf_len); + } + + while (_read_buf_len < BATCH_SIZE_LENGTH) { + Slice file_slice(_read_buf + _read_buf_pos + _read_buf_len, _get_valid_cap()); + size_t read_length = 0; + RETURN_IF_ERROR(_file_reader->read_at(0, file_slice, &read_length, NULL)); + if (read_length == 0) { + return Status::OK(); + } + _read_buf_len += read_length; + } + + _batch_size = _convert_to_length(_read_buf); + + if (_batch_size < 0) { + return Status::InternalError("Invalid batch size"); + } + + _read_buf_pos += BATCH_SIZE_LENGTH; + _read_buf_len -= BATCH_SIZE_LENGTH; + + if (_batch_size > _read_buf_cap) { + while (_read_buf_cap < _batch_size) { + _read_buf_cap *= 2; + } + + uint8_t* new_read_buf = new uint8_t[_read_buf_cap]; + memmove(new_read_buf, _read_buf + _read_buf_pos, _read_buf_len); + delete[] _read_buf; + _read_buf = new_read_buf; + } + return Status::OK(); +} + +int ArrowBatchReader::_get_valid_cap() { + return _read_buf_cap - _read_buf_pos - _read_buf_len; +} + +uint32_t ArrowBatchReader::_convert_to_length(uint8_t* data) { + uint32_t len = (((uint32_t)data[0]) << 24) & 0xff000000; + len |= (((uint32_t)data[1]) << 16) & 0xff0000; + len |= (((uint32_t)data[2]) << 8) & 0xff00; Review Comment: warning: 0xff00 is a magic number; consider replacing it with a named constant [readability-magic-numbers] ```cpp len |= (((uint32_t)data[2]) << 8) & 0xff00; ^ ``` ########## be/src/vec/exec/format/arrow/arrow_batch_reader.cpp: ########## @@ -0,0 +1,138 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "arrow_batch_reader.h" + +#include "arrow/array.h" +#include "arrow/io/buffered.h" +#include "arrow/io/stdio.h" +#include "arrow/ipc/options.h" +#include "arrow/ipc/reader.h" +#include "arrow/record_batch.h" +#include "arrow/result.h" +#include "common/logging.h" +#include "io/fs/stream_load_pipe.h" +#include "olap/wal_manager.h" +#include "runtime/runtime_state.h" + +#define DEFAULT_READ_BUF_CAP (4 * 1024 * 1024) +#define BATCH_SIZE_LENGTH (4) + +namespace doris::vectorized { + +ArrowBatchReader::ArrowBatchReader(io::FileReaderSPtr file_reader) + : _file_reader(file_reader), + _read_buf(new uint8_t[DEFAULT_READ_BUF_CAP]), + _read_buf_cap(DEFAULT_READ_BUF_CAP), + _read_buf_pos(0), + _read_buf_len(0), + _batch_size(-1) {} + +ArrowBatchReader::~ArrowBatchReader() = default; + +Status ArrowBatchReader::get_one_batch(uint8_t** data, int* length) { + _init(); + RETURN_IF_ERROR(_get_batch_size()); + RETURN_IF_ERROR(_get_batch_value()); + + *data = &_read_buf[_read_buf_pos]; + if (_batch_size <= 0) { + *length = 0; + } else { + *length = _read_buf_len; + } + return Status::OK(); +} + +void ArrowBatchReader::_init() { + _read_buf_pos = _read_buf_pos + _read_buf_len; + _batch_size = -1; + _read_buf_len = 0; +} + +Status ArrowBatchReader::_get_batch_size() { + if (_batch_size >= 0) { + return Status::OK(); + } + + if (_get_valid_cap() < BATCH_SIZE_LENGTH) { + memmove(_read_buf, _read_buf + _read_buf_pos, _read_buf_len); + } + + while (_read_buf_len < BATCH_SIZE_LENGTH) { + Slice file_slice(_read_buf + _read_buf_pos + _read_buf_len, _get_valid_cap()); + size_t read_length = 0; + RETURN_IF_ERROR(_file_reader->read_at(0, file_slice, &read_length, NULL)); + if (read_length == 0) { + return Status::OK(); + } + _read_buf_len += read_length; + } + + _batch_size = _convert_to_length(_read_buf); + + if (_batch_size < 0) { + return Status::InternalError("Invalid batch size"); + } + + _read_buf_pos += BATCH_SIZE_LENGTH; + _read_buf_len -= BATCH_SIZE_LENGTH; + + if (_batch_size > _read_buf_cap) { + while (_read_buf_cap < _batch_size) { + _read_buf_cap *= 2; + } + + uint8_t* new_read_buf = new uint8_t[_read_buf_cap]; + memmove(new_read_buf, _read_buf + _read_buf_pos, _read_buf_len); + delete[] _read_buf; + _read_buf = new_read_buf; + } + return Status::OK(); +} + +int ArrowBatchReader::_get_valid_cap() { + return _read_buf_cap - _read_buf_pos - _read_buf_len; +} + +uint32_t ArrowBatchReader::_convert_to_length(uint8_t* data) { Review Comment: warning: pointer parameter 'data' can be pointer to const [readability-non-const-parameter] be/src/vec/exec/format/arrow/arrow_batch_reader.h:65: ```diff - uint32_t _convert_to_length(uint8_t* data); + uint32_t _convert_to_length(const uint8_t* data); ``` ```suggestion uint32_t ArrowBatchReader::_convert_to_length(const uint8_t* data) { ``` ########## be/src/vec/exec/format/arrow/arrow_batch_reader.cpp: ########## @@ -0,0 +1,138 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "arrow_batch_reader.h" + +#include "arrow/array.h" +#include "arrow/io/buffered.h" +#include "arrow/io/stdio.h" +#include "arrow/ipc/options.h" +#include "arrow/ipc/reader.h" +#include "arrow/record_batch.h" +#include "arrow/result.h" +#include "common/logging.h" +#include "io/fs/stream_load_pipe.h" +#include "olap/wal_manager.h" +#include "runtime/runtime_state.h" + +#define DEFAULT_READ_BUF_CAP (4 * 1024 * 1024) +#define BATCH_SIZE_LENGTH (4) + +namespace doris::vectorized { + +ArrowBatchReader::ArrowBatchReader(io::FileReaderSPtr file_reader) + : _file_reader(file_reader), + _read_buf(new uint8_t[DEFAULT_READ_BUF_CAP]), + _read_buf_cap(DEFAULT_READ_BUF_CAP), + _read_buf_pos(0), + _read_buf_len(0), + _batch_size(-1) {} + +ArrowBatchReader::~ArrowBatchReader() = default; + +Status ArrowBatchReader::get_one_batch(uint8_t** data, int* length) { + _init(); + RETURN_IF_ERROR(_get_batch_size()); + RETURN_IF_ERROR(_get_batch_value()); + + *data = &_read_buf[_read_buf_pos]; + if (_batch_size <= 0) { + *length = 0; + } else { + *length = _read_buf_len; + } + return Status::OK(); +} + +void ArrowBatchReader::_init() { + _read_buf_pos = _read_buf_pos + _read_buf_len; + _batch_size = -1; + _read_buf_len = 0; +} + +Status ArrowBatchReader::_get_batch_size() { + if (_batch_size >= 0) { + return Status::OK(); + } + + if (_get_valid_cap() < BATCH_SIZE_LENGTH) { + memmove(_read_buf, _read_buf + _read_buf_pos, _read_buf_len); + } + + while (_read_buf_len < BATCH_SIZE_LENGTH) { + Slice file_slice(_read_buf + _read_buf_pos + _read_buf_len, _get_valid_cap()); + size_t read_length = 0; + RETURN_IF_ERROR(_file_reader->read_at(0, file_slice, &read_length, NULL)); + if (read_length == 0) { + return Status::OK(); + } + _read_buf_len += read_length; + } + + _batch_size = _convert_to_length(_read_buf); + + if (_batch_size < 0) { + return Status::InternalError("Invalid batch size"); + } + + _read_buf_pos += BATCH_SIZE_LENGTH; + _read_buf_len -= BATCH_SIZE_LENGTH; + + if (_batch_size > _read_buf_cap) { + while (_read_buf_cap < _batch_size) { + _read_buf_cap *= 2; + } + + uint8_t* new_read_buf = new uint8_t[_read_buf_cap]; + memmove(new_read_buf, _read_buf + _read_buf_pos, _read_buf_len); + delete[] _read_buf; + _read_buf = new_read_buf; + } + return Status::OK(); +} + +int ArrowBatchReader::_get_valid_cap() { + return _read_buf_cap - _read_buf_pos - _read_buf_len; +} + +uint32_t ArrowBatchReader::_convert_to_length(uint8_t* data) { + uint32_t len = (((uint32_t)data[0]) << 24) & 0xff000000; + len |= (((uint32_t)data[1]) << 16) & 0xff0000; + len |= (((uint32_t)data[2]) << 8) & 0xff00; + len |= ((uint32_t)data[3]) & 0xff; Review Comment: warning: 0xff is a magic number; consider replacing it with a named constant [readability-magic-numbers] ```cpp len |= ((uint32_t)data[3]) & 0xff; ^ ``` ########## be/src/vec/exec/format/arrow/arrow_batch_reader.h: ########## @@ -0,0 +1,84 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#pragma once + +#include <gen_cpp/PlanNodes_types.h> +#include <gen_cpp/internal_service.pb.h> +#include <stddef.h> +#include <stdint.h> Review Comment: warning: inclusion of deprecated C++ header 'stdint.h'; consider using 'cstdint' instead [modernize-deprecated-headers] ```suggestion #include <cstdint> ``` ########## be/src/vec/exec/format/arrow/arrow_batch_reader.h: ########## @@ -0,0 +1,84 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#pragma once + +#include <gen_cpp/PlanNodes_types.h> +#include <gen_cpp/internal_service.pb.h> +#include <stddef.h> +#include <stdint.h> + +#include <cstddef> +#include <memory> +#include <string> +#include <unordered_map> +#include <unordered_set> +#include <vector> + +#include "common/status.h" +#include "io/file_factory.h" +#include "io/fs/file_reader_writer_fwd.h" +#include "util/slice.h" +#include "vec/data_types/data_type.h" +#include "vec/exec/format/file_reader/new_plain_text_line_reader.h" +#include "vec/exec/format/generic_reader.h" + +namespace doris { + +namespace io { +class FileSystem; +struct IOContext; +} // namespace io + +namespace vectorized { + +struct ScannerCounter; +class Block; + +class ArrowBatchReader { + ENABLE_FACTORY_CREATOR(ArrowBatchReader); + +public: + ArrowBatchReader(io::FileReaderSPtr file_reader); + + ~ArrowBatchReader(); + + // Status read_one_batch(std::unique_ptr<uint8_t[]>* data, size_t* length); + Status get_one_batch(uint8_t** data, int* length); + +private: + void _init(); + Status _get_batch_size(); + uint32_t _convert_to_length(uint8_t* data); + Status _get_batch_value(); + int _get_valid_cap(); + +private: Review Comment: warning: redundant access specifier has the same accessibility as the previous access specifier [readability-redundant-access-specifiers] ```suggestion ``` <details> <summary>Additional context</summary> **be/src/vec/exec/format/arrow/arrow_batch_reader.h:62:** previously declared here ```cpp private: ^ ``` </details> ########## be/src/vec/exec/format/arrow/arrow_batch_reader.h: ########## @@ -0,0 +1,84 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#pragma once + +#include <gen_cpp/PlanNodes_types.h> +#include <gen_cpp/internal_service.pb.h> +#include <stddef.h> Review Comment: warning: inclusion of deprecated C++ header 'stddef.h'; consider using 'cstddef' instead [modernize-deprecated-headers] ```suggestion #include <cstddef> ``` ########## be/src/vec/exec/format/arrow/arrow_batch_reader.cpp: ########## @@ -0,0 +1,138 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "arrow_batch_reader.h" + +#include "arrow/array.h" +#include "arrow/io/buffered.h" +#include "arrow/io/stdio.h" +#include "arrow/ipc/options.h" +#include "arrow/ipc/reader.h" +#include "arrow/record_batch.h" +#include "arrow/result.h" +#include "common/logging.h" +#include "io/fs/stream_load_pipe.h" +#include "olap/wal_manager.h" +#include "runtime/runtime_state.h" + +#define DEFAULT_READ_BUF_CAP (4 * 1024 * 1024) +#define BATCH_SIZE_LENGTH (4) + +namespace doris::vectorized { + +ArrowBatchReader::ArrowBatchReader(io::FileReaderSPtr file_reader) + : _file_reader(file_reader), + _read_buf(new uint8_t[DEFAULT_READ_BUF_CAP]), + _read_buf_cap(DEFAULT_READ_BUF_CAP), + _read_buf_pos(0), + _read_buf_len(0), + _batch_size(-1) {} + +ArrowBatchReader::~ArrowBatchReader() = default; + +Status ArrowBatchReader::get_one_batch(uint8_t** data, int* length) { + _init(); + RETURN_IF_ERROR(_get_batch_size()); + RETURN_IF_ERROR(_get_batch_value()); + + *data = &_read_buf[_read_buf_pos]; + if (_batch_size <= 0) { + *length = 0; + } else { + *length = _read_buf_len; + } + return Status::OK(); +} + +void ArrowBatchReader::_init() { + _read_buf_pos = _read_buf_pos + _read_buf_len; + _batch_size = -1; + _read_buf_len = 0; +} + +Status ArrowBatchReader::_get_batch_size() { + if (_batch_size >= 0) { + return Status::OK(); + } + + if (_get_valid_cap() < BATCH_SIZE_LENGTH) { + memmove(_read_buf, _read_buf + _read_buf_pos, _read_buf_len); + } + + while (_read_buf_len < BATCH_SIZE_LENGTH) { + Slice file_slice(_read_buf + _read_buf_pos + _read_buf_len, _get_valid_cap()); + size_t read_length = 0; + RETURN_IF_ERROR(_file_reader->read_at(0, file_slice, &read_length, NULL)); + if (read_length == 0) { + return Status::OK(); + } + _read_buf_len += read_length; + } + + _batch_size = _convert_to_length(_read_buf); + + if (_batch_size < 0) { + return Status::InternalError("Invalid batch size"); + } + + _read_buf_pos += BATCH_SIZE_LENGTH; + _read_buf_len -= BATCH_SIZE_LENGTH; + + if (_batch_size > _read_buf_cap) { + while (_read_buf_cap < _batch_size) { + _read_buf_cap *= 2; + } + + uint8_t* new_read_buf = new uint8_t[_read_buf_cap]; Review Comment: warning: use auto when initializing with new to avoid duplicating the type name [modernize-use-auto] ```suggestion auto* new_read_buf = new uint8_t[_read_buf_cap]; ``` ########## be/src/vec/exec/format/arrow/arrow_batch_reader.h: ########## @@ -0,0 +1,84 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#pragma once + +#include <gen_cpp/PlanNodes_types.h> Review Comment: warning: 'gen_cpp/PlanNodes_types.h' file not found [clang-diagnostic-error] ```cpp #include <gen_cpp/PlanNodes_types.h> ^ ``` ########## be/src/vec/exec/format/arrow/arrow_batch_reader.cpp: ########## @@ -0,0 +1,138 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "arrow_batch_reader.h" + +#include "arrow/array.h" +#include "arrow/io/buffered.h" +#include "arrow/io/stdio.h" +#include "arrow/ipc/options.h" +#include "arrow/ipc/reader.h" +#include "arrow/record_batch.h" +#include "arrow/result.h" +#include "common/logging.h" +#include "io/fs/stream_load_pipe.h" +#include "olap/wal_manager.h" +#include "runtime/runtime_state.h" + +#define DEFAULT_READ_BUF_CAP (4 * 1024 * 1024) +#define BATCH_SIZE_LENGTH (4) + +namespace doris::vectorized { + +ArrowBatchReader::ArrowBatchReader(io::FileReaderSPtr file_reader) + : _file_reader(file_reader), + _read_buf(new uint8_t[DEFAULT_READ_BUF_CAP]), + _read_buf_cap(DEFAULT_READ_BUF_CAP), + _read_buf_pos(0), + _read_buf_len(0), + _batch_size(-1) {} + +ArrowBatchReader::~ArrowBatchReader() = default; + +Status ArrowBatchReader::get_one_batch(uint8_t** data, int* length) { + _init(); + RETURN_IF_ERROR(_get_batch_size()); + RETURN_IF_ERROR(_get_batch_value()); + + *data = &_read_buf[_read_buf_pos]; + if (_batch_size <= 0) { + *length = 0; + } else { + *length = _read_buf_len; + } + return Status::OK(); +} + +void ArrowBatchReader::_init() { + _read_buf_pos = _read_buf_pos + _read_buf_len; + _batch_size = -1; + _read_buf_len = 0; +} + +Status ArrowBatchReader::_get_batch_size() { + if (_batch_size >= 0) { + return Status::OK(); + } + + if (_get_valid_cap() < BATCH_SIZE_LENGTH) { + memmove(_read_buf, _read_buf + _read_buf_pos, _read_buf_len); + } + + while (_read_buf_len < BATCH_SIZE_LENGTH) { + Slice file_slice(_read_buf + _read_buf_pos + _read_buf_len, _get_valid_cap()); + size_t read_length = 0; + RETURN_IF_ERROR(_file_reader->read_at(0, file_slice, &read_length, NULL)); + if (read_length == 0) { + return Status::OK(); + } + _read_buf_len += read_length; + } + + _batch_size = _convert_to_length(_read_buf); + + if (_batch_size < 0) { + return Status::InternalError("Invalid batch size"); + } + + _read_buf_pos += BATCH_SIZE_LENGTH; + _read_buf_len -= BATCH_SIZE_LENGTH; + + if (_batch_size > _read_buf_cap) { + while (_read_buf_cap < _batch_size) { + _read_buf_cap *= 2; + } + + uint8_t* new_read_buf = new uint8_t[_read_buf_cap]; + memmove(new_read_buf, _read_buf + _read_buf_pos, _read_buf_len); + delete[] _read_buf; + _read_buf = new_read_buf; + } + return Status::OK(); +} + +int ArrowBatchReader::_get_valid_cap() { + return _read_buf_cap - _read_buf_pos - _read_buf_len; +} + +uint32_t ArrowBatchReader::_convert_to_length(uint8_t* data) { + uint32_t len = (((uint32_t)data[0]) << 24) & 0xff000000; Review Comment: warning: 0xff000000 is a magic number; consider replacing it with a named constant [readability-magic-numbers] ```cpp uint32_t len = (((uint32_t)data[0]) << 24) & 0xff000000; ^ ``` ########## be/src/vec/exec/format/arrow/arrow_batch_reader.cpp: ########## @@ -0,0 +1,138 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "arrow_batch_reader.h" + +#include "arrow/array.h" +#include "arrow/io/buffered.h" +#include "arrow/io/stdio.h" +#include "arrow/ipc/options.h" +#include "arrow/ipc/reader.h" +#include "arrow/record_batch.h" +#include "arrow/result.h" +#include "common/logging.h" +#include "io/fs/stream_load_pipe.h" +#include "olap/wal_manager.h" +#include "runtime/runtime_state.h" + +#define DEFAULT_READ_BUF_CAP (4 * 1024 * 1024) +#define BATCH_SIZE_LENGTH (4) + +namespace doris::vectorized { + +ArrowBatchReader::ArrowBatchReader(io::FileReaderSPtr file_reader) + : _file_reader(file_reader), + _read_buf(new uint8_t[DEFAULT_READ_BUF_CAP]), + _read_buf_cap(DEFAULT_READ_BUF_CAP), + _read_buf_pos(0), + _read_buf_len(0), + _batch_size(-1) {} + +ArrowBatchReader::~ArrowBatchReader() = default; + +Status ArrowBatchReader::get_one_batch(uint8_t** data, int* length) { + _init(); + RETURN_IF_ERROR(_get_batch_size()); + RETURN_IF_ERROR(_get_batch_value()); + + *data = &_read_buf[_read_buf_pos]; + if (_batch_size <= 0) { + *length = 0; + } else { + *length = _read_buf_len; + } + return Status::OK(); +} + +void ArrowBatchReader::_init() { + _read_buf_pos = _read_buf_pos + _read_buf_len; + _batch_size = -1; + _read_buf_len = 0; +} + +Status ArrowBatchReader::_get_batch_size() { + if (_batch_size >= 0) { + return Status::OK(); + } + + if (_get_valid_cap() < BATCH_SIZE_LENGTH) { + memmove(_read_buf, _read_buf + _read_buf_pos, _read_buf_len); + } + + while (_read_buf_len < BATCH_SIZE_LENGTH) { + Slice file_slice(_read_buf + _read_buf_pos + _read_buf_len, _get_valid_cap()); + size_t read_length = 0; + RETURN_IF_ERROR(_file_reader->read_at(0, file_slice, &read_length, NULL)); + if (read_length == 0) { + return Status::OK(); + } + _read_buf_len += read_length; + } + + _batch_size = _convert_to_length(_read_buf); + + if (_batch_size < 0) { + return Status::InternalError("Invalid batch size"); + } + + _read_buf_pos += BATCH_SIZE_LENGTH; + _read_buf_len -= BATCH_SIZE_LENGTH; + + if (_batch_size > _read_buf_cap) { + while (_read_buf_cap < _batch_size) { + _read_buf_cap *= 2; + } + + uint8_t* new_read_buf = new uint8_t[_read_buf_cap]; + memmove(new_read_buf, _read_buf + _read_buf_pos, _read_buf_len); + delete[] _read_buf; + _read_buf = new_read_buf; + } + return Status::OK(); +} + +int ArrowBatchReader::_get_valid_cap() { + return _read_buf_cap - _read_buf_pos - _read_buf_len; +} + +uint32_t ArrowBatchReader::_convert_to_length(uint8_t* data) { + uint32_t len = (((uint32_t)data[0]) << 24) & 0xff000000; + len |= (((uint32_t)data[1]) << 16) & 0xff0000; + len |= (((uint32_t)data[2]) << 8) & 0xff00; Review Comment: warning: 8 is a magic number; consider replacing it with a named constant [readability-magic-numbers] ```cpp len |= (((uint32_t)data[2]) << 8) & 0xff00; ^ ``` ########## be/src/vec/exec/format/arrow/arrow_batch_reader.cpp: ########## @@ -0,0 +1,138 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "arrow_batch_reader.h" + +#include "arrow/array.h" +#include "arrow/io/buffered.h" +#include "arrow/io/stdio.h" +#include "arrow/ipc/options.h" +#include "arrow/ipc/reader.h" +#include "arrow/record_batch.h" +#include "arrow/result.h" +#include "common/logging.h" +#include "io/fs/stream_load_pipe.h" +#include "olap/wal_manager.h" +#include "runtime/runtime_state.h" + +#define DEFAULT_READ_BUF_CAP (4 * 1024 * 1024) +#define BATCH_SIZE_LENGTH (4) + +namespace doris::vectorized { + +ArrowBatchReader::ArrowBatchReader(io::FileReaderSPtr file_reader) + : _file_reader(file_reader), + _read_buf(new uint8_t[DEFAULT_READ_BUF_CAP]), + _read_buf_cap(DEFAULT_READ_BUF_CAP), + _read_buf_pos(0), + _read_buf_len(0), + _batch_size(-1) {} + +ArrowBatchReader::~ArrowBatchReader() = default; + +Status ArrowBatchReader::get_one_batch(uint8_t** data, int* length) { + _init(); + RETURN_IF_ERROR(_get_batch_size()); + RETURN_IF_ERROR(_get_batch_value()); + + *data = &_read_buf[_read_buf_pos]; + if (_batch_size <= 0) { + *length = 0; + } else { + *length = _read_buf_len; + } + return Status::OK(); +} + +void ArrowBatchReader::_init() { + _read_buf_pos = _read_buf_pos + _read_buf_len; + _batch_size = -1; + _read_buf_len = 0; +} + +Status ArrowBatchReader::_get_batch_size() { + if (_batch_size >= 0) { + return Status::OK(); + } + + if (_get_valid_cap() < BATCH_SIZE_LENGTH) { + memmove(_read_buf, _read_buf + _read_buf_pos, _read_buf_len); + } + + while (_read_buf_len < BATCH_SIZE_LENGTH) { + Slice file_slice(_read_buf + _read_buf_pos + _read_buf_len, _get_valid_cap()); + size_t read_length = 0; + RETURN_IF_ERROR(_file_reader->read_at(0, file_slice, &read_length, NULL)); + if (read_length == 0) { + return Status::OK(); + } + _read_buf_len += read_length; + } + + _batch_size = _convert_to_length(_read_buf); + + if (_batch_size < 0) { + return Status::InternalError("Invalid batch size"); + } + + _read_buf_pos += BATCH_SIZE_LENGTH; + _read_buf_len -= BATCH_SIZE_LENGTH; + + if (_batch_size > _read_buf_cap) { + while (_read_buf_cap < _batch_size) { + _read_buf_cap *= 2; + } + + uint8_t* new_read_buf = new uint8_t[_read_buf_cap]; + memmove(new_read_buf, _read_buf + _read_buf_pos, _read_buf_len); + delete[] _read_buf; + _read_buf = new_read_buf; + } + return Status::OK(); +} + +int ArrowBatchReader::_get_valid_cap() { + return _read_buf_cap - _read_buf_pos - _read_buf_len; +} + +uint32_t ArrowBatchReader::_convert_to_length(uint8_t* data) { + uint32_t len = (((uint32_t)data[0]) << 24) & 0xff000000; + len |= (((uint32_t)data[1]) << 16) & 0xff0000; Review Comment: warning: 0xff0000 is a magic number; consider replacing it with a named constant [readability-magic-numbers] ```cpp len |= (((uint32_t)data[1]) << 16) & 0xff0000; ^ ``` ########## be/src/vec/exec/format/arrow/arrow_batch_reader.cpp: ########## @@ -0,0 +1,138 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "arrow_batch_reader.h" + +#include "arrow/array.h" +#include "arrow/io/buffered.h" +#include "arrow/io/stdio.h" +#include "arrow/ipc/options.h" +#include "arrow/ipc/reader.h" +#include "arrow/record_batch.h" +#include "arrow/result.h" +#include "common/logging.h" +#include "io/fs/stream_load_pipe.h" +#include "olap/wal_manager.h" +#include "runtime/runtime_state.h" + +#define DEFAULT_READ_BUF_CAP (4 * 1024 * 1024) +#define BATCH_SIZE_LENGTH (4) + +namespace doris::vectorized { + +ArrowBatchReader::ArrowBatchReader(io::FileReaderSPtr file_reader) + : _file_reader(file_reader), + _read_buf(new uint8_t[DEFAULT_READ_BUF_CAP]), + _read_buf_cap(DEFAULT_READ_BUF_CAP), + _read_buf_pos(0), + _read_buf_len(0), + _batch_size(-1) {} + +ArrowBatchReader::~ArrowBatchReader() = default; + +Status ArrowBatchReader::get_one_batch(uint8_t** data, int* length) { + _init(); + RETURN_IF_ERROR(_get_batch_size()); + RETURN_IF_ERROR(_get_batch_value()); + + *data = &_read_buf[_read_buf_pos]; + if (_batch_size <= 0) { + *length = 0; + } else { + *length = _read_buf_len; + } + return Status::OK(); +} + +void ArrowBatchReader::_init() { + _read_buf_pos = _read_buf_pos + _read_buf_len; + _batch_size = -1; + _read_buf_len = 0; +} + +Status ArrowBatchReader::_get_batch_size() { + if (_batch_size >= 0) { + return Status::OK(); + } + + if (_get_valid_cap() < BATCH_SIZE_LENGTH) { + memmove(_read_buf, _read_buf + _read_buf_pos, _read_buf_len); + } + + while (_read_buf_len < BATCH_SIZE_LENGTH) { + Slice file_slice(_read_buf + _read_buf_pos + _read_buf_len, _get_valid_cap()); + size_t read_length = 0; + RETURN_IF_ERROR(_file_reader->read_at(0, file_slice, &read_length, NULL)); + if (read_length == 0) { + return Status::OK(); + } + _read_buf_len += read_length; + } + + _batch_size = _convert_to_length(_read_buf); + + if (_batch_size < 0) { + return Status::InternalError("Invalid batch size"); + } + + _read_buf_pos += BATCH_SIZE_LENGTH; + _read_buf_len -= BATCH_SIZE_LENGTH; + + if (_batch_size > _read_buf_cap) { + while (_read_buf_cap < _batch_size) { + _read_buf_cap *= 2; + } + + uint8_t* new_read_buf = new uint8_t[_read_buf_cap]; + memmove(new_read_buf, _read_buf + _read_buf_pos, _read_buf_len); + delete[] _read_buf; + _read_buf = new_read_buf; + } + return Status::OK(); +} + +int ArrowBatchReader::_get_valid_cap() { + return _read_buf_cap - _read_buf_pos - _read_buf_len; +} + +uint32_t ArrowBatchReader::_convert_to_length(uint8_t* data) { + uint32_t len = (((uint32_t)data[0]) << 24) & 0xff000000; Review Comment: warning: 24 is a magic number; consider replacing it with a named constant [readability-magic-numbers] ```cpp uint32_t len = (((uint32_t)data[0]) << 24) & 0xff000000; ^ ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
