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/29 08:08:20 UTC

[GitHub] [doris] Henry2SS opened a new pull request, #10492: [feature-wip] support avro format in routine load and stream load

Henry2SS opened a new pull request, #10492:
URL: https://github.com/apache/doris/pull/10492

   # Proposed changes
   
   Issue Number: close #7650 
   
   ## Problem Summary:
   
   Support avro format in routine load and stream load.
   There are still several places to be improved.
   
   And tested locally for more than a week, loading is OK and no coredump, no mem leak.
   
   Still in progress:
   1. support routine load property named `avro_schema_path` to sepcified a schema file
   2. support avro::array type to doris::array
   3. support broker load in the future 
   
   ## Checklist(Required)
   
   1. Does it affect the original behavior: (No)
   4. Has unit tests been added: (No)
   5. Has document been added or modified: (No)
   6. Does it need to update dependencies: (Yes)
   7. Are there any changes that cannot be rolled back: (No)
   
   ## Further comments
   
   If this is a relatively large or complex change, kick off the discussion at [dev@doris.apache.org](mailto:dev@doris.apache.org) by explaining why you chose the solution you did and what alternatives you considered, etc...
   


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


[GitHub] [doris] github-actions[bot] commented on pull request #10492: [feature-wip] support avro format in routine load and stream load

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #10492:
URL: https://github.com/apache/doris/pull/10492#issuecomment-1437687446

   We're closing this PR because it hasn't been updated in a while.
   This isn't a judgement on the merit of the PR in any way. It's just a way of keeping the PR queue manageable.
   If you'd like to revive this PR, please reopen it and feel free a maintainer to remove the Stale tag!


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


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

Posted by GitBox <gi...@apache.org>.
Henry2SS commented on code in PR #10492:
URL: https://github.com/apache/doris/pull/10492#discussion_r913340417


##########
be/src/common/config.h:
##########
@@ -763,6 +763,9 @@ CONF_Int32(quick_compaction_batch_size, "10");
 // do compaction min rowsets
 CONF_Int32(quick_compaction_min_rowsets, "10");
 
+// Avro schema file path, set default to "${DORIS_HOME}/conf/avro_schema.json"
+CONF_String(avro_schema_file_path, "${DORIS_HOME}/conf/avro_schema.json");

Review Comment:
   should have a property of routine load named `avro_schema_name`, and the schema should be put on every BE.



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


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

Posted by GitBox <gi...@apache.org>.
Henry2SS commented on code in PR #10492:
URL: https://github.com/apache/doris/pull/10492#discussion_r913606632


