You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by GitBox <gi...@apache.org> on 2022/06/30 02:06:33 UTC

[GitHub] [doris] yinzhijian commented on a diff in pull request #10492: [feature-wip] support avro format in routine load and stream load

yinzhijian commented on code in PR #10492:
URL: https://github.com/apache/doris/pull/10492#discussion_r910542704


##########
be/src/exec/avro_scanner.cpp:
##########
@@ -0,0 +1,525 @@
+// 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 <exec/avro_scanner.h>
+
+#include <fstream>
+#include <iostream>
+#include <sstream>
+
+#include "common/config.h"
+#include "env/env.h"
+#include "exprs/expr.h"
+#include "gutil/strings/split.h"
+#include "runtime/exec_env.h"
+#include "runtime/mem_tracker.h"
+#include "runtime/runtime_state.h"
+
+namespace doris {
+
+void deserializeNoop(MemPool*, Tuple*, SlotDescriptor*, avro::Decoder&, int) {}
+
+AvroScanner::AvroScanner(RuntimeState* state, RuntimeProfile* profile,
+                         const TBrokerScanRangeParams& params,
+                         const std::vector<TBrokerRangeDesc>& ranges,
+                         const std::vector<TNetworkAddress>& broker_addresses,
+                         const std::vector<TExpr>& pre_filter_texprs, ScannerCounter* counter)
+        : BaseScanner(state, profile, params, ranges, broker_addresses, pre_filter_texprs, counter),
+          _ranges(ranges),
+          _broker_addresses(broker_addresses),
+          _cur_file_reader(nullptr),
+          _cur_avro_reader(nullptr),
+          _next_range(0),
+          _cur_reader_eof(false),
+          _scanner_eof(false) {}
+
+AvroScanner::~AvroScanner() {
+    close();
+}
+
+Status AvroScanner::open() {
+    Status st = BaseScanner::open();
+    RETURN_IF_ERROR(st);
+    return st;
+}
+
+Status AvroScanner::get_next(Tuple* tuple, MemPool* tuple_pool, bool* eof, bool* fill_tuple) {
+    SCOPED_TIMER(_read_timer);
+
+    // read one line
+    while (!_scanner_eof) {
+        if (_cur_file_reader == nullptr || _cur_reader_eof) {
+            RETURN_IF_ERROR(open_next_reader());
+            // If there isn't any more reader, break this
+            if (_scanner_eof) {
+                break;
+            }
+        }
+
+        bool is_empty_row = false;
+
+        RETURN_IF_ERROR(_cur_avro_reader->read_avro_row(_src_tuple, _src_slot_descs, tuple_pool,
+                                                        &is_empty_row, &_cur_reader_eof));
+        if (is_empty_row) {
+            continue;
+        }
+        COUNTER_UPDATE(_rows_read_counter, 1);
+        SCOPED_TIMER(_materialize_timer);
+        if (fill_dest_tuple(tuple, tuple_pool, fill_tuple)) {
+            break; // break if true
+        }
+    }
+    if (_scanner_eof) {
+        *eof = true;
+    } else {
+        *eof = false;
+    }
+    return Status::OK();
+}
+
+void AvroScanner::close() {
+    BaseScanner::close();
+    if (_cur_avro_reader != nullptr) {
+        delete _cur_avro_reader;
+        _cur_avro_reader = nullptr;
+    }
+    if (_cur_file_reader != nullptr) {
+        if (_stream_load_pipe != nullptr) {
+            _stream_load_pipe.reset();
+        } else {
+            delete _cur_file_reader;
+        }
+        _cur_file_reader = nullptr;
+    }
+}
+
+Status AvroScanner::open_file_reader() {
+    if (_cur_file_reader != nullptr) {
+        if (_stream_load_pipe != nullptr) {
+            _stream_load_pipe.reset();
+            _cur_file_reader = nullptr;
+        } else {
+            delete _cur_file_reader;
+            _cur_file_reader = nullptr;
+        }
+    }
+
+    const TBrokerRangeDesc& range = _ranges[_next_range];
+
+    switch (range.file_type) {
+    case TFileType::FILE_STREAM: {
+        _stream_load_pipe = _state->exec_env()->load_stream_mgr()->get(range.load_id);
+        if (_stream_load_pipe == nullptr) {
+            VLOG_NOTICE << "unknown stream load id: " << UniqueId(range.load_id);
+            return Status::InternalError("unknown stream load id");
+        }
+        _cur_file_reader = _stream_load_pipe.get();
+        break;
+    }
+    case TFileType::FILE_LOCAL:
+    case TFileType::FILE_BROKER:
+    case TFileType::FILE_S3:
+    default: {
+        std::stringstream ss;
+        ss << "Only support stream type. Unsupport file type, type=" << range.file_type;
+        return Status::InternalError(ss.str());
+    }
+    }
+    _cur_reader_eof = false;
+    return Status::OK();
+}
+
+Status AvroScanner::open_avro_reader() {
+    if (_cur_avro_reader != nullptr) {
+        delete _cur_avro_reader;
+        _cur_avro_reader = nullptr;
+    }
+
+    _cur_avro_reader = new AvroReader(_state, _counter, _profile, _cur_file_reader, nullptr);
+    RETURN_IF_ERROR(_cur_avro_reader->init());
+    return Status::OK();
+}
+
+Status AvroScanner::open_next_reader() {
+    if (_next_range >= _ranges.size()) {
+        _scanner_eof = true;
+        return Status::OK();
+    }
+
+    RETURN_IF_ERROR(open_file_reader());
+    RETURN_IF_ERROR(open_avro_reader());
+    _next_range++;
+
+    return Status::OK();
+}
+
+////// class AvroReader
+AvroReader::AvroReader(RuntimeState* state, ScannerCounter* counter, RuntimeProfile* profile,
+                       FileReader* file_reader, LineReader* line_reader)
+        : _next_line(0),
+          _total_lines(0),
+          _state(state),
+          _counter(counter),
+          _profile(profile),
+          _file_reader(file_reader),
+          _line_reader(line_reader),
+          _closed(false),
+          _decoder(avro::binaryDecoder()),
+          _in(nullptr) {
+    _bytes_read_counter = ADD_COUNTER(_profile, "BytesRead", TUnit::BYTES);
+    _read_timer = ADD_TIMER(_profile, "ReadTime");
+    _file_read_timer = ADD_TIMER(_profile, "FileReadTime");
+}
+
+AvroReader::~AvroReader() {
+    _close();
+}
+
+Status AvroReader::init() {
+    bool exist = FileUtils::check_exist(config::avro_schema_file_path);
+    if (!exist) {
+        return Status::InternalError("there is no avro schema file at " +
+                                     config::avro_schema_file_path +
+                                     ". Please put an schema file in json format.");
+    } else {

Review Comment:
   The else statement does not appear to be required.
   ```suggestion
       } 
   ```



-- 
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: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org