morningman commented on code in PR #26709:
URL: https://github.com/apache/doris/pull/26709#discussion_r1398411551


##########
be/src/vec/exec/format/arrow/arrow_reader.h:
##########
@@ -0,0 +1,78 @@
+// 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 "arrow_pip_input_stream.h"
+#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 ArrowReader : public GenericReader {

Review Comment:
   Better name it as `ArrowStreamReader`? Because it only support reading from 
stream



##########
be/src/vec/exec/format/arrow/arrow_pip_input_stream.cpp:
##########
@@ -0,0 +1,95 @@
+// 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_pip_input_stream.h"
+
+#include "arrow/array.h"
+#include "arrow/buffer.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"
+
+namespace doris::vectorized {
+
+ArrowPipInputStream::ArrowPipInputStream(io::FileReaderSPtr file_reader)
+        : _file_reader(file_reader), _pos(0), _begin(true), _read_buf(new 
uint8_t[4]) {
+    set_mode(arrow::io::FileMode::READ);
+}
+
+arrow::Status ArrowPipInputStream::Close() {
+    return arrow::Status::OK();
+}
+
+bool ArrowPipInputStream::closed() const {
+    return false;
+}
+
+arrow::Result<int64_t> ArrowPipInputStream::Tell() const {
+    return _pos;
+}
+
+Status ArrowPipInputStream::HasNext(bool* get) {
+    Slice file_slice(_read_buf, 4);
+    size_t read_length = 0;
+    RETURN_IF_ERROR(_file_reader->read_at(0, file_slice, &read_length, NULL));
+    if (read_length == 0) {
+        *get = false;
+    } else {
+        *get = true;
+    }
+    return Status::OK();
+}
+
+arrow::Result<int64_t> ArrowPipInputStream::Read(int64_t nbytes, void* out) {
+    uint8_t* out_ptr = (uint8_t*)out;
+    if (_begin) {
+        memmove(out_ptr, _read_buf, 4);
+        out_ptr += 4;
+        nbytes -= 4;
+    }
+
+    Slice file_slice(out_ptr, nbytes);
+    size_t read_length = 0;
+    Status status = _file_reader->read_at(0, file_slice, &read_length, NULL);
+    if (UNLIKELY(!status.ok())) {
+        return arrow::Status::IOError("Error to read data from pip");
+    }
+    // pos_ += (int64_t)read_length;

Review Comment:
   remove unused code



##########
be/src/vec/exec/format/arrow/batch_with_length_reader.cpp:
##########
@@ -0,0 +1,156 @@
+// 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)
+
+
+// BatchWithLengthReader used in fixed format:  <len><value><len><value>...
+// The <len> is of type int and represents the length of the <value>.

Review Comment:
   better use int64 to support batch size larger than 2GB in future



##########
be/src/vec/exec/format/arrow/batch_with_length_reader.cpp:
##########
@@ -0,0 +1,226 @@
+// 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"
+#include "arrow/result.h"

Review Comment:
   duplicate



##########
be/src/vec/exec/format/arrow/arrow_reader.cpp:
##########
@@ -0,0 +1,115 @@
+// 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_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 "arrow_pip_input_stream.h"
+#include "common/logging.h"
+#include "io/fs/stream_load_pipe.h"
+#include "olap/wal_manager.h"
+#include "runtime/descriptors.h"
+#include "runtime/runtime_state.h"
+#include "vec/utils/arrow_column_to_doris_column.h"
+
+namespace doris {
+class RuntimeProfile;
+} // namespace doris
+
+namespace doris::vectorized {
+
+ArrowReader::ArrowReader(RuntimeState* state, RuntimeProfile* profile, 
ScannerCounter* counter,
+                         const TFileScanRangeParams& params, const 
TFileRangeDesc& range,
+                         const std::vector<SlotDescriptor*>& file_slot_descs, 
io::IOContext* io_ctx)
+        : _state(state), _range(range), _file_slot_descs(file_slot_descs), 
_file_reader(nullptr) {}
+
+ArrowReader::~ArrowReader() = default;
+
+Status ArrowReader::init_reader() {
+    RETURN_IF_ERROR(FileFactory::create_pipe_reader(_range.load_id, 
&_file_reader, _state, false));
+    _pip_stream = ArrowPipInputStream::create_unique(_file_reader);
+    return Status::OK();
+}
+
+Status ArrowReader::get_next_block(Block* block, size_t* read_rows, bool* eof) 
{
+    bool has_next = false;
+    RETURN_IF_ERROR(_pip_stream->HasNext(&has_next));
+    if (!has_next) {
+        *read_rows = 0;
+        *eof = true;
+        return Status::OK();
+    }
+
+    // create a reader to read data
+    arrow::Result<std::shared_ptr<arrow::ipc::RecordBatchStreamReader>> tRet =
+            arrow::ipc::RecordBatchStreamReader::Open(_pip_stream.get(),
+                                                      
arrow::ipc::IpcReadOptions::Defaults());
+    if (!tRet.ok()) {
+        LOG(ERROR) << "failed to open stream reader: " << 
tRet.status().message();
+        return Status::InternalError("failed to open stream reader: {}", 
tRet.status().message());
+    }
+    auto reader = std::move(tRet).ValueUnsafe();
+
+    // get arrow data from reader
+    arrow::Result<arrow::RecordBatchVector> tRet2 = reader->ToRecordBatches();
+    if (!tRet2.ok()) {
+        LOG(ERROR) << "failed to read batch: " << tRet2.status().message();
+        return Status::InternalError("failed to read batch: {}", 
tRet2.status().message());
+    }
+    std::vector<std::shared_ptr<arrow::RecordBatch>> out_batches = 
std::move(tRet2).ValueUnsafe();
+
+    // convert arrow batch to block
+    cctz::time_zone ctzz;
+    TimezoneUtils::find_cctz_time_zone("UTC", ctzz);

Review Comment:
   2 questions:
   1. Why using UTC
   2. If always be UTC, this should be done once when init the arrow reader



##########
be/src/vec/exec/format/arrow/arrow_reader.cpp:
##########
@@ -0,0 +1,115 @@
+// 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_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 "arrow_pip_input_stream.h"
+#include "common/logging.h"
+#include "io/fs/stream_load_pipe.h"
+#include "olap/wal_manager.h"
+#include "runtime/descriptors.h"
+#include "runtime/runtime_state.h"
+#include "vec/utils/arrow_column_to_doris_column.h"
+
+namespace doris {
+class RuntimeProfile;
+} // namespace doris
+
+namespace doris::vectorized {
+
+ArrowReader::ArrowReader(RuntimeState* state, RuntimeProfile* profile, 
ScannerCounter* counter,
+                         const TFileScanRangeParams& params, const 
TFileRangeDesc& range,
+                         const std::vector<SlotDescriptor*>& file_slot_descs, 
io::IOContext* io_ctx)
+        : _state(state), _range(range), _file_slot_descs(file_slot_descs), 
_file_reader(nullptr) {}
+
+ArrowReader::~ArrowReader() = default;
+
+Status ArrowReader::init_reader() {
+    RETURN_IF_ERROR(FileFactory::create_pipe_reader(_range.load_id, 
&_file_reader, _state, false));
+    _pip_stream = ArrowPipInputStream::create_unique(_file_reader);
+    return Status::OK();
+}
+
+Status ArrowReader::get_next_block(Block* block, size_t* read_rows, bool* eof) 
{
+    bool has_next = false;
+    RETURN_IF_ERROR(_pip_stream->HasNext(&has_next));
+    if (!has_next) {
+        *read_rows = 0;
+        *eof = true;
+        return Status::OK();
+    }
+
+    // create a reader to read data
+    arrow::Result<std::shared_ptr<arrow::ipc::RecordBatchStreamReader>> tRet =

Review Comment:
   Why need to create stream reader for each call?
   Can it be reused?



-- 
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]

Reply via email to