github-actions[bot] commented on code in PR #26709: URL: https://github.com/apache/doris/pull/26709#discussion_r1392553224
########## be/src/vec/exec/format/arrow/batch_with_length_reader.cpp: ########## @@ -0,0 +1,147 @@ +// 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 "batch_with_length_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/batch_with_length_reader.cpp: ########## @@ -0,0 +1,147 @@ +// 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 "batch_with_length_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/batch_with_length_reader.cpp: ########## @@ -0,0 +1,147 @@ +// 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 "batch_with_length_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/batch_with_length_reader.cpp: ########## @@ -0,0 +1,147 @@ +// 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 "batch_with_length_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 { + +BatchWithLengthReader::BatchWithLengthReader(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) {} + +BatchWithLengthReader::~BatchWithLengthReader() = default; + +Status BatchWithLengthReader::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 = _batch_size; + } + return Status::OK(); +} + +void BatchWithLengthReader::_init() { + if (_batch_size > 0) { + _read_buf_pos += _batch_size; + _read_buf_len -= _batch_size; + } + _batch_size = -1; +} + +Status BatchWithLengthReader::_get_batch_size() { + if (_batch_size >= 0) { + return Status::OK(); + } + + _ensure_cap(BATCH_SIZE_LENGTH); + + RETURN_IF_ERROR(_get_data_from_reader(BATCH_SIZE_LENGTH)); + if (_read_buf_len < BATCH_SIZE_LENGTH) { + return Status::OK(); + } + + _batch_size = _convert_to_length(_read_buf + _read_buf_pos); + + 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; + } + + // The current buff capacity is not enough and needs to be expanded + auto* 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; + _read_buf_pos = 0; + } + return Status::OK(); +} + +Status BatchWithLengthReader::_get_data_from_reader(int req_size) { + while (_read_buf_len < req_size) { + Slice file_slice(_read_buf + _read_buf_pos + _read_buf_len, + _read_buf_cap - _read_buf_pos - _read_buf_len); + size_t read_length = 0; + RETURN_IF_ERROR(_file_reader->read_at(0, file_slice, &read_length, NULL)); + _read_buf_len += read_length; + if (read_length == 0) { + break; + } + } + return Status::OK(); +} + +void BatchWithLengthReader::_ensure_cap(int req_size) { + if (_read_buf_len < req_size && _read_buf_cap - _read_buf_pos < req_size) { + // there is not enough space for data, + // we need to move the data that has been used before and the useful data forward + memmove(_read_buf, _read_buf + _read_buf_pos, _read_buf_len); + _read_buf_pos = 0; + } +} + +uint32_t BatchWithLengthReader::_convert_to_length(const 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; ^ ``` ########## be/src/vec/exec/format/arrow/batch_with_length_reader.cpp: ########## @@ -0,0 +1,147 @@ +// 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 "batch_with_length_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 { + +BatchWithLengthReader::BatchWithLengthReader(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) {} + +BatchWithLengthReader::~BatchWithLengthReader() = default; + +Status BatchWithLengthReader::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 = _batch_size; + } + return Status::OK(); +} + +void BatchWithLengthReader::_init() { + if (_batch_size > 0) { + _read_buf_pos += _batch_size; + _read_buf_len -= _batch_size; + } + _batch_size = -1; +} + +Status BatchWithLengthReader::_get_batch_size() { + if (_batch_size >= 0) { + return Status::OK(); + } + + _ensure_cap(BATCH_SIZE_LENGTH); + + RETURN_IF_ERROR(_get_data_from_reader(BATCH_SIZE_LENGTH)); + if (_read_buf_len < BATCH_SIZE_LENGTH) { + return Status::OK(); + } + + _batch_size = _convert_to_length(_read_buf + _read_buf_pos); + + 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; + } + + // The current buff capacity is not enough and needs to be expanded + auto* 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; + _read_buf_pos = 0; + } + return Status::OK(); +} + +Status BatchWithLengthReader::_get_data_from_reader(int req_size) { + while (_read_buf_len < req_size) { + Slice file_slice(_read_buf + _read_buf_pos + _read_buf_len, + _read_buf_cap - _read_buf_pos - _read_buf_len); + size_t read_length = 0; + RETURN_IF_ERROR(_file_reader->read_at(0, file_slice, &read_length, NULL)); + _read_buf_len += read_length; + if (read_length == 0) { + break; + } + } + return Status::OK(); +} + +void BatchWithLengthReader::_ensure_cap(int req_size) { + if (_read_buf_len < req_size && _read_buf_cap - _read_buf_pos < req_size) { + // there is not enough space for data, + // we need to move the data that has been used before and the useful data forward + memmove(_read_buf, _read_buf + _read_buf_pos, _read_buf_len); + _read_buf_pos = 0; + } +} + +uint32_t BatchWithLengthReader::_convert_to_length(const 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/batch_with_length_reader.cpp: ########## @@ -0,0 +1,147 @@ +// 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 "batch_with_length_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 { + +BatchWithLengthReader::BatchWithLengthReader(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) {} + +BatchWithLengthReader::~BatchWithLengthReader() = default; + +Status BatchWithLengthReader::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 = _batch_size; + } + return Status::OK(); +} + +void BatchWithLengthReader::_init() { + if (_batch_size > 0) { + _read_buf_pos += _batch_size; + _read_buf_len -= _batch_size; + } + _batch_size = -1; +} + +Status BatchWithLengthReader::_get_batch_size() { + if (_batch_size >= 0) { + return Status::OK(); + } + + _ensure_cap(BATCH_SIZE_LENGTH); + + RETURN_IF_ERROR(_get_data_from_reader(BATCH_SIZE_LENGTH)); + if (_read_buf_len < BATCH_SIZE_LENGTH) { + return Status::OK(); + } + + _batch_size = _convert_to_length(_read_buf + _read_buf_pos); + + 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; + } + + // The current buff capacity is not enough and needs to be expanded + auto* 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; + _read_buf_pos = 0; + } + return Status::OK(); +} + +Status BatchWithLengthReader::_get_data_from_reader(int req_size) { + while (_read_buf_len < req_size) { + Slice file_slice(_read_buf + _read_buf_pos + _read_buf_len, + _read_buf_cap - _read_buf_pos - _read_buf_len); + size_t read_length = 0; + RETURN_IF_ERROR(_file_reader->read_at(0, file_slice, &read_length, NULL)); + _read_buf_len += read_length; + if (read_length == 0) { + break; + } + } + return Status::OK(); +} + +void BatchWithLengthReader::_ensure_cap(int req_size) { + if (_read_buf_len < req_size && _read_buf_cap - _read_buf_pos < req_size) { + // there is not enough space for data, + // we need to move the data that has been used before and the useful data forward + memmove(_read_buf, _read_buf + _read_buf_pos, _read_buf_len); + _read_buf_pos = 0; + } +} + +uint32_t BatchWithLengthReader::_convert_to_length(const 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/batch_with_length_reader.cpp: ########## @@ -0,0 +1,147 @@ +// 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 "batch_with_length_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 { + +BatchWithLengthReader::BatchWithLengthReader(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) {} + +BatchWithLengthReader::~BatchWithLengthReader() = default; + +Status BatchWithLengthReader::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 = _batch_size; + } + return Status::OK(); +} + +void BatchWithLengthReader::_init() { + if (_batch_size > 0) { + _read_buf_pos += _batch_size; + _read_buf_len -= _batch_size; + } + _batch_size = -1; +} + +Status BatchWithLengthReader::_get_batch_size() { + if (_batch_size >= 0) { + return Status::OK(); + } + + _ensure_cap(BATCH_SIZE_LENGTH); + + RETURN_IF_ERROR(_get_data_from_reader(BATCH_SIZE_LENGTH)); + if (_read_buf_len < BATCH_SIZE_LENGTH) { + return Status::OK(); + } + + _batch_size = _convert_to_length(_read_buf + _read_buf_pos); + + 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; + } + + // The current buff capacity is not enough and needs to be expanded + auto* 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; + _read_buf_pos = 0; + } + return Status::OK(); +} + +Status BatchWithLengthReader::_get_data_from_reader(int req_size) { + while (_read_buf_len < req_size) { + Slice file_slice(_read_buf + _read_buf_pos + _read_buf_len, + _read_buf_cap - _read_buf_pos - _read_buf_len); + size_t read_length = 0; + RETURN_IF_ERROR(_file_reader->read_at(0, file_slice, &read_length, NULL)); + _read_buf_len += read_length; + if (read_length == 0) { + break; + } + } + return Status::OK(); +} + +void BatchWithLengthReader::_ensure_cap(int req_size) { + if (_read_buf_len < req_size && _read_buf_cap - _read_buf_pos < req_size) { + // there is not enough space for data, + // we need to move the data that has been used before and the useful data forward + memmove(_read_buf, _read_buf + _read_buf_pos, _read_buf_len); + _read_buf_pos = 0; + } +} + +uint32_t BatchWithLengthReader::_convert_to_length(const 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/batch_with_length_reader.cpp: ########## @@ -0,0 +1,147 @@ +// 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 "batch_with_length_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 { + +BatchWithLengthReader::BatchWithLengthReader(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) {} + +BatchWithLengthReader::~BatchWithLengthReader() = default; + +Status BatchWithLengthReader::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 = _batch_size; + } + return Status::OK(); +} + +void BatchWithLengthReader::_init() { + if (_batch_size > 0) { + _read_buf_pos += _batch_size; + _read_buf_len -= _batch_size; + } + _batch_size = -1; +} + +Status BatchWithLengthReader::_get_batch_size() { + if (_batch_size >= 0) { + return Status::OK(); + } + + _ensure_cap(BATCH_SIZE_LENGTH); + + RETURN_IF_ERROR(_get_data_from_reader(BATCH_SIZE_LENGTH)); + if (_read_buf_len < BATCH_SIZE_LENGTH) { + return Status::OK(); + } + + _batch_size = _convert_to_length(_read_buf + _read_buf_pos); + + 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; + } + + // The current buff capacity is not enough and needs to be expanded + auto* 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; + _read_buf_pos = 0; + } + return Status::OK(); +} + +Status BatchWithLengthReader::_get_data_from_reader(int req_size) { + while (_read_buf_len < req_size) { + Slice file_slice(_read_buf + _read_buf_pos + _read_buf_len, + _read_buf_cap - _read_buf_pos - _read_buf_len); + size_t read_length = 0; + RETURN_IF_ERROR(_file_reader->read_at(0, file_slice, &read_length, NULL)); + _read_buf_len += read_length; + if (read_length == 0) { + break; + } + } + return Status::OK(); +} + +void BatchWithLengthReader::_ensure_cap(int req_size) { + if (_read_buf_len < req_size && _read_buf_cap - _read_buf_pos < req_size) { + // there is not enough space for data, + // we need to move the data that has been used before and the useful data forward + memmove(_read_buf, _read_buf + _read_buf_pos, _read_buf_len); + _read_buf_pos = 0; + } +} + +uint32_t BatchWithLengthReader::_convert_to_length(const 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/batch_with_length_reader.cpp: ########## @@ -0,0 +1,147 @@ +// 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 "batch_with_length_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 { + +BatchWithLengthReader::BatchWithLengthReader(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) {} + +BatchWithLengthReader::~BatchWithLengthReader() = default; + +Status BatchWithLengthReader::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 = _batch_size; + } + return Status::OK(); +} + +void BatchWithLengthReader::_init() { + if (_batch_size > 0) { + _read_buf_pos += _batch_size; + _read_buf_len -= _batch_size; + } + _batch_size = -1; +} + +Status BatchWithLengthReader::_get_batch_size() { + if (_batch_size >= 0) { + return Status::OK(); + } + + _ensure_cap(BATCH_SIZE_LENGTH); + + RETURN_IF_ERROR(_get_data_from_reader(BATCH_SIZE_LENGTH)); + if (_read_buf_len < BATCH_SIZE_LENGTH) { + return Status::OK(); + } + + _batch_size = _convert_to_length(_read_buf + _read_buf_pos); + + 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; + } + + // The current buff capacity is not enough and needs to be expanded + auto* 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; + _read_buf_pos = 0; + } + return Status::OK(); +} + +Status BatchWithLengthReader::_get_data_from_reader(int req_size) { + while (_read_buf_len < req_size) { + Slice file_slice(_read_buf + _read_buf_pos + _read_buf_len, + _read_buf_cap - _read_buf_pos - _read_buf_len); + size_t read_length = 0; + RETURN_IF_ERROR(_file_reader->read_at(0, file_slice, &read_length, NULL)); + _read_buf_len += read_length; + if (read_length == 0) { + break; + } + } + return Status::OK(); +} + +void BatchWithLengthReader::_ensure_cap(int req_size) { + if (_read_buf_len < req_size && _read_buf_cap - _read_buf_pos < req_size) { + // there is not enough space for data, + // we need to move the data that has been used before and the useful data forward + memmove(_read_buf, _read_buf + _read_buf_pos, _read_buf_len); + _read_buf_pos = 0; + } +} + +uint32_t BatchWithLengthReader::_convert_to_length(const 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/batch_with_length_reader.h: ########## @@ -0,0 +1,77 @@ +// 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/batch_with_length_reader.cpp: ########## @@ -0,0 +1,147 @@ +// 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 "batch_with_length_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 { + +BatchWithLengthReader::BatchWithLengthReader(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) {} + +BatchWithLengthReader::~BatchWithLengthReader() = default; + +Status BatchWithLengthReader::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 = _batch_size; + } + return Status::OK(); +} + +void BatchWithLengthReader::_init() { + if (_batch_size > 0) { + _read_buf_pos += _batch_size; + _read_buf_len -= _batch_size; + } + _batch_size = -1; +} + +Status BatchWithLengthReader::_get_batch_size() { + if (_batch_size >= 0) { + return Status::OK(); + } + + _ensure_cap(BATCH_SIZE_LENGTH); + + RETURN_IF_ERROR(_get_data_from_reader(BATCH_SIZE_LENGTH)); + if (_read_buf_len < BATCH_SIZE_LENGTH) { + return Status::OK(); + } + + _batch_size = _convert_to_length(_read_buf + _read_buf_pos); + + 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; + } + + // The current buff capacity is not enough and needs to be expanded + auto* 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; + _read_buf_pos = 0; + } + return Status::OK(); +} + +Status BatchWithLengthReader::_get_data_from_reader(int req_size) { + while (_read_buf_len < req_size) { + Slice file_slice(_read_buf + _read_buf_pos + _read_buf_len, + _read_buf_cap - _read_buf_pos - _read_buf_len); + size_t read_length = 0; + RETURN_IF_ERROR(_file_reader->read_at(0, file_slice, &read_length, NULL)); + _read_buf_len += read_length; + if (read_length == 0) { + break; + } + } + return Status::OK(); +} + +void BatchWithLengthReader::_ensure_cap(int req_size) { + if (_read_buf_len < req_size && _read_buf_cap - _read_buf_pos < req_size) { + // there is not enough space for data, + // we need to move the data that has been used before and the useful data forward + memmove(_read_buf, _read_buf + _read_buf_pos, _read_buf_len); + _read_buf_pos = 0; + } +} + +uint32_t BatchWithLengthReader::_convert_to_length(const 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; ^ ``` -- 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]