##########
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 {
+        try {
+            _schema = avro::compileJsonSchemaFromFile(config::avro_schema_file_path.c_str());
+        } catch (avro::Exception& e) {
+            return Status::InternalError(std::string("schema get from json failed.") + e.what());
+        }
+    }
+
+    if (_schema.root()->type() != avro::AVRO_RECORD) {
+        return Status::DataQualityError("Root schema must be a record");
+    }
+
+    return Status::OK();
+}
+
+void AvroReader::_close() {
+    if (_closed) {
+        return;
+    }
+    _closed = true;
+}
+
+Status AvroReader::_get_avro_doc(size_t* size, bool* eof, MemPool* tuple_pool, Tuple* tuple,
+                                 const std::vector<SlotDescriptor*>& slot_descs) {
+    SCOPED_TIMER(_file_read_timer);
+
+    if (_avro_str_ptr != nullptr) {
+        _avro_str_ptr.reset();
+    }
+    int64_t length = 0;
+    RETURN_IF_ERROR(_file_reader->read_one_message(&_avro_str_ptr, &length));
+
+    *size = length;
+    if (length == 0) {
+        *eof = true;
+    }
+
+    _bytes_read_counter += *size;
+    if (*eof) {
+        return Status::OK();
+    }
+
+    try {
+        auto schema_root = _schema.root();
+        _in = avro::memoryInputStream(_avro_str_ptr.get(), *size);
+        _decoder->init(*_in);
+    } catch (avro::Exception& e) {
+        return Status::DataQualityError(std::string("data quality is not good.") + e.what());
+    }
+
+    return Status::OK();
+}
+
+void AvroReader::_fill_slot(Tuple* tuple, SlotDescriptor* slot_desc, MemPool* mem_pool,
+                            const uint8_t* value, int32_t len) {
+    tuple->set_not_null(slot_desc->null_indicator_offset());
+    void* slot = tuple->get_slot(slot_desc->tuple_offset());
+    StringValue* str_slot = reinterpret_cast<StringValue*>(slot);
+    str_slot->ptr = reinterpret_cast<char*>(mem_pool->allocate(len));
+    memcpy(str_slot->ptr, value, len);
+    str_slot->len = len;
+}
+
+AvroReader::DeserializeFn AvroReader::createDeserializeFn(avro::NodePtr root_node,
+                                                          SlotDescriptor* slot_desc) {
+    uint8_t tmp_buf[128] = {0};
+    int32_t wbytes = 0;
+    switch (root_node->type()) {
+    case avro::AVRO_BYTES:
+        [[fallthrough]];
+    case avro::AVRO_STRING: {
+        return [val_string = std::string(), this](MemPool* tuple_pool, Tuple* tuple,
+                                                  SlotDescriptor* slot_desc, avro::Decoder& decoder,
+                                                  int nullcount) mutable {
+            decoder.decodeString(val_string);
+            if (val_string.empty()) {
+                if (slot_desc->is_nullable()) {
+                    tuple->set_null(slot_desc->null_indicator_offset());
+                    nullcount++;
+                } else {
+                    throw avro::Exception("NULL data for non-nullable column" +
+                                          slot_desc->col_name());
+                }
+            } else {
+                _fill_slot(tuple, slot_desc, tuple_pool, (uint8_t*)val_string.c_str(),
+                           (int32_t)strlen(val_string.c_str()));
+            }
+        };
+    } break;
+    case avro::AVRO_INT: {
+        return [tmp_buf, wbytes, this](MemPool* tuple_pool, Tuple* tuple, SlotDescriptor* slot_desc,
+                                       avro::Decoder& decoder, int nullcount) mutable {
+            int32_t val_int = decoder.decodeInt();
+            wbytes = sprintf((char*)tmp_buf, "%d", val_int);
+            _fill_slot(tuple, slot_desc, tuple_pool, tmp_buf, wbytes);
+        };
+    } break;
+    case avro::AVRO_LONG: {
+        return [tmp_buf, wbytes, this](MemPool* tuple_pool, Tuple* tuple, SlotDescriptor* slot_desc,
+                                       avro::Decoder& decoder, int nullcount) mutable {
+            int64_t val_long = decoder.decodeLong();
+            wbytes = sprintf((char*)tmp_buf, "%ld", val_long);
+            _fill_slot(tuple, slot_desc, tuple_pool, tmp_buf, wbytes);
+        };
+    } break;
+    case avro::AVRO_FLOAT: {
+        return [tmp_buf, wbytes, this](MemPool* tuple_pool, Tuple* tuple, SlotDescriptor* slot_desc,
+                                       avro::Decoder& decoder, int nullcount) mutable {
+            float val_float = decoder.decodeFloat();
+            wbytes = sprintf((char*)tmp_buf, "%f", val_float);
+            _fill_slot(tuple, slot_desc, tuple_pool, tmp_buf, wbytes);
+        };
+    } break;
+    case avro::AVRO_DOUBLE: {
+        return [tmp_buf, wbytes, this](MemPool* tuple_pool, Tuple* tuple, SlotDescriptor* slot_desc,
+                                       avro::Decoder& decoder, int nullcount) mutable {
+            double val_double = decoder.decodeDouble();
+            wbytes = sprintf((char*)tmp_buf, "%lf", val_double);
+            _fill_slot(tuple, slot_desc, tuple_pool, tmp_buf, wbytes);
+        };
+    } break;
+    case avro::AVRO_BOOL: {
+        return [tmp_buf, wbytes, this](MemPool* tuple_pool, Tuple* tuple, SlotDescriptor* slot_desc,
+                                       avro::Decoder& decoder, int nullcount) mutable {
+            bool val_bool = decoder.decodeBool();
+            wbytes = sprintf((char*)tmp_buf, "%d", val_bool);
+            _fill_slot(tuple, slot_desc, tuple_pool, tmp_buf, wbytes);
+        };
+    } break;
+    case avro::AVRO_ARRAY: {
+        // should have array type info
+        // TODO :
+    } break;
+    case avro::AVRO_UNION: {
+        auto nullable_deserializer = [root_node, slot_desc, this](size_t non_null_union_index) {
+            auto nested_deserialize =
+                    createDeserializeFn(root_node->leafAt(non_null_union_index), slot_desc);
+            return [non_null_union_index, nested_deserialize, this](
+                           MemPool* tuple_pool, Tuple* tuple, SlotDescriptor* slot_desc,
+                           avro::Decoder& decoder, int nullcount) mutable {
+                size_t union_index = decoder.decodeUnionIndex();
+                if (union_index == non_null_union_index) {
+                    nested_deserialize(tuple_pool, tuple, slot_desc, decoder, nullcount);
+                } else {
+                    tuple->set_null(slot_desc->null_indicator_offset());
+                }
+            };
+        };
+        if (root_node->leaves() == 2 && slot_desc->is_nullable()) {
+            if (root_node->leafAt(0)->type() == avro::AVRO_NULL) return nullable_deserializer(1);
+            if (root_node->leafAt(1)->type() == avro::AVRO_NULL) return nullable_deserializer(0);
+        }
+    } break;
+    case avro::AVRO_NULL: {
+        if (slot_desc->is_nullable()) {
+            return [this](MemPool* tuple_pool, Tuple* tuple, SlotDescriptor* slot_desc,
+                          avro::Decoder& decoder, int nullcount) mutable {
+                tuple->set_null(slot_desc->null_indicator_offset());
+                nullcount++;
+            };
+        } else {
+            throw avro::Exception("NULL data for non-nullable column" + slot_desc->col_name());
+        }
+    } break;
+    case avro::AVRO_ENUM:
+        [[fallthrough]];
+    case avro::AVRO_FIXED:
+        [[fallthrough]];
+    case avro::AVRO_MAP:
+        [[fallthrough]];
+    case avro::AVRO_RECORD:
+        [[fallthrough]];
+    default: {
+        throw avro::Exception("Not support " + root_node->type() + std::string("type yet."));
+    } break;
+    }
+    throw avro::Exception("Type " + slot_desc->type().type +
+                          std::string("is not compatible with Avro ") +

Review Comment:
   Thx for your advice~ Fixed all the comments above.



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


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

Posted by GitBox <gi...@apache.org>.
yinzhijian commented on code in PR #10492:
URL: https://github.com/apache/doris/pull/10492#discussion_r910543619


##########
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 {
+        try {
+            _schema = avro::compileJsonSchemaFromFile(config::avro_schema_file_path.c_str());
+        } catch (avro::Exception& e) {
+            return Status::InternalError(std::string("schema get from json failed.") + e.what());
+        }
+    }
+
+    if (_schema.root()->type() != avro::AVRO_RECORD) {
+        return Status::DataQualityError("Root schema must be a record");
+    }
+
+    return Status::OK();
+}
+
+void AvroReader::_close() {
+    if (_closed) {
+        return;
+    }
+    _closed = true;
+}
+
+Status AvroReader::_get_avro_doc(size_t* size, bool* eof, MemPool* tuple_pool, Tuple* tuple,
+                                 const std::vector<SlotDescriptor*>& slot_descs) {
+    SCOPED_TIMER(_file_read_timer);
+
+    if (_avro_str_ptr != nullptr) {
+        _avro_str_ptr.reset();
+    }
+    int64_t length = 0;
+    RETURN_IF_ERROR(_file_reader->read_one_message(&_avro_str_ptr, &length));
+
+    *size = length;
+    if (length == 0) {
+        *eof = true;
+    }
+
+    _bytes_read_counter += *size;
+    if (*eof) {
+        return Status::OK();
+    }
+
+    try {
+        auto schema_root = _schema.root();
+        _in = avro::memoryInputStream(_avro_str_ptr.get(), *size);
+        _decoder->init(*_in);
+    } catch (avro::Exception& e) {
+        return Status::DataQualityError(std::string("data quality is not good.") + e.what());
+    }
+
+    return Status::OK();
+}
+
+void AvroReader::_fill_slot(Tuple* tuple, SlotDescriptor* slot_desc, MemPool* mem_pool,
+                            const uint8_t* value, int32_t len) {
+    tuple->set_not_null(slot_desc->null_indicator_offset());
+    void* slot = tuple->get_slot(slot_desc->tuple_offset());
+    StringValue* str_slot = reinterpret_cast<StringValue*>(slot);
+    str_slot->ptr = reinterpret_cast<char*>(mem_pool->allocate(len));
+    memcpy(str_slot->ptr, value, len);
+    str_slot->len = len;
+}
+
+AvroReader::DeserializeFn AvroReader::createDeserializeFn(avro::NodePtr root_node,
+                                                          SlotDescriptor* slot_desc) {
+    uint8_t tmp_buf[128] = {0};
+    int32_t wbytes = 0;
+    switch (root_node->type()) {
+    case avro::AVRO_BYTES:
+        [[fallthrough]];
+    case avro::AVRO_STRING: {
+        return [val_string = std::string(), this](MemPool* tuple_pool, Tuple* tuple,
+                                                  SlotDescriptor* slot_desc, avro::Decoder& decoder,
+                                                  int nullcount) mutable {
+            decoder.decodeString(val_string);
+            if (val_string.empty()) {
+                if (slot_desc->is_nullable()) {
+                    tuple->set_null(slot_desc->null_indicator_offset());
+                    nullcount++;
+                } else {
+                    throw avro::Exception("NULL data for non-nullable column" +
+                                          slot_desc->col_name());
+                }
+            } else {
+                _fill_slot(tuple, slot_desc, tuple_pool, (uint8_t*)val_string.c_str(),
+                           (int32_t)strlen(val_string.c_str()));
+            }
+        };
+    } break;
+    case avro::AVRO_INT: {
+        return [tmp_buf, wbytes, this](MemPool* tuple_pool, Tuple* tuple, SlotDescriptor* slot_desc,
+                                       avro::Decoder& decoder, int nullcount) mutable {
+            int32_t val_int = decoder.decodeInt();
+            wbytes = sprintf((char*)tmp_buf, "%d", val_int);
+            _fill_slot(tuple, slot_desc, tuple_pool, tmp_buf, wbytes);
+        };
+    } break;
+    case avro::AVRO_LONG: {
+        return [tmp_buf, wbytes, this](MemPool* tuple_pool, Tuple* tuple, SlotDescriptor* slot_desc,
+                                       avro::Decoder& decoder, int nullcount) mutable {
+            int64_t val_long = decoder.decodeLong();
+            wbytes = sprintf((char*)tmp_buf, "%ld", val_long);
+            _fill_slot(tuple, slot_desc, tuple_pool, tmp_buf, wbytes);
+        };
+    } break;
+    case avro::AVRO_FLOAT: {
+        return [tmp_buf, wbytes, this](MemPool* tuple_pool, Tuple* tuple, SlotDescriptor* slot_desc,
+                                       avro::Decoder& decoder, int nullcount) mutable {
+            float val_float = decoder.decodeFloat();
+            wbytes = sprintf((char*)tmp_buf, "%f", val_float);
+            _fill_slot(tuple, slot_desc, tuple_pool, tmp_buf, wbytes);
+        };
+    } break;
+    case avro::AVRO_DOUBLE: {
+        return [tmp_buf, wbytes, this](MemPool* tuple_pool, Tuple* tuple, SlotDescriptor* slot_desc,
+                                       avro::Decoder& decoder, int nullcount) mutable {
+            double val_double = decoder.decodeDouble();
+            wbytes = sprintf((char*)tmp_buf, "%lf", val_double);
+            _fill_slot(tuple, slot_desc, tuple_pool, tmp_buf, wbytes);
+        };
+    } break;
+    case avro::AVRO_BOOL: {
+        return [tmp_buf, wbytes, this](MemPool* tuple_pool, Tuple* tuple, SlotDescriptor* slot_desc,
+                                       avro::Decoder& decoder, int nullcount) mutable {
+            bool val_bool = decoder.decodeBool();
+            wbytes = sprintf((char*)tmp_buf, "%d", val_bool);
+            _fill_slot(tuple, slot_desc, tuple_pool, tmp_buf, wbytes);
+        };
+    } break;
+    case avro::AVRO_ARRAY: {
+        // should have array type info
+        // TODO :
+    } break;
+    case avro::AVRO_UNION: {
+        auto nullable_deserializer = [root_node, slot_desc, this](size_t non_null_union_index) {
+            auto nested_deserialize =
+                    createDeserializeFn(root_node->leafAt(non_null_union_index), slot_desc);
+            return [non_null_union_index, nested_deserialize, this](
+                           MemPool* tuple_pool, Tuple* tuple, SlotDescriptor* slot_desc,
+                           avro::Decoder& decoder, int nullcount) mutable {
+                size_t union_index = decoder.decodeUnionIndex();
+                if (union_index == non_null_union_index) {
+                    nested_deserialize(tuple_pool, tuple, slot_desc, decoder, nullcount);
+                } else {
+                    tuple->set_null(slot_desc->null_indicator_offset());
+                }
+            };
+        };
+        if (root_node->leaves() == 2 && slot_desc->is_nullable()) {
+            if (root_node->leafAt(0)->type() == avro::AVRO_NULL) return nullable_deserializer(1);
+            if (root_node->leafAt(1)->type() == avro::AVRO_NULL) return nullable_deserializer(0);
+        }
+    } break;
+    case avro::AVRO_NULL: {
+        if (slot_desc->is_nullable()) {
+            return [this](MemPool* tuple_pool, Tuple* tuple, SlotDescriptor* slot_desc,
+                          avro::Decoder& decoder, int nullcount) mutable {
+                tuple->set_null(slot_desc->null_indicator_offset());
+                nullcount++;
+            };
+        } else {
+            throw avro::Exception("NULL data for non-nullable column" + slot_desc->col_name());
+        }
+    } break;
+    case avro::AVRO_ENUM:
+        [[fallthrough]];
+    case avro::AVRO_FIXED:
+        [[fallthrough]];
+    case avro::AVRO_MAP:
+        [[fallthrough]];
+    case avro::AVRO_RECORD:
+        [[fallthrough]];
+    default: {
+        throw avro::Exception("Not support " + root_node->type() + std::string("type yet."));
+    } break;
+    }
+    throw avro::Exception("Type " + slot_desc->type().type +
+                          std::string("is not compatible with Avro ") +

Review Comment:
   ```suggestion
                             std::string(" is not compatible with Avro ") +
   ```



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


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

Posted by GitBox <gi...@apache.org>.
yinzhijian commented on code in PR #10492:
URL: https://github.com/apache/doris/pull/10492#discussion_r910543548


##########
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 {
+        try {
+            _schema = avro::compileJsonSchemaFromFile(config::avro_schema_file_path.c_str());
+        } catch (avro::Exception& e) {
+            return Status::InternalError(std::string("schema get from json failed.") + e.what());
+        }
+    }
+
+    if (_schema.root()->type() != avro::AVRO_RECORD) {
+        return Status::DataQualityError("Root schema must be a record");
+    }
+
+    return Status::OK();
+}
+
+void AvroReader::_close() {
+    if (_closed) {
+        return;
+    }
+    _closed = true;
+}
+
+Status AvroReader::_get_avro_doc(size_t* size, bool* eof, MemPool* tuple_pool, Tuple* tuple,
+                                 const std::vector<SlotDescriptor*>& slot_descs) {
+    SCOPED_TIMER(_file_read_timer);
+
+    if (_avro_str_ptr != nullptr) {
+        _avro_str_ptr.reset();
+    }
+    int64_t length = 0;
+    RETURN_IF_ERROR(_file_reader->read_one_message(&_avro_str_ptr, &length));
+
+    *size = length;
+    if (length == 0) {
+        *eof = true;
+    }
+
+    _bytes_read_counter += *size;
+    if (*eof) {
+        return Status::OK();
+    }
+
+    try {
+        auto schema_root = _schema.root();
+        _in = avro::memoryInputStream(_avro_str_ptr.get(), *size);
+        _decoder->init(*_in);
+    } catch (avro::Exception& e) {
+        return Status::DataQualityError(std::string("data quality is not good.") + e.what());
+    }
+
+    return Status::OK();
+}
+
+void AvroReader::_fill_slot(Tuple* tuple, SlotDescriptor* slot_desc, MemPool* mem_pool,
+                            const uint8_t* value, int32_t len) {
+    tuple->set_not_null(slot_desc->null_indicator_offset());
+    void* slot = tuple->get_slot(slot_desc->tuple_offset());
+    StringValue* str_slot = reinterpret_cast<StringValue*>(slot);
+    str_slot->ptr = reinterpret_cast<char*>(mem_pool->allocate(len));
+    memcpy(str_slot->ptr, value, len);
+    str_slot->len = len;
+}
+
+AvroReader::DeserializeFn AvroReader::createDeserializeFn(avro::NodePtr root_node,
+                                                          SlotDescriptor* slot_desc) {
+    uint8_t tmp_buf[128] = {0};
+    int32_t wbytes = 0;
+    switch (root_node->type()) {
+    case avro::AVRO_BYTES:
+        [[fallthrough]];
+    case avro::AVRO_STRING: {
+        return [val_string = std::string(), this](MemPool* tuple_pool, Tuple* tuple,
+                                                  SlotDescriptor* slot_desc, avro::Decoder& decoder,
+                                                  int nullcount) mutable {
+            decoder.decodeString(val_string);
+            if (val_string.empty()) {
+                if (slot_desc->is_nullable()) {
+                    tuple->set_null(slot_desc->null_indicator_offset());
+                    nullcount++;
+                } else {
+                    throw avro::Exception("NULL data for non-nullable column" +
+                                          slot_desc->col_name());
+                }
+            } else {
+                _fill_slot(tuple, slot_desc, tuple_pool, (uint8_t*)val_string.c_str(),
+                           (int32_t)strlen(val_string.c_str()));
+            }
+        };
+    } break;
+    case avro::AVRO_INT: {
+        return [tmp_buf, wbytes, this](MemPool* tuple_pool, Tuple* tuple, SlotDescriptor* slot_desc,
+                                       avro::Decoder& decoder, int nullcount) mutable {
+            int32_t val_int = decoder.decodeInt();
+            wbytes = sprintf((char*)tmp_buf, "%d", val_int);
+            _fill_slot(tuple, slot_desc, tuple_pool, tmp_buf, wbytes);
+        };
+    } break;
+    case avro::AVRO_LONG: {
+        return [tmp_buf, wbytes, this](MemPool* tuple_pool, Tuple* tuple, SlotDescriptor* slot_desc,
+                                       avro::Decoder& decoder, int nullcount) mutable {
+            int64_t val_long = decoder.decodeLong();
+            wbytes = sprintf((char*)tmp_buf, "%ld", val_long);
+            _fill_slot(tuple, slot_desc, tuple_pool, tmp_buf, wbytes);
+        };
+    } break;
+    case avro::AVRO_FLOAT: {
+        return [tmp_buf, wbytes, this](MemPool* tuple_pool, Tuple* tuple, SlotDescriptor* slot_desc,
+                                       avro::Decoder& decoder, int nullcount) mutable {
+            float val_float = decoder.decodeFloat();
+            wbytes = sprintf((char*)tmp_buf, "%f", val_float);
+            _fill_slot(tuple, slot_desc, tuple_pool, tmp_buf, wbytes);
+        };
+    } break;
+    case avro::AVRO_DOUBLE: {
+        return [tmp_buf, wbytes, this](MemPool* tuple_pool, Tuple* tuple, SlotDescriptor* slot_desc,
+                                       avro::Decoder& decoder, int nullcount) mutable {
+            double val_double = decoder.decodeDouble();
+            wbytes = sprintf((char*)tmp_buf, "%lf", val_double);
+            _fill_slot(tuple, slot_desc, tuple_pool, tmp_buf, wbytes);
+        };
+    } break;
+    case avro::AVRO_BOOL: {
+        return [tmp_buf, wbytes, this](MemPool* tuple_pool, Tuple* tuple, SlotDescriptor* slot_desc,
+                                       avro::Decoder& decoder, int nullcount) mutable {
+            bool val_bool = decoder.decodeBool();
+            wbytes = sprintf((char*)tmp_buf, "%d", val_bool);
+            _fill_slot(tuple, slot_desc, tuple_pool, tmp_buf, wbytes);
+        };
+    } break;
+    case avro::AVRO_ARRAY: {
+        // should have array type info
+        // TODO :
+    } break;
+    case avro::AVRO_UNION: {
+        auto nullable_deserializer = [root_node, slot_desc, this](size_t non_null_union_index) {
+            auto nested_deserialize =
+                    createDeserializeFn(root_node->leafAt(non_null_union_index), slot_desc);
+            return [non_null_union_index, nested_deserialize, this](
+                           MemPool* tuple_pool, Tuple* tuple, SlotDescriptor* slot_desc,
+                           avro::Decoder& decoder, int nullcount) mutable {
+                size_t union_index = decoder.decodeUnionIndex();
+                if (union_index == non_null_union_index) {
+                    nested_deserialize(tuple_pool, tuple, slot_desc, decoder, nullcount);
+                } else {
+                    tuple->set_null(slot_desc->null_indicator_offset());
+                }
+            };
+        };
+        if (root_node->leaves() == 2 && slot_desc->is_nullable()) {
+            if (root_node->leafAt(0)->type() == avro::AVRO_NULL) return nullable_deserializer(1);
+            if (root_node->leafAt(1)->type() == avro::AVRO_NULL) return nullable_deserializer(0);
+        }
+    } break;
+    case avro::AVRO_NULL: {
+        if (slot_desc->is_nullable()) {
+            return [this](MemPool* tuple_pool, Tuple* tuple, SlotDescriptor* slot_desc,
+                          avro::Decoder& decoder, int nullcount) mutable {
+                tuple->set_null(slot_desc->null_indicator_offset());
+                nullcount++;
+            };
+        } else {
+            throw avro::Exception("NULL data for non-nullable column" + slot_desc->col_name());
+        }
+    } break;
+    case avro::AVRO_ENUM:
+        [[fallthrough]];
+    case avro::AVRO_FIXED:
+        [[fallthrough]];
+    case avro::AVRO_MAP:
+        [[fallthrough]];
+    case avro::AVRO_RECORD:
+        [[fallthrough]];
+    default: {
+        throw avro::Exception("Not support " + root_node->type() + std::string("type yet."));

Review Comment:
   ```suggestion
           throw avro::Exception("Not support " + root_node->type() + std::string(" type yet."));
   ```



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


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

Posted by GitBox <gi...@apache.org>.
Henry2SS commented on code in PR #10492:
URL: https://github.com/apache/doris/pull/10492#discussion_r916626276


##########
be/src/common/config.h:
##########
@@ -763,6 +763,9 @@ CONF_Int32(quick_compaction_batch_size, "10");
 // do compaction min rowsets
 CONF_Int32(quick_compaction_min_rowsets, "10");
 
+// Avro schema file path, set default to "${DORIS_HOME}/conf/avro_schema.json"
+CONF_String(avro_schema_file_path, "${DORIS_HOME}/conf/avro_schema.json");

Review Comment:
   > The old query engine with `tuple` and `RowBatch` interface will be deprecated in the future. So you'd better implement this feature in vec engine, with `block` interface.
   
   Hi, support Vectorized `VAvroScanner` now.



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


[GitHub] [doris] github-actions[bot] closed pull request #10492: [feature-wip] support avro format in routine load and stream load

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] closed pull request #10492: [feature-wip] support avro format in routine load and stream load
URL: https://github.com/apache/doris/pull/10492


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


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

Posted by GitBox <gi...@apache.org>.
Henry2SS commented on code in PR #10492:
URL: https://github.com/apache/doris/pull/10492#discussion_r913340417


##########
be/src/common/config.h:
##########
@@ -763,6 +763,9 @@ CONF_Int32(quick_compaction_batch_size, "10");
 // do compaction min rowsets
 CONF_Int32(quick_compaction_min_rowsets, "10");
 
+// Avro schema file path, set default to "${DORIS_HOME}/conf/avro_schema.json"
+CONF_String(avro_schema_file_path, "${DORIS_HOME}/conf/avro_schema.json");

Review Comment:
   should have a property of routine load named `avro_schema_name`, and the schema should be put on every BE.
   Or, the schema can be put on FE. And BE pull it from FE.



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


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

Posted by GitBox <gi...@apache.org>.
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


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

Posted by GitBox <gi...@apache.org>.
morningman commented on code in PR #10492:
URL: https://github.com/apache/doris/pull/10492#discussion_r911599063


##########
be/src/common/config.h:
##########
@@ -763,6 +763,9 @@ CONF_Int32(quick_compaction_batch_size, "10");
 // do compaction min rowsets
 CONF_Int32(quick_compaction_min_rowsets, "10");
 
+// Avro schema file path, set default to "${DORIS_HOME}/conf/avro_schema.json"
+CONF_String(avro_schema_file_path, "${DORIS_HOME}/conf/avro_schema.json");

Review Comment:
   So that each Doris cluster only support one avro schema?



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